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

79 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const {
generateUserMetadataID,
getUserMetadataParams,
} = require("../../db/utils")
const { InternalTables } = require("../../db/utils")
2021-06-09 05:29:34 +12:00
const { getGlobalUsers } = require("../../utilities/global")
const { getFullUser } = require("../../utilities/users")
function removeGlobalProps(user) {
// make sure to always remove some of the global user props
delete user.password
delete user.roles
delete user.builder
return user
}
2021-05-03 19:31:09 +12:00
exports.fetchMetadata = async function (ctx) {
const database = new CouchDB(ctx.appId)
const global = await getGlobalUsers(ctx.appId)
const metadata = (
await database.allDocs(
getUserMetadataParams(null, {
include_docs: true,
})
)
2021-05-04 22:32:22 +12:00
).rows.map(row => row.doc)
const users = []
for (let user of global) {
// find the metadata that matches up to the global ID
2021-05-04 22:32:22 +12:00
const info = metadata.find(meta => meta._id.includes(user._id))
// remove these props, not for the correct DB
users.push({
...user,
...info,
tableId: InternalTables.USER_METADATA,
// make sure the ID is always a local ID, not a global one
_id: generateUserMetadataID(user._id),
})
}
ctx.body = users
2020-05-07 21:53:34 +12:00
}
2020-04-08 04:25:09 +12:00
2021-05-03 19:31:09 +12:00
exports.updateSelfMetadata = async function (ctx) {
// overwrite the ID with current users
2021-04-22 22:45:22 +12:00
ctx.request.body._id = ctx.user._id
// make sure no stale rev
delete ctx.request.body._rev
await exports.updateMetadata(ctx)
}
2021-05-03 19:31:09 +12:00
exports.updateMetadata = async function (ctx) {
const appId = ctx.appId
const db = new CouchDB(appId)
const user = removeGlobalProps(ctx.request.body)
const metadata = {
tableId: InternalTables.USER_METADATA,
...user,
}
ctx.body = await db.put(metadata)
2020-06-26 21:05:09 +12:00
}
2021-05-03 19:31:09 +12:00
exports.destroyMetadata = async function (ctx) {
const db = new CouchDB(ctx.appId)
try {
const dbUser = await db.get(ctx.params.id)
await db.remove(dbUser._id, dbUser._rev)
} catch (err) {
// error just means the global user has no config in this app
}
ctx.body = {
message: `User metadata ${ctx.params.id} deleted.`,
}
2020-05-15 02:12:30 +12:00
}
2021-05-03 19:31:09 +12:00
exports.findMetadata = async function (ctx) {
ctx.body = await getFullUser(ctx, ctx.params.id)
2020-05-07 21:53:34 +12:00
}