From 191fe4ebf6b85fed4c5103201fafea7b1fc81923 Mon Sep 17 00:00:00 2001 From: vicent Date: Tue, 23 May 2023 12:40:49 +0200 Subject: [PATCH] =?UTF-8?q?refs=20#5468=20feat:=20a=C3=B1adidas=20comproba?= =?UTF-8?q?ciones=20acls=20en=20el=20back=20en=20'Alias=20de=20correo'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../back/methods/account/add-mail-alias.js | 34 +++++++++++++++++++ .../back/methods/account/delete-mail-alias.js | 29 ++++++++++++++++ modules/account/back/models/account.js | 2 ++ modules/account/front/aliases/index.js | 7 ++-- 4 files changed, 70 insertions(+), 2 deletions(-) create mode 100644 modules/account/back/methods/account/add-mail-alias.js create mode 100644 modules/account/back/methods/account/delete-mail-alias.js 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!'));