File tree Expand file tree Collapse file tree 2 files changed +41
-1
lines changed
test-harness/tests/specs/unit Expand file tree Collapse file tree 2 files changed +41
-1
lines changed Original file line number Diff line number Diff line change @@ -258,9 +258,33 @@ component singleton accessors="true" {
258
258
}
259
259
if ( results ) {
260
260
throw ( type = " NotAuthorized" , message = arguments .message );
261
+ }
262
+ return this ;
263
+ }
264
+
265
+ /**
266
+ * Verifies that the passed in user object must be the same as the authenticated user.
267
+ * Equality is done by evaluating the `getid()` method on both objects.
268
+ * If the equality check fails, a `NotAuthorized` exception is thrown.
269
+ *
270
+ * @throws NoUserLoggedIn
271
+ * @throws NotAuthorized
272
+ *
273
+ * @user The user to test for equality
274
+ * @message The error message to throw in the exception
275
+ */
276
+ CBSecurity function secureSameUser (
277
+ required user ,
278
+ message = variables .DEFAULT_ERROR_MESSAGE
279
+ ){
280
+ if ( ! sameUser ( arguments .user ) ) {
281
+ throw (
282
+ type = " NotAuthorized" ,
283
+ message = arguments .message
284
+ );
261
285
}
262
286
return this ;
263
- }
287
+ }
264
288
265
289
/**
266
290
* Alias proxy if somebody is coming from cbguard, proxies to the secure() method
Original file line number Diff line number Diff line change @@ -301,6 +301,22 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbsecurity.model
301
301
cbsecurity .secureWhen ( function ( user ){ return false ; } );
302
302
});
303
303
});
304
+ describe ( " secureSameUser() method" , function (){
305
+ it ( " can secure if the logged in user is not the user passed" , function (){
306
+ mockUser .$( " getId" , 1 );
307
+ var testUser = createStub ().$( " getId" , 2 );
308
+
309
+ expect ( function (){
310
+ cbsecurity .secureSameUser ( testUser );
311
+ }).toThrow ( " NotAuthorized" );
312
+ });
313
+
314
+ it ( " can allow if the logged in user is the user passed" , function (){
315
+ mockUser .$( " getId" , 1 );
316
+ var testUser = createStub ().$( " getId" , 1 );
317
+ cbsecurity .secureSameUser ( testUser );
318
+ });
319
+ });
304
320
});
305
321
306
322
});
You can’t perform that action at this time.
0 commit comments