Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make things support multi-owner #2454

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions common/models/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,26 +210,42 @@ module.exports = function(Role) {
if (callback) callback(null, matches(ownerId, userId));
return;
} else {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That way you allow multiple owner only when it's not base on the userId / owner field ... see https://github.com/strongloop/loopback/pull/3106/files for my implementation

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pierreclr Hi, for some reason I'm no longer maintaining this pr, it's glad to see somebody else fulfilled the request for mult-owner. Hope to see this be merged into the matser soon.

var relTries = 0;
var matched = false;
function processRelatedUser(err, user) {
if (matched) {
return;
}
var checkFinish = function() {
if (relTries === modelClass.relations.length) {
debug('No matching belongsTo relation in model %j and user: %j', modelId, userId);
if (callback) callback(null, false);
}
};
if (!err && user) {
relTries++;
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 {
relTries++;
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);
}
}
});
};
Expand Down
154 changes: 58 additions & 96 deletions test/role.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@ describe('role model', function() {
var Album = app.registry.createModel('Album', {
name: String,
userId: Number,
customerId: Number,
}, {
relations: {
user: {
Expand All @@ -311,109 +312,70 @@ 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);
});
});
});
});

Expand Down