From bbf7575f4d0074ad35fe04033259f1ac4fdd54d1 Mon Sep 17 00:00:00 2001 From: Jose Lorenzo Rodriguez Date: Thu, 5 Jun 2014 21:32:11 +0200 Subject: [PATCH] Adding a couple methods to AuthComponent that are handy for doing meta programming on the authorization flow --- src/Controller/Component/AuthComponent.php | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/Controller/Component/AuthComponent.php b/src/Controller/Component/AuthComponent.php index ee1b5672493..a8c88ecf68d 100644 --- a/src/Controller/Component/AuthComponent.php +++ b/src/Controller/Component/AuthComponent.php @@ -220,6 +220,23 @@ class AuthComponent extends Component { */ protected $_methods = array(); +/** + * The instance of the Authenticate provider that was used for + * successfully logging in the current user after calling `login()` + * in the same request + * + * @var Cake\Auth\BaseAuthenticate + */ + protected $_authenticateProvider; + +/** + * The instance of the Authorize provider that was used to grant + * access to the current user to the url they are requesting. + * + * @var Cake\Auth\BaseAuthorize + */ + protected $_authorizationProvider; + /** * Initializes AuthComponent for use in the controller. * @@ -455,6 +472,7 @@ public function isAuthorized($user = null, Request $request = null) { } foreach ($this->_authorizeObjects as $authorizer) { if ($authorizer->authorize($user, $request) === true) { + $this->_authorizationProvider = $authorizer; return true; } } @@ -722,6 +740,7 @@ public function identify(Request $request, Response $response) { foreach ($this->_authenticateObjects as $auth) { $result = $auth->authenticate($request, $response); if (!empty($result) && is_array($result)) { + $this->_authenticateProvider = $auth; return $result; } } @@ -783,4 +802,25 @@ public function flash($message, $type = 'error') { $this->session->flash($message, 'error', $params + compact('key')); } +/** + * If login was called during this request and the suer was successfully + * authenticated, this function will return the instance of the authentication + * object that was used for logging the user in. + * + * @return \Cake\Auth\BaseAuthenticate|null + */ + public function loginProvider() { + return $this->_authenticateProvider; + } + +/** + * If there was any authorization processing for the current request, this function + * will return the instance of the Authorization object that granted access to the + * user to the current address. + * + * @return \Cake\Auth\BaseAuthorize|null + */ + public function authorizationProvider() { + return $this->_authenticateProvider; + } }