Merge pull request #254 from strongloop/feature/role-id

Set the role id to be generated
This commit is contained in:
Raymond Feng 2014-06-17 10:37:06 -07:00
commit d5ed57e60d
2 changed files with 37 additions and 3 deletions

View File

@ -7,7 +7,7 @@ var AccessContext = require('./access-context').AccessContext;
// Role model
var RoleSchema = {
id: {type: String, id: true}, // Id
id: {type: String, id: true, generated: true}, // Id
name: {type: String, required: true}, // The name of a role
description: String, // Description
@ -20,8 +20,8 @@ var RoleSchema = {
* Map principals to roles
*/
var RoleMappingSchema = {
id: {type: String, id: true}, // Id
roleId: String, // The role id
id: {type: String, id: true, generated: true}, // Id
// roleId: String, // The role id, to be injected by the belongsTo relation
principalType: String, // The principal type, such as user, application, or role
principalId: String // The principal id
};

View File

@ -77,6 +77,40 @@ describe('role model', function () {
});
it("should automatically generate role id", function () {
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
// console.log('User: ', user.id);
Role.create({name: 'userRole'}, function (err, role) {
assert(role.id);
role.principals.create({principalType: RoleMapping.USER, principalId: user.id}, function (err, p) {
assert(p.id);
assert.equal(p.roleId, role.id);
Role.find(function (err, roles) {
assert(!err);
assert.equal(roles.length, 1);
assert.equal(roles[0].name, 'userRole');
});
role.principals(function (err, principals) {
assert(!err);
// console.log(principals);
assert.equal(principals.length, 1);
assert.equal(principals[0].principalType, RoleMapping.USER);
assert.equal(principals[0].principalId, user.id);
});
role.users(function (err, users) {
assert(!err);
assert.equal(users.length, 1);
assert.equal(users[0].principalType, RoleMapping.USER);
assert.equal(users[0].principalId, user.id);
});
});
});
});
});
it("should support getRoles() and isInRole()", function () {
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
// console.log('User: ', user.id);