From 8a497eb66f657973785ad812450f64a157df2001 Mon Sep 17 00:00:00 2001 From: upupzealot Date: Mon, 20 Jun 2016 22:08:11 +0800 Subject: [PATCH 1/5] Make things support multi-owner When a model has two or more "belongsTo" relations, $owner will check them all, instead of only checking the first one. --- common/models/role.js | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/common/models/role.js b/common/models/role.js index 8458c9a4d..08aa24bfd 100644 --- a/common/models/role.js +++ b/common/models/role.js @@ -210,26 +210,42 @@ module.exports = function(Role) { if (callback) callback(null, matches(ownerId, userId)); return; } else { + var rel_tries = 0; + var matched = false; + function processRelatedUser(err, user) { + if(matched) { + return; + } + var checkFinish = function() { + if(rel_tries === modelClass.relations.length) { + debug('No matching belongsTo relation found for model %j and user: %j', modelId, userId); + if (callback) callback(null, false); + } + } + if (!err && user) { + rel_tries++; + debug('User found: %j', user.id); + var result = matches(user.id, userId); + debug('User matches result: %s !', result); + if(result) { + matched = true; + if (callback) callback(null, matches(user.id, userId)); + } + checkFinish(); + } else { + rel_tries++; + checkFinish(); + } + } + // Try to follow belongsTo for (var r in modelClass.relations) { var rel = modelClass.relations[r]; if (rel.type === 'belongsTo' && isUserClass(rel.modelTo)) { debug('Checking relation %s to %s: %j', r, rel.modelTo.modelName, rel); inst[r](processRelatedUser); - return; } } - debug('No matching belongsTo relation found for model %j and user: %j', modelId, userId); - if (callback) callback(null, false); - } - - function processRelatedUser(err, user) { - if (!err && user) { - debug('User found: %j', user.id); - if (callback) callback(null, matches(user.id, userId)); - } else { - if (callback) callback(err, false); - } } }); }; From 483fa953ab08517374065543c633c439385d6f07 Mon Sep 17 00:00:00 2001 From: upupzealot Date: Mon, 20 Jun 2016 22:18:53 +0800 Subject: [PATCH 2/5] format the code --- common/models/role.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/common/models/role.js b/common/models/role.js index 08aa24bfd..13dd502d5 100644 --- a/common/models/role.js +++ b/common/models/role.js @@ -210,30 +210,30 @@ module.exports = function(Role) { if (callback) callback(null, matches(ownerId, userId)); return; } else { - var rel_tries = 0; + var relTries = 0; var matched = false; function processRelatedUser(err, user) { - if(matched) { + if (matched) { return; } var checkFinish = function() { - if(rel_tries === modelClass.relations.length) { + if (relTries === modelClass.relations.length) { debug('No matching belongsTo relation found for model %j and user: %j', modelId, userId); if (callback) callback(null, false); } - } + }; if (!err && user) { - rel_tries++; + relTries++; debug('User found: %j', user.id); var result = matches(user.id, userId); debug('User matches result: %s !', result); - if(result) { + if (result) { matched = true; if (callback) callback(null, matches(user.id, userId)); } checkFinish(); } else { - rel_tries++; + relTries++; checkFinish(); } } From 0f0cdf03670f4ea5c51e4106c72f9d2262a28e50 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 21 Jun 2016 15:06:44 +0800 Subject: [PATCH 3/5] fix the long line --- common/models/role.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/common/models/role.js b/common/models/role.js index 13dd502d5..1d0030a68 100644 --- a/common/models/role.js +++ b/common/models/role.js @@ -218,7 +218,7 @@ module.exports = function(Role) { } var checkFinish = function() { if (relTries === modelClass.relations.length) { - debug('No matching belongsTo relation found for model %j and user: %j', modelId, userId); + debug('No matching belongsTo relation in model %j and user: %j', modelId, userId); if (callback) callback(null, false); } }; From 811ac75da963c29c0327e53129509201cf88e554 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 22 Jun 2016 12:33:28 +0800 Subject: [PATCH 4/5] add some test --- test/role.test.js | 153 +++++++++++++++++----------------------------- 1 file changed, 57 insertions(+), 96 deletions(-) diff --git a/test/role.test.js b/test/role.test.js index 17ba5e08e..5f628c2b1 100644 --- a/test/role.test.js +++ b/test/role.test.js @@ -303,6 +303,7 @@ describe('role model', function() { var Album = app.registry.createModel('Album', { name: String, userId: Number, + customerId: Number, }, { relations: { user: { @@ -311,109 +312,69 @@ describe('role model', function() { foreignKey: 'userId', }, }, + relations: { + customer: { + type: 'belongsTo', + model: 'User', + foreignKey: 'customerId', + }, + }, }); app.model(Album, { dataSource: 'db' }); User.create({ name: 'Raymond', email: 'x@y.com', password: 'foobar' }, function(err, user) { - if (err) return done(err); - async.parallel([ - function(next) { - Role.isInRole( - 'returnPromise', - { principalType: ACL.USER, principalId: user.id }, - function(err, yes) { - if (err) return next(err); - assert(yes); - next(); - }); - }, - function(next) { - Role.isInRole( - Role.AUTHENTICATED, - { principalType: ACL.USER, principalId: user.id }, - function(err, yes) { - if (err) next(err); - assert(yes); - next(); - }); - }, - function(next) { - Role.isInRole( - Role.AUTHENTICATED, - { principalType: ACL.USER, principalId: null }, - function(err, yes) { - if (err) next(err); - assert(!yes); - next(); - }); - }, - function(next) { - Role.isInRole( - Role.UNAUTHENTICATED, - { principalType: ACL.USER, principalId: user.id }, - function(err, yes) { - if (err) return next(err); - assert(!yes); - next(); - }); - }, - function(next) { - Role.isInRole( - Role.UNAUTHENTICATED, - { principalType: ACL.USER, principalId: null }, - function(err, yes) { - if (err) return next(err); - assert(yes); - next(); - }); - }, - function(next) { - Role.isInRole( - Role.EVERYONE, - { principalType: ACL.USER, principalId: user.id }, - function(err, yes) { - if (err) return next(err); - assert(yes); - next(); - }); - }, - function(next) { - Role.isInRole( - Role.EVERYONE, - { principalType: ACL.USER, principalId: null }, - function(err, yes) { - if (err) return next(err); - assert(yes); - next(); - }); - }, - function(next) { - Album.create({ name: 'Album 1', userId: user.id }, function(err, album1) { - if (err) return done(err); - var role = { - principalType: ACL.USER, principalId: user.id, - model: Album, id: album1.id, - }; - Role.isInRole(Role.OWNER, role, function(err, yes) { - if (err) return next(err); - assert(yes); + User.create({ name: 'Eric', email: 'z@y.com', password: 'foobar' }, function(err, user2) { + Role.isInRole('returnPromise', { principalType: ACL.USER, principalId: user.id }, + function(err, yes) { + assert(!err && yes); + }); - Album.create({ name: 'Album 2' }, function(err, album2) { - if (err) return next(err); - role = { - principalType: ACL.USER, principalId: user.id, - model: Album, id: album2.id, - }; - Role.isInRole(Role.OWNER, role, function(err, yes) { - if (err) return next(err); - assert(!yes); - next(); - }); - }); + Role.isInRole(Role.AUTHENTICATED, { principalType: ACL.USER, principalId: user.id }, + function(err, yes) { + assert(!err && yes); + }); + + Role.isInRole(Role.AUTHENTICATED, { principalType: ACL.USER, principalId: null }, + function(err, yes) { + assert(!err && !yes); + }); + + Role.isInRole(Role.UNAUTHENTICATED, { principalType: ACL.USER, principalId: user.id }, + function(err, yes) { + assert(!err && !yes); + }); + Role.isInRole(Role.UNAUTHENTICATED, { principalType: ACL.USER, principalId: null }, + function(err, yes) { + assert(!err && yes); + }); + + Role.isInRole(Role.EVERYONE, { principalType: ACL.USER, principalId: user.id }, + function(err, yes) { + assert(!err && yes); + }); + + Role.isInRole(Role.EVERYONE, { principalType: ACL.USER, principalId: null }, + function(err, yes) { + assert(!err && yes); + }); + + Album.create({ name: 'Album 1', userId: user.id, customerId: user2.id }, function(err, album1) { + var role = { principalType: ACL.USER, principalId: user.id, model: Album, id: album1.id }; + Role.isInRole(Role.OWNER, role, function(err, yes) { + assert(!err && yes); + }); + role = { principalType: ACL.USER, principalId: user2.id, model: Album, id: album1.id }; + Role.isInRole(Role.OWNER, role, function(err, yes) { + assert(!err && yes); + }); + Album.create({ name: 'Album 2' }, function(err, album2) { + role = { principalType: ACL.USER, principalId: user.id, model: Album, id: album2.id }; + Role.isInRole(Role.OWNER, role, function(err, yes) { + assert(!err && !yes); }); }); - }, - ], done); + }); + }); }); }); From 071f8def2b77c64ce0bf52e44f9154bf3e756567 Mon Sep 17 00:00:00 2001 From: Candy Date: Mon, 31 Oct 2016 14:19:42 -0400 Subject: [PATCH 5/5] Fix indentation --- test/role.test.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/role.test.js b/test/role.test.js index 5f628c2b1..a65a0c180 100644 --- a/test/role.test.js +++ b/test/role.test.js @@ -358,7 +358,8 @@ describe('role model', function() { assert(!err && yes); }); - Album.create({ name: 'Album 1', userId: user.id, customerId: user2.id }, function(err, album1) { + Album.create({ name: 'Album 1', userId: user.id, customerId: user2.id }, + function(err, album1) { var role = { principalType: ACL.USER, principalId: user.id, model: Album, id: album1.id }; Role.isInRole(Role.OWNER, role, function(err, yes) { assert(!err && yes);