CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.5 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.5
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • AclComponent
  • AuthComponent
  • CookieComponent
  • EmailComponent
  • PaginatorComponent
  • RequestHandlerComponent
  • SecurityComponent
  • SessionComponent
  1: <?php
  2: /**
  3:  * Authentication component
  4:  *
  5:  * Manages user logins and permissions.
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * For full copyright and license information, please see the LICENSE.txt
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @package       Cake.Controller.Component
 17:  * @since         CakePHP(tm) v 0.10.0.1076
 18:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 19:  */
 20: 
 21: App::uses('Component', 'Controller');
 22: App::uses('Router', 'Routing');
 23: App::uses('Security', 'Utility');
 24: App::uses('Debugger', 'Utility');
 25: App::uses('Hash', 'Utility');
 26: App::uses('CakeSession', 'Model/Datasource');
 27: App::uses('BaseAuthorize', 'Controller/Component/Auth');
 28: App::uses('BaseAuthenticate', 'Controller/Component/Auth');
 29: 
 30: /**
 31:  * Authentication control component class
 32:  *
 33:  * Binds access control with user authentication and session management.
 34:  *
 35:  * @package       Cake.Controller.Component
 36:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
 37:  */
 38: class AuthComponent extends Component {
 39: 
 40: /**
 41:  * Constant for 'all'
 42:  *
 43:  * @var string
 44:  */
 45:     const ALL = 'all';
 46: 
 47: /**
 48:  * Other components utilized by AuthComponent
 49:  *
 50:  * @var array
 51:  */
 52:     public $components = array('Session', 'RequestHandler');
 53: 
 54: /**
 55:  * An array of authentication objects to use for authenticating users. You can configure
 56:  * multiple adapters and they will be checked sequentially when users are identified.
 57:  *
 58:  * {{{
 59:  *  $this->Auth->authenticate = array(
 60:  *      'Form' => array(
 61:  *          'userModel' => 'Users.User'
 62:  *      )
 63:  *  );
 64:  * }}}
 65:  *
 66:  * Using the class name without 'Authenticate' as the key, you can pass in an array of settings for each
 67:  * authentication object. Additionally you can define settings that should be set to all authentications objects
 68:  * using the 'all' key:
 69:  *
 70:  * {{{
 71:  *  $this->Auth->authenticate = array(
 72:  *      'all' => array(
 73:  *          'userModel' => 'Users.User',
 74:  *          'scope' => array('User.active' => 1)
 75:  *      ),
 76:  *      'Form',
 77:  *      'Basic'
 78:  *  );
 79:  * }}}
 80:  *
 81:  * You can also use AuthComponent::ALL instead of the string 'all'.
 82:  *
 83:  * @var array
 84:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
 85:  */
 86:     public $authenticate = array('Form');
 87: 
 88: /**
 89:  * Objects that will be used for authentication checks.
 90:  *
 91:  * @var array
 92:  */
 93:     protected $_authenticateObjects = array();
 94: 
 95: /**
 96:  * An array of authorization objects to use for authorizing users. You can configure
 97:  * multiple adapters and they will be checked sequentially when authorization checks are done.
 98:  *
 99:  * {{{
100:  *  $this->Auth->authorize = array(
101:  *      'Crud' => array(
102:  *          'actionPath' => 'controllers/'
103:  *      )
104:  *  );
105:  * }}}
106:  *
107:  * Using the class name without 'Authorize' as the key, you can pass in an array of settings for each
108:  * authorization object. Additionally you can define settings that should be set to all authorization objects
109:  * using the 'all' key:
110:  *
111:  * {{{
112:  *  $this->Auth->authorize = array(
113:  *      'all' => array(
114:  *          'actionPath' => 'controllers/'
115:  *      ),
116:  *      'Crud',
117:  *      'CustomAuth'
118:  *  );
119:  * }}}
120:  *
121:  * You can also use AuthComponent::ALL instead of the string 'all'
122:  *
123:  * @var mixed
124:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization
125:  */
126:     public $authorize = false;
127: 
128: /**
129:  * Objects that will be used for authorization checks.
130:  *
131:  * @var array
132:  */
133:     protected $_authorizeObjects = array();
134: 
135: /**
136:  * The name of an optional view element to render when an Ajax request is made
137:  * with an invalid or expired session
138:  *
139:  * @var string
140:  */
141:     public $ajaxLogin = null;
142: 
143: /**
144:  * Settings to use when Auth needs to do a flash message with SessionComponent::setFlash().
145:  * Available keys are:
146:  *
147:  * - `element` - The element to use, defaults to 'default'.
148:  * - `key` - The key to use, defaults to 'auth'
149:  * - `params` - The array of additional params to use, defaults to array()
150:  *
151:  * @var array
152:  */
153:     public $flash = array(
154:         'element' => 'default',
155:         'key' => 'auth',
156:         'params' => array()
157:     );
158: 
159: /**
160:  * The session key name where the record of the current user is stored. Default
161:  * key is "Auth.User". If you are using only stateless authenticators set this
162:  * to false to ensure session is not started.
163:  *
164:  * @var string
165:  */
166:     public static $sessionKey = 'Auth.User';
167: 
168: /**
169:  * The current user, used for stateless authentication when
170:  * sessions are not available.
171:  *
172:  * @var array
173:  */
174:     protected static $_user = array();
175: 
176: /**
177:  * A URL (defined as a string or array) to the controller action that handles
178:  * logins. Defaults to `/users/login`.
179:  *
180:  * @var mixed
181:  */
182:     public $loginAction = array(
183:         'controller' => 'users',
184:         'action' => 'login',
185:         'plugin' => null
186:     );
187: 
188: /**
189:  * Normally, if a user is redirected to the $loginAction page, the location they
190:  * were redirected from will be stored in the session so that they can be
191:  * redirected back after a successful login. If this session value is not
192:  * set, redirectUrl() method will return the URL specified in $loginRedirect.
193:  *
194:  * @var mixed
195:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::$loginRedirect
196:  */
197:     public $loginRedirect = null;
198: 
199: /**
200:  * The default action to redirect to after the user is logged out. While AuthComponent does
201:  * not handle post-logout redirection, a redirect URL will be returned from AuthComponent::logout().
202:  * Defaults to AuthComponent::$loginAction.
203:  *
204:  * @var mixed
205:  * @see AuthComponent::$loginAction
206:  * @see AuthComponent::logout()
207:  */
208:     public $logoutRedirect = null;
209: 
210: /**
211:  * Error to display when user attempts to access an object or action to which they do not have
212:  * access.
213:  *
214:  * @var string|bool
215:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#AuthComponent::$authError
216:  */
217:     public $authError = null;
218: 
219: /**
220:  * Controls handling of unauthorized access.
221:  * - For default value `true` unauthorized user is redirected to the referrer URL
222:  *   or AuthComponent::$loginRedirect or '/'.
223:  * - If set to a string or array the value is used as a URL to redirect to.
224:  * - If set to false a ForbiddenException exception is thrown instead of redirecting.
225:  *
226:  * @var mixed
227:  */
228:     public $unauthorizedRedirect = true;
229: 
230: /**
231:  * Controller actions for which user validation is not required.
232:  *
233:  * @var array
234:  * @see AuthComponent::allow()
235:  */
236:     public $allowedActions = array();
237: 
238: /**
239:  * Request object
240:  *
241:  * @var CakeRequest
242:  */
243:     public $request;
244: 
245: /**
246:  * Response object
247:  *
248:  * @var CakeResponse
249:  */
250:     public $response;
251: 
252: /**
253:  * Method list for bound controller.
254:  *
255:  * @var array
256:  */
257:     protected $_methods = array();
258: 
259: /**
260:  * Initializes AuthComponent for use in the controller.
261:  *
262:  * @param Controller $controller A reference to the instantiating controller object
263:  * @return void
264:  */
265:     public function initialize(Controller $controller) {
266:         $this->request = $controller->request;
267:         $this->response = $controller->response;
268:         $this->_methods = $controller->methods;
269: 
270:         if (Configure::read('debug') > 0) {
271:             Debugger::checkSecurityKeys();
272:         }
273:     }
274: 
275: /**
276:  * Main execution method. Handles redirecting of invalid users, and processing
277:  * of login form data.
278:  *
279:  * @param Controller $controller A reference to the instantiating controller object
280:  * @return bool
281:  */
282:     public function startup(Controller $controller) {
283:         $methods = array_flip(array_map('strtolower', $controller->methods));
284:         $action = strtolower($controller->request->params['action']);
285: 
286:         $isMissingAction = (
287:             $controller->scaffold === false &&
288:             !isset($methods[$action])
289:         );
290: 
291:         if ($isMissingAction) {
292:             return true;
293:         }
294: 
295:         if (!$this->_setDefaults()) {
296:             return false;
297:         }
298: 
299:         if ($this->_isAllowed($controller)) {
300:             return true;
301:         }
302: 
303:         if (!$this->_getUser()) {
304:             return $this->_unauthenticated($controller);
305:         }
306: 
307:         if ($this->_isLoginAction($controller) ||
308:             empty($this->authorize) ||
309:             $this->isAuthorized($this->user())
310:         ) {
311:             return true;
312:         }
313: 
314:         return $this->_unauthorized($controller);
315:     }
316: 
317: /**
318:  * Checks whether current action is accessible without authentication.
319:  *
320:  * @param Controller $controller A reference to the instantiating controller object
321:  * @return bool True if action is accessible without authentication else false
322:  */
323:     protected function _isAllowed(Controller $controller) {
324:         $action = strtolower($controller->request->params['action']);
325:         if (in_array($action, array_map('strtolower', $this->allowedActions))) {
326:             return true;
327:         }
328:         return false;
329:     }
330: 
331: /**
332:  * Handles unauthenticated access attempt. First the `unathenticated()` method
333:  * of the last authenticator in the chain will be called. The authenticator can
334:  * handle sending response or redirection as appropriate and return `true` to
335:  * indicate no furthur action is necessary. If authenticator returns null this
336:  * method redirects user to login action. If it's an ajax request and
337:  * $ajaxLogin is specified that element is rendered else a 403 http status code
338:  * is returned.
339:  *
340:  * @param Controller $controller A reference to the controller object.
341:  * @return bool True if current action is login action else false.
342:  */
343:     protected function _unauthenticated(Controller $controller) {
344:         if (empty($this->_authenticateObjects)) {
345:             $this->constructAuthenticate();
346:         }
347:         $auth = $this->_authenticateObjects[count($this->_authenticateObjects) - 1];
348:         if ($auth->unauthenticated($this->request, $this->response)) {
349:             return false;
350:         }
351: 
352:         if ($this->_isLoginAction($controller)) {
353:             if (empty($controller->request->data)) {
354:                 if (!$this->Session->check('Auth.redirect') && env('HTTP_REFERER')) {
355:                     $this->Session->write('Auth.redirect', $controller->referer(null, true));
356:                 }
357:             }
358:             return true;
359:         }
360: 
361:         if (!$controller->request->is('ajax')) {
362:             $this->flash($this->authError);
363:             $this->Session->write('Auth.redirect', $controller->request->here(false));
364:             $controller->redirect($this->loginAction);
365:             return false;
366:         }
367:         if (!empty($this->ajaxLogin)) {
368:             $controller->response->statusCode(403);
369:             $controller->viewPath = 'Elements';
370:             $response = $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
371:             $response->send();
372:             $this->_stop();
373:             return false;
374:         }
375:         $controller->redirect(null, 403);
376:         return false;
377:     }
378: 
379: /**
380:  * Normalizes $loginAction and checks if current request URL is same as login action.
381:  *
382:  * @param Controller $controller A reference to the controller object.
383:  * @return bool True if current action is login action else false.
384:  */
385:     protected function _isLoginAction(Controller $controller) {
386:         $url = '';
387:         if (isset($controller->request->url)) {
388:             $url = $controller->request->url;
389:         }
390:         $url = Router::normalize($url);
391:         $loginAction = Router::normalize($this->loginAction);
392: 
393:         return $loginAction === $url;
394:     }
395: 
396: /**
397:  * Handle unauthorized access attempt
398:  *
399:  * @param Controller $controller A reference to the controller object
400:  * @return bool Returns false
401:  * @throws ForbiddenException
402:  * @see AuthComponent::$unauthorizedRedirect
403:  */
404:     protected function _unauthorized(Controller $controller) {
405:         if ($this->unauthorizedRedirect === false) {
406:             throw new ForbiddenException($this->authError);
407:         }
408: 
409:         $this->flash($this->authError);
410:         if ($this->unauthorizedRedirect === true) {
411:             $default = '/';
412:             if (!empty($this->loginRedirect)) {
413:                 $default = $this->loginRedirect;
414:             }
415:             $url = $controller->referer($default, true);
416:         } else {
417:             $url = $this->unauthorizedRedirect;
418:         }
419:         $controller->redirect($url, null, true);
420:         return false;
421:     }
422: 
423: /**
424:  * Attempts to introspect the correct values for object properties.
425:  *
426:  * @return bool True
427:  */
428:     protected function _setDefaults() {
429:         $defaults = array(
430:             'logoutRedirect' => $this->loginAction,
431:             'authError' => __d('cake', 'You are not authorized to access that location.')
432:         );
433:         foreach ($defaults as $key => $value) {
434:             if (!isset($this->{$key}) || $this->{$key} === true) {
435:                 $this->{$key} = $value;
436:             }
437:         }
438:         return true;
439:     }
440: 
441: /**
442:  * Check if the provided user is authorized for the request.
443:  *
444:  * Uses the configured Authorization adapters to check whether or not a user is authorized.
445:  * Each adapter will be checked in sequence, if any of them return true, then the user will
446:  * be authorized for the request.
447:  *
448:  * @param array $user The user to check the authorization of. If empty the user in the session will be used.
449:  * @param CakeRequest $request The request to authenticate for. If empty, the current request will be used.
450:  * @return bool True if $user is authorized, otherwise false
451:  */
452:     public function isAuthorized($user = null, CakeRequest $request = null) {
453:         if (empty($user) && !$this->user()) {
454:             return false;
455:         }
456:         if (empty($user)) {
457:             $user = $this->user();
458:         }
459:         if (empty($request)) {
460:             $request = $this->request;
461:         }
462:         if (empty($this->_authorizeObjects)) {
463:             $this->constructAuthorize();
464:         }
465:         foreach ($this->_authorizeObjects as $authorizer) {
466:             if ($authorizer->authorize($user, $request) === true) {
467:                 return true;
468:             }
469:         }
470:         return false;
471:     }
472: 
473: /**
474:  * Loads the authorization objects configured.
475:  *
476:  * @return mixed Either null when authorize is empty, or the loaded authorization objects.
477:  * @throws CakeException
478:  */
479:     public function constructAuthorize() {
480:         if (empty($this->authorize)) {
481:             return;
482:         }
483:         $this->_authorizeObjects = array();
484:         $config = Hash::normalize((array)$this->authorize);
485:         $global = array();
486:         if (isset($config[AuthComponent::ALL])) {
487:             $global = $config[AuthComponent::ALL];
488:             unset($config[AuthComponent::ALL]);
489:         }
490:         foreach ($config as $class => $settings) {
491:             list($plugin, $class) = pluginSplit($class, true);
492:             $className = $class . 'Authorize';
493:             App::uses($className, $plugin . 'Controller/Component/Auth');
494:             if (!class_exists($className)) {
495:                 throw new CakeException(__d('cake_dev', 'Authorization adapter "%s" was not found.', $class));
496:             }
497:             if (!method_exists($className, 'authorize')) {
498:                 throw new CakeException(__d('cake_dev', 'Authorization objects must implement an %s method.', 'authorize()'));
499:             }
500:             $settings = array_merge($global, (array)$settings);
501:             $this->_authorizeObjects[] = new $className($this->_Collection, $settings);
502:         }
503:         return $this->_authorizeObjects;
504:     }
505: 
506: /**
507:  * Takes a list of actions in the current controller for which authentication is not required, or
508:  * no parameters to allow all actions.
509:  *
510:  * You can use allow with either an array, or var args.
511:  *
512:  * `$this->Auth->allow(array('edit', 'add'));` or
513:  * `$this->Auth->allow('edit', 'add');` or
514:  * `$this->Auth->allow();` to allow all actions
515:  *
516:  * @param string|array $action Controller action name or array of actions
517:  * @return void
518:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-public
519:  */
520:     public function allow($action = null) {
521:         $args = func_get_args();
522:         if (empty($args) || $action === null) {
523:             $this->allowedActions = $this->_methods;
524:             return;
525:         }
526:         if (isset($args[0]) && is_array($args[0])) {
527:             $args = $args[0];
528:         }
529:         $this->allowedActions = array_merge($this->allowedActions, $args);
530:     }
531: 
532: /**
533:  * Removes items from the list of allowed/no authentication required actions.
534:  *
535:  * You can use deny with either an array, or var args.
536:  *
537:  * `$this->Auth->deny(array('edit', 'add'));` or
538:  * `$this->Auth->deny('edit', 'add');` or
539:  * `$this->Auth->deny();` to remove all items from the allowed list
540:  *
541:  * @param string|array $action Controller action name or array of actions
542:  * @return void
543:  * @see AuthComponent::allow()
544:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-require-authorization
545:  */
546:     public function deny($action = null) {
547:         $args = func_get_args();
548:         if (empty($args) || $action === null) {
549:             $this->allowedActions = array();
550:             return;
551:         }
552:         if (isset($args[0]) && is_array($args[0])) {
553:             $args = $args[0];
554:         }
555:         foreach ($args as $arg) {
556:             $i = array_search($arg, $this->allowedActions);
557:             if (is_int($i)) {
558:                 unset($this->allowedActions[$i]);
559:             }
560:         }
561:         $this->allowedActions = array_values($this->allowedActions);
562:     }
563: 
564: /**
565:  * Maps action names to CRUD operations.
566:  *
567:  * Used for controller-based authentication. Make sure
568:  * to configure the authorize property before calling this method. As it delegates $map to all the
569:  * attached authorize objects.
570:  *
571:  * @param array $map Actions to map
572:  * @return void
573:  * @see BaseAuthorize::mapActions()
574:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#mapping-actions-when-using-crudauthorize
575:  */
576:     public function mapActions($map = array()) {
577:         if (empty($this->_authorizeObjects)) {
578:             $this->constructAuthorize();
579:         }
580:         foreach ($this->_authorizeObjects as $auth) {
581:             $auth->mapActions($map);
582:         }
583:     }
584: 
585: /**
586:  * Log a user in.
587:  *
588:  * If a $user is provided that data will be stored as the logged in user. If `$user` is empty or not
589:  * specified, the request will be used to identify a user. If the identification was successful,
590:  * the user record is written to the session key specified in AuthComponent::$sessionKey. Logging in
591:  * will also change the session id in order to help mitigate session replays.
592:  *
593:  * @param array $user Either an array of user data, or null to identify a user using the current request.
594:  * @return bool True on login success, false on failure
595:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
596:  */
597:     public function login($user = null) {
598:         $this->_setDefaults();
599: 
600:         if (empty($user)) {
601:             $user = $this->identify($this->request, $this->response);
602:         }
603:         if ($user) {
604:             $this->Session->renew();
605:             $this->Session->write(self::$sessionKey, $user);
606:         }
607:         return (bool)$this->user();
608:     }
609: 
610: /**
611:  * Log a user out.
612:  *
613:  * Returns the logout action to redirect to. Triggers the logout() method of
614:  * all the authenticate objects, so they can perform custom logout logic.
615:  * AuthComponent will remove the session data, so there is no need to do that
616:  * in an authentication object. Logging out will also renew the session id.
617:  * This helps mitigate issues with session replays.
618:  *
619:  * @return string AuthComponent::$logoutRedirect
620:  * @see AuthComponent::$logoutRedirect
621:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#logging-users-out
622:  */
623:     public function logout() {
624:         $this->_setDefaults();
625:         if (empty($this->_authenticateObjects)) {
626:             $this->constructAuthenticate();
627:         }
628:         $user = $this->user();
629:         foreach ($this->_authenticateObjects as $auth) {
630:             $auth->logout($user);
631:         }
632:         $this->Session->delete(self::$sessionKey);
633:         $this->Session->delete('Auth.redirect');
634:         $this->Session->renew();
635:         return Router::normalize($this->logoutRedirect);
636:     }
637: 
638: /**
639:  * Get the current user.
640:  *
641:  * Will prefer the static user cache over sessions. The static user
642:  * cache is primarily used for stateless authentication. For stateful authentication,
643:  * cookies + sessions will be used.
644:  *
645:  * @param string $key field to retrieve. Leave null to get entire User record
646:  * @return array|null User record. or null if no user is logged in.
647:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
648:  */
649:     public static function user($key = null) {
650:         if (!empty(self::$_user)) {
651:             $user = self::$_user;
652:         } elseif (self::$sessionKey && CakeSession::check(self::$sessionKey)) {
653:             $user = CakeSession::read(self::$sessionKey);
654:         } else {
655:             return null;
656:         }
657:         if ($key === null) {
658:             return $user;
659:         }
660:         return Hash::get($user, $key);
661:     }
662: 
663: /**
664:  * Similar to AuthComponent::user() except if the session user cannot be found, connected authentication
665:  * objects will have their getUser() methods called. This lets stateless authentication methods function correctly.
666:  *
667:  * @return bool true if a user can be found, false if one cannot.
668:  */
669:     protected function _getUser() {
670:         $user = $this->user();
671:         if ($user) {
672:             $this->Session->delete('Auth.redirect');
673:             return true;
674:         }
675: 
676:         if (empty($this->_authenticateObjects)) {
677:             $this->constructAuthenticate();
678:         }
679:         foreach ($this->_authenticateObjects as $auth) {
680:             $result = $auth->getUser($this->request);
681:             if (!empty($result) && is_array($result)) {
682:                 self::$_user = $result;
683:                 return true;
684:             }
685:         }
686: 
687:         return false;
688:     }
689: 
690: /**
691:  * Backwards compatible alias for AuthComponent::redirectUrl().
692:  *
693:  * @param string|array $url Optional URL to write as the login redirect URL.
694:  * @return string Redirect URL
695:  * @deprecated 3.0.0 Since 2.3.0, use AuthComponent::redirectUrl() instead
696:  */
697:     public function redirect($url = null) {
698:         return $this->redirectUrl($url);
699:     }
700: 
701: /**
702:  * Get the URL a user should be redirected to upon login.
703:  *
704:  * Pass a URL in to set the destination a user should be redirected to upon
705:  * logging in.
706:  *
707:  * If no parameter is passed, gets the authentication redirect URL. The URL
708:  * returned is as per following rules:
709:  *
710:  *  - Returns the normalized URL from session Auth.redirect value if it is
711:  *    present and for the same domain the current app is running on.
712:  *  - If there is no session value and there is a $loginRedirect, the $loginRedirect
713:  *    value is returned.
714:  *  - If there is no session and no $loginRedirect, / is returned.
715:  *
716:  * @param string|array $url Optional URL to write as the login redirect URL.
717:  * @return string Redirect URL
718:  */
719:     public function redirectUrl($url = null) {
720:         if ($url !== null) {
721:             $redir = $url;
722:             $this->Session->write('Auth.redirect', $redir);
723:         } elseif ($this->Session->check('Auth.redirect')) {
724:             $redir = $this->Session->read('Auth.redirect');
725:             $this->Session->delete('Auth.redirect');
726: 
727:             if (Router::normalize($redir) === Router::normalize($this->loginAction)) {
728:                 $redir = $this->loginRedirect;
729:             }
730:         } elseif ($this->loginRedirect) {
731:             $redir = $this->loginRedirect;
732:         } else {
733:             $redir = '/';
734:         }
735:         if (is_array($redir)) {
736:             return Router::url($redir + array('base' => false));
737:         }
738:         return $redir;
739:     }
740: 
741: /**
742:  * Use the configured authentication adapters, and attempt to identify the user
743:  * by credentials contained in $request.
744:  *
745:  * @param CakeRequest $request The request that contains authentication data.
746:  * @param CakeResponse $response The response
747:  * @return array User record data, or false, if the user could not be identified.
748:  */
749:     public function identify(CakeRequest $request, CakeResponse $response) {
750:         if (empty($this->_authenticateObjects)) {
751:             $this->constructAuthenticate();
752:         }
753:         foreach ($this->_authenticateObjects as $auth) {
754:             $result = $auth->authenticate($request, $response);
755:             if (!empty($result) && is_array($result)) {
756:                 return $result;
757:             }
758:         }
759:         return false;
760:     }
761: 
762: /**
763:  * Loads the configured authentication objects.
764:  *
765:  * @return mixed either null on empty authenticate value, or an array of loaded objects.
766:  * @throws CakeException
767:  */
768:     public function constructAuthenticate() {
769:         if (empty($this->authenticate)) {
770:             return;
771:         }
772:         $this->_authenticateObjects = array();
773:         $config = Hash::normalize((array)$this->authenticate);
774:         $global = array();
775:         if (isset($config[AuthComponent::ALL])) {
776:             $global = $config[AuthComponent::ALL];
777:             unset($config[AuthComponent::ALL]);
778:         }
779:         foreach ($config as $class => $settings) {
780:             if (!empty($settings['className'])) {
781:                 $class = $settings['className'];
782:                 unset($settings['className']);
783:             }
784:             list($plugin, $class) = pluginSplit($class, true);
785:             $className = $class . 'Authenticate';
786:             App::uses($className, $plugin . 'Controller/Component/Auth');
787:             if (!class_exists($className)) {
788:                 throw new CakeException(__d('cake_dev', 'Authentication adapter "%s" was not found.', $class));
789:             }
790:             if (!method_exists($className, 'authenticate')) {
791:                 throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()'));
792:             }
793:             $settings = array_merge($global, (array)$settings);
794:             $this->_authenticateObjects[] = new $className($this->_Collection, $settings);
795:         }
796:         return $this->_authenticateObjects;
797:     }
798: 
799: /**
800:  * Hash a password with the application's salt value (as defined with Configure::write('Security.salt');
801:  *
802:  * This method is intended as a convenience wrapper for Security::hash(). If you want to use
803:  * a hashing/encryption system not supported by that method, do not use this method.
804:  *
805:  * @param string $password Password to hash
806:  * @return string Hashed password
807:  * @deprecated 3.0.0 Since 2.4. Use Security::hash() directly or a password hasher object.
808:  */
809:     public static function password($password) {
810:         return Security::hash($password, null, true);
811:     }
812: 
813: /**
814:  * Check whether or not the current user has data in the session, and is considered logged in.
815:  *
816:  * @return bool true if the user is logged in, false otherwise
817:  * @deprecated 3.0.0 Since 2.5. Use AuthComponent::user() directly.
818:  */
819:     public function loggedIn() {
820:         return (bool)$this->user();
821:     }
822: 
823: /**
824:  * Set a flash message. Uses the Session component, and values from AuthComponent::$flash.
825:  *
826:  * @param string $message The message to set.
827:  * @return void
828:  */
829:     public function flash($message) {
830:         if ($message === false) {
831:             return;
832:         }
833:         $this->Session->setFlash($message, $this->flash['element'], $this->flash['params'], $this->flash['key']);
834:     }
835: 
836: }
837: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs