salix/modules/account/back/models/mail-alias-account.js

46 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

const ForbiddenError = require('vn-loopback/util/forbiddenError');
module.exports = Self => {
Self.rewriteDbError(function(err) {
if (err.code === 'ER_DUP_ENTRY')
return new UserError(`You already have the mailAlias`);
return err;
});
Self.beforeRemote('create', async function(ctx) {
const mailAlias = ctx.args.data?.mailAlias;
if (!mailAlias) return;
await checkModifyPermission(ctx, mailAlias);
2023-07-12 13:07:06 +00:00
});
Self.beforeRemote('deleteById', async function(ctx) {
const instance = await Self.findById(ctx.args.id,
{fields: ['mailAlias']}
);
await checkModifyPermission(ctx, instance.mailAlias);
2023-07-12 13:07:06 +00:00
});
async function checkModifyPermission(ctx, mailAliasFk) {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const canEditAlias = await models.ACL.checkAccessAcl(ctx,
'MailAliasAccount', 'canEditAlias', 'WRITE');
if (canEditAlias) return;
const allowedRoles = await models.MailAliasAcl.find({
fields: ['roleFk'],
where: {mailAliasFk}
2023-12-11 09:16:46 +00:00
});
const nRoles = allowedRoles.length &&
await models.RoleMapping.count({
principalId: userId,
principalType: 'USER',
roleId: {inq: allowedRoles.map(x => x.roleFk)}
});
if (!nRoles)
throw new ForbiddenError('You are not allowed to modify the alias');
2023-12-11 07:54:58 +00:00
}
};