Permalink
Browse files

Implement 'remeber me' login functionality

  • Loading branch information...
borivojevic committed Aug 8, 2013
1 parent 1d00558 commit e49de595d019530dd92d0e38ffd908b4d4de3815
View
@@ -188,7 +188,8 @@
*
*/
Configure::write('Session', array(
- 'defaults' => 'php'
+ 'defaults' => 'php',
+ 'cookieTimeout' => 0
));
/**
@@ -43,6 +43,18 @@ public function beforeFilter() {
$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
$this->Auth->authorize = array('Controller');
$this->Auth->redirectUrl(array('controller' => 'posts', 'action' => 'add'));
+ $this->Auth->authenticate = array(
+ 'Cookie' => array(
+ 'fields' => array(
+ 'username' => 'username',
+ 'password' => 'password'
+ ),
+ 'userModel' => 'User',
+ ),
+ 'Form'
+ );
+
+ $this->Cookie->type('rijndael');
$this->set('authUser', $this->Auth->user());
}
@@ -0,0 +1,83 @@
+<?php
+App::uses('BaseAuthenticate', 'Controller/Component/Auth');
+App::uses('AuthComponent', 'Controller/Component');
+App::uses('Router', 'Routing');
+
+/**
+ * An authentication adapter for AuthComponent. Provides the ability to authenticate using COOKIE
+ *
+ * {{{
+ * $this->Auth->authenticate = array(
+ * 'Authenticate.Cookie' => array(
+ * 'fields' => array(
+ * 'username' => 'username',
+ * 'password' => 'password'
+ * ),
+ * 'userModel' => 'User',
+ * 'scope' => array('User.active' => 1),
+ * 'crypt' => 'rijndael', // Defaults to rijndael(safest), optionally set to 'cipher' if required
+ * 'cookie' => array(
+ * 'name' => 'RememberMe',
+ * 'time' => '+2 weeks',
+ * )
+ * )
+ * )
+ * }}}
+ *
+ */
+class CookieAuthenticate extends BaseAuthenticate {
+
+ public function __construct(ComponentCollection $collection, $settings) {
+ $this->settings['cookie'] = array(
+ 'name' => 'RememberMe',
+ 'time' => '+2 weeks',
+ 'base' => Router::getRequest()->base
+ );
+ $this->settings['crypt'] = 'rijndael';
+ parent::__construct($collection, $settings);
+ }
+
+/**
+ * Authenticates the identity contained in the cookie. Will use the `settings.userModel`, and `settings.fields`
+ * to find COOKIE data that is used to find a matching record in the `settings.userModel`. Will return false if
+ * there is no cookie data, either username or password is missing, of if the scope conditions have not been met.
+ *
+ * @param CakeRequest $request The unused request object
+ * @return mixed False on login failure. An array of User data on success.
+ * @throws CakeException
+ */
+ public function getUser(CakeRequest $request) {
+ if (!isset($this->_Collection->Cookie) || !$this->_Collection->Cookie instanceof CookieComponent) {
+ throw new CakeException('CookieComponent is not loaded');
+ }
+
+ $this->_Collection->Cookie->type($this->settings['crypt']);
+ list(, $model) = pluginSplit($this->settings['userModel']);
+
+ $data = $this->_Collection->Cookie->read($model);
+ if (empty($data)) {
+ return false;
+ }
+
+ extract($this->settings['fields']);
+ if (empty($data[$username]) || empty($data[$password])) {
+ return false;
+ }
+
+ $user = $this->_findUser($data[$username], $data[$password]);
+ if ($user) {
+ $this->_Collection->Session->write(AuthComponent::$sessionKey, $user);
+ return $user;
+ }
+ return false;
+ }
+
+ public function authenticate(CakeRequest $request, CakeResponse $response) {
+ return $this->getUser($request);
+ }
+
+ public function logout($user) {
+ $this->_Collection->Cookie->destroy();
+ }
+
+}
@@ -15,11 +15,28 @@ public function beforeFilter() {
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
+ $this->_setCookie($this->Auth->user('id'));
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
+
+ if ($this->Auth->loggedIn() || $this->Auth->login()) {
+ return $this->redirect($this->Auth->redirectUrl());
+ }
+ }
+
+ protected function _setCookie($id) {
+ if (!$this->request->data('User.remember_me')) {
+ return false;
+ }
+ $data = array(
+ 'username' => $this->request->data('User.username'),
+ 'password' => $this->request->data('User.password')
+ );
+ $this->Cookie->write('User', $data, true, '+2 week');
+ return true;
}
public function logout() {
View
@@ -3,9 +3,11 @@
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Please enter your username and password'); ?></legend>
- <?php echo $this->Form->input('username');
- echo $this->Form->input('password');
- ?>
+ <?php
+ echo $this->Form->input('username');
+ echo $this->Form->input('password');
+ echo $this->Form->checkbox('remember_me');
+ ?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>

0 comments on commit e49de59

Please sign in to comment.