Skip to content

Commit cfecdad

Browse files
committed
all methods implemented
1 parent f1d4ede commit cfecdad

File tree

2 files changed

+154
-5
lines changed

2 files changed

+154
-5
lines changed

models/CBSecurity.cfc

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,13 @@ component singleton accessors="true" {
286286
* @fail The closure/lambda/udf that executes if the context fails
287287
*/
288288
function when( required permissions, required success, fail ){
289-
289+
arguments.permissions = arrayWrap( arguments.permissions );
290+
if( has( arguments.permissions ) ){
291+
arguments.success( getAuthService().getUser(), arguments.permissions );
292+
} else if( !isNull( arguments.fail ) ){
293+
arguments.fail( getAuthService().getUser(), arguments.permissions );
294+
}
295+
return this;
290296
}
291297

292298
/**
@@ -309,7 +315,13 @@ component singleton accessors="true" {
309315
* @fail The closure/lambda/udf that executes if the context fails
310316
*/
311317
function whenAll( required permissions, required success, fail ){
312-
318+
arguments.permissions = arrayWrap( arguments.permissions );
319+
if( all( arguments.permissions ) ){
320+
arguments.success( getAuthService().getUser(), arguments.permissions );
321+
} else if( !isNull( arguments.fail ) ){
322+
arguments.fail( getAuthService().getUser(), arguments.permissions );
323+
}
324+
return this;
313325
}
314326

315327
/**
@@ -332,7 +344,13 @@ component singleton accessors="true" {
332344
* @fail The closure/lambda/udf that executes if the context fails
333345
*/
334346
function whenNone( required permissions, required success, fail ){
335-
347+
arguments.permissions = arrayWrap( arguments.permissions );
348+
if( none( arguments.permissions ) ){
349+
arguments.success( getAuthService().getUser(), arguments.permissions );
350+
} else if( !isNull( arguments.fail ) ){
351+
arguments.fail( getAuthService().getUser(), arguments.permissions );
352+
}
353+
return this;
336354
}
337355

338356
/***************************************************************/

test-harness/tests/specs/unit/CBSecurityTest.cfc

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,139 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbsecurity.model
9494
});
9595
});
9696

97+
98+
describe( "action context methods", function(){
99+
describe( "when() methods", function(){
100+
it( "can call the sucess closure when the permissions pass", function(){
101+
var testVar = false;
102+
mockUser.$( "hasPermission", true );
103+
104+
cbsecurity.when( "test", function( user ){
105+
testVar = true;
106+
} );
107+
108+
expect( testVar ).toBeTrue();
109+
});
110+
it( "can call the fail closure when the permissions fail", function(){
111+
var testVar = "";
112+
mockUser.$( "hasPermission", false );
113+
114+
cbsecurity.when(
115+
"test",
116+
// success
117+
function( user ){
118+
testVar = false;
119+
},
120+
// fail
121+
function( user ){
122+
testVar = true;
123+
}
124+
);
125+
126+
expect( testVar ).toBeTrue();
127+
});
128+
it( "can ignore the success when the permissions fail and no fail has been provided", function(){
129+
var testVar = "";
130+
mockUser.$( "hasPermission", false );
131+
132+
cbsecurity.when(
133+
"test",
134+
// success
135+
function( user ){
136+
testVar = false;
137+
}
138+
);
139+
expect( testVar ).toBe( "" );
140+
});
141+
});
142+
describe( "whenAll() methods", function(){
143+
it( "can call the sucess closure when the permissions pass", function(){
144+
var testVar = false;
145+
mockUser.$( "hasPermission" ).$results( true, true );
146+
147+
cbsecurity.whenAll( "test,test2", function( user ){
148+
testVar = true;
149+
} );
150+
151+
expect( testVar ).toBeTrue();
152+
});
153+
it( "can call the fail closure when the permissions fail", function(){
154+
var testVar = "";
155+
mockUser.$( "hasPermission", false );
156+
157+
cbsecurity.whenAll(
158+
"test",
159+
// success
160+
function( user ){
161+
testVar = false;
162+
},
163+
// fail
164+
function( user ){
165+
testVar = true;
166+
}
167+
);
168+
169+
expect( testVar ).toBeTrue();
170+
});
171+
it( "can ignore the success when the permissions fail and no fail has been provided", function(){
172+
var testVar = "";
173+
mockUser.$( "hasPermission", false );
174+
175+
cbsecurity.whenAll(
176+
"test",
177+
// success
178+
function( user ){
179+
testVar = false;
180+
}
181+
);
182+
expect( testVar ).toBe( "" );
183+
});
184+
});
185+
describe( "whenNone() methods", function(){
186+
it( "can call the sucess closure when the permissions are none", function(){
187+
var testVar = false;
188+
mockUser.$( "hasPermission" ).$results( false, false );
189+
190+
cbsecurity.whenNone( "test,test2", function( user ){
191+
testVar = true;
192+
} );
193+
194+
expect( testVar ).toBeTrue();
195+
});
196+
it( "can call the fail closure when the permissions are found", function(){
197+
var testVar = "";
198+
mockUser.$( "hasPermission", true );
199+
200+
cbsecurity.whenNone(
201+
"test",
202+
// success
203+
function( user ){
204+
testVar = false;
205+
},
206+
// fail
207+
function( user ){
208+
testVar = true;
209+
}
210+
);
211+
212+
expect( testVar ).toBeTrue();
213+
});
214+
it( "can ignore the success when the permissions are found and no fail has been provided", function(){
215+
var testVar = "";
216+
mockUser.$( "hasPermission", true );
217+
218+
cbsecurity.whenNone(
219+
"test",
220+
// success
221+
function( user ){
222+
testVar = false;
223+
}
224+
);
225+
expect( testVar ).toBe( "" );
226+
});
227+
});
228+
});
229+
97230
describe( "blocking methods", function(){
98231
describe( "secure() method", function(){
99232
it( "can allow a secure() function if permissions pass", function(){
@@ -149,7 +282,6 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbsecurity.model
149282
}).toThrow( type="NotAuthorized", regex="Invalid User Baby" );
150283
});
151284
});
152-
153285
describe( "secureWhen() method", function(){
154286
it( "can secure if a boolean true is passed", function(){
155287
expect( function(){
@@ -168,7 +300,6 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbsecurity.model
168300
it( "can allow if a closure executes as false", function(){
169301
cbsecurity.secureWhen( function( user ){ return false; } );
170302
});
171-
172303
});
173304
});
174305

0 commit comments

Comments
 (0)