1
0
Fork 0
mirror of synced 2024-06-28 02:50:50 +12:00
budibase/packages/server/src/api/controllers/user.js

112 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const bcrypt = require("../../utilities/bcrypt")
2020-11-25 03:04:14 +13:00
const { generateUserID, getUserParams, ViewNames } = require("../../db/utils")
const { getRole } = require("../../utilities/security/roles")
const { UserStatus } = require("../../constants")
2020-04-08 04:25:09 +12:00
2020-06-30 01:56:41 +12:00
exports.fetch = async function(ctx) {
const database = new CouchDB(ctx.user.appId)
const users = (
await database.allDocs(
getUserParams(null, {
include_docs: true,
})
)
).rows.map(row => row.doc)
// user hashed password shouldn't ever be returned
for (let user of users) {
delete user.password
}
ctx.body = users
2020-05-07 21:53:34 +12:00
}
2020-04-08 04:25:09 +12:00
2020-06-30 01:56:41 +12:00
exports.create = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
const { email, password, roleId } = ctx.request.body
2020-05-22 01:31:23 +12:00
2020-12-05 01:22:45 +13:00
if (!email || !password) {
ctx.throw(400, "email and Password Required.")
2020-05-22 01:31:23 +12:00
}
const role = await getRole(ctx.user.appId, roleId)
if (!role) ctx.throw(400, "Invalid Role")
2020-05-22 01:31:23 +12:00
const hashedPassword = await bcrypt.hash(password)
2020-05-22 01:31:23 +12:00
const user = {
...ctx.request.body,
// these must all be after the object spread, make sure
// any values are overwritten, generateUserID will always
// generate the same ID for the user as it is not UUID based
2020-12-05 01:22:45 +13:00
_id: generateUserID(email),
2020-05-07 21:53:34 +12:00
type: "user",
password: hashedPassword,
2020-11-25 03:04:14 +13:00
tableId: ViewNames.USERS,
2020-05-22 01:31:23 +12:00
}
// add the active status to a user if its not provided
if (user.status == null) {
user.status = UserStatus.ACTIVE
}
2020-05-22 01:31:23 +12:00
try {
const response = await db.post(user)
ctx.status = 200
ctx.message = "User created successfully."
ctx.userId = response._id
ctx.body = {
_rev: response.rev,
2020-12-05 01:22:45 +13:00
email,
}
} catch (err) {
if (err.status === 409) {
ctx.throw(400, "User exists already")
} else {
ctx.throw(err.status, err)
}
2020-04-11 03:37:59 +12:00
}
2020-05-07 21:53:34 +12:00
}
2020-04-10 03:53:48 +12:00
2020-06-30 01:56:41 +12:00
exports.update = async function(ctx) {
const db = new CouchDB(ctx.user.appId)
2020-06-26 21:05:09 +12:00
const user = ctx.request.body
let dbUser
// get user incase password removed
if (user._id) {
dbUser = await db.get(user._id)
}
if (user.password) {
user.password = await bcrypt.hash(user.password)
} else {
delete user.password
}
2020-05-22 01:31:23 +12:00
const response = await db.put({
password: dbUser.password,
...user,
})
2020-06-26 21:05:09 +12:00
user._rev = response.rev
ctx.status = 200
2020-12-05 01:22:45 +13:00
ctx.message = `User ${ctx.request.body.email} updated successfully.`
ctx.body = response
2020-06-26 21:05:09 +12:00
}
2020-06-30 01:56:41 +12:00
exports.destroy = async function(ctx) {
const database = new CouchDB(ctx.user.appId)
2020-12-05 01:22:45 +13:00
await database.destroy(generateUserID(ctx.params.email))
ctx.message = `User ${ctx.params.email} deleted.`
2020-05-15 02:12:30 +12:00
ctx.status = 200
}
2020-06-30 01:56:41 +12:00
exports.find = async function(ctx) {
const database = new CouchDB(ctx.user.appId)
let lookup = ctx.params.email
? generateUserID(ctx.params.email)
: ctx.params.userId
const user = await database.get(lookup)
if (user) {
delete user.password
2020-04-11 03:37:59 +12:00
}
ctx.body = user
2020-05-07 21:53:34 +12:00
}