Merge pull request #3230 from strongloop/fix/context-passing-for-isOwner
Fix context passing in OWNER role resolver
This commit is contained in:
commit
5ebc9b6a2e
|
@ -182,7 +182,8 @@ module.exports = function(Role) {
|
|||
var user = context.getUser();
|
||||
var userId = user && user.id;
|
||||
var principalType = user && user.principalType;
|
||||
Role.isOwner(modelClass, modelId, userId, principalType, callback);
|
||||
var opts = {accessToken: context.accessToken};
|
||||
Role.isOwner(modelClass, modelId, userId, principalType, opts, callback);
|
||||
});
|
||||
|
||||
function isUserClass(modelClass) {
|
||||
|
@ -213,15 +214,21 @@ module.exports = function(Role) {
|
|||
* @param {*} modelId The model ID
|
||||
* @param {*} userId The user ID
|
||||
* @param {String} principalType The user principalType (optional)
|
||||
* @options {Object} options
|
||||
* @property {accessToken} The access token used to authorize the current user.
|
||||
* @callback {Function} [callback] The callback function
|
||||
* @param {String|Error} err The error string or object
|
||||
* @param {Boolean} isOwner True if the user is an owner.
|
||||
* @promise
|
||||
*/
|
||||
Role.isOwner = function isOwner(modelClass, modelId, userId, principalType, callback) {
|
||||
if (!callback && typeof principalType === 'function') {
|
||||
Role.isOwner = function isOwner(modelClass, modelId, userId, principalType, options, callback) {
|
||||
if (!callback && typeof options === 'function') {
|
||||
callback = options;
|
||||
options = {};
|
||||
} else if (!callback && typeof principalType === 'function') {
|
||||
callback = principalType;
|
||||
principalType = undefined;
|
||||
options = {};
|
||||
}
|
||||
principalType = principalType || Principal.USER;
|
||||
|
||||
|
@ -251,7 +258,7 @@ module.exports = function(Role) {
|
|||
return callback.promise;
|
||||
}
|
||||
|
||||
modelClass.findById(modelId, function(err, inst) {
|
||||
modelClass.findById(modelId, options, function(err, inst) {
|
||||
if (err || !inst) {
|
||||
debug('Model not found for id %j', modelId);
|
||||
return callback(err, false);
|
||||
|
|
|
@ -533,6 +533,35 @@ describe('role model', function() {
|
|||
.then(isInRole => expect(isInRole).to.be.true());
|
||||
});
|
||||
|
||||
it('passes accessToken to modelClass.findById when resolving OWNER', () => {
|
||||
const Album = app.registry.createModel('Album', {name: String});
|
||||
app.model(Album, {dataSource: 'db'});
|
||||
Album.belongsTo(User);
|
||||
|
||||
let observedOptions = null;
|
||||
Album.observe('access', ctx => {
|
||||
observedOptions = ctx.options;
|
||||
return Promise.resolve();
|
||||
});
|
||||
|
||||
let user, token;
|
||||
return User.create({email: 'test@example.com', password: 'pass'})
|
||||
.then(u => {
|
||||
user = u;
|
||||
return Album.create({name: 'Album 1', userId: user.id});
|
||||
})
|
||||
.then(album => {
|
||||
return Role.isInRole(Role.OWNER, {
|
||||
principalType: ACL.USER, principalId: user.id,
|
||||
model: Album, id: album.id,
|
||||
accessToken: 'test-token',
|
||||
});
|
||||
})
|
||||
.then(isInRole => {
|
||||
expect(observedOptions).to.eql({accessToken: 'test-token'});
|
||||
});
|
||||
});
|
||||
|
||||
describe('isMappedToRole', function() {
|
||||
var user, app, role;
|
||||
|
||||
|
|
Loading…
Reference in New Issue