AuthComponent.php 23 KB

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