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

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.6
      • 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: 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', '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 array
 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 array
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')) {
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->redirect(null, 403);
377:         return false;
378:     }
379: 
380: /**
381:  * Normalizes $loginAction and checks if current request URL is same as login action.
382:  *
383:  * @param Controller $controller A reference to the controller object.
384:  * @return bool True if current action is login action else false.
385:  */
386:     protected function _isLoginAction(Controller $controller) {
387:         $url = '';
388:         if (isset($controller->request->url)) {
389:             $url = $controller->request->url;
390:         }
391:         $url = Router::normalize($url);
392:         $loginAction = Router::normalize($this->loginAction);
393: 
394:         return $loginAction === $url;
395:     }
396: 
397: /**
398:  * Handle unauthorized access attempt
399:  *
400:  * @param Controller $controller A reference to the controller object
401:  * @return bool Returns false
402:  * @throws ForbiddenException
403:  * @see AuthComponent::$unauthorizedRedirect
404:  */
405:     protected function _unauthorized(Controller $controller) {
406:         if ($this->unauthorizedRedirect === false) {
407:             throw new ForbiddenException($this->authError);
408:         }
409: 
410:         $this->flash($this->authError);
411:         if ($this->unauthorizedRedirect === true) {
412:             $default = '/';
413:             if (!empty($this->loginRedirect)) {
414:                 $default = $this->loginRedirect;
415:             }
416:             $url = $controller->referer($default, true);
417:         } else {
418:             $url = $this->unauthorizedRedirect;
419:         }
420:         $controller->redirect($url, null, true);
421:         return false;
422:     }
423: 
424: /**
425:  * Attempts to introspect the correct values for object properties.
426:  *
427:  * @return bool True
428:  */
429:     protected function _setDefaults() {
430:         $defaults = array(
431:             'logoutRedirect' => $this->loginAction,
432:             'authError' => __d('cake', 'You are not authorized to access that location.')
433:         );
434:         foreach ($defaults as $key => $value) {
435:             if (!isset($this->{$key}) || $this->{$key} === true) {
436:                 $this->{$key} = $value;
437:             }
438:         }
439:         return true;
440:     }
441: 
442: /**
443:  * Check if the provided user is authorized for the request.
444:  *
445:  * Uses the configured Authorization adapters to check whether or not a user is authorized.
446:  * Each adapter will be checked in sequence, if any of them return true, then the user will
447:  * be authorized for the request.
448:  *
449:  * @param array $user The user to check the authorization of. If empty the user in the session will be used.
450:  * @param CakeRequest $request The request to authenticate for. If empty, the current request will be used.
451:  * @return bool True if $user is authorized, otherwise false
452:  */
453:     public function isAuthorized($user = null, CakeRequest $request = null) {
454:         if (empty($user) && !$this->user()) {
455:             return false;
456:         }
457:         if (empty($user)) {
458:             $user = $this->user();
459:         }
460:         if (empty($request)) {
461:             $request = $this->request;
462:         }
463:         if (empty($this->_authorizeObjects)) {
464:             $this->constructAuthorize();
465:         }
466:         foreach ($this->_authorizeObjects as $authorizer) {
467:             if ($authorizer->authorize($user, $request) === true) {
468:                 return true;
469:             }
470:         }
471:         return false;
472:     }
473: 
474: /**
475:  * Loads the authorization objects configured.
476:  *
477:  * @return mixed Either null when authorize is empty, or the loaded authorization objects.
478:  * @throws CakeException
479:  */
480:     public function constructAuthorize() {
481:         if (empty($this->authorize)) {
482:             return;
483:         }
484:         $this->_authorizeObjects = array();
485:         $config = Hash::normalize((array)$this->authorize);
486:         $global = array();
487:         if (isset($config[AuthComponent::ALL])) {
488:             $global = $config[AuthComponent::ALL];
489:             unset($config[AuthComponent::ALL]);
490:         }
491:         foreach ($config as $class => $settings) {
492:             list($plugin, $class) = pluginSplit($class, true);
493:             $className = $class . 'Authorize';
494:             App::uses($className, $plugin . 'Controller/Component/Auth');
495:             if (!class_exists($className)) {
496:                 throw new CakeException(__d('cake_dev', 'Authorization adapter "%s" was not found.', $class));
497:             }
498:             if (!method_exists($className, 'authorize')) {
499:                 throw new CakeException(__d('cake_dev', 'Authorization objects must implement an %s method.', 'authorize()'));
500:             }
501:             $settings = array_merge($global, (array)$settings);
502:             $this->_authorizeObjects[] = new $className($this->_Collection, $settings);
503:         }
504:         return $this->_authorizeObjects;
505:     }
506: 
507: /**
508:  * Takes a list of actions in the current controller for which authentication is not required, or
509:  * no parameters to allow all actions.
510:  *
511:  * You can use allow with either an array, or var args.
512:  *
513:  * `$this->Auth->allow(array('edit', 'add'));` or
514:  * `$this->Auth->allow('edit', 'add');` or
515:  * `$this->Auth->allow();` to allow all actions
516:  *
517:  * @param string|array $action Controller action name or array of actions
518:  * @return void
519:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-public
520:  */
521:     public function allow($action = null) {
522:         $args = func_get_args();
523:         if (empty($args) || $action === null) {
524:             $this->allowedActions = $this->_methods;
525:             return;
526:         }
527:         if (isset($args[0]) && is_array($args[0])) {
528:             $args = $args[0];
529:         }
530:         $this->allowedActions = array_merge($this->allowedActions, $args);
531:     }
532: 
533: /**
534:  * Removes items from the list of allowed/no authentication required actions.
535:  *
536:  * You can use deny with either an array, or var args.
537:  *
538:  * `$this->Auth->deny(array('edit', 'add'));` or
539:  * `$this->Auth->deny('edit', 'add');` or
540:  * `$this->Auth->deny();` to remove all items from the allowed list
541:  *
542:  * @param string|array $action Controller action name or array of actions
543:  * @return void
544:  * @see AuthComponent::allow()
545:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-require-authorization
546:  */
547:     public function deny($action = null) {
548:         $args = func_get_args();
549:         if (empty($args) || $action === null) {
550:             $this->allowedActions = array();
551:             return;
552:         }
553:         if (isset($args[0]) && is_array($args[0])) {
554:             $args = $args[0];
555:         }
556:         foreach ($args as $arg) {
557:             $i = array_search($arg, $this->allowedActions);
558:             if (is_int($i)) {
559:                 unset($this->allowedActions[$i]);
560:             }
561:         }
562:         $this->allowedActions = array_values($this->allowedActions);
563:     }
564: 
565: /**
566:  * Maps action names to CRUD operations.
567:  *
568:  * Used for controller-based authentication. Make sure
569:  * to configure the authorize property before calling this method. As it delegates $map to all the
570:  * attached authorize objects.
571:  *
572:  * @param array $map Actions to map
573:  * @return void
574:  * @see BaseAuthorize::mapActions()
575:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#mapping-actions-when-using-crudauthorize
576:  * @deprecated 3.0.0 Map actions using `actionMap` config key on authorize objects instead
577:  */
578:     public function mapActions($map = array()) {
579:         if (empty($this->_authorizeObjects)) {
580:             $this->constructAuthorize();
581:         }
582:         $mappedActions = array();
583:         foreach ($this->_authorizeObjects as $auth) {
584:             $mappedActions = Hash::merge($mappedActions, $auth->mapActions($map));
585:         }
586:         if (empty($map)) {
587:             return $mappedActions;
588:         }
589:     }
590: 
591: /**
592:  * Log a user in.
593:  *
594:  * If a $user is provided that data will be stored as the logged in user. If `$user` is empty or not
595:  * specified, the request will be used to identify a user. If the identification was successful,
596:  * the user record is written to the session key specified in AuthComponent::$sessionKey. Logging in
597:  * will also change the session id in order to help mitigate session replays.
598:  *
599:  * @param array $user Either an array of user data, or null to identify a user using the current request.
600:  * @return bool True on login success, false on failure
601:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
602:  */
603:     public function login($user = null) {
604:         $this->_setDefaults();
605: 
606:         if (empty($user)) {
607:             $user = $this->identify($this->request, $this->response);
608:         }
609:         if ($user) {
610:             $this->Session->renew();
611:             $this->Session->write(self::$sessionKey, $user);
612:             $event = new CakeEvent('Auth.afterIdentify', $this, array('user' => $user));
613:             $this->_Collection->getController()->getEventManager()->dispatch($event);
614:         }
615:         return (bool)$this->user();
616:     }
617: 
618: /**
619:  * Log a user out.
620:  *
621:  * Returns the logout action to redirect to. Triggers the logout() method of
622:  * all the authenticate objects, so they can perform custom logout logic.
623:  * AuthComponent will remove the session data, so there is no need to do that
624:  * in an authentication object. Logging out will also renew the session id.
625:  * This helps mitigate issues with session replays.
626:  *
627:  * @return string AuthComponent::$logoutRedirect
628:  * @see AuthComponent::$logoutRedirect
629:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#logging-users-out
630:  */
631:     public function logout() {
632:         $this->_setDefaults();
633:         if (empty($this->_authenticateObjects)) {
634:             $this->constructAuthenticate();
635:         }
636:         $user = $this->user();
637:         foreach ($this->_authenticateObjects as $auth) {
638:             $auth->logout($user);
639:         }
640:         $this->Session->delete(self::$sessionKey);
641:         $this->Session->delete('Auth.redirect');
642:         $this->Session->renew();
643:         return Router::normalize($this->logoutRedirect);
644:     }
645: 
646: /**
647:  * Get the current user.
648:  *
649:  * Will prefer the static user cache over sessions. The static user
650:  * cache is primarily used for stateless authentication. For stateful authentication,
651:  * cookies + sessions will be used.
652:  *
653:  * @param string $key field to retrieve. Leave null to get entire User record
654:  * @return array|null User record. or null if no user is logged in.
655:  * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
656:  */
657:     public static function user($key = null) {
658:         if (!empty(self::$_user)) {
659:             $user = self::$_user;
660:         } elseif (self::$sessionKey && CakeSession::check(self::$sessionKey)) {
661:             $user = CakeSession::read(self::$sessionKey);
662:         } else {
663:             return null;
664:         }
665:         if ($key === null) {
666:             return $user;
667:         }
668:         return Hash::get($user, $key);
669:     }
670: 
671: /**
672:  * Similar to AuthComponent::user() except if the session user cannot be found, connected authentication
673:  * objects will have their getUser() methods called. This lets stateless authentication methods function correctly.
674:  *
675:  * @return bool true if a user can be found, false if one cannot.
676:  */
677:     protected function _getUser() {
678:         $user = $this->user();
679:         if ($user) {
680:             $this->Session->delete('Auth.redirect');
681:             return true;
682:         }
683: 
684:         if (empty($this->_authenticateObjects)) {
685:             $this->constructAuthenticate();
686:         }
687:         foreach ($this->_authenticateObjects as $auth) {
688:             $result = $auth->getUser($this->request);
689:             if (!empty($result) && is_array($result)) {
690:                 self::$_user = $result;
691:                 return true;
692:             }
693:         }
694: 
695:         return false;
696:     }
697: 
698: /**
699:  * Backwards compatible alias for AuthComponent::redirectUrl().
700:  *
701:  * @param string|array $url Optional URL to write as the login redirect URL.
702:  * @return string Redirect URL
703:  * @deprecated 3.0.0 Since 2.3.0, use AuthComponent::redirectUrl() instead
704:  */
705:     public function redirect($url = null) {
706:         return $this->redirectUrl($url);
707:     }
708: 
709: /**
710:  * Get the URL a user should be redirected to upon login.
711:  *
712:  * Pass a URL in to set the destination a user should be redirected to upon
713:  * logging in.
714:  *
715:  * If no parameter is passed, gets the authentication redirect URL. The URL
716:  * returned is as per following rules:
717:  *
718:  *  - Returns the normalized URL from session Auth.redirect value if it is
719:  *    present and for the same domain the current app is running on.
720:  *  - If there is no session value and there is a $loginRedirect, the $loginRedirect
721:  *    value is returned.
722:  *  - If there is no session and no $loginRedirect, / is returned.
723:  *
724:  * @param string|array $url Optional URL to write as the login redirect URL.
725:  * @return string Redirect URL
726:  */
727:     public function redirectUrl($url = null) {
728:         if ($url !== null) {
729:             $redir = $url;
730:             $this->Session->write('Auth.redirect', $redir);
731:         } elseif ($this->Session->check('Auth.redirect')) {
732:             $redir = $this->Session->read('Auth.redirect');
733:             $this->Session->delete('Auth.redirect');
734: 
735:             if (Router::normalize($redir) === Router::normalize($this->loginAction)) {
736:                 $redir = $this->loginRedirect;
737:             }
738:         } elseif ($this->loginRedirect) {
739:             $redir = $this->loginRedirect;
740:         } else {
741:             $redir = '/';
742:         }
743:         if (is_array($redir)) {
744:             return Router::url($redir + array('base' => false));
745:         }
746:         return $redir;
747:     }
748: 
749: /**
750:  * Use the configured authentication adapters, and attempt to identify the user
751:  * by credentials contained in $request.
752:  *
753:  * @param CakeRequest $request The request that contains authentication data.
754:  * @param CakeResponse $response The response
755:  * @return array User record data, or false, if the user could not be identified.
756:  */
757:     public function identify(CakeRequest $request, CakeResponse $response) {
758:         if (empty($this->_authenticateObjects)) {
759:             $this->constructAuthenticate();
760:         }
761:         foreach ($this->_authenticateObjects as $auth) {
762:             $result = $auth->authenticate($request, $response);
763:             if (!empty($result) && is_array($result)) {
764:                 return $result;
765:             }
766:         }
767:         return false;
768:     }
769: 
770: /**
771:  * Loads the configured authentication objects.
772:  *
773:  * @return mixed either null on empty authenticate value, or an array of loaded objects.
774:  * @throws CakeException
775:  */
776:     public function constructAuthenticate() {
777:         if (empty($this->authenticate)) {
778:             return;
779:         }
780:         $this->_authenticateObjects = array();
781:         $config = Hash::normalize((array)$this->authenticate);
782:         $global = array();
783:         if (isset($config[AuthComponent::ALL])) {
784:             $global = $config[AuthComponent::ALL];
785:             unset($config[AuthComponent::ALL]);
786:         }
787:         foreach ($config as $class => $settings) {
788:             if (!empty($settings['className'])) {
789:                 $class = $settings['className'];
790:                 unset($settings['className']);
791:             }
792:             list($plugin, $class) = pluginSplit($class, true);
793:             $className = $class . 'Authenticate';
794:             App::uses($className, $plugin . 'Controller/Component/Auth');
795:             if (!class_exists($className)) {
796:                 throw new CakeException(__d('cake_dev', 'Authentication adapter "%s" was not found.', $class));
797:             }
798:             if (!method_exists($className, 'authenticate')) {
799:                 throw new CakeException(__d('cake_dev', 'Authentication objects must implement an %s method.', 'authenticate()'));
800:             }
801:             $settings = array_merge($global, (array)$settings);
802:             $auth = new $className($this->_Collection, $settings);
803:             $this->_Collection->getController()->getEventManager()->attach($auth);
804:             $this->_authenticateObjects[] = $auth;
805:         }
806:         return $this->_authenticateObjects;
807:     }
808: 
809: /**
810:  * Hash a password with the application's salt value (as defined with Configure::write('Security.salt');
811:  *
812:  * This method is intended as a convenience wrapper for Security::hash(). If you want to use
813:  * a hashing/encryption system not supported by that method, do not use this method.
814:  *
815:  * @param string $password Password to hash
816:  * @return string Hashed password
817:  * @deprecated 3.0.0 Since 2.4. Use Security::hash() directly or a password hasher object.
818:  */
819:     public static function password($password) {
820:         return Security::hash($password, null, true);
821:     }
822: 
823: /**
824:  * Check whether or not the current user has data in the session, and is considered logged in.
825:  *
826:  * @return bool true if the user is logged in, false otherwise
827:  * @deprecated 3.0.0 Since 2.5. Use AuthComponent::user() directly.
828:  */
829:     public function loggedIn() {
830:         return (bool)$this->user();
831:     }
832: 
833: /**
834:  * Set a flash message. Uses the Session component, and values from AuthComponent::$flash.
835:  *
836:  * @param string $message The message to set.
837:  * @return void
838:  */
839:     public function flash($message) {
840:         if ($message === false) {
841:             return;
842:         }
843:         $this->Session->setFlash($message, $this->flash['element'], $this->flash['params'], $this->flash['key']);
844:     }
845: 
846: }
847: 
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