2019-06-06 11:59:11 +00:00
|
|
|
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
|
2019-06-06 11:59:11 +00:00
|
|
|
* @return {Boolean} True for user with read privileges
|
|
|
|
*/
|
2019-11-22 12:46:38 +00:00
|
|
|
Self.hasReadRole = async(ctx, id, options) => {
|
2019-06-06 11:59:11 +00:00
|
|
|
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-06-06 11:59:11 +00:00
|
|
|
|
2019-11-22 12:46:38 +00:00
|
|
|
return await hasRole(ctx, dmsType, options);
|
2019-06-06 11:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2019-06-06 11:59:11 +00:00
|
|
|
* @return {Boolean} True for user with write privileges
|
|
|
|
*/
|
2019-11-22 12:46:38 +00:00
|
|
|
Self.hasWriteRole = async(ctx, id, options) => {
|
2019-06-06 11:59:11 +00:00
|
|
|
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-06-06 11:59:11 +00:00
|
|
|
|
2019-11-22 12:46:38 +00:00
|
|
|
return await hasRole(ctx, dmsType, options);
|
2019-06-06 11:59:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-06-06 11:59:11 +00:00
|
|
|
*/
|
2019-11-22 12:46:38 +00:00
|
|
|
async function hasRole(ctx, dmsType, options) {
|
2019-06-06 11:59:11 +00:00
|
|
|
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);
|
2019-06-06 11:59:11 +00:00
|
|
|
|
|
|
|
if (isRoot || hasRequiredRole)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|