1616
1717use Authorization \AuthorizationServiceInterface ;
1818use Authorization \Exception \ForbiddenException ;
19- use Authorization \Exception \MissingIdentityException ;
2019use Authorization \IdentityInterface ;
20+ use Authorization \Policy \Result ;
21+ use Authorization \Policy \ResultInterface ;
2122use Cake \Controller \Component ;
2223use Cake \Http \ServerRequest ;
2324use InvalidArgumentException ;
@@ -65,7 +66,11 @@ public function authorize($resource, $action = null)
6566 $ action = $ this ->getDefaultAction ($ request );
6667 }
6768
68- if ($ this ->can ($ resource , $ action )) {
69+ $ result = $ this ->can ($ resource , $ action );
70+ if (!$ result instanceof ResultInterface) {
71+ $ result = new Result ($ result );
72+ }
73+ if ($ result ->getStatus ()) {
6974 return ;
7075 }
7176
@@ -76,7 +81,7 @@ public function authorize($resource, $action = null)
7681 } else {
7782 $ name = gettype ($ resource );
7883 }
79- throw new ForbiddenException ([$ action , $ name ]);
84+ throw new ForbiddenException ($ result , [$ action , $ name ]);
8085 }
8186
8287 /**
@@ -87,7 +92,7 @@ public function authorize($resource, $action = null)
8792 *
8893 * @param object $resource The resource to check authorization on.
8994 * @param string|null $action The action to check authorization for.
90- * @return bool
95+ * @return bool|\Authorization\Policy\ResultInterface
9196 */
9297 public function can ($ resource , $ action = null )
9398 {
@@ -97,15 +102,11 @@ public function can($resource, $action = null)
97102 }
98103
99104 $ identity = $ this ->getIdentity ($ request );
100- if (empty ($ identity ) && $ this ->getService ($ this ->request )->can (null , $ action , $ resource )) {
101- return true ;
102- }
103-
104- if (!empty ($ identity ) && $ identity ->can ($ action , $ resource )) {
105- return true ;
105+ if (empty ($ identity )) {
106+ return $ this ->getService ($ this ->request )->can (null , $ action , $ resource );
106107 }
107108
108- return false ;
109+ return $ identity -> can ( $ action , $ resource ) ;
109110 }
110111
111112 /**
0 commit comments