2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
|
|
|
const bcrypt = require("../../utilities/bcrypt")
|
2020-11-24 15:04:14 +01:00
|
|
|
const { generateUserID, getUserParams, ViewNames } = require("../../db/utils")
|
2020-12-02 18:08:25 +01:00
|
|
|
const { getRole } = require("../../utilities/security/roles")
|
2021-02-22 13:29:49 +01:00
|
|
|
const { UserStatus } = require("../../constants")
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const database = new CouchDB(ctx.user.appId)
|
2020-12-08 18:33:08 +01:00
|
|
|
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 11:53:34 +02:00
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.create = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-12-08 12:42:29 +01:00
|
|
|
const { email, password, roleId } = ctx.request.body
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2020-12-04 13:22:45 +01:00
|
|
|
if (!email || !password) {
|
|
|
|
ctx.throw(400, "email and Password Required.")
|
2020-05-21 15:31:23 +02:00
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2020-12-02 18:08:25 +01:00
|
|
|
const role = await getRole(ctx.user.appId, roleId)
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2020-12-02 14:20:56 +01:00
|
|
|
if (!role) ctx.throw(400, "Invalid Role")
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2020-12-08 18:33:08 +01:00
|
|
|
const hashedPassword = await bcrypt.hash(password)
|
2020-05-21 15:31:23 +02:00
|
|
|
const user = {
|
2020-12-08 18:33:08 +01:00
|
|
|
...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-04 13:22:45 +01:00
|
|
|
_id: generateUserID(email),
|
2020-05-07 11:53:34 +02:00
|
|
|
type: "user",
|
2020-12-08 18:33:08 +01:00
|
|
|
password: hashedPassword,
|
2020-11-24 15:04:14 +01:00
|
|
|
tableId: ViewNames.USERS,
|
2020-05-21 15:31:23 +02:00
|
|
|
}
|
2021-02-22 12:39:58 +01:00
|
|
|
// add the active status to a user if its not provided
|
2021-02-22 13:29:49 +01:00
|
|
|
if (user.status == null) {
|
|
|
|
user.status = UserStatus.ACTIVE
|
2021-02-22 12:39:58 +01:00
|
|
|
}
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2020-11-06 13:30:30 +01: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-04 13:22:45 +01:00
|
|
|
email,
|
2020-11-06 13:30:30 +01:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.status === 409) {
|
|
|
|
ctx.throw(400, "User exists already")
|
|
|
|
} else {
|
|
|
|
ctx.throw(err.status, err)
|
|
|
|
}
|
2020-04-10 17:37:59 +02:00
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-09 17:53:48 +02:00
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.update = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.user.appId)
|
2020-06-26 11:05:09 +02:00
|
|
|
const user = ctx.request.body
|
2021-02-22 12:39:58 +01:00
|
|
|
let dbUser
|
|
|
|
// get user incase password removed
|
|
|
|
if (user._id) {
|
|
|
|
dbUser = await db.get(user._id)
|
|
|
|
}
|
2020-12-08 18:33:08 +01:00
|
|
|
if (user.password) {
|
|
|
|
user.password = await bcrypt.hash(user.password)
|
|
|
|
} else {
|
|
|
|
delete user.password
|
|
|
|
}
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2021-02-22 12:39:58 +01:00
|
|
|
const response = await db.put({
|
|
|
|
password: dbUser.password,
|
|
|
|
...user,
|
|
|
|
})
|
2020-06-26 11:05:09 +02:00
|
|
|
user._rev = response.rev
|
|
|
|
|
|
|
|
ctx.status = 200
|
2020-12-04 13:22:45 +01:00
|
|
|
ctx.message = `User ${ctx.request.body.email} updated successfully.`
|
2020-06-29 13:14:15 +02:00
|
|
|
ctx.body = response
|
2020-06-26 11:05:09 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.destroy = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const database = new CouchDB(ctx.user.appId)
|
2020-12-04 13:22:45 +01:00
|
|
|
await database.destroy(generateUserID(ctx.params.email))
|
|
|
|
ctx.message = `User ${ctx.params.email} deleted.`
|
2020-05-14 16:12:30 +02:00
|
|
|
ctx.status = 200
|
|
|
|
}
|
|
|
|
|
2020-06-29 15:56:41 +02:00
|
|
|
exports.find = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const database = new CouchDB(ctx.user.appId)
|
2020-12-09 11:52:18 +01:00
|
|
|
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-10 17:37:59 +02:00
|
|
|
}
|
2020-12-09 11:52:18 +01:00
|
|
|
ctx.body = user
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|