2013-11-10 06:22:16 +00:00
|
|
|
var assert = require('assert');
|
|
|
|
var loopback = require('../index');
|
2013-11-12 06:16:51 +00:00
|
|
|
var role = require('../lib/models/role');
|
|
|
|
var Role = role.Role;
|
|
|
|
var RoleMapping = role.RoleMapping;
|
2013-11-10 06:22:16 +00:00
|
|
|
var User = loopback.User;
|
2013-11-19 19:58:30 +00:00
|
|
|
var ACL = require('../lib/models/acl');
|
2013-11-10 06:22:16 +00:00
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
function checkResult(err, result) {
|
|
|
|
// console.log(err, result);
|
|
|
|
assert(!err);
|
|
|
|
}
|
|
|
|
|
|
|
|
describe('role model', function () {
|
|
|
|
|
|
|
|
it("should define role/role relations", function () {
|
|
|
|
Role.create({name: 'user'}, function (err, userRole) {
|
|
|
|
Role.create({name: 'admin'}, function (err, adminRole) {
|
|
|
|
userRole.principals.create({principalType: RoleMapping.ROLE, principalId: adminRole.id}, function (err, mapping) {
|
|
|
|
Role.find(function (err, roles) {
|
|
|
|
assert.equal(roles.length, 2);
|
|
|
|
});
|
|
|
|
RoleMapping.find(function (err, mappings) {
|
|
|
|
assert.equal(mappings.length, 1);
|
|
|
|
assert.equal(mappings[0].principalType, RoleMapping.ROLE);
|
|
|
|
assert.equal(mappings[0].principalId, adminRole.id);
|
|
|
|
});
|
|
|
|
userRole.principals(function (err, principals) {
|
|
|
|
assert.equal(principals.length, 1);
|
|
|
|
});
|
|
|
|
userRole.roles(function (err, roles) {
|
|
|
|
assert.equal(roles.length, 1);
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it("should define role/user relations", 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) {
|
|
|
|
role.principals.create({principalType: RoleMapping.USER, principalId: user.id}, function (err, p) {
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.find(function (err, roles) {
|
2013-11-12 18:10:32 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.equal(roles.length, 1);
|
|
|
|
assert.equal(roles[0].name, 'userRole');
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
role.principals(function (err, principals) {
|
2013-11-12 18:10:32 +00:00
|
|
|
assert(!err);
|
|
|
|
// console.log(principals);
|
|
|
|
assert.equal(principals.length, 1);
|
|
|
|
assert.equal(principals[0].principalType, RoleMapping.USER);
|
|
|
|
assert.equal(principals[0].principalId, user.id);
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
role.users(function (err, users) {
|
2013-11-12 18:10:32 +00:00
|
|
|
assert(!err);
|
|
|
|
assert.equal(users.length, 1);
|
|
|
|
assert.equal(users[0].principalType, RoleMapping.USER);
|
|
|
|
assert.equal(users[0].principalId, user.id);
|
2013-11-12 06:16:51 +00:00
|
|
|
});
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2013-11-12 06:16:51 +00:00
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|
2013-11-12 18:10:32 +00:00
|
|
|
|
2013-11-12 18:47:59 +00:00
|
|
|
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);
|
|
|
|
Role.create({name: 'userRole'}, function (err, role) {
|
|
|
|
role.principals.create({principalType: RoleMapping.USER, principalId: user.id}, function (err, p) {
|
|
|
|
// Role.find(console.log);
|
|
|
|
// role.principals(console.log);
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.isInRole('userRole', {principalType: RoleMapping.USER, principalId: user.id}, function (err, exists) {
|
2013-11-12 18:47:59 +00:00
|
|
|
assert(!err && exists === true);
|
|
|
|
});
|
|
|
|
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.isInRole('userRole', {principalType: RoleMapping.APP, principalId: user.id}, function (err, exists) {
|
2013-11-12 18:47:59 +00:00
|
|
|
assert(!err && exists === false);
|
|
|
|
});
|
|
|
|
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.isInRole('userRole', {principalType: RoleMapping.USER, principalId: 100}, function (err, exists) {
|
2013-11-12 18:47:59 +00:00
|
|
|
assert(!err && exists === false);
|
|
|
|
});
|
|
|
|
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.getRoles(RoleMapping.USER, user.id, function (err, roles) {
|
2013-11-12 18:47:59 +00:00
|
|
|
assert.equal(roles.length, 1);
|
|
|
|
assert.equal(roles[0], role.id);
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.getRoles(RoleMapping.APP, user.id, function (err, roles) {
|
2013-11-12 18:47:59 +00:00
|
|
|
assert.equal(roles.length, 0);
|
|
|
|
});
|
2013-11-19 19:58:30 +00:00
|
|
|
Role.getRoles(RoleMapping.USER, 100, function (err, roles) {
|
2013-11-12 18:47:59 +00:00
|
|
|
assert.equal(roles.length, 0);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-19 19:58:30 +00:00
|
|
|
it("should support owner role resolver", function () {
|
|
|
|
var ds = loopback.createDataSource({connector: 'memory'});
|
|
|
|
User.attachTo(ds);
|
|
|
|
Role.attachTo(ds);
|
|
|
|
RoleMapping.attachTo(ds);
|
|
|
|
|
|
|
|
var Album = ds.createModel('Album', {
|
|
|
|
name: String,
|
|
|
|
userId: Number
|
|
|
|
}, {
|
|
|
|
relations: {
|
|
|
|
user: {
|
|
|
|
type: 'belongsTo',
|
|
|
|
model: 'User',
|
|
|
|
foreignKey: 'userId'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
User.create({name: 'Raymond', email: 'x@y.com', password: 'foobar'}, function (err, user) {
|
|
|
|
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.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);
|
|
|
|
});
|
|
|
|
|
|
|
|
// console.log('User: ', user.id);
|
|
|
|
Album.create({name: 'Album 1', userId: user.id}, function (err, album1) {
|
|
|
|
Role.isInRole(Role.OWNER, {principalType: ACL.USER, principalId: user.id, model: Album, id: album1.id}, function (err, yes) {
|
|
|
|
assert(!err && yes);
|
|
|
|
});
|
|
|
|
Album.create({name: 'Album 2'}, function (err, album2) {
|
|
|
|
Role.isInRole(Role.OWNER, {principalType: ACL.USER, principalId: user.id, model: Album, id: album2.id}, function (err, yes) {
|
|
|
|
assert(!err && !yes);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2013-11-10 06:22:16 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-11-12 18:10:32 +00:00
|
|
|
|