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.8 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.8
      • 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
  • None

Classes

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