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
+ }
0 commit comments