2023-07-12 11:55:57 +00:00
|
|
|
|
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
|
|
|
|
module.exports = Self => {
|
|
|
|
require('../methods/mail-alias-account/addAlias')(Self);
|
|
|
|
require('../methods/mail-alias-account/removeAlias')(Self);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if current user has
|
2023-07-12 12:04:17 +00:00
|
|
|
* grant to add/remove alias
|
2023-07-12 11:55:57 +00:00
|
|
|
*
|
|
|
|
* @param {Object} ctx - Request context
|
|
|
|
* @param {Interger} mailAlias - mailAlias id
|
|
|
|
* @param {Object} options - Query options
|
|
|
|
* @return {Boolean} True for user with grant
|
|
|
|
*/
|
|
|
|
Self.hasGrant = async function(ctx, mailAlias, options) {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
|
|
|
|
const canEditAlias = await models.ACL.checkAccessAcl(ctx, 'MailAliasAccount', 'canEditAlias', 'WRITE');
|
|
|
|
if (canEditAlias) return true;
|
|
|
|
|
|
|
|
const user = await models.VnUser.findById(userId, {fields: ['hasGrant']}, options);
|
|
|
|
if (!user.hasGrant)
|
|
|
|
throw new UserError(`You don't have grant privilege`);
|
|
|
|
|
|
|
|
const account = await models.Account.findById(userId, {
|
|
|
|
fields: ['id'],
|
|
|
|
include: {
|
|
|
|
relation: 'aliases',
|
|
|
|
scope: {
|
|
|
|
fields: ['mailAlias']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const aliases = account.aliases().map(alias => alias.mailAlias);
|
|
|
|
|
|
|
|
const hasAlias = aliases.includes(mailAlias);
|
|
|
|
if (!hasAlias)
|
|
|
|
throw new UserError(`You cannot assign/remove an alias that you are not assigned to`);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
};
|