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

124 lines
2.9 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-04-19 22:34:07 +12:00
const { UserStatus } = require("../../../constants")
const FIRST_USER_EMAIL = "test@test.com"
const FIRST_USER_PASSWORD = "test"
2021-04-19 22:38:54 +12:00
const GLOBAL_DB = StaticDatabases.GLOBAL.name
2021-04-19 22:34:07 +12:00
2021-04-24 05:54:12 +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
const dbUser = await getGlobalUserByEmail(email)
if (dbUser != null && (dbUser._id !== _id || Array.isArray(dbUser))) {
ctx.throw(400, "Email address already in use.")
}
// 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,
}
// add the active status to a user if its not provided
if (user.status == null) {
user.status = UserStatus.ACTIVE
}
try {
const response = await db.post({
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)
}
}
}
exports.firstUser = async ctx => {
2021-04-29 01:28:25 +12:00
const existing = await getGlobalUserByEmail(FIRST_USER_EMAIL)
const params = {}
if (existing) {
params._id = existing._id
params._rev = existing._rev
}
ctx.request.body = {
2021-04-29 01:28:25 +12:00
...params,
email: FIRST_USER_EMAIL,
password: FIRST_USER_PASSWORD,
roles: {},
builder: {
global: true,
},
}
2021-04-24 05:54:12 +12:00
await exports.save(ctx)
}
2021-04-24 05:54:12 +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
}
}
// called internally by app server user fetch
2021-04-24 05:54:12 +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,
})
)
const users = response.rows.map(row => row.doc)
// 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-04-24 05:54:12 +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
}