Skip to content
This repository was archived by the owner on Dec 14, 2023. It is now read-only.

Commit 6af6d21

Browse files
authored
Add perm to ensure agreement is own (#298)
* Add perm to ensure agreement is own * Change require path for test
1 parent f72eec9 commit 6af6d21

File tree

5 files changed

+35
-2
lines changed

5 files changed

+35
-2
lines changed

config/perm/agreements.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ module.exports = function(){
44
return {
55
'load': [{
66
role: 'basic-user',
7+
customValidator: [{
8+
role: 'cd-agreements',
9+
cmd: 'is_own_agreement',
10+
}],
711
}],
812

913
'save': [{
@@ -14,6 +18,10 @@ module.exports = function(){
1418
}],
1519
'loadUserAgreement': [{
1620
role: 'basic-user',
21+
customValidator: [{
22+
role: 'cd-users',
23+
cmd: 'is_self'
24+
}]
1725
}],
1826
'list': [{
1927
role: 'cdf-admin',

agreements.js renamed to lib/agreements/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ module.exports = function (options) {
1313
seneca.add({role: plugin, cmd: 'load'}, cmd_load);
1414
seneca.add({role: plugin, cmd: 'getVersion'}, cmd_get_version);
1515
seneca.add({role: plugin, cmd: 'loadUserAgreement'}, cmd_load_user_agreement);
16+
seneca.add({role: plugin, cmd: 'is_own_agreement'}, require('./is-own-agreement'));
1617

1718
function cmd_get_version (args, done) {
1819
var version = 2;

lib/agreements/is-own-agreement.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
'use strict';
2+
var async = require('async');
3+
var _ = require('lodash');
4+
5+
6+
function isOwnAgreement (args, cb) {
7+
const seneca = this;
8+
const plugin = args.role;
9+
const userId = args.user.id;
10+
const id = args.params.id;
11+
seneca.act({ role: 'cd-agreements', cmd: 'load', id }, function (err, agreement) {
12+
if (err) {
13+
seneca.log.error(seneca.customValidatorLogFormatter('cd-agreements', 'isOwnAgreement', err, { id, userId }));
14+
return cb(null, {'allowed': false});
15+
}
16+
var isSelf = false;
17+
if (agreement.userId === userId) {
18+
isSelf = true;
19+
}
20+
return cb(null, {'allowed': isSelf});
21+
});
22+
}
23+
24+
module.exports = isOwnAgreement;

service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ require('./migrate-psql-db.js')(function (err) {
5252
console.log('Migrations ok');
5353

5454
seneca.use(require('./email-notifications.js'));
55-
seneca.use(require('./agreements.js'));
55+
seneca.use(require('./lib/agreements'));
5656
seneca.use(require('./profiles.js'),
5757
{ postgresql: config['postgresql-store'],
5858
logger: log.logger

test/users-spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ if (using_postgres) seneca.use('postgresql-store', config["postgresql-store"]);
2020

2121
seneca
2222
.use(__dirname + '/../users.js', { 'postgresql': config['postgresql-store'], 'users': config['users']})
23-
.use(__dirname + '/../agreements.js')
23+
.use(__dirname + '/../lib/agreements')
2424
.use(__dirname + '/../profiles.js')
2525
.use(__dirname + '/../email-notifications.js')
2626
.use(__dirname + '/stubs/cd-nodebb-api.js')

0 commit comments

Comments
 (0)