2023-07-12 11:55:57 +00:00
|
|
|
|
2024-01-29 12:48:02 +00:00
|
|
|
const ForbiddenError = require('vn-loopback/util/forbiddenError');
|
2023-07-12 11:55:57 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2023-12-01 12:58:45 +00:00
|
|
|
Self.rewriteDbError(function(err) {
|
|
|
|
if (err.code === 'ER_DUP_ENTRY')
|
|
|
|
return new UserError(`You already have the mailAlias`);
|
|
|
|
return err;
|
|
|
|
});
|
|
|
|
|
2024-01-29 12:48:02 +00:00
|
|
|
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
|
|
|
});
|
2024-01-29 12:48:02 +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
|
|
|
});
|
2023-07-12 11:55:57 +00:00
|
|
|
|
2023-12-01 12:58:45 +00:00
|
|
|
async function checkModifyPermission(ctx, mailAliasFk) {
|
2023-07-12 11:55:57 +00:00
|
|
|
const models = Self.app.models;
|
2024-01-29 12:48:02 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2023-07-12 11:55:57 +00:00
|
|
|
|
2024-01-29 12:48:02 +00:00
|
|
|
const canEditAlias = await models.ACL.checkAccessAcl(ctx,
|
|
|
|
'MailAliasAccount', 'canEditAlias', 'WRITE');
|
|
|
|
if (canEditAlias) return;
|
2023-07-12 11:55:57 +00:00
|
|
|
|
2024-01-29 12:48:02 +00:00
|
|
|
const allowedRoles = await models.MailAliasAcl.find({
|
|
|
|
fields: ['roleFk'],
|
|
|
|
where: {mailAliasFk}
|
2023-12-11 09:16:46 +00:00
|
|
|
});
|
2024-01-29 12:48:02 +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
|
|
|
}
|
2023-07-12 11:55:57 +00:00
|
|
|
};
|