diff --git a/modules/account/back/methods/account/add-mail-alias.js b/modules/account/back/methods/account/add-mail-alias.js new file mode 100644 index 000000000..814ddaf99 --- /dev/null +++ b/modules/account/back/methods/account/add-mail-alias.js @@ -0,0 +1,34 @@ + +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('addMailAlias', { + description: 'Add a mail alias', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'number', + description: 'The user id', + http: {source: 'path'} + }, { + arg: 'mailAlias', + type: 'number', + description: 'The mail alias', + required: true + }], + http: { + path: `/:id/addMailAlias`, + verb: 'POST' + } + }); + + Self.addMailAlias = async function(ctx, id, mailAlias) { + const models = Self.app.models; + + const isAuthorized = await models.Worker.isAuthorized(ctx, id); + if (!isAuthorized) + throw new UserError(`You don't have enough privileges`); + + return models.MailAliasAccount.create({mailAlias: mailAlias, account: id}); + }; +}; diff --git a/modules/account/back/methods/account/delete-mail-alias.js b/modules/account/back/methods/account/delete-mail-alias.js new file mode 100644 index 000000000..018a1e0b5 --- /dev/null +++ b/modules/account/back/methods/account/delete-mail-alias.js @@ -0,0 +1,29 @@ + +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethodCtx('deleteMailAlias', { + description: 'Delete a mail alias', + accessType: 'WRITE', + accepts: [{ + arg: 'id', + type: 'number', + description: 'The mail alias account to id', + http: {source: 'path'} + }], + http: { + path: `/:id/deleteMailAlias`, + verb: 'POST' + } + }); + + Self.deleteMailAlias = async function(ctx, id) { + const models = Self.app.models; + + const isAuthorized = await models.Worker.isAuthorized(ctx, id); + if (!isAuthorized) + throw new UserError(`You don't have enough privileges`); + + return models.MailAliasAccount.destroyById(id); + }; +}; diff --git a/modules/account/back/models/account.js b/modules/account/back/models/account.js index ce00c4f58..e44d10547 100644 --- a/modules/account/back/models/account.js +++ b/modules/account/back/models/account.js @@ -8,4 +8,6 @@ module.exports = Self => { require('../methods/account/change-password')(Self); require('../methods/account/set-password')(Self); require('../methods/account/change-mail-forwarding')(Self); + require('../methods/account/add-mail-alias')(Self); + require('../methods/account/delete-mail-alias')(Self); }; diff --git a/modules/account/front/aliases/index.js b/modules/account/front/aliases/index.js index c7c5cb82d..70bcc5d44 100644 --- a/modules/account/front/aliases/index.js +++ b/modules/account/front/aliases/index.js @@ -34,7 +34,10 @@ export default class Controller extends Section { } onAddSave() { - return this.$http.post(`MailAliasAccounts`, this.addData) + const params = { + mailAlias: this.addData.mailAlias + }; + return this.$http.post(`Accounts/${this.$params.id}/addMailAlias`, params) .then(() => this.refresh()) .then(() => this.vnApp.showSuccess( this.$t('Subscribed to alias!')) @@ -42,7 +45,7 @@ export default class Controller extends Section { } onRemove(row) { - return this.$http.delete(`MailAliasAccounts/${row.id}`) + return this.$http.post(`Accounts/${row.id}/deleteMailAlias`) .then(() => { this.$.data.splice(this.$.data.indexOf(row), 1); this.vnApp.showSuccess(this.$t('Unsubscribed from alias!'));