@@ -142,6 +142,34 @@ describe('access control - integration', function() {
142142 } ) ;
143143
144144 describe ( '/banks' , function ( ) {
145+ var SPECIAL_USER = { email :
'[email protected] ' , password :
'test' } ; 146+
147+ // define dynamic role that would only grant access when the authenticated user's email is equal to
148+ // SPECIAL_USER's email
149+
150+ before ( function ( ) {
151+ var roleModel = app . registry . getModel ( 'Role' ) ;
152+ var userModel = app . registry . getModel ( 'user' ) ;
153+
154+ roleModel . registerResolver ( '$dynamic-role' , function ( role , context , callback ) {
155+ if ( ! ( context && context . accessToken && context . accessToken . userId ) ) {
156+ return process . nextTick ( function ( ) {
157+ callback && callback ( null , false ) ;
158+ } ) ;
159+ }
160+ var accessToken = context . accessToken ;
161+ userModel . findById ( accessToken . userId , function ( err , user ) {
162+ if ( err ) {
163+ return callback ( err , false ) ;
164+ }
165+ if ( user && user . email === SPECIAL_USER . email ) {
166+ return callback ( null , true ) ;
167+ }
168+ return callback ( null , false ) ;
169+ } ) ;
170+ } ) ;
171+ } ) ;
172+
145173 lt . beforeEach . givenModel ( 'bank' ) ;
146174
147175 lt . it . shouldBeAllowedWhenCalledAnonymously ( 'GET' , '/api/banks' ) ;
@@ -163,6 +191,7 @@ describe('access control - integration', function() {
163191 lt . it . shouldBeDeniedWhenCalledAnonymously ( 'DELETE' , urlForBank ) ;
164192 lt . it . shouldBeDeniedWhenCalledUnauthenticated ( 'DELETE' , urlForBank ) ;
165193 lt . it . shouldBeDeniedWhenCalledByUser ( CURRENT_USER , 'DELETE' , urlForBank ) ;
194+ lt . it . shouldBeAllowedWhenCalledByUser ( SPECIAL_USER , 'DELETE' , urlForBank ) ;
166195
167196 function urlForBank ( ) {
168197 return '/api/banks/' + this . bank . id ;
0 commit comments