1
0
Fork 0
mirror of synced 2024-06-28 11:00:55 +12:00
budibase/packages/worker/src/api/controllers/global/users.ts

153 lines
3.9 KiB
TypeScript
Raw Normal View History

import { EmailTemplatePurpose } from "../../../constants"
import { checkInviteCode } from "../../../utilities/redis"
import { sendEmail } from "../../../utilities/email"
2022-04-08 12:28:22 +12:00
import { users } from "../../../sdk"
const {
2022-04-08 12:28:22 +12:00
errors,
users: usersCore,
tenancy,
db: dbUtils,
2022-04-12 23:34:36 +12:00
events,
2022-04-08 12:28:22 +12:00
} = require("@budibase/backend-core")
export const save = async (ctx: any) => {
try {
2022-04-08 12:28:22 +12:00
const user = await users.save(ctx.request.body)
ctx.body = user
} catch (err: any) {
ctx.throw(err.status || 400, err)
}
}
const parseBooleanParam = (param: any) => {
return !(param && param === "false")
}
export const adminUser = async (ctx: any) => {
const { email, password, tenantId } = ctx.request.body
// 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)
2022-04-08 12:28:22 +12:00
if (await tenancy.doesTenantExist(tenantId)) {
ctx.throw(403, "Organisation already exists.")
}
const response = await tenancy.doWithGlobalDB(tenantId, async (db: any) => {
return db.allDocs(
dbUtils.getGlobalUserParams(null, {
include_docs: true,
})
)
})
2021-09-24 10:25:25 +12:00
if (response.rows.some((row: any) => row.doc.admin)) {
2022-04-08 12:28:22 +12:00
ctx.throw(403, "You cannot initialise once a global user has been created.")
}
const user = {
2021-05-06 08:56:43 +12:00
email: email,
password: password,
2021-09-22 20:29:51 +12:00
createdAt: Date.now(),
roles: {},
builder: {
2021-05-06 08:56:43 +12:00
global: true,
},
admin: {
global: true,
},
tenantId,
}
try {
2022-04-08 12:28:22 +12:00
ctx.body = await tenancy.doInTenant(tenantId, async () => {
return users.save(user, hashPassword, requirePassword)
})
} 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
await users.destroy(id, ctx.user)
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
}
}
// called internally by app server user fetch
export const fetch = async (ctx: any) => {
2022-04-08 12:28:22 +12:00
const all = await users.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) => {
2022-04-08 12:28:22 +12:00
ctx.body = await users.getUser(ctx.params.id)
2021-04-19 22:34:07 +12:00
}
export const tenantUserLookup = async (ctx: any) => {
const id = ctx.params.id
2022-04-08 12:28:22 +12:00
const user = await tenancy.getTenantUser(id)
if (user) {
ctx.body = user
} else {
2021-09-18 00:41:22 +12:00
ctx.throw(400, "No tenant user found.")
}
}
export const invite = async (ctx: any) => {
let { email, userInfo } = ctx.request.body
2022-04-08 12:28:22 +12:00
const existing = await usersCore.getGlobalUserByEmail(email)
2021-05-06 02:17:15 +12:00
if (existing) {
ctx.throw(400, "Email address already in use.")
}
if (!userInfo) {
userInfo = {}
}
2022-04-08 12:28:22 +12:00
userInfo.tenantId = tenancy.getTenantId()
const opts: any = {
2021-05-12 02:24:17 +12:00
subject: "{{ company }} platform invitation",
info: userInfo,
}
await sendEmail(email, EmailTemplatePurpose.INVITATION, opts)
2021-05-06 02:17:15 +12:00
ctx.body = {
2021-05-06 02:19:44 +12:00
message: "Invitation has been sent.",
2021-05-06 02:17:15 +12:00
}
2022-04-12 23:34:36 +12:00
events.user.invited(userInfo)
}
export const inviteAccept = async (ctx: any) => {
const { inviteCode, password, firstName, lastName } = ctx.request.body
2021-05-06 02:17:15 +12:00
try {
// info is an extension of the user object that was stored by global
const { email, info }: any = await checkInviteCode(inviteCode)
2022-04-12 23:34:36 +12:00
ctx.body = await tenancy.doInTenant(info.tenantId, async () => {
const user = await users.save({
firstName,
lastName,
password,
email,
...info,
2022-04-08 12:28:22 +12:00
})
2022-04-12 23:34:36 +12:00
events.user.inviteAccepted(user)
return user
2022-04-08 12:28:22 +12:00
})
} catch (err: any) {
if (err.code === errors.codes.USAGE_LIMIT_EXCEEDED) {
// explicitly re-throw limit exceeded errors
ctx.throw(400, err)
}
2021-05-06 02:17:15 +12:00
ctx.throw(400, "Unable to create new user, invitation invalid.")
}
}