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-11-13 16:35:20 +01:00
|
|
|
const {
|
|
|
|
BUILTIN_LEVEL_ID_ARRAY,
|
|
|
|
} = require("../../utilities/security/accessLevels")
|
2020-05-21 15:31:23 +02:00
|
|
|
const {
|
2020-11-12 18:06:55 +01:00
|
|
|
BUILTIN_PERMISSION_NAMES,
|
|
|
|
} = require("../../utilities/security/permissions")
|
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-10-02 13:37:46 +02:00
|
|
|
const data = await database.allDocs(
|
|
|
|
getUserParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
2020-05-07 11:53:34 +02:00
|
|
|
ctx.body = data.rows.map(row => row.doc)
|
|
|
|
}
|
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-11-12 18:06:55 +01:00
|
|
|
const {
|
|
|
|
username,
|
|
|
|
password,
|
|
|
|
name,
|
|
|
|
accessLevelId,
|
|
|
|
permissions,
|
|
|
|
} = ctx.request.body
|
2020-05-21 15:31:23 +02:00
|
|
|
|
|
|
|
if (!username || !password) {
|
|
|
|
ctx.throw(400, "Username and Password Required.")
|
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
const accessLevel = await checkAccessLevel(db, accessLevelId)
|
2020-04-23 15:37:08 +02:00
|
|
|
|
2020-05-21 15:31:23 +02:00
|
|
|
if (!accessLevel) ctx.throw(400, "Invalid Access Level")
|
|
|
|
|
|
|
|
const user = {
|
2020-10-02 13:37:46 +02:00
|
|
|
_id: generateUserID(username),
|
2020-04-23 15:37:08 +02:00
|
|
|
username,
|
|
|
|
password: await bcrypt.hash(password),
|
2020-05-14 16:12:30 +02:00
|
|
|
name: name || username,
|
2020-05-07 11:53:34 +02:00
|
|
|
type: "user",
|
2020-05-21 15:31:23 +02:00
|
|
|
accessLevelId,
|
2020-11-12 18:06:55 +01:00
|
|
|
permissions: permissions || [BUILTIN_PERMISSION_NAMES.POWER],
|
2020-11-24 15:04:14 +01:00
|
|
|
tableId: ViewNames.USERS,
|
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,
|
|
|
|
username,
|
|
|
|
name,
|
|
|
|
}
|
|
|
|
} 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
|
2020-06-29 15:55:12 +02:00
|
|
|
const dbUser = db.get(ctx.request.body._id)
|
|
|
|
const newData = { ...dbUser, ...user }
|
2020-05-21 15:31:23 +02:00
|
|
|
|
2020-06-29 15:55:12 +02:00
|
|
|
const response = await db.put(newData)
|
2020-06-26 11:05:09 +02:00
|
|
|
user._rev = response.rev
|
|
|
|
|
|
|
|
ctx.status = 200
|
2020-06-29 13:14:15 +02:00
|
|
|
ctx.message = `User ${ctx.request.body.username} updated successfully.`
|
|
|
|
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-10-02 13:37:46 +02:00
|
|
|
await database.destroy(generateUserID(ctx.params.username))
|
2020-05-14 16:12:30 +02:00
|
|
|
ctx.message = `User ${ctx.params.username} deleted.`
|
|
|
|
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-10-02 13:37:46 +02:00
|
|
|
const user = await database.get(generateUserID(ctx.params.username))
|
2020-04-10 17:37:59 +02:00
|
|
|
ctx.body = {
|
2020-05-14 16:12:30 +02:00
|
|
|
username: user.username,
|
|
|
|
name: user.name,
|
|
|
|
_rev: user._rev,
|
2020-04-10 17:37:59 +02:00
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-05-21 15:31:23 +02:00
|
|
|
|
|
|
|
const checkAccessLevel = async (db, accessLevelId) => {
|
|
|
|
if (!accessLevelId) return
|
2020-11-13 16:35:20 +01:00
|
|
|
if (BUILTIN_LEVEL_ID_ARRAY.indexOf(accessLevelId) !== -1) {
|
2020-05-21 15:31:23 +02:00
|
|
|
return {
|
|
|
|
_id: accessLevelId,
|
|
|
|
name: accessLevelId,
|
|
|
|
permissions: [],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return await db.get(accessLevelId)
|
|
|
|
}
|