2021-04-19 12:34:07 +02:00
|
|
|
const { newid } = require("../hashing")
|
2021-05-13 12:06:08 +02:00
|
|
|
const Replication = require("./Replication")
|
2021-04-19 12:34:07 +02:00
|
|
|
|
2021-05-13 14:29:53 +02:00
|
|
|
const UNICODE_MAX = "\ufff0"
|
|
|
|
const SEPARATOR = "_"
|
|
|
|
|
2021-04-19 18:31:47 +02:00
|
|
|
exports.ViewNames = {
|
|
|
|
USER_BY_EMAIL: "by_email",
|
|
|
|
}
|
|
|
|
|
2021-04-07 12:33:16 +02:00
|
|
|
exports.StaticDatabases = {
|
2021-04-16 19:09:34 +02:00
|
|
|
GLOBAL: {
|
|
|
|
name: "global-db",
|
2021-04-07 12:33:16 +02:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
const DocumentTypes = {
|
|
|
|
USER: "us",
|
2021-04-19 12:34:07 +02:00
|
|
|
GROUP: "group",
|
2021-04-20 19:14:36 +02:00
|
|
|
CONFIG: "config",
|
2021-04-21 17:42:44 +02:00
|
|
|
TEMPLATE: "template",
|
2021-05-13 14:29:53 +02:00
|
|
|
APP: "app",
|
|
|
|
APP_DEV: "app_dev",
|
2021-04-07 12:33:16 +02:00
|
|
|
}
|
|
|
|
|
2021-04-08 12:20:37 +02:00
|
|
|
exports.DocumentTypes = DocumentTypes
|
2021-05-13 14:29:53 +02:00
|
|
|
exports.APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
|
|
|
exports.APP_DEV_PREFIX = DocumentTypes.APP_DEV + SEPARATOR
|
2021-04-13 14:26:13 +02:00
|
|
|
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}`,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-04-20 18:17:44 +02:00
|
|
|
* Generates a new global user ID.
|
|
|
|
* @returns {string} The new user ID which the user doc can be stored under.
|
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.generateGlobalUserID = id => {
|
2021-04-21 22:08:04 +02:00
|
|
|
return `${DocumentTypes.USER}${SEPARATOR}${id || newid()}`
|
2021-04-20 18:17:44 +02:00
|
|
|
}
|
|
|
|
|
2021-04-19 17:16:46 +02:00
|
|
|
/**
|
|
|
|
* Gets parameters for retrieving users.
|
2021-04-07 12:33:16 +02:00
|
|
|
*/
|
2021-04-21 17:42:44 +02:00
|
|
|
exports.getGlobalUserParams = (globalId, otherProps = {}) => {
|
2021-04-19 18:31:47 +02:00
|
|
|
if (!globalId) {
|
|
|
|
globalId = ""
|
2021-04-09 16:11:49 +02:00
|
|
|
}
|
2021-04-07 12:33:16 +02:00
|
|
|
return {
|
|
|
|
...otherProps,
|
2021-04-19 18:31:47 +02:00
|
|
|
startkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}`,
|
|
|
|
endkey: `${DocumentTypes.USER}${SEPARATOR}${globalId}${UNICODE_MAX}`,
|
2021-04-07 12:33:16 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 17:42:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a template ID.
|
|
|
|
* @param ownerId The owner/user of the template, this could be global or a group level.
|
|
|
|
*/
|
2021-05-04 12:32:22 +02:00
|
|
|
exports.generateTemplateID = ownerId => {
|
2021-05-10 15:41:52 +02:00
|
|
|
return `${DocumentTypes.TEMPLATE}${SEPARATOR}${ownerId}${SEPARATOR}${newid()}`
|
2021-04-21 17:42:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-09 16:11:49 +02:00
|
|
|
}
|
2021-04-07 12:33:16 +02:00
|
|
|
return {
|
|
|
|
...otherProps,
|
2021-04-21 17:42:44 +02:00
|
|
|
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.
|
2021-04-22 15:53:19 +02:00
|
|
|
* @param {Object} db - db instance to query
|
2021-04-22 14:46:54 +02:00
|
|
|
* @param {Object} scopes - the type, group and userID scopes of the configuration.
|
|
|
|
* @returns The most granular configuration document based on the scope.
|
|
|
|
*/
|
2021-05-06 11:51:21 +02:00
|
|
|
const getScopedFullConfig = async function (db, { type, user, group }) {
|
2021-04-22 14:46:54 +02:00
|
|
|
const response = await db.allDocs(
|
|
|
|
getConfigParams(
|
|
|
|
{ type, user, group },
|
|
|
|
{
|
|
|
|
include_docs: true,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
2021-05-04 18:31:06 +02:00
|
|
|
|
|
|
|
function determineScore(row) {
|
2021-04-22 15:07:00 +02:00
|
|
|
const config = row.doc
|
2021-04-22 14:46:54 +02:00
|
|
|
|
|
|
|
// Config is specific to a user and a group
|
|
|
|
if (config._id.includes(generateConfigID({ type, user, group }))) {
|
2021-05-04 18:31:06 +02:00
|
|
|
return 4
|
2021-04-22 15:07:00 +02:00
|
|
|
} else if (config._id.includes(generateConfigID({ type, user }))) {
|
|
|
|
// Config is specific to a user only
|
2021-05-04 18:31:06 +02:00
|
|
|
return 3
|
2021-04-22 15:07:00 +02:00
|
|
|
} else if (config._id.includes(generateConfigID({ type, group }))) {
|
|
|
|
// Config is specific to a group only
|
2021-05-04 18:31:06 +02:00
|
|
|
return 2
|
2021-04-22 15:07:00 +02:00
|
|
|
} else if (config._id.includes(generateConfigID({ type }))) {
|
|
|
|
// Config is specific to a type only
|
2021-05-04 18:31:06 +02:00
|
|
|
return 1
|
2021-04-22 14:46:54 +02:00
|
|
|
}
|
2021-05-04 18:31:06 +02:00
|
|
|
return 0
|
|
|
|
}
|
2021-04-22 14:46:54 +02:00
|
|
|
|
2021-04-22 15:07:00 +02:00
|
|
|
// Find the config with the most granular scope based on context
|
2021-05-04 19:14:13 +02:00
|
|
|
const scopedConfig = response.rows.sort(
|
|
|
|
(a, b) => determineScore(a) - determineScore(b)
|
|
|
|
)[0]
|
2021-04-22 15:07:00 +02:00
|
|
|
|
2021-05-05 21:58:31 +02:00
|
|
|
return scopedConfig && scopedConfig.doc
|
2021-04-22 14:46:54 +02:00
|
|
|
}
|
|
|
|
|
2021-05-06 11:51:21 +02:00
|
|
|
async function getScopedConfig(db, params) {
|
|
|
|
const configDoc = await getScopedFullConfig(db, params)
|
|
|
|
return configDoc && configDoc.config ? configDoc.config : configDoc
|
|
|
|
}
|
|
|
|
|
2021-05-13 12:06:08 +02:00
|
|
|
exports.Replication = Replication
|
2021-05-06 11:51:21 +02:00
|
|
|
exports.getScopedConfig = getScopedConfig
|
2021-04-22 14:46:54 +02:00
|
|
|
exports.generateConfigID = generateConfigID
|
|
|
|
exports.getConfigParams = getConfigParams
|
2021-05-06 11:51:21 +02:00
|
|
|
exports.getScopedFullConfig = getScopedFullConfig
|