AuthComponent.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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. By default unauthorized user is
  202. * redirected to the referrer url or AuthComponent::$loginRedirect or '/'.
  203. * If set to false a ForbiddenException exception is thrown instead of redirecting.
  204. *
  205. * @var boolean
  206. */
  207. public $unauthorizedRedirect = true;
  208. /**
  209. * Controller actions for which user validation is not required.
  210. *
  211. * @var array
  212. * @see AuthComponent::allow()
  213. */
  214. public $allowedActions = array();
  215. /**
  216. * Request object
  217. *
  218. * @var CakeRequest
  219. */
  220. public $request;
  221. /**
  222. * Response object
  223. *
  224. * @var CakeResponse
  225. */
  226. public $response;
  227. /**
  228. * Method list for bound controller
  229. *
  230. * @var array
  231. */
  232. protected $_methods = array();
  233. /**
  234. * Initializes AuthComponent for use in the controller
  235. *
  236. * @param Controller $controller A reference to the instantiating controller object
  237. * @return void
  238. */
  239. public function initialize(Controller $controller) {
  240. $this->request = $controller->request;
  241. $this->response = $controller->response;
  242. $this->_methods = $controller->methods;
  243. if (Configure::read('debug') > 0) {
  244. Debugger::checkSecurityKeys();
  245. }
  246. }
  247. /**
  248. * Main execution method. Handles redirecting of invalid users, and processing
  249. * of login form data.
  250. *
  251. * @param Controller $controller A reference to the instantiating controller object
  252. * @return boolean
  253. */
  254. public function startup(Controller $controller) {
  255. $methods = array_flip(array_map('strtolower', $controller->methods));
  256. $action = strtolower($controller->request->params['action']);
  257. $isMissingAction = (
  258. $controller->scaffold === false &&
  259. !isset($methods[$action])
  260. );
  261. if ($isMissingAction) {
  262. return true;
  263. }
  264. if (!$this->_setDefaults()) {
  265. return false;
  266. }
  267. $request = $controller->request;
  268. $url = '';
  269. if (isset($request->url)) {
  270. $url = $request->url;
  271. }
  272. $url = Router::normalize($url);
  273. $loginAction = Router::normalize($this->loginAction);
  274. if ($loginAction != $url && in_array($action, array_map('strtolower', $this->allowedActions))) {
  275. return true;
  276. }
  277. if ($loginAction == $url) {
  278. if (empty($request->data)) {
  279. if (!$this->Session->check('Auth.redirect') && !$this->loginRedirect && env('HTTP_REFERER')) {
  280. $this->Session->write('Auth.redirect', $controller->referer(null, true));
  281. }
  282. }
  283. return true;
  284. }
  285. if (!$this->_getUser()) {
  286. if (!$request->is('ajax')) {
  287. $this->flash($this->authError);
  288. $this->Session->write('Auth.redirect', $request->here());
  289. $controller->redirect($loginAction);
  290. return false;
  291. }
  292. if (!empty($this->ajaxLogin)) {
  293. $controller->viewPath = 'Elements';
  294. echo $controller->render($this->ajaxLogin, $this->RequestHandler->ajaxLayout);
  295. $this->_stop();
  296. return false;
  297. }
  298. $controller->redirect(null, 403);
  299. }
  300. if (empty($this->authorize) || $this->isAuthorized($this->user())) {
  301. return true;
  302. }
  303. return $this->_unauthorized($controller);
  304. }
  305. /**
  306. * Handle unauthorized access attempt
  307. *
  308. * @param Controller $controller A reference to the controller object
  309. * @return boolean Returns false
  310. * @throws ForbiddenException
  311. */
  312. protected function _unauthorized(Controller $controller) {
  313. if (!$this->unauthorizedRedirect) {
  314. throw new ForbiddenException($this->authError);
  315. }
  316. $this->flash($this->authError);
  317. $default = '/';
  318. if (!empty($this->loginRedirect)) {
  319. $default = $this->loginRedirect;
  320. }
  321. $controller->redirect($controller->referer($default, true), null, true);
  322. return false;
  323. }
  324. /**
  325. * Attempts to introspect the correct values for object properties.
  326. *
  327. * @return boolean
  328. */
  329. protected function _setDefaults() {
  330. $defaults = array(
  331. 'logoutRedirect' => $this->loginAction,
  332. 'authError' => __d('cake', 'You are not authorized to access that location.')
  333. );
  334. foreach ($defaults as $key => $value) {
  335. if (empty($this->{$key})) {
  336. $this->{$key} = $value;
  337. }
  338. }
  339. return true;
  340. }
  341. /**
  342. * Uses the configured Authorization adapters to check whether or not a user is authorized.
  343. * Each adapter will be checked in sequence, if any of them return true, then the user will
  344. * be authorized for the request.
  345. *
  346. * @param array $user The user to check the authorization of. If empty the user in the session will be used.
  347. * @param CakeRequest $request The request to authenticate for. If empty, the current request will be used.
  348. * @return boolean True if $user is authorized, otherwise false
  349. */
  350. public function isAuthorized($user = null, $request = null) {
  351. if (empty($user) && !$this->user()) {
  352. return false;
  353. }
  354. if (empty($user)) {
  355. $user = $this->user();
  356. }
  357. if (empty($request)) {
  358. $request = $this->request;
  359. }
  360. if (empty($this->_authorizeObjects)) {
  361. $this->constructAuthorize();
  362. }
  363. foreach ($this->_authorizeObjects as $authorizer) {
  364. if ($authorizer->authorize($user, $request) === true) {
  365. return true;
  366. }
  367. }
  368. return false;
  369. }
  370. /**
  371. * Loads the authorization objects configured.
  372. *
  373. * @return mixed Either null when authorize is empty, or the loaded authorization objects.
  374. * @throws CakeException
  375. */
  376. public function constructAuthorize() {
  377. if (empty($this->authorize)) {
  378. return;
  379. }
  380. $this->_authorizeObjects = array();
  381. $config = Hash::normalize((array)$this->authorize);
  382. $global = array();
  383. if (isset($config[AuthComponent::ALL])) {
  384. $global = $config[AuthComponent::ALL];
  385. unset($config[AuthComponent::ALL]);
  386. }
  387. foreach ($config as $class => $settings) {
  388. list($plugin, $class) = pluginSplit($class, true);
  389. $className = $class . 'Authorize';
  390. App::uses($className, $plugin . 'Controller/Component/Auth');
  391. if (!class_exists($className)) {
  392. throw new CakeException(__d('cake_dev', 'Authorization adapter "%s" was not found.', $class));
  393. }
  394. if (!method_exists($className, 'authorize')) {
  395. throw new CakeException(__d('cake_dev', 'Authorization objects must implement an authorize method.'));
  396. }
  397. $settings = array_merge($global, (array)$settings);
  398. $this->_authorizeObjects[] = new $className($this->_Collection, $settings);
  399. }
  400. return $this->_authorizeObjects;
  401. }
  402. /**
  403. * Takes a list of actions in the current controller for which authentication is not required, or
  404. * no parameters to allow all actions.
  405. *
  406. * You can use allow with either an array, or var args.
  407. *
  408. * `$this->Auth->allow(array('edit', 'add'));` or
  409. * `$this->Auth->allow('edit', 'add');` or
  410. * `$this->Auth->allow();` to allow all actions
  411. *
  412. * @param string|array $action,... Controller action name or array of actions
  413. * @return void
  414. * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-public
  415. */
  416. public function allow($action = null) {
  417. $args = func_get_args();
  418. if (empty($args) || $action === null) {
  419. $this->allowedActions = $this->_methods;
  420. return;
  421. }
  422. if (isset($args[0]) && is_array($args[0])) {
  423. $args = $args[0];
  424. }
  425. $this->allowedActions = array_merge($this->allowedActions, $args);
  426. }
  427. /**
  428. * Removes items from the list of allowed/no authentication required actions.
  429. *
  430. * You can use deny with either an array, or var args.
  431. *
  432. * `$this->Auth->deny(array('edit', 'add'));` or
  433. * `$this->Auth->deny('edit', 'add');` or
  434. * `$this->Auth->deny();` to remove all items from the allowed list
  435. *
  436. * @param string|array $action,... Controller action name or array of actions
  437. * @return void
  438. * @see AuthComponent::allow()
  439. * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#making-actions-require-authorization
  440. */
  441. public function deny($action = null) {
  442. $args = func_get_args();
  443. if (empty($args) || $action === null) {
  444. $this->allowedActions = array();
  445. return;
  446. }
  447. if (isset($args[0]) && is_array($args[0])) {
  448. $args = $args[0];
  449. }
  450. foreach ($args as $arg) {
  451. $i = array_search($arg, $this->allowedActions);
  452. if (is_int($i)) {
  453. unset($this->allowedActions[$i]);
  454. }
  455. }
  456. $this->allowedActions = array_values($this->allowedActions);
  457. }
  458. /**
  459. * Maps action names to CRUD operations. Used for controller-based authentication. Make sure
  460. * to configure the authorize property before calling this method. As it delegates $map to all the
  461. * attached authorize objects.
  462. *
  463. * @param array $map Actions to map
  464. * @return void
  465. * @see BaseAuthorize::mapActions()
  466. * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#mapping-actions-when-using-crudauthorize
  467. */
  468. public function mapActions($map = array()) {
  469. if (empty($this->_authorizeObjects)) {
  470. $this->constructAuthorize();
  471. }
  472. foreach ($this->_authorizeObjects as $auth) {
  473. $auth->mapActions($map);
  474. }
  475. }
  476. /**
  477. * 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
  478. * specified, the request will be used to identify a user. If the identification was successful,
  479. * the user record is written to the session key specified in AuthComponent::$sessionKey. Logging in
  480. * will also change the session id in order to help mitigate session replays.
  481. *
  482. * @param array $user Either an array of user data, or null to identify a user using the current request.
  483. * @return boolean True on login success, false on failure
  484. * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#identifying-users-and-logging-them-in
  485. */
  486. public function login($user = null) {
  487. $this->_setDefaults();
  488. if (empty($user)) {
  489. $user = $this->identify($this->request, $this->response);
  490. }
  491. if ($user) {
  492. $this->Session->renew();
  493. $this->Session->write(self::$sessionKey, $user);
  494. }
  495. return $this->loggedIn();
  496. }
  497. /**
  498. * Logs a user out, and returns the login action to redirect to.
  499. * Triggers the logout() method of all the authenticate objects, so they can perform
  500. * custom logout logic. AuthComponent will remove the session data, so
  501. * there is no need to do that in an authentication object. Logging out
  502. * will also renew the session id. This helps mitigate issues with session replays.
  503. *
  504. * @return string AuthComponent::$logoutRedirect
  505. * @see AuthComponent::$logoutRedirect
  506. * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#logging-users-out
  507. */
  508. public function logout() {
  509. $this->_setDefaults();
  510. if (empty($this->_authenticateObjects)) {
  511. $this->constructAuthenticate();
  512. }
  513. $user = $this->user();
  514. foreach ($this->_authenticateObjects as $auth) {
  515. $auth->logout($user);
  516. }
  517. $this->Session->delete(self::$sessionKey);
  518. $this->Session->delete('Auth.redirect');
  519. $this->Session->renew();
  520. return Router::normalize($this->logoutRedirect);
  521. }
  522. /**
  523. * Get the current user.
  524. *
  525. * Will prefer the static user cache over sessions. The static user
  526. * cache is primarily used for stateless authentication. For stateful authentication,
  527. * cookies + sessions will be used.
  528. *
  529. * @param string $key field to retrieve. Leave null to get entire User record
  530. * @return mixed User record. or null if no user is logged in.
  531. * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
  532. */
  533. public static function user($key = null) {
  534. if (empty(self::$_user) && !CakeSession::check(self::$sessionKey)) {
  535. return null;
  536. }
  537. if (!empty(self::$_user)) {
  538. $user = self::$_user;
  539. } else {
  540. $user = CakeSession::read(self::$sessionKey);
  541. }
  542. if ($key === null) {
  543. return $user;
  544. }
  545. return Hash::get($user, $key);
  546. }
  547. /**
  548. * Similar to AuthComponent::user() except if the session user cannot be found, connected authentication
  549. * objects will have their getUser() methods called. This lets stateless authentication methods function correctly.
  550. *
  551. * @return boolean true if a user can be found, false if one cannot.
  552. */
  553. protected function _getUser() {
  554. $user = $this->user();
  555. if ($user) {
  556. return true;
  557. }
  558. if (empty($this->_authenticateObjects)) {
  559. $this->constructAuthenticate();
  560. }
  561. foreach ($this->_authenticateObjects as $auth) {
  562. $result = $auth->getUser($this->request);
  563. if (!empty($result) && is_array($result)) {
  564. self::$_user = $result;
  565. return true;
  566. }
  567. }
  568. return false;
  569. }
  570. /**
  571. * If no parameter is passed, gets the authentication redirect URL. Pass a url in to
  572. * set the destination a user should be redirected to upon logging in. Will fallback to
  573. * AuthComponent::$loginRedirect if there is no stored redirect value.
  574. *
  575. * @param string|array $url Optional URL to write as the login redirect URL.
  576. * @return string Redirect URL
  577. */
  578. public function redirect($url = null) {
  579. if (!is_null($url)) {
  580. $redir = $url;
  581. $this->Session->write('Auth.redirect', $redir);
  582. } elseif ($this->Session->check('Auth.redirect')) {
  583. $redir = $this->Session->read('Auth.redirect');
  584. $this->Session->delete('Auth.redirect');
  585. if (Router::normalize($redir) == Router::normalize($this->loginAction)) {
  586. $redir = $this->loginRedirect;
  587. }
  588. } else {
  589. $redir = $this->loginRedirect;
  590. }
  591. return Router::normalize($redir);
  592. }
  593. /**
  594. * Use the configured authentication adapters, and attempt to identify the user
  595. * by credentials contained in $request.
  596. *
  597. * @param CakeRequest $request The request that contains authentication data.
  598. * @param CakeResponse $response The response
  599. * @return array User record data, or false, if the user could not be identified.
  600. */
  601. public function identify(CakeRequest $request, CakeResponse $response) {
  602. if (empty($this->_authenticateObjects)) {
  603. $this->constructAuthenticate();
  604. }
  605. foreach ($this->_authenticateObjects as $auth) {
  606. $result = $auth->authenticate($request, $response);
  607. if (!empty($result) && is_array($result)) {
  608. return $result;
  609. }
  610. }
  611. return false;
  612. }
  613. /**
  614. * loads the configured authentication objects.
  615. *
  616. * @return mixed either null on empty authenticate value, or an array of loaded objects.
  617. * @throws CakeException
  618. */
  619. public function constructAuthenticate() {
  620. if (empty($this->authenticate)) {
  621. return;
  622. }
  623. $this->_authenticateObjects = array();
  624. $config = Hash::normalize((array)$this->authenticate);
  625. $global = array();
  626. if (isset($config[AuthComponent::ALL])) {
  627. $global = $config[AuthComponent::ALL];
  628. unset($config[AuthComponent::ALL]);
  629. }
  630. foreach ($config as $class => $settings) {
  631. list($plugin, $class) = pluginSplit($class, true);
  632. $className = $class . 'Authenticate';
  633. App::uses($className, $plugin . 'Controller/Component/Auth');
  634. if (!class_exists($className)) {
  635. throw new CakeException(__d('cake_dev', 'Authentication adapter "%s" was not found.', $class));
  636. }
  637. if (!method_exists($className, 'authenticate')) {
  638. throw new CakeException(__d('cake_dev', 'Authentication objects must implement an authenticate method.'));
  639. }
  640. $settings = array_merge($global, (array)$settings);
  641. $this->_authenticateObjects[] = new $className($this->_Collection, $settings);
  642. }
  643. return $this->_authenticateObjects;
  644. }
  645. /**
  646. * Hash a password with the application's salt value (as defined with Configure::write('Security.salt');
  647. *
  648. * This method is intended as a convenience wrapper for Security::hash(). If you want to use
  649. * a hashing/encryption system not supported by that method, do not use this method.
  650. *
  651. * @param string $password Password to hash
  652. * @return string Hashed password
  653. * @link http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#hashing-passwords
  654. */
  655. public static function password($password) {
  656. return Security::hash($password, null, true);
  657. }
  658. /**
  659. * Component shutdown. If user is logged in, wipe out redirect.
  660. *
  661. * @param Controller $controller Instantiating controller
  662. * @return void
  663. */
  664. public function shutdown(Controller $controller) {
  665. if ($this->loggedIn()) {
  666. $this->Session->delete('Auth.redirect');
  667. }
  668. }
  669. /**
  670. * Check whether or not the current user has data in the session, and is considered logged in.
  671. *
  672. * @return boolean true if the user is logged in, false otherwise
  673. */
  674. public function loggedIn() {
  675. return $this->user() != array();
  676. }
  677. /**
  678. * Set a flash message. Uses the Session component, and values from AuthComponent::$flash.
  679. *
  680. * @param string $message The message to set.
  681. * @return void
  682. */
  683. public function flash($message) {
  684. $this->Session->setFlash($message, $this->flash['element'], $this->flash['params'], $this->flash['key']);
  685. }
  686. }