salix/back/models/dms-type.js

66 lines
2.0 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
/**
* Checks if current user has
* read privileges over a dms
*
* @param {Object} ctx - Request context
* @param {Interger} id - DmsType id
2019-11-22 12:46:38 +00:00
* @param {Object} options - Query options
* @return {Boolean} True for user with read privileges
*/
2019-11-22 12:46:38 +00:00
Self.hasReadRole = async(ctx, id, options) => {
const models = Self.app.models;
const dmsType = await models.DmsType.findById(id, {
include: {
relation: 'readRole'
}
2019-11-22 12:46:38 +00:00
}, options);
2019-11-22 12:46:38 +00:00
return await hasRole(ctx, dmsType, options);
};
/**
* Checks if current user has
* write privileges over a dms
*
* @param {Object} ctx - Request context
* @param {Interger} id - DmsType id
2019-11-22 12:46:38 +00:00
* @param {Object} options - Query options
* @return {Boolean} True for user with write privileges
*/
2019-11-22 12:46:38 +00:00
Self.hasWriteRole = async(ctx, id, options) => {
const models = Self.app.models;
const dmsType = await models.DmsType.findById(id, {
include: {
relation: 'writeRole'
}
2019-11-22 12:46:38 +00:00
}, options);
2019-11-22 12:46:38 +00:00
return await hasRole(ctx, dmsType, options);
};
/**
* Checks if current user has
* read or write privileges
* @param {Object} ctx - Context
* @param {Object} dmsType - Dms type [read/write]
2019-11-22 12:46:38 +00:00
* @param {Object} options - Query options
*/
2019-11-22 12:46:38 +00:00
async function hasRole(ctx, dmsType, options) {
const models = Self.app.models;
const myUserId = ctx.req.accessToken.userId;
const readRole = dmsType.readRole() && dmsType.readRole().name;
const writeRole = dmsType.writeRole() && dmsType.writeRole().name;
const requiredRole = readRole || writeRole;
2019-11-22 12:46:38 +00:00
const hasRequiredRole = await models.Account.hasRole(myUserId, requiredRole, options);
const isRoot = await models.Account.hasRole(myUserId, 'root', options);
if (isRoot || hasRequiredRole)
return true;
return false;
}
};