AuthComponent.php 22 KB

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