|
| 1 | +<?php |
| 2 | +App::uses('BaseAuthenticate', 'Controller/Component/Auth'); |
| 3 | +App::uses('AuthComponent', 'Controller/Component'); |
| 4 | +App::uses('Router', 'Routing'); |
| 5 | + |
| 6 | +/** |
| 7 | + * An authentication adapter for AuthComponent. Provides the ability to authenticate using COOKIE |
| 8 | + * |
| 9 | + * {{{ |
| 10 | + * $this->Auth->authenticate = array( |
| 11 | + * 'Authenticate.Cookie' => array( |
| 12 | + * 'fields' => array( |
| 13 | + * 'username' => 'username', |
| 14 | + * 'password' => 'password' |
| 15 | + * ), |
| 16 | + * 'userModel' => 'User', |
| 17 | + * 'scope' => array('User.active' => 1), |
| 18 | + * 'crypt' => 'rijndael', // Defaults to rijndael(safest), optionally set to 'cipher' if required |
| 19 | + * 'cookie' => array( |
| 20 | + * 'name' => 'RememberMe', |
| 21 | + * 'time' => '+2 weeks', |
| 22 | + * ) |
| 23 | + * ) |
| 24 | + * ) |
| 25 | + * }}} |
| 26 | + * |
| 27 | + * @author Ceeram |
| 28 | + * @copyright Ceeram |
| 29 | + * @license MIT |
| 30 | + * @link https://github.com/ceeram/Authenticate |
| 31 | + */ |
| 32 | +class CookieAuthenticate extends BaseAuthenticate { |
| 33 | + |
| 34 | + public function __construct(ComponentCollection $collection, $settings) { |
| 35 | + $this->settings['cookie'] = array( |
| 36 | + 'name' => 'RememberMe', |
| 37 | + 'time' => '+2 weeks', |
| 38 | + 'base' => Router::getRequest()->base |
| 39 | + ); |
| 40 | + $this->settings['crypt'] = 'rijndael'; |
| 41 | + parent::__construct($collection, $settings); |
| 42 | + } |
| 43 | + |
| 44 | +/** |
| 45 | + * Authenticates the identity contained in the cookie. Will use the `settings.userModel`, and `settings.fields` |
| 46 | + * to find COOKIE data that is used to find a matching record in the `settings.userModel`. Will return false if |
| 47 | + * there is no cookie data, either username or password is missing, of if the scope conditions have not been met. |
| 48 | + * |
| 49 | + * @param CakeRequest $request The unused request object |
| 50 | + * @return mixed False on login failure. An array of User data on success. |
| 51 | + * @throws CakeException |
| 52 | + */ |
| 53 | + public function getUser(CakeRequest $request) { |
| 54 | + if (!isset($this->_Collection->Cookie) || !$this->_Collection->Cookie instanceof CookieComponent) { |
| 55 | + throw new CakeException('CookieComponent is not loaded'); |
| 56 | + } |
| 57 | + |
| 58 | + $this->_Collection->Cookie->type($this->settings['crypt']); |
| 59 | + list(, $model) = pluginSplit($this->settings['userModel']); |
| 60 | + |
| 61 | + $data = $this->_Collection->Cookie->read($model); |
| 62 | + if (empty($data)) { |
| 63 | + return false; |
| 64 | + } |
| 65 | + |
| 66 | + extract($this->settings['fields']); |
| 67 | + if (empty($data[$username]) || empty($data[$password])) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + $user = $this->_findUser($data[$username], $data[$password]); |
| 72 | + if ($user) { |
| 73 | + $this->_Collection->Session->write(AuthComponent::$sessionKey, $user); |
| 74 | + return $user; |
| 75 | + } |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + public function authenticate(CakeRequest $request, CakeResponse $response) { |
| 80 | + return $this->getUser($request); |
| 81 | + } |
| 82 | + |
| 83 | + public function logout($user) { |
| 84 | + $this->_Collection->Cookie->destroy(); |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments