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

300 lines
7 KiB
JavaScript
Raw Normal View History

const {
generateGlobalUserID,
getGlobalUserParams,
StaticDatabases,
} = require("@budibase/auth/db")
2021-07-17 05:24:32 +12:00
const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils
2021-05-06 02:17:15 +12:00
const { UserStatus, EmailTemplatePurpose } = require("../../../constants")
const { DEFAULT_TENANT_ID } = require("@budibase/auth/constants")
2021-05-06 02:17:15 +12:00
const { checkInviteCode } = require("../../../utilities/redis")
const { sendEmail } = require("../../../utilities/email")
2021-07-07 05:10:04 +12:00
const { user: userCache } = require("@budibase/auth/cache")
const { invalidateSessions } = require("@budibase/auth/sessions")
const CouchDB = require("../../../db")
const env = require("../../../environment")
const {
getGlobalDB,
getTenantId,
doesTenantExist,
tryAddTenant,
updateTenantId,
} = require("@budibase/auth/tenancy")
2021-04-19 22:34:07 +12:00
const PLATFORM_INFO_DB = StaticDatabases.PLATFORM_INFO.name
2021-07-19 23:57:52 +12:00
async function allUsers() {
const db = getGlobalDB()
const response = await db.allDocs(
getGlobalUserParams(null, {
include_docs: true,
})
)
return response.rows.map(row => row.doc)
}
async function saveUser(user, tenantId) {
if (!tenantId) {
throw "No tenancy specified."
}
// need to set the context for this request, as specified
updateTenantId(tenantId)
// specify the tenancy incase we're making a new admin user (public)
const db = getGlobalDB(tenantId)
let { email, password, _id } = user
// make sure another user isn't using the same email
let dbUser
if (email) {
dbUser = await getGlobalUserByEmail(email)
if (dbUser != null && (dbUser._id !== _id || Array.isArray(dbUser))) {
throw "Email address already in use."
}
} else {
dbUser = await db.get(_id)
}
// get the password, make sure one is defined
let hashedPassword
if (password) {
hashedPassword = await hash(password)
} else if (dbUser) {
hashedPassword = dbUser.password
} else {
throw "Password must be specified."
}
_id = _id || generateGlobalUserID()
user = {
...dbUser,
...user,
_id,
2021-04-19 22:34:07 +12:00
password: hashedPassword,
tenantId,
2021-04-19 22:34:07 +12:00
}
// make sure the roles object is always present
if (!user.roles) {
user.roles = {}
}
2021-04-19 22:34:07 +12:00
// add the active status to a user if its not provided
if (user.status == null) {
user.status = UserStatus.ACTIVE
}
try {
const response = await db.put({
password: hashedPassword,
2021-04-19 22:34:07 +12:00
...user,
})
await tryAddTenant(tenantId, _id, email)
2021-07-07 05:10:04 +12:00
await userCache.invalidateUser(response.id)
return {
2021-04-19 22:34:07 +12:00
_id: response.id,
_rev: response.rev,
email,
}
} catch (err) {
if (err.status === 409) {
throw "User exists already"
2021-04-19 22:34:07 +12:00
} else {
throw err
2021-04-19 22:34:07 +12:00
}
}
}
exports.save = async ctx => {
try {
ctx.body = await saveUser(ctx.request.body, getTenantId())
} catch (err) {
ctx.throw(err.status || 400, err)
}
}
exports.adminUser = async ctx => {
const { email, password, tenantId } = ctx.request.body
if (await doesTenantExist(tenantId)) {
ctx.throw(403, "Organisation already exists.")
}
const db = getGlobalDB(tenantId)
const response = await db.allDocs(
getGlobalUserParams(null, {
include_docs: true,
})
)
if (response.rows.some(row => row.doc.admin)) {
ctx.throw(
403,
"You cannot initialise once an global user has been created."
)
}
const user = {
2021-05-06 08:56:43 +12:00
email: email,
password: password,
roles: {},
builder: {
2021-05-06 08:56:43 +12:00
global: true,
},
admin: {
global: true,
},
tenantId,
}
try {
ctx.body = await saveUser(user, tenantId)
} catch (err) {
ctx.throw(err.status || 400, err)
}
}
2021-05-04 22:32:22 +12:00
exports.destroy = async ctx => {
const db = getGlobalDB()
const dbUser = await db.get(ctx.params.id)
2021-04-19 22:34:07 +12:00
await db.remove(dbUser._id, dbUser._rev)
2021-07-07 05:10:04 +12:00
await userCache.invalidateUser(dbUser._id)
await invalidateSessions(dbUser._id)
2021-04-19 22:34:07 +12:00
ctx.body = {
message: `User ${ctx.params.id} deleted.`,
2021-04-19 22:34:07 +12:00
}
}
exports.removeAppRole = async ctx => {
const { appId } = ctx.params
const db = getGlobalDB()
const users = await allUsers(ctx)
const bulk = []
2021-07-08 10:29:19 +12:00
const cacheInvalidations = []
for (let user of users) {
if (user.roles[appId]) {
2021-07-08 10:29:19 +12:00
cacheInvalidations.push(userCache.invalidateUser(user._id))
delete user.roles[appId]
bulk.push(user)
}
}
await db.bulkDocs(bulk)
2021-07-08 10:29:19 +12:00
await Promise.all(cacheInvalidations)
ctx.body = {
message: "App role removed from all users",
}
}
exports.getSelf = async ctx => {
if (!ctx.user) {
ctx.throw(403, "User not logged in")
}
ctx.params = {
id: ctx.user._id,
}
// this will set the body
await exports.find(ctx)
}
exports.updateSelf = async ctx => {
const db = getGlobalDB()
const user = await db.get(ctx.user._id)
if (ctx.request.body.password) {
ctx.request.body.password = await hash(ctx.request.body.password)
}
// don't allow sending up an ID/Rev, always use the existing one
delete ctx.request.body._id
delete ctx.request.body._rev
const response = await db.put({
...user,
...ctx.request.body,
})
2021-07-08 10:29:19 +12:00
await userCache.invalidateUser(user._id)
ctx.body = {
_id: response.id,
_rev: response.rev,
}
}
2021-04-19 22:34:07 +12:00
// called internally by app server user fetch
2021-05-04 22:32:22 +12:00
exports.fetch = async ctx => {
const users = await allUsers(ctx)
2021-04-19 22:34:07 +12:00
// user hashed password shouldn't ever be returned
for (let user of users) {
if (user) {
delete user.password
}
}
ctx.body = users
}
// called internally by app server user find
2021-05-04 22:32:22 +12:00
exports.find = async ctx => {
const db = getGlobalDB()
2021-04-19 22:34:07 +12:00
let user
try {
user = await db.get(ctx.params.id)
2021-04-19 22:34:07 +12:00
} catch (err) {
// no user found, just return nothing
user = {}
}
if (user) {
delete user.password
}
ctx.body = user
}
exports.tenantLookup = async ctx => {
const id = ctx.params.id
// lookup, could be email or userId, either will return a doc
const db = new CouchDB(PLATFORM_INFO_DB)
let tenantId = null
try {
const doc = await db.get(id)
if (doc && doc.tenantId) {
tenantId = doc.tenantId
}
} catch (err) {
if (!env.MULTI_TENANCY) {
tenantId = DEFAULT_TENANT_ID
} else {
ctx.throw(400, "No tenant found.")
}
}
ctx.body = {
tenantId,
}
}
exports.invite = async ctx => {
let { email, userInfo } = ctx.request.body
const existing = await getGlobalUserByEmail(email)
2021-05-06 02:17:15 +12:00
if (existing) {
ctx.throw(400, "Email address already in use.")
}
if (!userInfo) {
userInfo = {}
}
userInfo.tenantId = getTenantId()
await sendEmail(email, EmailTemplatePurpose.INVITATION, {
2021-05-12 02:24:17 +12:00
subject: "{{ company }} platform invitation",
info: userInfo,
2021-05-12 02:24:17 +12:00
})
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
}
}
exports.inviteAccept = async ctx => {
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 } = await checkInviteCode(inviteCode)
ctx.body = await saveUser(
{
firstName,
lastName,
password,
email,
...info,
},
info.tenantId
)
2021-05-06 02:17:15 +12:00
} catch (err) {
ctx.throw(400, "Unable to create new user, invitation invalid.")
}
}