Skip to content

Commit 409ab1b

Browse files
committed
Merge branch 'development'
2 parents bd4e5e9 + 498e31f commit 409ab1b

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

box.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name":"ColdBox Security",
3-
"version":"2.10.0",
3+
"version":"2.11.0",
44
"location":"https://downloads.ortussolutions.com/ortussolutions/coldbox-modules/cbsecurity/@build.version@/[email protected]@.zip",
55
"author":"Ortus Solutions.com <[email protected]>",
66
"slug":"cbsecurity",

changelog.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
----
99

10+
## [2.11.0] => 2021-MAR-10
11+
12+
### Added
13+
14+
* Add a `secureSameUser` method to throw when passed a different user #29 (https://github.com/coldbox-modules/cbsecurity/pull/29)
15+
16+
----
17+
1018
## [2.10.0] => 2021-FEB-12
1119

1220
### Added

models/CBSecurity.cfc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,9 +258,33 @@ component singleton accessors="true" {
258258
}
259259
if ( results ) {
260260
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+
);
261285
}
262286
return this;
263-
}
287+
}
264288

265289
/**
266290
* Alias proxy if somebody is coming from cbguard, proxies to the secure() method

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,22 @@ component extends="coldbox.system.testing.BaseModelTest" model="cbsecurity.model
301301
cbsecurity.secureWhen( function( user ){ return false; } );
302302
});
303303
});
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+
});
304320
});
305321

306322
});

0 commit comments

Comments
 (0)