refs #6085 model y back
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Carlos Satorres 2023-11-29 13:55:44 +01:00
parent e548ef4dae
commit 9a3efdd6fe
2 changed files with 99 additions and 0 deletions

View File

@ -0,0 +1,70 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
require('../methods/notification/getList')(Self);
Self.observe('before save', async function(ctx) {
await checkModifyPermission(ctx);
});
Self.observe('before delete', async function(ctx) {
await checkModifyPermission(ctx);
});
async function checkModifyPermission(ctx) {
const models = Self.app.models;
const instance = ctx.instance;
const userId = ctx.options.accessToken.userId;
let mailAliasFk;
let roleFk;
if (instance) {
mailAliasFk = instance.mailAliasFk;
roleFk = instance.roleFk;
} else {
const mailAliasAcl = await models.MailAlias.findById(ctx.where.id);
mailAliasFk = mailAliasAcl.id;
roleFk = mailAliasAcl.roleFk;
}
const role = await models.VnUser.findById(roleFk, {fields: ['id', 'role']});
const available = await Self.getAvailable(roleFk);
const hasAcl = available.has(mailAliasFk);
if (!hasAcl || (userId.role != role))
throw new UserError('The alias cant be modified');
}
Self.getAvailable = async function(userId, options) {
const availableMailAliasMap = new Map();
const models = Self.app.models;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
const roles = await models.RoleMapping.find({
fields: ['roleId'],
where: {principalId: userId}
}, myOptions);
const availableMailAlias = await models.MailAliasAcl.find({
fields: ['mailAliasFk', 'roleFk'],
include: {relation: 'roleFk'},
where: {
roleFk: {
inq: roles.map(role => role.roleId),
},
}
}, myOptions);
for (available of availableMailAlias) {
availableMailAliasMap.set(available.mailAliasFk, {
mailAliasFk: available.mailAliasFk,
});
}
return availableMailAliasMap;
};
};

View File

@ -0,0 +1,29 @@
{
"name": "mailAliasACL",
"base": "VnModel",
"options": {
"mysql": {
"table": "account.mailAliasACL"
}
},
"properties": {
"mailAliasFk": {
"type": "number"
},
"roleFk": {
"type": "number"
}
},
"relations": {
"mailAlias": {
"type": "belongsTo",
"model": "VnUser",
"foreignKey": "mailAliasFk"
},
"role": {
"type": "belongsTo",
"model": "VnUser",
"foreignKey": "roleFk"
}
}
}