1
0
Fork 0
mirror of synced 2024-06-23 08:30:31 +12:00
budibase/packages/worker/src/api/controllers/global/users.ts

463 lines
12 KiB
TypeScript
Raw Normal View History

import * as userSdk from "../../../sdk/users"
import env from "../../../environment"
import {
AcceptUserInviteRequest,
AcceptUserInviteResponse,
AddSSoUserRequest,
BulkUserRequest,
BulkUserResponse,
CloudAccount,
2022-11-12 04:43:41 +13:00
CreateAdminUserRequest,
CreateAdminUserResponse,
Ctx,
InviteUserRequest,
InviteUsersRequest,
2023-11-10 04:13:59 +13:00
InviteUsersResponse,
LockName,
LockType,
MigrationType,
PlatformUserByEmail,
SaveUserResponse,
2022-10-04 02:02:58 +13:00
SearchUsersRequest,
User,
UserCtx,
} from "@budibase/types"
import {
accounts,
cache,
ErrorCode,
events,
migrations,
platform,
tenancy,
db,
locks,
} from "@budibase/backend-core"
import { checkAnyUserExists } from "../../../utilities/users"
2023-03-01 06:05:11 +13:00
import { isEmailConfigured } from "../../../utilities/email"
2022-07-26 23:17:01 +12:00
const MAX_USERS_UPLOAD_LIMIT = 1000
export const save = async (ctx: UserCtx<User, SaveUserResponse>) => {
try {
const currentUserId = ctx.user?._id
const requestUser = ctx.request.body
const user = await userSdk.db.save(requestUser, { currentUserId })
ctx.body = {
_id: user._id!,
_rev: user._rev!,
email: user.email,
}
} catch (err: any) {
ctx.throw(err.status || 400, err)
}
}
export const addSsoSupport = async (ctx: Ctx<AddSSoUserRequest>) => {
const { email, ssoId } = ctx.request.body
try {
// Status is changed to 404 from getUserDoc if user is not found
2024-03-29 00:04:27 +13:00
let userByEmail = (await platform.users.getUserDoc(
email
)) as PlatformUserByEmail
await platform.users.addSsoUser(
ssoId,
email,
userByEmail.userId,
userByEmail.tenantId
)
ctx.status = 200
} catch (err: any) {
ctx.throw(err.status || 400, err)
}
}
const bulkDelete = async (userIds: string[], currentUserId: string) => {
if (userIds?.indexOf(currentUserId) !== -1) {
throw new Error("Unable to delete self.")
}
return await userSdk.db.bulkDelete(userIds)
}
2022-07-26 23:17:01 +12:00
const bulkCreate = async (users: User[], groupIds: string[]) => {
if (!env.SELF_HOSTED && users.length > MAX_USERS_UPLOAD_LIMIT) {
throw new Error(
2022-07-26 23:17:01 +12:00
"Max limit for upload is 1000 users. Please reduce file size and try again."
)
}
return await userSdk.db.bulkCreate(users, groupIds)
}
2022-07-26 23:17:01 +12:00
2023-05-04 22:58:23 +12:00
export const bulkUpdate = async (
ctx: Ctx<BulkUserRequest, BulkUserResponse>
) => {
const currentUserId = ctx.user._id
2023-05-04 22:58:23 +12:00
const input = ctx.request.body
let created, deleted
2022-07-18 23:33:56 +12:00
try {
if (input.create) {
created = await bulkCreate(input.create.users, input.create.groups)
}
if (input.delete) {
deleted = await bulkDelete(input.delete.userIds, currentUserId)
}
} catch (err: any) {
2022-09-24 09:21:51 +12:00
ctx.throw(err.status || 400, err?.message || err)
}
2023-05-04 22:58:23 +12:00
ctx.body = { created, deleted }
}
const parseBooleanParam = (param: any) => {
return !(param && param === "false")
}
export const adminUser = async (
ctx: Ctx<CreateAdminUserRequest, CreateAdminUserResponse>
) => {
const { email, password, tenantId, ssoId, givenName, familyName } =
ctx.request.body
if (await platform.tenants.exists(tenantId)) {
ctx.throw(403, "Organisation already exists.")
}
if (env.MULTI_TENANCY) {
// store the new tenant record in the platform db
await platform.tenants.addTenant(tenantId)
await migrations.backPopulateMigrations({
type: MigrationType.GLOBAL,
tenantId,
})
}
await tenancy.doInTenant(tenantId, async () => {
// account portal sends a pre-hashed password - honour param to prevent double hashing
const hashPassword = parseBooleanParam(ctx.request.query.hashPassword)
// account portal sends no password for SSO users
const requirePassword = parseBooleanParam(ctx.request.query.requirePassword)
const userExists = await checkAnyUserExists()
if (userExists) {
ctx.throw(
403,
"You cannot initialise once an global user has been created."
)
}
try {
const finalUser = await userSdk.db.createAdminUser(email, tenantId, {
2024-03-22 04:19:50 +13:00
password,
ssoId,
hashPassword,
requirePassword,
firstName: givenName,
lastName: familyName,
})
2021-09-24 10:25:25 +12:00
// events
let account: CloudAccount | undefined
if (!env.SELF_HOSTED && !env.DISABLE_ACCOUNT_PORTAL) {
account = await accounts.getAccountByTenantId(tenantId)
}
await events.identification.identifyTenantGroup(tenantId, account)
ctx.body = {
_id: finalUser._id!,
_rev: finalUser._rev!,
email: finalUser.email,
}
} catch (err: any) {
ctx.throw(err.status || 400, err)
}
})
}
export const countByApp = async (ctx: any) => {
const appId = ctx.params.appId
try {
ctx.body = await userSdk.db.countUsersByApp(appId)
} catch (err: any) {
ctx.throw(err.status || 400, err)
}
}
export const destroy = async (ctx: any) => {
2022-04-08 12:28:22 +12:00
const id = ctx.params.id
if (id === ctx.user._id) {
ctx.throw(400, "Unable to delete self.")
}
2022-07-20 01:20:57 +12:00
await userSdk.db.destroy(id)
2022-07-20 01:20:57 +12:00
2021-04-19 22:34:07 +12:00
ctx.body = {
2022-04-08 12:28:22 +12:00
message: `User ${id} deleted.`,
}
2021-04-19 22:34:07 +12:00
}
2023-05-04 22:58:23 +12:00
export const getAppUsers = async (ctx: Ctx<SearchUsersRequest>) => {
const body = ctx.request.body
const users = await userSdk.db.getUsersByAppAccess({
appId: body.appId,
limit: body.limit,
})
2023-02-28 22:37:03 +13:00
ctx.body = { data: users }
}
2023-05-04 22:58:23 +12:00
export const search = async (ctx: Ctx<SearchUsersRequest>) => {
const body = ctx.request.body
2023-02-28 22:37:03 +13:00
// TODO: for now only two supported search keys; string.email and equal._id
if (body?.query) {
// Clean numeric prefixing. This will overwrite duplicate search fields,
// but this is fine because we only support a single custom search on
// email and id
for (let filters of Object.values(body.query)) {
if (filters && typeof filters === "object") {
for (let [field, value] of Object.entries(filters)) {
delete filters[field]
const cleanedField = db.removeKeyNumbering(field)
if (filters[cleanedField] !== undefined) {
ctx.throw(400, "Only 1 filter per field is supported")
}
filters[cleanedField] = value
}
}
}
// Validate we aren't trying to search on any illegal fields
if (!userSdk.core.isSupportedUserSearch(body.query)) {
ctx.throw(400, "Can only search by string.email, equal._id or oneOf._id")
}
}
if (body.paginate === false) {
2023-02-28 22:37:03 +13:00
await getAppUsers(ctx)
} else {
2023-03-11 05:06:53 +13:00
const paginated = await userSdk.core.paginatedUsers(body)
2023-02-28 22:37:03 +13:00
// user hashed password shouldn't ever be returned
for (let user of paginated.data) {
if (user) {
delete user.password
}
2021-04-19 22:34:07 +12:00
}
2023-02-28 22:37:03 +13:00
ctx.body = paginated
2021-04-19 22:34:07 +12:00
}
}
// called internally by app server user fetch
export const fetch = async (ctx: any) => {
const all = await userSdk.db.allUsers()
2021-04-19 22:34:07 +12:00
// user hashed password shouldn't ever be returned
for (let user of all) {
2021-04-19 22:34:07 +12:00
if (user) {
delete user.password
}
}
2022-03-26 05:08:12 +13:00
ctx.body = all
2021-04-19 22:34:07 +12:00
}
// called internally by app server user find
export const find = async (ctx: any) => {
ctx.body = await userSdk.db.getUser(ctx.params.id)
2021-04-19 22:34:07 +12:00
}
export const tenantUserLookup = async (ctx: any) => {
const id = ctx.params.id
const user = await userSdk.core.getPlatformUser(id)
if (user) {
ctx.body = user
} else {
2021-09-18 00:41:22 +12:00
ctx.throw(400, "No tenant user found.")
}
}
2023-02-23 23:38:03 +13:00
/*
Encapsulate the app user onboarding flows here.
*/
2023-11-10 04:13:59 +13:00
export const onboardUsers = async (
ctx: Ctx<InviteUsersRequest, InviteUsersResponse>
) => {
2023-11-10 04:02:44 +13:00
if (await isEmailConfigured()) {
await inviteMultiple(ctx)
return
}
2023-02-28 22:37:03 +13:00
2023-11-10 04:02:44 +13:00
let createdPasswords: Record<string, string> = {}
const users: User[] = ctx.request.body.map(invite => {
let password = Math.random().toString(36).substring(2, 22)
createdPasswords[invite.email] = password
return {
email: invite.email,
password,
forceResetPassword: true,
roles: invite.userInfo.apps,
admin: invite.userInfo.admin,
builder: invite.userInfo.builder,
tenantId: tenancy.getTenantId(),
2023-02-23 23:38:03 +13:00
}
2023-11-10 04:02:44 +13:00
})
2023-11-10 04:13:59 +13:00
let resp = await userSdk.db.bulkCreate(users)
for (const user of resp.successful) {
2023-11-10 04:02:44 +13:00
user.password = createdPasswords[user.email]
2023-11-10 04:13:59 +13:00
}
2023-11-10 04:02:44 +13:00
ctx.body = { ...resp, created: true }
2023-02-23 23:38:03 +13:00
}
2023-05-04 22:58:23 +12:00
export const invite = async (ctx: Ctx<InviteUserRequest>) => {
const request = ctx.request.body
2023-03-01 11:27:02 +13:00
2023-05-04 22:58:23 +12:00
let multiRequest = [request]
2023-03-01 11:27:02 +13:00
const response = await userSdk.invite(multiRequest)
// explicitly throw for single user invite
if (response.unsuccessful.length) {
const reason = response.unsuccessful[0].reason
if (reason === "Unavailable") {
ctx.throw(400, reason)
} else {
ctx.throw(500, reason)
}
}
2021-05-06 02:17:15 +12:00
ctx.body = {
2021-05-06 02:19:44 +12:00
message: "Invitation has been sent.",
2023-02-23 23:38:03 +13:00
successful: response.successful,
unsuccessful: response.unsuccessful,
2021-05-06 02:17:15 +12:00
}
}
2023-05-04 22:58:23 +12:00
export const inviteMultiple = async (ctx: Ctx<InviteUsersRequest>) => {
2023-11-10 04:02:44 +13:00
ctx.body = await userSdk.invite(ctx.request.body)
2022-07-05 20:21:59 +12:00
}
2023-01-28 02:44:57 +13:00
export const checkInvite = async (ctx: any) => {
const { code } = ctx.params
let invite
try {
invite = await cache.invite.getCode(code)
2023-01-28 02:44:57 +13:00
} catch (e) {
Per user pricing (#10378) * Update pro version to 2.4.44-alpha.9 (#10231) Co-authored-by: Budibase Staging Release Bot <> * Track installation and unique tenant id on licence activate (#10146) * changes and exports * removing the extend * Lint + tidy * Update account.ts --------- Co-authored-by: Rory Powell <rory.codes@gmail.com> Co-authored-by: mike12345567 <me@michaeldrury.co.uk> * Type updates for loading new plans (#10245) * Add new quota for max users on free plan * Split available vs purchased plan & price type definitions. Update usages of available prices and plans * Type fixes * Add types for minimums * New `PlanModel` type for `PER_USER` and `DAY_PASS` (#10247) * Add new quota for max users on free plan * Split available vs purchased plan & price type definitions. Update usages of available prices and plans * Type fixes * Add types for minimums * New `PlanModel` type for `PER_USER` and `DAY_PASS` * Add loadEnvFiles to lerna config for run command to prevent local test failures * Fix types in license test structure * Add quotas integration to user create / delete + migration (#10250) * Add new quota for max users on free plan * Split available vs purchased plan & price type definitions. Update usages of available prices and plans * Type fixes * Add types for minimums * New `PlanModel` type for `PER_USER` and `DAY_PASS` * Add loadEnvFiles to lerna config for run command to prevent local test failures * Fix types in license test structure * Add quotas integration to user create / delete * Always sync user count from view total_rows value for accuracy * Add migration to sync users * Add syncUsers.spec.ts * Lint * Types and structures for user subscription quantity sync (#10280) * Add new quota for max users on free plan * Split available vs purchased plan & price type definitions. Update usages of available prices and plans * Type fixes * Add types for minimums * New `PlanModel` type for `PER_USER` and `DAY_PASS` * Add loadEnvFiles to lerna config for run command to prevent local test failures * Fix types in license test structure * Add quotas integration to user create / delete * Always sync user count from view total_rows value for accuracy * Add migration to sync users * Add syncUsers.spec.ts * Prevent old installs from activating, track install info via get license request instead of on activation. * Add usesInvoicing to PurchasedPlan * Add min/max users to PurchasedPlan * Additional test structures for generating a license, remove maxUsers from PurchasedPlan - this is already present in the license quotas * Stripe integration for monthly prorations on annual plans * Integrate annual prorations with test clocks * Updated types, test utils and date processing for licensing (#10346) * Add new quota for max users on free plan * Split available vs purchased plan & price type definitions. Update usages of available prices and plans * Type fixes * Add types for minimums * New `PlanModel` type for `PER_USER` and `DAY_PASS` * Add loadEnvFiles to lerna config for run command to prevent local test failures * Fix types in license test structure * Add quotas integration to user create / delete * Always sync user count from view total_rows value for accuracy * Add migration to sync users * Add syncUsers.spec.ts * Prevent old installs from activating, track install info via get license request instead of on activation. * Add usesInvoicing to PurchasedPlan * Add min/max users to PurchasedPlan * Additional test structures for generating a license, remove maxUsers from PurchasedPlan - this is already present in the license quotas * Stripe integration for monthly prorations on annual plans * Integrate annual prorations with test clocks * Updated types, test utils and date processing * Lint * Pricing/billing page (#10353) * bbui updates for billing page * Require all PlanTypes in PlanMinimums for compile time safety * fix test package utils * Incoming user limits warnings (#10379) * incoming user limits warning * fix inlinealert button * add corretc button link and text to user alert * pr comments * simplify limit check * Types and test updates for subscription quantity changes in account-portal (#10372) * Add chance extensions for `arrayOf`. Update events spies with license events * Add generics to doInTenant response * Update account structure with quota usage * User count limits (#10385) * incoming user limits warning * fix inlinealert button * add corretc button link and text to user alert * pr comments * simplify limit check * user limit messaging on add users modal * user limit messaging on import users modal * update licensing store to be more generic * some styling updates * remove console log * Store tweaks * Add startDate to Quota type --------- Co-authored-by: Rory Powell <rory.codes@gmail.com> * Lint * Support custom lock options * Reactivity fixes for add user modals * Update ethereal email creds * Add warn for getting invite from code error * Extract disabling user import condition * Handling unlimited users in modals logic and adding start date processing to store * Lint * Integration testing fixes (#10389) * lint --------- Co-authored-by: Mateus Badan de Pieri <mateuspieri@gmail.com> Co-authored-by: mike12345567 <me@michaeldrury.co.uk> Co-authored-by: Peter Clement <PClmnt@users.noreply.github.com>
2023-04-24 20:31:48 +12:00
console.warn("Error getting invite from code", e)
2023-01-28 02:44:57 +13:00
ctx.throw(400, "There was a problem with the invite")
return
2023-01-28 02:44:57 +13:00
}
ctx.body = {
email: invite.email,
}
}
2023-02-23 23:38:03 +13:00
export const getUserInvites = async (ctx: any) => {
try {
// Restricted to the currently authenticated tenant
ctx.body = await cache.invite.getInviteCodes()
2023-02-23 23:38:03 +13:00
} catch (e) {
ctx.throw(400, "There was a problem fetching invites")
}
}
export const updateInvite = async (ctx: any) => {
const { code } = ctx.params
let updateBody = { ...ctx.request.body }
delete updateBody.email
let invite
try {
invite = await cache.invite.getCode(code)
2023-02-23 23:38:03 +13:00
} catch (e) {
ctx.throw(400, "There was a problem with the invite")
return
2023-02-23 23:38:03 +13:00
}
let updated = {
...invite,
}
if (!updateBody?.builder?.apps && updated.info?.builder?.apps) {
updated.info.builder.apps = []
} else if (updateBody?.builder) {
updated.info.builder = updateBody.builder
2023-08-31 03:43:24 +12:00
}
2023-02-23 23:38:03 +13:00
if (!updateBody?.apps || !Object.keys(updateBody?.apps).length) {
updated.info.apps = []
} else {
updated.info = {
...invite.info,
apps: {
...invite.info.apps,
...updateBody.apps,
},
}
}
await cache.invite.updateCode(code, updated)
2023-02-23 23:38:03 +13:00
ctx.body = { ...invite }
}
export const inviteAccept = async (
ctx: Ctx<AcceptUserInviteRequest, AcceptUserInviteResponse>
) => {
const { inviteCode, password, firstName, lastName } = ctx.request.body
2021-05-06 02:17:15 +12:00
try {
await locks.doWithLock(
{
type: LockType.AUTO_EXTEND,
name: LockName.PROCESS_USER_INVITE,
resource: inviteCode,
2024-01-16 23:49:34 +13:00
systemLock: true,
},
async () => {
// info is an extension of the user object that was stored by global
const { email, info } = await cache.invite.getCode(inviteCode)
const user = await tenancy.doInTenant(info.tenantId, async () => {
let request: any = {
firstName,
lastName,
password,
email,
admin: { global: info?.admin?.global || false },
roles: info.apps,
tenantId: info.tenantId,
}
const builder: { global: boolean; apps?: string[] } = {
global: info?.builder?.global || false,
}
2023-02-23 23:38:03 +13:00
if (info?.builder?.apps) {
builder.apps = info.builder.apps
request.builder = builder
}
delete info.apps
request = {
...request,
...info,
}
2023-02-23 23:38:03 +13:00
const saved = await userSdk.db.save(request)
2024-01-16 23:28:35 +13:00
await events.user.inviteAccepted(saved)
return saved
})
await cache.invite.deleteCode(inviteCode)
ctx.body = {
_id: user._id!,
_rev: user._rev!,
email: user.email,
}
}
)
} catch (err: any) {
if (err.code === ErrorCode.USAGE_LIMIT_EXCEEDED) {
// explicitly re-throw limit exceeded errors
ctx.throw(400, err)
}
console.warn("Error inviting user", err)
2024-01-16 23:07:03 +13:00
ctx.throw(400, err || "Unable to create new user, invitation invalid.")
}
}