budibase/packages/auth/src/db/utils.js

164 lines
4.1 KiB
JavaScript
Raw Normal View History

2021-04-19 12:34:07 +02:00
const { newid } = require("../hashing")
exports.ViewNames = {
USER_BY_EMAIL: "by_email",
}
2021-04-07 12:33:16 +02:00
exports.StaticDatabases = {
GLOBAL: {
name: "global-db",
2021-04-07 12:33:16 +02:00
},
}
const DocumentTypes = {
USER: "us",
APP: "app",
2021-04-19 12:34:07 +02:00
GROUP: "group",
2021-04-20 19:14:36 +02:00
CONFIG: "config",
TEMPLATE: "template",
2021-04-07 12:33:16 +02:00
}
exports.DocumentTypes = DocumentTypes
2021-04-07 12:33:16 +02:00
const UNICODE_MAX = "\ufff0"
const SEPARATOR = "_"
exports.SEPARATOR = SEPARATOR
2021-04-19 12:34:07 +02:00
/**
* Generates a new group ID.
* @returns {string} The new group ID which the group doc can be stored under.
*/
exports.generateGroupID = () => {
return `${DocumentTypes.GROUP}${SEPARATOR}${newid()}`
}
2021-04-07 12:33:16 +02:00
/**
2021-04-19 17:16:46 +02:00
* Gets parameters for retrieving groups.
*/
exports.getGroupParams = (id = "", otherProps = {}) => {
return {
...otherProps,
startkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}`,
endkey: `${DocumentTypes.GROUP}${SEPARATOR}${id}${UNICODE_MAX}`,
}
}
/**
* Generates a new global user ID.
* @returns {string} The new user ID which the user doc can be stored under.
*/
2021-04-21 22:08:04 +02:00
exports.generateGlobalUserID = id => {
return `${DocumentTypes.USER}${SEPARATOR}${id || newid()}`
}
2021-04-19 17:16:46 +02:00
/**
* Gets parameters for retrieving users.
2021-04-07 12:33:16 +02:00
*/
exports.getGlobalUserParams = (globalId, otherProps = {}) => {
if (!globalId) {
globalId = ""
}
2021-04-07 12:33:16 +02:00
return {
...otherProps,
startkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}`,
endkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}${UNICODE_MAX}`,
2021-04-07 12:33:16 +02:00
}
}
/**
* Generates a template ID.
* @param ownerId The owner/user of the template, this could be global or a group level.
*/
exports.generateTemplateID = ownerId => {
return `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}${newid()}`
}
/**
* Gets parameters for retrieving templates. Owner ID must be specified, either global or a group level.
*/
exports.getTemplateParams = (ownerId, templateId, otherProps = {}) => {
if (!templateId) {
templateId = ""
}
2021-04-21 19:15:57 +02:00
let final
if (templateId) {
final = templateId
} else {
final = `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}${SEPARATOR}`
}
2021-04-07 12:33:16 +02:00
return {
...otherProps,
startkey: final,
endkey: `${final}${UNICODE_MAX}`,
2021-04-07 12:33:16 +02:00
}
}
2021-04-20 19:14:36 +02:00
/**
* Generates a new configuration ID.
* @returns {string} The new configuration ID which the config doc can be stored under.
*/
2021-04-22 14:46:54 +02:00
const generateConfigID = ({ type, group, user }) => {
2021-04-22 12:45:22 +02:00
const scope = [type, group, user].filter(Boolean).join(SEPARATOR)
2021-04-20 19:14:36 +02:00
2021-04-22 12:45:22 +02:00
return `${DocumentTypes.CONFIG}${SEPARATOR}${scope}`
2021-04-20 19:14:36 +02:00
}
/**
* Gets parameters for retrieving configurations.
*/
2021-04-22 14:46:54 +02:00
const getConfigParams = ({ type, group, user }, otherProps = {}) => {
2021-04-22 12:45:22 +02:00
const scope = [type, group, user].filter(Boolean).join(SEPARATOR)
2021-04-20 19:14:36 +02:00
return {
...otherProps,
2021-04-22 12:45:22 +02:00
startkey: `${DocumentTypes.CONFIG}${SEPARATOR}${scope}`,
endkey: `${DocumentTypes.CONFIG}${SEPARATOR}${scope}${UNICODE_MAX}`,
2021-04-20 19:14:36 +02:00
}
}
2021-04-22 14:46:54 +02:00
/**
* Returns the most granular configuration document from the DB based on the type, group and userID passed.
* @param {*} db - db instance to quer
* @param {Object} scopes - the type, group and userID scopes of the configuration.
* @returns The most granular configuration document based on the scope.
*/
const determineScopedConfig = async function(db, { type, user, group }) {
const response = await db.allDocs(
getConfigParams(
{ type, user, group },
{
include_docs: true,
}
)
)
const configs = response.rows.map(row => row.doc)
// Find the config with the most granular scope based on context
const scopedConfig = configs.find(config => {
// Config is specific to a user and a group
if (config._id.includes(generateConfigID({ type, user, group }))) {
return config
}
// Config is specific to a user
if (config._id.includes(generateConfigID({ type, user }))) {
return config
}
// Config is specific to a group only
if (config._id.includes(generateConfigID({ type, group }))) {
return config
}
// Config specific to a config type only
return config
})
return scopedConfig
}
exports.generateConfigID = generateConfigID
exports.getConfigParams = getConfigParams
exports.determineScopedConfig = determineScopedConfig