1
0
Fork 0
mirror of synced 2024-06-30 20:10:54 +12:00
budibase/packages/worker/src/api/controllers/admin/users.js

200 lines
4.7 KiB
JavaScript
Raw Normal View History

2021-04-19 22:34:07 +12:00
const CouchDB = require("../../../db")
const {
generateGlobalUserID,
getGlobalUserParams,
2021-04-19 22:34:07 +12:00
StaticDatabases,
} = require("@budibase/auth").db
const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils
2021-05-06 02:17:15 +12:00
const { UserStatus, EmailTemplatePurpose } = require("../../../constants")
const { checkInviteCode } = require("../../../utilities/redis")
const { sendEmail } = require("../../../utilities/email")
2021-04-19 22:34:07 +12:00
2021-04-19 22:38:54 +12:00
const GLOBAL_DB = StaticDatabases.GLOBAL.name
2021-04-19 22:34:07 +12:00
2021-05-04 22:32:22 +12:00
exports.save = async ctx => {
2021-04-19 22:38:54 +12:00
const db = new CouchDB(GLOBAL_DB)
2021-04-19 22:34:07 +12:00
const { email, password, _id } = ctx.request.body
// 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))) {
ctx.throw(400, "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 {
ctx.throw(400, "Password must be specified.")
}
2021-04-19 22:34:07 +12:00
let user = {
...dbUser,
2021-04-19 22:34:07 +12:00
...ctx.request.body,
_id: _id || generateGlobalUserID(),
2021-04-19 22:34:07 +12:00
password: hashedPassword,
}
// 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,
})
ctx.body = {
_id: response.id,
_rev: response.rev,
email,
}
} catch (err) {
if (err.status === 409) {
ctx.throw(400, "User exists already")
} else {
ctx.throw(err.status, err)
}
}
}
2021-05-06 08:56:43 +12:00
exports.adminUser = async ctx => {
const db = new CouchDB(GLOBAL_DB)
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 admin user has been created.")
}
2021-05-06 08:56:43 +12:00
const { email, password } = ctx.request.body
ctx.request.body = {
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,
},
}
2021-04-24 05:54:12 +12:00
await exports.save(ctx)
}
2021-05-04 22:32:22 +12:00
exports.destroy = async ctx => {
2021-04-19 22:38:54 +12:00
const db = new CouchDB(GLOBAL_DB)
const dbUser = await db.get(ctx.params.id)
2021-04-19 22:34:07 +12:00
await db.remove(dbUser._id, dbUser._rev)
ctx.body = {
message: `User ${ctx.params.id} deleted.`,
2021-04-19 22:34:07 +12:00
}
}
exports.getSelf = async ctx => {
ctx.params = {
id: ctx.user._id,
}
// this will set the body
await exports.find(ctx)
}
exports.updateSelf = async ctx => {
const db = new CouchDB(GLOBAL_DB)
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,
})
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 => {
2021-04-19 22:38:54 +12:00
const db = new CouchDB(GLOBAL_DB)
2021-04-19 22:34:07 +12:00
const response = await db.allDocs(
getGlobalUserParams(null, {
2021-04-19 22:34:07 +12:00
include_docs: true,
})
)
2021-05-04 22:32:22 +12:00
const users = response.rows.map(row => row.doc)
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 => {
2021-04-19 22:38:54 +12:00
const db = new CouchDB(GLOBAL_DB)
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.invite = async ctx => {
2021-05-06 02:17:15 +12:00
const { email } = 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.")
}
2021-05-12 02:24:17 +12:00
await sendEmail(email, EmailTemplatePurpose.INVITATION, {
subject: "{{ company }} platform invitation",
})
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 {
const email = await checkInviteCode(inviteCode)
// only pass through certain props for accepting
ctx.request.body = {
firstName,
lastName,
password,
email,
}
2021-05-06 02:17:15 +12:00
// this will flesh out the body response
await exports.save(ctx)
} catch (err) {
ctx.throw(400, "Unable to create new user, invitation invalid.")
}
}