@@ -15,16 +15,13 @@ var supertest = require('supertest');
1515var Role = loopback . Role ;
1616var RoleMapping = loopback . RoleMapping ;
1717var User = loopback . User ;
18- var testModel ;
1918var async = require ( 'async' ) ;
2019
2120// Speed up the password hashing algorithm for tests
2221User . settings . saltWorkFactor = 4 ;
2322
2423var ds = null ;
25- before ( function ( ) {
26- ds = loopback . createDataSource ( { connector : loopback . Memory } ) ;
27- } ) ;
24+ var testModel ;
2825
2926describe ( 'ACL model' , function ( ) {
3027 it ( 'provides DEFAULT_SCOPE constant' , ( ) => {
@@ -33,16 +30,7 @@ describe('ACL model', function() {
3330} ) ;
3431
3532describe ( 'security scopes' , function ( ) {
36- beforeEach ( function ( ) {
37- var ds = this . ds = loopback . createDataSource ( { connector : loopback . Memory } ) ;
38- testModel = loopback . PersistedModel . extend ( 'testModel' ) ;
39- ACL . attachTo ( ds ) ;
40- Role . attachTo ( ds ) ;
41- RoleMapping . attachTo ( ds ) ;
42- User . attachTo ( ds ) ;
43- Scope . attachTo ( ds ) ;
44- testModel . attachTo ( ds ) ;
45- } ) ;
33+ beforeEach ( setupTestModels ) ;
4634
4735 it ( 'should allow access to models for the given scope by wildcard' , function ( done ) {
4836 Scope . create ( { name : 'userScope' , description : 'access user information' } ,
@@ -98,6 +86,8 @@ describe('security scopes', function() {
9886} ) ;
9987
10088describe ( 'security ACLs' , function ( ) {
89+ beforeEach ( setupTestModels ) ;
90+
10191 it ( 'supports checkPermission() returning a promise' , function ( ) {
10292 return ACL . create ( {
10393 principalType : ACL . USER ,
@@ -115,6 +105,44 @@ describe('security ACLs', function() {
115105 } ) ;
116106 } ) ;
117107
108+ it ( 'supports ACL rules with a wildcard for models' , function ( ) {
109+ const A_USER_ID = 'a-test-user' ;
110+
111+ // By default, access is allowed to all users
112+ return assertPermission ( ACL . ALLOW , 'initial state' )
113+ // An ACL rule applying to all models denies access to everybody
114+ . then ( ( ) => ACL . create ( {
115+ model : '*' ,
116+ property : '*' ,
117+ accessType : '*' ,
118+ principalType : 'ROLE' ,
119+ principalId : '$everyone' ,
120+ permission : 'DENY' ,
121+ } ) )
122+ . then ( ( ) => assertPermission ( ACL . DENY , 'all denied' ) )
123+ // A rule for a specific model overrides the rule matching all models
124+ . then ( ( ) => ACL . create ( {
125+ model : testModel . modelName ,
126+ property : '*' ,
127+ accessType : '*' ,
128+ principalType : ACL . USER ,
129+ principalId : A_USER_ID ,
130+ permission : ACL . ALLOW ,
131+ } ) )
132+ . then ( ( ) => assertPermission ( ACL . ALLOW , 'only a single model allowed' ) ) ;
133+
134+ function assertPermission ( expectedPermission , msg ) {
135+ return ACL . checkAccessForContext ( {
136+ principals : [ { type : ACL . USER , id : A_USER_ID } ] ,
137+ model : testModel . modelName ,
138+ accessType : ACL . ALL ,
139+ } ) . then ( accessContext => {
140+ const actual = accessContext . isAllowed ( ) ? ACL . ALLOW : ACL . DENY ;
141+ expect ( actual , msg ) . to . equal ( expectedPermission ) ;
142+ } ) ;
143+ }
144+ } ) ;
145+
118146 it ( 'supports checkAccessForContext() returning a promise' , function ( ) {
119147 var testModel = ds . createModel ( 'testModel' , {
120148 acls : [
@@ -399,7 +427,6 @@ describe('security ACLs', function() {
399427 } ) ;
400428
401429 it ( 'should check access against LDL, ACL, and Role' , function ( done ) {
402- // var log = console.log;
403430 var log = function ( ) { } ;
404431
405432 // Create
@@ -645,3 +672,14 @@ describe('authorized roles propagation in RemotingContext', function() {
645672 . expect ( 200 ) ;
646673 }
647674} ) ;
675+
676+ function setupTestModels ( ) {
677+ ds = this . ds = loopback . createDataSource ( { connector : loopback . Memory } ) ;
678+ testModel = loopback . PersistedModel . extend ( 'testModel' ) ;
679+ ACL . attachTo ( ds ) ;
680+ Role . attachTo ( ds ) ;
681+ RoleMapping . attachTo ( ds ) ;
682+ User . attachTo ( ds ) ;
683+ Scope . attachTo ( ds ) ;
684+ testModel . attachTo ( ds ) ;
685+ }
0 commit comments