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

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