Skip to content

Commit bbd296e

Browse files
committed
cbauth validator finalized
1 parent 39923ac commit bbd296e

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

models/CBAuthValidator.cfc

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* Copyright since 2016 by Ortus Solutions, Corp
3+
* www.ortussolutions.com
4+
* ---
5+
* This is the core validator which leverages CF Security via cflogin and cfloginuser
6+
* https://helpx.adobe.com/coldfusion/developing-applications/developing-cfml-applications/securing-applications/using-coldfusion-security-tags-and-functions.html
7+
*/
8+
component singleton{
9+
10+
// Injection
11+
property name="cbauth" inject="authenticationService@cbauth";
12+
13+
/**
14+
* This function is called once an incoming event matches a security rule.
15+
* You will receive the security rule that matched and an instance of the
16+
* ColdBox controller.
17+
*
18+
* You must return a struct with two keys:
19+
* - allow:boolean True, user can continue access, false, invalid access actions will ensue
20+
* - type:string(authentication|authorization) The type of block that ocurred. Either an authentication or an authorization issue.
21+
* - messages:string Info/debug messages
22+
*
23+
* @return { allow:boolean, type:authentication|authorization, messages:string }
24+
*/
25+
struct function ruleValidator( required rule, required controller ){
26+
return validateSecurity( arguments.rule.permissions );
27+
}
28+
29+
/**
30+
* This function is called once access to a handler/action is detected.
31+
* You will receive the secured annotation value and an instance of the ColdBox Controller
32+
*
33+
* You must return a struct with two keys:
34+
* - allow:boolean True, user can continue access, false, invalid access actions will ensue
35+
* - type:string(authentication|authorization) The type of block that ocurred. Either an authentication or an authorization issue.
36+
* - messages:string Info/debug messages
37+
*
38+
* @return { allow:boolean, type:authentication|authorization, messages:string }
39+
*/
40+
struct function annotationValidator( required securedValue, required controller ){
41+
return validateSecurity( arguments.securedValue );
42+
}
43+
44+
/**
45+
* Validate Security via CBAuth
46+
*
47+
* @permissions
48+
*/
49+
private function validateSecurity( required permissions ){
50+
var results = { "allow" : false, "type" : "authentication", "messages" : "" };
51+
52+
// Are we logged in?
53+
if( variables.cbauth.isLoggedIn() ){
54+
55+
// Do we have any permissions?
56+
if( listLen( arguments.permissions ) ){
57+
results.allow = variables.cbauth.getUser().hasPermission( arguments.permissions );
58+
results.type = "authorization";
59+
} else {
60+
// We are satisfied!
61+
results.allow = true;
62+
}
63+
}
64+
65+
return results;
66+
}
67+
68+
}

models/CFValidator.cfc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ component singleton{
1212
* You will receive the security rule that matched and an instance of the
1313
* ColdBox controller.
1414
*
15-
* allow : True, user can continue access, false, invalid access actions will ensue
16-
* type : Is the issue an authentication or an authorization issue.
15+
* You must return a struct with two keys:
16+
* - allow:boolean True, user can continue access, false, invalid access actions will ensue
17+
* - type:string(authentication|authorization) The type of block that ocurred. Either an authentication or an authorization issue.
18+
* - messages:string Info/debug messages
1719
*
18-
* @return { allow:boolean, type:authentication|authorization }
20+
* @return { allow:boolean, type:authentication|authorization, messages:string }
1921
*/
2022
struct function ruleValidator( required rule, required controller ){
2123
return validateSecurity( arguments.rule.roles );
@@ -28,8 +30,9 @@ component singleton{
2830
* You must return a struct with two keys:
2931
* - allow:boolean True, user can continue access, false, invalid access actions will ensue
3032
* - type:string(authentication|authorization) The type of block that ocurred. Either an authentication or an authorization issue.
33+
* - messages:string Info/debug messages
3134
*
32-
* @return { allow:boolean, type:string(authentication|authorization) }
35+
* @return { allow:boolean, type:authentication|authorization, messages:string }
3336
*/
3437
struct function annotationValidator( required securedValue, required controller ){
3538
return validateSecurity( arguments.securedValue );
@@ -52,7 +55,7 @@ component singleton{
5255
results.type = "authorization";
5356
} else {
5457
// We are satisfied!
55-
results.allow.true;
58+
results.allow = true;
5659
}
5760
}
5861

0 commit comments

Comments
 (0)