From 7aaaf5492cebb382e1474ad6a6384843f9ca17e1 Mon Sep 17 00:00:00 2001 From: vicent Date: Wed, 12 Jul 2023 15:07:06 +0200 Subject: [PATCH] refs #5887 refacotr: move code to hook --- back/methods/vn-user/specs/addAlias.spec.js | 68 ------------------- db/changes/232601/01-aclAddAlias.sql | 7 +- .../methods/mail-alias-account/addAlias.js | 45 ------------ .../methods/mail-alias-account/removeAlias.js | 51 -------------- .../account/back/models/mail-alias-account.js | 25 ++++--- modules/account/front/aliases/index.js | 14 ++-- modules/account/front/aliases/index.spec.js | 9 +-- 7 files changed, 32 insertions(+), 187 deletions(-) delete mode 100644 back/methods/vn-user/specs/addAlias.spec.js delete mode 100644 modules/account/back/methods/mail-alias-account/addAlias.js delete mode 100644 modules/account/back/methods/mail-alias-account/removeAlias.js diff --git a/back/methods/vn-user/specs/addAlias.spec.js b/back/methods/vn-user/specs/addAlias.spec.js deleted file mode 100644 index 880c08139..000000000 --- a/back/methods/vn-user/specs/addAlias.spec.js +++ /dev/null @@ -1,68 +0,0 @@ -const {models} = require('vn-loopback/server/server'); - -describe('VnUser addAlias()', () => { - const employeeId = 1; - const sysadminId = 66; - const developerId = 9; - const customerId = 2; - const mailAlias = 1; - it('should throw an error when user not has privileges', async() => { - const ctx = {req: {accessToken: {userId: employeeId}}}; - const tx = await models.VnUser.beginTransaction({}); - - let error; - try { - const options = {transaction: tx}; - - await models.MailAliasAccount.addAlias(ctx, employeeId, mailAlias, options); - - await tx.rollback(); - } catch (e) { - error = e; - await tx.rollback(); - } - - expect(error.message).toContain(`You don't have grant privilege`); - }); - - it('should throw an error when user has privileges but not has the role from user', async() => { - const ctx = {req: {accessToken: {userId: sysadminId}}}; - const tx = await models.VnUser.beginTransaction({}); - - let error; - try { - const options = {transaction: tx}; - - await models.MailAliasAccount.addAlias(ctx, employeeId, mailAlias, options); - - await tx.rollback(); - } catch (e) { - error = e; - await tx.rollback(); - } - - expect(error.message).toContain(`You cannot assign/remove an alias that you are not assigned to`); - }); - - it('should add an alias', async() => { - const ctx = {req: {accessToken: {userId: developerId}}}; - const tx = await models.VnUser.beginTransaction({}); - - let result; - try { - const options = {transaction: tx}; - - const user = await models.VnUser.findById(developerId, null, options); - await user.updateAttribute('hasGrant', true, options); - - result = await models.MailAliasAccount.addAlias(ctx, customerId, mailAlias, options); - - await tx.rollback(); - } catch (e) { - await tx.rollback(); - } - - expect(result.mailAlias).toBe(mailAlias); - expect(result.account).toBe(customerId); - }); -}); diff --git a/db/changes/232601/01-aclAddAlias.sql b/db/changes/232601/01-aclAddAlias.sql index db2100bed..d4df3cd44 100644 --- a/db/changes/232601/01-aclAddAlias.sql +++ b/db/changes/232601/01-aclAddAlias.sql @@ -1,5 +1,8 @@ +DELETE FROM `salix`.`ACL` WHERE model = 'MailAliasAccount'; + INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId) VALUES - ('MailAliasAccount', 'addAlias', 'WRITE', 'ALLOW', 'ROLE', 'employee'), - ('MailAliasAccount', 'removeAlias', 'WRITE', 'ALLOW', 'ROLE', 'employee'), + ('MailAliasAccount', '*', 'READ', 'ALLOW', 'ROLE', 'employee'), + ('MailAliasAccount', 'create', 'WRITE', 'ALLOW', 'ROLE', 'employee'), + ('MailAliasAccount', 'deleteById', 'WRITE', 'ALLOW', 'ROLE', 'employee'), ('MailAliasAccount', 'canEditAlias', 'WRITE', 'ALLOW', 'ROLE', 'itManagement'); diff --git a/modules/account/back/methods/mail-alias-account/addAlias.js b/modules/account/back/methods/mail-alias-account/addAlias.js deleted file mode 100644 index 74624b63c..000000000 --- a/modules/account/back/methods/mail-alias-account/addAlias.js +++ /dev/null @@ -1,45 +0,0 @@ -module.exports = Self => { - Self.remoteMethod('addAlias', { - description: 'Add an alias if the user has the grant', - accessType: 'WRITE', - accepts: [ - { - arg: 'ctx', - type: 'Object', - http: {source: 'context'} - }, - { - arg: 'id', - type: 'number', - required: true, - description: 'The user id', - http: {source: 'path'} - }, - { - arg: 'mailAlias', - type: 'number', - description: 'The new alias for user', - required: true - } - ], - http: { - path: `/:id/addAlias`, - verb: 'POST' - } - }); - - Self.addAlias = async function(ctx, id, mailAlias, options) { - const models = Self.app.models; - const myOptions = {}; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - await Self.hasGrant(ctx, mailAlias, myOptions); - - return models.MailAliasAccount.create({ - mailAlias: mailAlias, - account: id - }, myOptions); - }; -}; diff --git a/modules/account/back/methods/mail-alias-account/removeAlias.js b/modules/account/back/methods/mail-alias-account/removeAlias.js deleted file mode 100644 index c32911f4d..000000000 --- a/modules/account/back/methods/mail-alias-account/removeAlias.js +++ /dev/null @@ -1,51 +0,0 @@ -const UserError = require('vn-loopback/util/user-error'); - -module.exports = Self => { - Self.remoteMethod('removeAlias', { - description: 'Remove alias if the user has the grant', - accessType: 'WRITE', - accepts: [ - { - arg: 'ctx', - type: 'Object', - http: {source: 'context'} - }, - { - arg: 'id', - type: 'number', - required: true, - description: 'The user id', - http: {source: 'path'} - }, - { - arg: 'mailAlias', - type: 'number', - description: 'The alias to delete', - required: true - } - ], - http: { - path: `/:id/removeAlias`, - verb: 'POST' - } - }); - - Self.removeAlias = async function(ctx, id, mailAlias, options) { - const models = Self.app.models; - const myOptions = {}; - - if (typeof options == 'object') - Object.assign(myOptions, options); - - await Self.hasGrant(ctx, mailAlias, myOptions); - - const mailAliasAccount = await models.MailAliasAccount.findOne({ - where: { - mailAlias: mailAlias, - account: id - } - }, myOptions); - - await mailAliasAccount.destroy(myOptions); - }; -}; diff --git a/modules/account/back/models/mail-alias-account.js b/modules/account/back/models/mail-alias-account.js index 0875bf79a..6f5213f24 100644 --- a/modules/account/back/models/mail-alias-account.js +++ b/modules/account/back/models/mail-alias-account.js @@ -2,8 +2,17 @@ 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); + Self.observe('before save', async ctx => { + const changes = ctx.currentInstance || ctx.instance; + + await Self.hasGrant(ctx, changes.mailAlias); + }); + + Self.observe('before delete', async ctx => { + const mailAliasAccount = await Self.findById(ctx.where.id); + + await Self.hasGrant(ctx, mailAliasAccount.mailAlias); + }); /** * Checks if current user has @@ -11,17 +20,17 @@ module.exports = Self => { * * @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) { + Self.hasGrant = async function(ctx, mailAlias) { const models = Self.app.models; - const userId = ctx.req.accessToken.userId; + const accessToken = {req: {accessToken: ctx.options.accessToken}}; + const userId = accessToken.req.accessToken.userId; - const canEditAlias = await models.ACL.checkAccessAcl(ctx, 'MailAliasAccount', 'canEditAlias', 'WRITE'); + const canEditAlias = await models.ACL.checkAccessAcl(accessToken, 'MailAliasAccount', 'canEditAlias', 'WRITE'); if (canEditAlias) return true; - const user = await models.VnUser.findById(userId, {fields: ['hasGrant']}, options); + const user = await models.VnUser.findById(userId, {fields: ['hasGrant']}); if (!user.hasGrant) throw new UserError(`You don't have grant privilege`); @@ -33,7 +42,7 @@ module.exports = Self => { fields: ['mailAlias'] } } - }, options); + }); const aliases = account.aliases().map(alias => alias.mailAlias); diff --git a/modules/account/front/aliases/index.js b/modules/account/front/aliases/index.js index b4ada07e5..0fc806a71 100644 --- a/modules/account/front/aliases/index.js +++ b/modules/account/front/aliases/index.js @@ -21,11 +21,12 @@ export default class Controller extends Section { } onAddClick() { + this.addData = {account: this.$params.id}; this.$.dialog.show(); } onAddSave() { - return this.$http.post(`MailAliasAccounts/${this.$params.id}/addAlias`, this.addData) + return this.$http.post(`MailAliasAccounts`, this.addData) .then(() => this.refresh()) .then(() => this.vnApp.showSuccess( this.$t('Subscribed to alias!')) @@ -33,12 +34,11 @@ export default class Controller extends Section { } onRemove(row) { - const params = { - mailAlias: row.mailAlias - }; - return this.$http.post(`MailAliasAccounts/${this.$params.id}/removeAlias`, params) - .then(() => this.refresh()) - .then(() => this.vnApp.showSuccess(this.$t('Data saved!'))); + return this.$http.delete(`MailAliasAccounts/${row.id}`) + .then(() => { + this.$.data.splice(this.$.data.indexOf(row), 1); + this.vnApp.showSuccess(this.$t('Unsubscribed from alias!')); + }); } } diff --git a/modules/account/front/aliases/index.spec.js b/modules/account/front/aliases/index.spec.js index f72c06ab4..466f1e1e9 100644 --- a/modules/account/front/aliases/index.spec.js +++ b/modules/account/front/aliases/index.spec.js @@ -25,9 +25,8 @@ describe('component vnUserAliases', () => { describe('onAddSave()', () => { it('should add the new row', () => { controller.addData = {account: 1}; - controller.$params = {id: 1}; - $httpBackend.expectPOST('MailAliasAccounts/1/addAlias').respond(); + $httpBackend.expectPOST('MailAliasAccounts').respond(); $httpBackend.expectGET('MailAliasAccounts').respond('foo'); controller.onAddSave(); $httpBackend.flush(); @@ -42,14 +41,12 @@ describe('component vnUserAliases', () => { {id: 1, alias: 'foo'}, {id: 2, alias: 'bar'} ]; - controller.$params = {id: 1}; - $httpBackend.expectPOST('MailAliasAccounts/1/removeAlias').respond(); - $httpBackend.expectGET('MailAliasAccounts').respond(controller.$.data[1]); + $httpBackend.expectDELETE('MailAliasAccounts/1').respond(); controller.onRemove(controller.$.data[0]); $httpBackend.flush(); - expect(controller.$.data).toEqual({id: 2, alias: 'bar'}); + expect(controller.$.data).toEqual([{id: 2, alias: 'bar'}]); expect(controller.vnApp.showSuccess).toHaveBeenCalled(); }); });