creating users from csv
This commit is contained in:
parent
42e4ad6fb8
commit
6758574e7f
|
@ -7,20 +7,38 @@
|
||||||
notifications,
|
notifications,
|
||||||
} from "@budibase/bbui"
|
} from "@budibase/bbui"
|
||||||
import { groups } from "stores/portal"
|
import { groups } from "stores/portal"
|
||||||
|
import { emailValidator } from "../../../../../../helpers/validation"
|
||||||
|
|
||||||
import { Constants } from "@budibase/frontend-core"
|
import { Constants } from "@budibase/frontend-core"
|
||||||
|
|
||||||
const BYTES_IN_MB = 1000000
|
const BYTES_IN_MB = 1000000
|
||||||
const FILE_SIZE_LIMIT = BYTES_IN_MB * 5
|
const FILE_SIZE_LIMIT = BYTES_IN_MB * 5
|
||||||
|
|
||||||
export let showOnboardingTypeModal
|
export let createUsersFromCsv
|
||||||
|
|
||||||
let files = []
|
let files = []
|
||||||
|
|
||||||
let csvString = undefined
|
let csvString = undefined
|
||||||
|
let userEmails = []
|
||||||
|
let userGroups = []
|
||||||
|
let usersRole = null
|
||||||
|
$: invalidEmails = []
|
||||||
|
|
||||||
|
const validEmails = userEmails => {
|
||||||
|
for (const email of userEmails) {
|
||||||
|
if (emailValidator(email) !== true) invalidEmails.push(email)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!invalidEmails.length) return true
|
||||||
|
|
||||||
|
notifications.error(
|
||||||
|
`Error, please check the following email${
|
||||||
|
invalidEmails.length > 1 ? "s" : ""
|
||||||
|
}: ${invalidEmails.join(", ")}`
|
||||||
|
)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
function parseCsv() {}
|
|
||||||
*/
|
|
||||||
async function handleFile(evt) {
|
async function handleFile(evt) {
|
||||||
const fileArray = Array.from(evt.target.files)
|
const fileArray = Array.from(evt.target.files)
|
||||||
if (fileArray.some(file => file.size >= FILE_SIZE_LIMIT)) {
|
if (fileArray.some(file => file.size >= FILE_SIZE_LIMIT)) {
|
||||||
|
@ -37,6 +55,8 @@
|
||||||
reader.addEventListener("load", function (e) {
|
reader.addEventListener("load", function (e) {
|
||||||
csvString = e.target.result
|
csvString = e.target.result
|
||||||
files = fileArray
|
files = fileArray
|
||||||
|
|
||||||
|
userEmails = csvString.split("\n")
|
||||||
})
|
})
|
||||||
reader.readAsText(fileArray[0])
|
reader.readAsText(fileArray[0])
|
||||||
}
|
}
|
||||||
|
@ -49,8 +69,8 @@
|
||||||
showCancelButton={false}
|
showCancelButton={false}
|
||||||
cancelText="Cancel"
|
cancelText="Cancel"
|
||||||
showCloseIcon={false}
|
showCloseIcon={false}
|
||||||
onConfirm={showOnboardingTypeModal}
|
onConfirm={() => createUsersFromCsv({ userEmails, usersRole, userGroups })}
|
||||||
disabled={!files.length}
|
disabled={!userEmails.length || !validEmails(userEmails) || !usersRole}
|
||||||
>
|
>
|
||||||
<Body size="S">Import your users email addrresses from a CSV</Body>
|
<Body size="S">Import your users email addrresses from a CSV</Body>
|
||||||
|
|
||||||
|
@ -61,14 +81,18 @@
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<RadioGroup options={Constants.BuilderRoleDescriptions} />
|
<RadioGroup
|
||||||
|
bind:value={usersRole}
|
||||||
|
options={Constants.BuilderRoleDescriptions}
|
||||||
|
/>
|
||||||
|
|
||||||
<Multiselect
|
<Multiselect
|
||||||
|
bind:value={userGroups}
|
||||||
placeholder="Select User Groups"
|
placeholder="Select User Groups"
|
||||||
label="User Groups"
|
label="User Groups"
|
||||||
options={$groups}
|
options={$groups}
|
||||||
getOptionLabel={option => option.name}
|
getOptionLabel={option => option.name}
|
||||||
getOptionValue={option => option.name}
|
getOptionValue={option => option._id}
|
||||||
/>
|
/>
|
||||||
</ModalContent>
|
</ModalContent>
|
||||||
|
|
||||||
|
|
|
@ -117,6 +117,25 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const createUsersFromCsv = async userCsvData => {
|
||||||
|
const { userEmails, usersRole, userGroups } = userCsvData
|
||||||
|
|
||||||
|
const users = []
|
||||||
|
for (const email of userEmails) {
|
||||||
|
const newUser = {
|
||||||
|
email: email,
|
||||||
|
role: usersRole,
|
||||||
|
password: Math.random().toString(36).substring(2, 22),
|
||||||
|
forceResetPassword: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
users.push(newUser)
|
||||||
|
}
|
||||||
|
|
||||||
|
userData = { groups: userGroups, users: users }
|
||||||
|
return createUser()
|
||||||
|
}
|
||||||
|
|
||||||
async function createUser() {
|
async function createUser() {
|
||||||
try {
|
try {
|
||||||
await users.create(userData)
|
await users.create(userData)
|
||||||
|
@ -258,7 +277,7 @@
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
<Modal bind:this={importUsersModal}>
|
<Modal bind:this={importUsersModal}>
|
||||||
<ImportUsersModal {showOnboardingTypeModal} />
|
<ImportUsersModal {createUsersFromCsv} />
|
||||||
</Modal>
|
</Modal>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
|
Loading…
Reference in New Issue