refs #5887 refacotr: move code to hook
gitea/salix/pipeline/head There was a failure building this commit
Details
gitea/salix/pipeline/head There was a failure building this commit
Details
This commit is contained in:
parent
ef6a2ae578
commit
7aaaf5492c
|
@ -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);
|
|
||||||
});
|
|
||||||
});
|
|
|
@ -1,5 +1,8 @@
|
||||||
|
DELETE FROM `salix`.`ACL` WHERE model = 'MailAliasAccount';
|
||||||
|
|
||||||
INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId)
|
INSERT INTO `salix`.`ACL` (model, property, accessType, permission, principalType, principalId)
|
||||||
VALUES
|
VALUES
|
||||||
('MailAliasAccount', 'addAlias', 'WRITE', 'ALLOW', 'ROLE', 'employee'),
|
('MailAliasAccount', '*', 'READ', 'ALLOW', 'ROLE', 'employee'),
|
||||||
('MailAliasAccount', 'removeAlias', 'WRITE', 'ALLOW', 'ROLE', 'employee'),
|
('MailAliasAccount', 'create', 'WRITE', 'ALLOW', 'ROLE', 'employee'),
|
||||||
|
('MailAliasAccount', 'deleteById', 'WRITE', 'ALLOW', 'ROLE', 'employee'),
|
||||||
('MailAliasAccount', 'canEditAlias', 'WRITE', 'ALLOW', 'ROLE', 'itManagement');
|
('MailAliasAccount', 'canEditAlias', 'WRITE', 'ALLOW', 'ROLE', 'itManagement');
|
||||||
|
|
|
@ -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);
|
|
||||||
};
|
|
||||||
};
|
|
|
@ -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);
|
|
||||||
};
|
|
||||||
};
|
|
|
@ -2,8 +2,17 @@
|
||||||
const UserError = require('vn-loopback/util/user-error');
|
const UserError = require('vn-loopback/util/user-error');
|
||||||
|
|
||||||
module.exports = Self => {
|
module.exports = Self => {
|
||||||
require('../methods/mail-alias-account/addAlias')(Self);
|
Self.observe('before save', async ctx => {
|
||||||
require('../methods/mail-alias-account/removeAlias')(Self);
|
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
|
* Checks if current user has
|
||||||
|
@ -11,17 +20,17 @@ module.exports = Self => {
|
||||||
*
|
*
|
||||||
* @param {Object} ctx - Request context
|
* @param {Object} ctx - Request context
|
||||||
* @param {Interger} mailAlias - mailAlias id
|
* @param {Interger} mailAlias - mailAlias id
|
||||||
* @param {Object} options - Query options
|
|
||||||
* @return {Boolean} True for user with grant
|
* @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 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;
|
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)
|
if (!user.hasGrant)
|
||||||
throw new UserError(`You don't have grant privilege`);
|
throw new UserError(`You don't have grant privilege`);
|
||||||
|
|
||||||
|
@ -33,7 +42,7 @@ module.exports = Self => {
|
||||||
fields: ['mailAlias']
|
fields: ['mailAlias']
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, options);
|
});
|
||||||
|
|
||||||
const aliases = account.aliases().map(alias => alias.mailAlias);
|
const aliases = account.aliases().map(alias => alias.mailAlias);
|
||||||
|
|
||||||
|
|
|
@ -21,11 +21,12 @@ export default class Controller extends Section {
|
||||||
}
|
}
|
||||||
|
|
||||||
onAddClick() {
|
onAddClick() {
|
||||||
|
this.addData = {account: this.$params.id};
|
||||||
this.$.dialog.show();
|
this.$.dialog.show();
|
||||||
}
|
}
|
||||||
|
|
||||||
onAddSave() {
|
onAddSave() {
|
||||||
return this.$http.post(`MailAliasAccounts/${this.$params.id}/addAlias`, this.addData)
|
return this.$http.post(`MailAliasAccounts`, this.addData)
|
||||||
.then(() => this.refresh())
|
.then(() => this.refresh())
|
||||||
.then(() => this.vnApp.showSuccess(
|
.then(() => this.vnApp.showSuccess(
|
||||||
this.$t('Subscribed to alias!'))
|
this.$t('Subscribed to alias!'))
|
||||||
|
@ -33,12 +34,11 @@ export default class Controller extends Section {
|
||||||
}
|
}
|
||||||
|
|
||||||
onRemove(row) {
|
onRemove(row) {
|
||||||
const params = {
|
return this.$http.delete(`MailAliasAccounts/${row.id}`)
|
||||||
mailAlias: row.mailAlias
|
.then(() => {
|
||||||
};
|
this.$.data.splice(this.$.data.indexOf(row), 1);
|
||||||
return this.$http.post(`MailAliasAccounts/${this.$params.id}/removeAlias`, params)
|
this.vnApp.showSuccess(this.$t('Unsubscribed from alias!'));
|
||||||
.then(() => this.refresh())
|
});
|
||||||
.then(() => this.vnApp.showSuccess(this.$t('Data saved!')));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -25,9 +25,8 @@ describe('component vnUserAliases', () => {
|
||||||
describe('onAddSave()', () => {
|
describe('onAddSave()', () => {
|
||||||
it('should add the new row', () => {
|
it('should add the new row', () => {
|
||||||
controller.addData = {account: 1};
|
controller.addData = {account: 1};
|
||||||
controller.$params = {id: 1};
|
|
||||||
|
|
||||||
$httpBackend.expectPOST('MailAliasAccounts/1/addAlias').respond();
|
$httpBackend.expectPOST('MailAliasAccounts').respond();
|
||||||
$httpBackend.expectGET('MailAliasAccounts').respond('foo');
|
$httpBackend.expectGET('MailAliasAccounts').respond('foo');
|
||||||
controller.onAddSave();
|
controller.onAddSave();
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
@ -42,14 +41,12 @@ describe('component vnUserAliases', () => {
|
||||||
{id: 1, alias: 'foo'},
|
{id: 1, alias: 'foo'},
|
||||||
{id: 2, alias: 'bar'}
|
{id: 2, alias: 'bar'}
|
||||||
];
|
];
|
||||||
controller.$params = {id: 1};
|
|
||||||
|
|
||||||
$httpBackend.expectPOST('MailAliasAccounts/1/removeAlias').respond();
|
$httpBackend.expectDELETE('MailAliasAccounts/1').respond();
|
||||||
$httpBackend.expectGET('MailAliasAccounts').respond(controller.$.data[1]);
|
|
||||||
controller.onRemove(controller.$.data[0]);
|
controller.onRemove(controller.$.data[0]);
|
||||||
$httpBackend.flush();
|
$httpBackend.flush();
|
||||||
|
|
||||||
expect(controller.$.data).toEqual({id: 2, alias: 'bar'});
|
expect(controller.$.data).toEqual([{id: 2, alias: 'bar'}]);
|
||||||
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
|
expect(controller.vnApp.showSuccess).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue