From b6c2cfe3d4786cd6fb8df13f25a6f88882fe772e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=E4mer?= Date: Fri, 5 Oct 2012 09:54:08 +0200 Subject: [PATCH 01/33] Fixing the instructions in the readme.md --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index a2f700b7e..2b10d9092 100644 --- a/readme.md +++ b/readme.md @@ -16,7 +16,7 @@ The plugin is pretty easy to set up, all you need to do is to copy it to you app or - ./Console/cake Migrations.migration all --plugin Users + ./Console/cake Migrations.migration run all --plugin Users You will also need the [CakeDC Search plugin](http://github.com/CakeDC/search), just grab it and put it into your application's plugin folder. From db6623a64a5b5a780bc23759bc7f07bd700cd81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=E4mer?= Date: Fri, 5 Oct 2012 09:54:20 +0200 Subject: [PATCH 02/33] Fixing a syntax error --- Controller/UsersAppController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Controller/UsersAppController.php b/Controller/UsersAppController.php index 2c90c3a38..c54f477d6 100644 --- a/Controller/UsersAppController.php +++ b/Controller/UsersAppController.php @@ -28,7 +28,7 @@ class UsersAppController extends AppController { * @return boolean True if allowed */ public function isAuthorized() { - return parent::isAuthorized; + return parent::isAuthorized(); } } From ec9343093e8c0e0e9dc912f77bd9cef7efd6be23 Mon Sep 17 00:00:00 2001 From: Callum Macdonald Date: Wed, 17 Oct 2012 15:45:56 +0300 Subject: [PATCH 03/33] Make the text Remember Me a label for the checkbox Using FormHelper::input() instead of FormHelper::checkbox() allows the text "Remember Me" to be supplied as a label, and so the text is clickable instead of just the checkbox. --- View/Users/login.ctp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/View/Users/login.ctp b/View/Users/login.ctp index 460e1255d..46c642502 100644 --- a/View/Users/login.ctp +++ b/View/Users/login.ctp @@ -21,7 +21,7 @@ echo $this->Form->input('password', array( 'label' => __d('users', 'Password'))); - echo '

' . __d('users', 'Remember Me') . $this->Form->checkbox('remember_me') . '

'; + echo '

' . $this->Form->input('remember_me', array('type' => 'checkbox', 'label' => __d('users', 'Remember Me'))) . '

'; echo '

' . $this->Html->link(__d('users', 'I forgot my password'), array('action' => 'reset_password')) . '

'; echo $this->Form->hidden('User.return_to', array( From b049129fc9af015b9dda0e57ba3eaee5fa4c4f12 Mon Sep 17 00:00:00 2001 From: Callum Macdonald Date: Wed, 17 Oct 2012 17:04:06 +0300 Subject: [PATCH 04/33] Remember me cookie name / key were swapped According to the readme the cookie name is Users and the key is rememberMe, rather than the other way round. --- Controller/UsersController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index b0a8269b6..1b6af2248 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -652,13 +652,13 @@ protected function _sendPasswordReset($admin = null, $options = array()) { * @return void * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html */ - protected function _setCookie($options = array(), $cookieKey = 'User') { + protected function _setCookie($options = array(), $cookieKey = 'rememberMe') { if (empty($this->request->data[$this->modelClass]['remember_me'])) { $this->Cookie->delete($cookieKey); } else { $validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time'); $defaults = array( - 'name' => 'rememberMe'); + 'name' => 'Users'); $options = array_merge($defaults, $options); foreach ($options as $key => $value) { From ed62e880229f1c8b648c13478cb1aa96d97520ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Wed, 17 Oct 2012 18:01:57 +0200 Subject: [PATCH 05/33] Correcting the rememberMe cookie instructions in the readme.md --- readme.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/readme.md b/readme.md index 2b10d9092..624cd3176 100644 --- a/readme.md +++ b/readme.md @@ -44,10 +44,10 @@ To use the "remember me" checkbox which sets a cookie on the login page you will public function restoreLoginFromCookie() { $this->Cookie->name = 'Users'; $cookie = $this->Cookie->read('rememberMe'); - if (!empty($cookie) && !$this->Auth->user()) { - $data['User'][$this->Auth->fields['username']] = $cookie[$this->Auth->fields['username']]; - $data['User'][$this->Auth->fields['password']] = $cookie[$this->Auth->fields['password']]; - $this->Auth->login($data); + if (!empty($cookie)) { + $this->request->data['User'][$this->Auth->fields['username']] = $cookie[$this->Auth->fields['username']]; + $this->request->data['User'][$this->Auth->fields['password']] = $cookie[$this->Auth->fields['password']]; + $this->Auth->login(); } } From 7d92ed26e5c1c98fd2370c51bf1ba23675929142 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Wed, 17 Oct 2012 18:03:12 +0200 Subject: [PATCH 06/33] Started refactoring the remember me feature --- Controller/Component/RememberMeComponent.php | 123 +++++++++++++++++++ Controller/UsersController.php | 30 ++--- 2 files changed, 132 insertions(+), 21 deletions(-) create mode 100644 Controller/Component/RememberMeComponent.php diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php new file mode 100644 index 000000000..9cae1d28b --- /dev/null +++ b/Controller/Component/RememberMeComponent.php @@ -0,0 +1,123 @@ + true, + 'userModel' => 'User', + 'cookieKey' => 'rememberMe', + 'cookieName' => 'Users', + 'fields' => array( + 'email', + 'username', + 'password', + ), + ); + +/** + * Constructor + * + * @param ComponentCollection $collection A ComponentCollection for this component + * @param array $settings Array of settings. + */ + public function __construct(ComponentCollection $collection, $settings = array()) { + parent::__construct($collection, $settings); + + $this->settings = Set::merge($this->_defaults, $settings); + $this->Controller = $collection->getController(); + $this->request = $this->Controller->request; + } + +/** + * + * + * @param Controller $controller + * @return void + */ + public function initialize(Controller $controller) { + if ($this->settings['autoLogin'] == true && !$this->Auth->loggedIn()) { + $this->restoreLoginFromCookie(); + } + } + +/** + * + */ + public function restoreLoginFromCookie() { + extract($this->settings); + + $this->Cookie->name = $cookieName; + $cookie = $this->Cookie->read($cookieKey); + + if (!empty($cookie)) { + foreach ($fields as $field) { + if (!empty($cookie[$field])) { + $this->request->data[$userModel][$field] = $cookie[$field]; + } + } + $this->Auth->login(); + } + } + +/** + * + */ + public function setCookie($options = array()) { + extract($this->settings); + + $validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time'); + $defaults = array( + 'name' => 'Users'); + + $options = array_merge($defaults, $options); + + foreach ($options as $key => $value) { + if (in_array($key, $validProperties)) { + $this->Cookie->{$key} = $value; + } + } + + $cookieData = array(); + foreach ($fields as $field) { + if (isset($this->request->data[$userModel][$field]) && !empty($this->request->data[$userModel][$field])) { + $cookieData[$field] = $this->request->data[$userModel][$field]; + } + } + + $this->Cookie->write($cookieKey, $cookieData, true, '1 Month'); + } + + public function destroyCookie() { + extract($this->settings); + $this->Cookie->name = $cookieName; + $this->Cookie->destroy(); + } + +} \ No newline at end of file diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 1b6af2248..2b9401d7b 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -22,6 +22,7 @@ * @property SecurityComponent $Security * @property SessionComponent $Session * @property User $User + * @property RememberMeComponent $RememberMe */ class UsersController extends UsersAppController { @@ -56,6 +57,7 @@ class UsersController extends UsersAppController { 'Paginator', 'Security', 'Search.Prg', + 'Users.RememberMe', ); /** @@ -358,7 +360,11 @@ public function login() { $this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged in'), $this->Auth->user('username'))); if (!empty($this->request->data)) { $data = $this->request->data[$this->modelClass]; - $this->_setCookie(); + if (empty($this->request->data[$this->modelClass]['remember_me'])) { + $this->RememberMe->destroyCookie(); + } else { + $this->_setCookie(); + } } if (empty($data['return_to'])) { @@ -431,6 +437,7 @@ public function logout() { $user = $this->Auth->user(); $this->Session->destroy(); $this->Cookie->destroy(); + $this->RememberMe->destroyCookie(); $this->Session->setFlash(sprintf(__d('users', '%s you have successfully logged out'), $user[$this->{$this->modelClass}->displayField])); $this->redirect($this->Auth->logout()); } @@ -653,26 +660,7 @@ protected function _sendPasswordReset($admin = null, $options = array()) { * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html */ protected function _setCookie($options = array(), $cookieKey = 'rememberMe') { - if (empty($this->request->data[$this->modelClass]['remember_me'])) { - $this->Cookie->delete($cookieKey); - } else { - $validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time'); - $defaults = array( - 'name' => 'Users'); - - $options = array_merge($defaults, $options); - foreach ($options as $key => $value) { - if (in_array($key, $validProperties)) { - $this->Cookie->{$key} = $value; - } - } - - $cookieData = array( - 'email' => $this->request->data[$this->modelClass]['email'], - 'password' => $this->request->data[$this->modelClass]['password']); - $this->Cookie->write($cookieKey, $cookieData, true, '1 Month'); - } - unset($this->request->data[$this->modelClass]['remember_me']); + $this->RememberMe->setCookie($options); } /** From b757d07c7defa8f2558a1534775c16868a06e4d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Thu, 18 Oct 2012 00:34:43 +0200 Subject: [PATCH 07/33] Working on making the remember me functionality a component --- Controller/Component/RememberMeComponent.php | 71 ++++++++++++------- Controller/UsersController.php | 7 +- .../Component/RememberMeComponentTest.php | 4 ++ 3 files changed, 56 insertions(+), 26 deletions(-) create mode 100644 Test/Case/Controller/Component/RememberMeComponentTest.php diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index 9cae1d28b..863a7dbab 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -14,7 +14,6 @@ class RememberMeComponent extends Component { * @var array */ public $components = array( - 'Auth', 'Cookie'); /** @@ -24,6 +23,13 @@ class RememberMeComponent extends Component { */ public $request; +/** + * Settings + * + * @var array + */ + public $settings = array(); + /** * Default settings * @@ -33,7 +39,8 @@ class RememberMeComponent extends Component { 'autoLogin' => true, 'userModel' => 'User', 'cookieKey' => 'rememberMe', - 'cookieName' => 'Users', + 'cookie' => array( + 'name' => 'Users'), 'fields' => array( 'email', 'username', @@ -49,10 +56,8 @@ class RememberMeComponent extends Component { */ public function __construct(ComponentCollection $collection, $settings = array()) { parent::__construct($collection, $settings); - $this->settings = Set::merge($this->_defaults, $settings); - $this->Controller = $collection->getController(); - $this->request = $this->Controller->request; + $this->configureCookie($this->settings['cookie']); } /** @@ -61,19 +66,24 @@ public function __construct(ComponentCollection $collection, $settings = array() * @param Controller $controller * @return void */ - public function initialize(Controller $controller) { + public function startup(Controller $controller) { + $this->Controller = $controller; + $this->request = $this->Controller->request; + $this->response = $this->Controller->response; + $this->Auth = $this->Controller->Auth; + if ($this->settings['autoLogin'] == true && !$this->Auth->loggedIn()) { $this->restoreLoginFromCookie(); } } /** + * Logs the user again in based on the cookie data * + * @return boolean True on login success, false on failure */ public function restoreLoginFromCookie() { extract($this->settings); - - $this->Cookie->name = $cookieName; $cookie = $this->Cookie->read($cookieKey); if (!empty($cookie)) { @@ -82,28 +92,19 @@ public function restoreLoginFromCookie() { $this->request->data[$userModel][$field] = $cookie[$field]; } } - $this->Auth->login(); + return $this->Auth->login(); } } /** + * Sets the cookie with the specified fields * + * @param options + * @return void */ - public function setCookie($options = array()) { + public function setCookie() { extract($this->settings); - $validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time'); - $defaults = array( - 'name' => 'Users'); - - $options = array_merge($defaults, $options); - - foreach ($options as $key => $value) { - if (in_array($key, $validProperties)) { - $this->Cookie->{$key} = $value; - } - } - $cookieData = array(); foreach ($fields as $field) { if (isset($this->request->data[$userModel][$field]) && !empty($this->request->data[$userModel][$field])) { @@ -111,13 +112,35 @@ public function setCookie($options = array()) { } } - $this->Cookie->write($cookieKey, $cookieData, true, '1 Month'); + $this->Cookie->write($cookieKey, $cookieData, true); } public function destroyCookie() { extract($this->settings); - $this->Cookie->name = $cookieName; $this->Cookie->destroy(); } +/** + * Configures the cookie component instance + * + * @param array $options + * @throws InvalidArgumentException Thrown if an invalid option key was passed + * @return void + */ + public function configureCookie($options = array()) { + $validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time'); + $defaults = array( + 'time' => '1 month', + 'name' => 'Users'); + + $options = array_merge($defaults, $options); + + foreach ($options as $key => $value) { + if (in_array($key, $validProperties)) { + $this->Cookie->{$key} = $value; + } else { + throw new InvalidArgumentException(__('users', 'Invalid options %s', $key)); + } + } + } } \ No newline at end of file diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 2b9401d7b..b1f243d8b 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -655,11 +655,14 @@ protected function _sendPasswordReset($admin = null, $options = array()) { * Sets the cookie to remember the user * * @param array Cookie component properties as array, like array('domain' => 'yourdomain.com') - * @param string Cookie data keyname for the userdata, its default is "User". This is set to User and NOT using the model alias to make sure it works with different apps with different user models across different (sub)domains. + * @param string $cookieKey * @return void * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html + * @deprecated Use the RememberMe Component */ - protected function _setCookie($options = array(), $cookieKey = 'rememberMe') { + protected function _setCookie($options = array(), $cookieKey) { + $this->RememberMe->settings['cookieKey'] = $cookieKey; + $this->RememberMe->configureCookie($options); $this->RememberMe->setCookie($options); } diff --git a/Test/Case/Controller/Component/RememberMeComponentTest.php b/Test/Case/Controller/Component/RememberMeComponentTest.php new file mode 100644 index 000000000..114a4fb6e --- /dev/null +++ b/Test/Case/Controller/Component/RememberMeComponentTest.php @@ -0,0 +1,4 @@ + Date: Thu, 18 Oct 2012 00:38:22 +0200 Subject: [PATCH 08/33] Updating the copyright headers --- Config/Migration/001_initialize_users_schema.php | 2 +- Config/Migration/002_renaming.php | 2 +- Config/Migration/map.php | 2 +- Config/Schema/schema.php | 2 +- Controller/Component/RememberMeComponent.php | 12 ++++++++++++ Controller/UserDetailsController.php | 4 ++-- Controller/UsersAppController.php | 4 ++-- Controller/UsersController.php | 4 ++-- Model/User.php | 4 ++-- Model/UserDetail.php | 4 ++-- Model/UsersAppModel.php | 4 ++-- Test/Case/Controller/UserDetailsControllerTest.php | 4 ++-- Test/Case/Controller/UsersControllerTest.php | 4 ++-- Test/Case/Model/UserDetailTest.php | 4 ++-- Test/Case/Model/UserTest.php | 4 ++-- Test/Fixture/UserDetailFixture.php | 4 ++-- Test/Fixture/UserFixture.php | 4 ++-- View/Elements/pagination.ctp | 4 ++-- View/Emails/text/account_verification.ctp | 4 ++-- View/Emails/text/new_password.ctp | 4 ++-- View/Emails/text/password_reset_request.ctp | 4 ++-- View/UserDetails/add.ctp | 4 ++-- View/UserDetails/admin_add.ctp | 4 ++-- View/UserDetails/admin_edit.ctp | 4 ++-- View/UserDetails/admin_index.ctp | 4 ++-- View/UserDetails/admin_view.ctp | 4 ++-- View/UserDetails/edit.ctp | 4 ++-- View/UserDetails/index.ctp | 4 ++-- View/UserDetails/view.ctp | 4 ++-- View/Users/add.ctp | 4 ++-- View/Users/admin_add.ctp | 4 ++-- View/Users/admin_edit.ctp | 4 ++-- View/Users/admin_index.ctp | 4 ++-- View/Users/admin_view.ctp | 4 ++-- View/Users/change_password.ctp | 4 ++-- View/Users/dashboard.ctp | 4 ++-- View/Users/edit.ctp | 4 ++-- View/Users/index.ctp | 4 ++-- View/Users/login.ctp | 4 ++-- View/Users/request_password_change.ctp | 4 ++-- View/Users/search.ctp | 4 ++-- View/Users/view.ctp | 4 ++-- 42 files changed, 90 insertions(+), 78 deletions(-) diff --git a/Config/Migration/001_initialize_users_schema.php b/Config/Migration/001_initialize_users_schema.php index 643d44a9d..612c7fbe0 100644 --- a/Config/Migration/001_initialize_users_schema.php +++ b/Config/Migration/001_initialize_users_schema.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2012, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Config/Migration/002_renaming.php b/Config/Migration/002_renaming.php index cd39be85e..a975b078c 100644 --- a/Config/Migration/002_renaming.php +++ b/Config/Migration/002_renaming.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2012, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Config/Migration/map.php b/Config/Migration/map.php index 4ebb86efd..2881b9c44 100644 --- a/Config/Migration/map.php +++ b/Config/Migration/map.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2012, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Config/Schema/schema.php b/Config/Schema/schema.php index 6736df080..a58c54e8f 100644 --- a/Config/Schema/schema.php +++ b/Config/Schema/schema.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2012, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index 863a7dbab..078c2068a 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -1,4 +1,16 @@ diff --git a/View/Emails/text/account_verification.ctp b/View/Emails/text/account_verification.ctp index 057d6d62e..15135681b 100644 --- a/View/Emails/text/account_verification.ctp +++ b/View/Emails/text/account_verification.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_add.ctp b/View/UserDetails/admin_add.ctp index cda182e8a..fc5577fee 100644 --- a/View/UserDetails/admin_add.ctp +++ b/View/UserDetails/admin_add.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_edit.ctp b/View/UserDetails/admin_edit.ctp index 29df7c009..dc83e7d26 100644 --- a/View/UserDetails/admin_edit.ctp +++ b/View/UserDetails/admin_edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_index.ctp b/View/UserDetails/admin_index.ctp index a4f04f30b..0434adf68 100644 --- a/View/UserDetails/admin_index.ctp +++ b/View/UserDetails/admin_index.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_view.ctp b/View/UserDetails/admin_view.ctp index f7c26d43f..35a900cc4 100644 --- a/View/UserDetails/admin_view.ctp +++ b/View/UserDetails/admin_view.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/edit.ctp b/View/UserDetails/edit.ctp index c9e9dea81..eb1559d89 100644 --- a/View/UserDetails/edit.ctp +++ b/View/UserDetails/edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/index.ctp b/View/UserDetails/index.ctp index f2ad92870..9c0c563a4 100644 --- a/View/UserDetails/index.ctp +++ b/View/UserDetails/index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/add.ctp b/View/Users/add.ctp index e85fbd86d..8a329ef9b 100644 --- a/View/Users/add.ctp +++ b/View/Users/add.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_add.ctp b/View/Users/admin_add.ctp index 37b523324..95d194160 100644 --- a/View/Users/admin_add.ctp +++ b/View/Users/admin_add.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_edit.ctp b/View/Users/admin_edit.ctp index 569466f91..9d5214b5f 100644 --- a/View/Users/admin_edit.ctp +++ b/View/Users/admin_edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_index.ctp b/View/Users/admin_index.ctp index 4039e6e75..b3fb92649 100644 --- a/View/Users/admin_index.ctp +++ b/View/Users/admin_index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_view.ctp b/View/Users/admin_view.ctp index 6c98bce43..629591fbe 100644 --- a/View/Users/admin_view.ctp +++ b/View/Users/admin_view.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/change_password.ctp b/View/Users/change_password.ctp index 187d7a796..08660ad39 100644 --- a/View/Users/change_password.ctp +++ b/View/Users/change_password.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/dashboard.ctp b/View/Users/dashboard.ctp index 29b1c2819..54c915137 100644 --- a/View/Users/dashboard.ctp +++ b/View/Users/dashboard.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/edit.ctp b/View/Users/edit.ctp index 912b4e355..134539fb4 100644 --- a/View/Users/edit.ctp +++ b/View/Users/edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/index.ctp b/View/Users/index.ctp index 84a00b8fd..58e7ff085 100644 --- a/View/Users/index.ctp +++ b/View/Users/index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/login.ctp b/View/Users/login.ctp index 46c642502..ef12db24b 100644 --- a/View/Users/login.ctp +++ b/View/Users/login.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/request_password_change.ctp b/View/Users/request_password_change.ctp index 54d989701..9b80201ca 100644 --- a/View/Users/request_password_change.ctp +++ b/View/Users/request_password_change.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/search.ctp b/View/Users/search.ctp index 019fc1714..11f7ffb69 100644 --- a/View/Users/search.ctp +++ b/View/Users/search.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/view.ctp b/View/Users/view.ctp index 4b1a98f18..991f1ed79 100644 --- a/View/Users/view.ctp +++ b/View/Users/view.ctp @@ -1,11 +1,11 @@ From e8e8b92efcf7e7045680d59e88c665718859d740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Thu, 18 Oct 2012 23:01:33 +0200 Subject: [PATCH 09/33] Adding tests for the new RememberMeComponent --- Controller/Component/RememberMeComponent.php | 26 +++-- .../Component/RememberMeComponentTest.php | 110 ++++++++++++++++++ 2 files changed, 127 insertions(+), 9 deletions(-) diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index 078c2068a..bf98d7c42 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -56,9 +56,7 @@ class RememberMeComponent extends Component { 'fields' => array( 'email', 'username', - 'password', - ), - ); + 'password')); /** * Constructor @@ -73,7 +71,7 @@ public function __construct(ComponentCollection $collection, $settings = array() } /** - * + * startup * * @param Controller $controller * @return void @@ -111,25 +109,35 @@ public function restoreLoginFromCookie() { /** * Sets the cookie with the specified fields * - * @param options + * @param array Optional, login credentials array in the form of Model.field, if empty this->request[''] will be used * @return void */ - public function setCookie() { + public function setCookie($data = array()) { extract($this->settings); + if (empty($data)) { + $data = $this->request->data; + } + $cookieData = array(); + foreach ($fields as $field) { - if (isset($this->request->data[$userModel][$field]) && !empty($this->request->data[$userModel][$field])) { - $cookieData[$field] = $this->request->data[$userModel][$field]; + if (isset($data[$userModel][$field]) && !empty($data[$userModel][$field])) { + $cookieData[$field] = $data[$userModel][$field]; } } $this->Cookie->write($cookieKey, $cookieData, true); } +/** + * Destroys the remember me cookie + * + * @return void + */ public function destroyCookie() { extract($this->settings); - $this->Cookie->destroy(); + $this->Cookie->destroy($cookie['name']); } /** diff --git a/Test/Case/Controller/Component/RememberMeComponentTest.php b/Test/Case/Controller/Component/RememberMeComponentTest.php index 114a4fb6e..b95c79058 100644 --- a/Test/Case/Controller/Component/RememberMeComponentTest.php +++ b/Test/Case/Controller/Component/RememberMeComponentTest.php @@ -1,4 +1,114 @@ Controller = new RememberMeComponentTestController(new CakeRequest(), new CakeResponse()); + $this->Controller->constructClasses(); + + $this->RememberMe = $this->Controller->RememberMe; + $this->RememberMe->Cookie = $this->getMock('CookieComponent', + array(), + array($this->Controller->Components)); + $this->RememberMe->Auth = $this->getMock('AuthComponent', + array(), + array($this->Controller->Components)); + } + +/** + * testSetCookie + * + * @return void + */ + public function testSetCookie() { + $this->RememberMe->Cookie->expects($this->once()) + ->method('write') + ->with('rememberMe', array( + 'email' => 'email', + 'password' => 'password'), true); + + $this->RememberMe->setCookie(array( + 'User' => array( + 'email' => 'email', + 'password' => 'password'))); + } + +/** + * testRestoreLoginFromCookie + * + * @return void + */ + public function testRestoreLoginFromCookie() { + $this->RememberMe->Cookie->expects($this->once()) + ->method('read') + ->with($this->equalTo('rememberMe')) + ->will($this->returnValue(array( + 'email' => 'email', + 'password' => 'password'))); + + $this->RememberMe->Auth->expects($this->once()) + ->method('login'); + + $this->RememberMe->restoreLoginFromCookie(); + + $this->assertEqual($this->RememberMe->request->data, array( + 'User' => array( + 'email' => 'email', + 'password' => 'password'))); + } + +/** + * testDestroyCookie + * + * @return void + */ + public function testDestroyCookie() { + $this->RememberMe->Cookie->expects($this->once()) + ->method('destroy') + ->with($this->equalTo('Users')); + $this->RememberMe->destroyCookie(); + } + } \ No newline at end of file From b44a4a72fcbc041bccdcf512ab00d72067062892 Mon Sep 17 00:00:00 2001 From: J Miller Date: Mon, 22 Oct 2012 00:59:42 -0700 Subject: [PATCH 10/33] Dispatch Users.afterLogin event Allows your application to listen for this event and perform actions after a user logs in. Passes the boolean isFirstLogin, so you can perform unique actions only on the first time a user logs in. http://book.cakephp.org/2.0/en/core-libraries/events.html --- Controller/UsersController.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 1b6af2248..083706f89 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -349,6 +349,10 @@ public function add() { public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { + $this->getEventManager()->dispatch(new CakeEvent('Users.afterLogin', $this, array( + 'isFirstLogin' => !$this->Auth->user('last_login') + ))); + $this->User->id = $this->Auth->user('id'); $this->User->saveField('last_login', date('Y-m-d H:i:s')); From 2be3d890348d80410ed91e9540ae417d97ea0ecb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 22 Oct 2012 23:42:50 +0200 Subject: [PATCH 11/33] Fixing coding standards, commas vs tabs --- Test/Case/Controller/UsersControllerTest.php | 178 +++++++++---------- 1 file changed, 89 insertions(+), 89 deletions(-) diff --git a/Test/Case/Controller/UsersControllerTest.php b/Test/Case/Controller/UsersControllerTest.php index 293f05fac..6434a93a8 100644 --- a/Test/Case/Controller/UsersControllerTest.php +++ b/Test/Case/Controller/UsersControllerTest.php @@ -64,7 +64,7 @@ public function beforeFilter() { * Public interface to _setCookie */ public function setCookie($options = array()) { - parent::_setCookie($options); + parent::_setCookie($options); } /** @@ -205,20 +205,20 @@ public function startTest() { $this->Users->CakeEmail = $this->getMock('CakeEmail'); $this->Users->CakeEmail->expects($this->any()) - ->method('to') - ->will($this->returnSelf()); + ->method('to') + ->will($this->returnSelf()); $this->Users->CakeEmail->expects($this->any()) - ->method('from') - ->will($this->returnSelf()); + ->method('from') + ->will($this->returnSelf()); $this->Users->CakeEmail->expects($this->any()) - ->method('subject') - ->will($this->returnSelf()); + ->method('subject') + ->will($this->returnSelf()); $this->Users->CakeEmail->expects($this->any()) - ->method('template') - ->will($this->returnSelf()); + ->method('template') + ->will($this->returnSelf()); $this->Users->CakeEmail->expects($this->any()) - ->method('viewVars') - ->will($this->returnSelf()); + ->method('viewVars') + ->will($this->returnSelf()); $this->Users->Components->disable('Security'); } @@ -243,28 +243,28 @@ public function testUserLogin() { $this->Users->request->url = '/users/users/login'; $this->Collection = $this->getMock('ComponentCollection'); - $this->Users->Auth = $this->getMock('AuthComponent', array('login', 'user', 'redirect'), array($this->Collection)); - $this->Users->Auth->expects($this->once()) - ->method('login') - ->will($this->returnValue(true)); - $this->Users->Auth->staticExpects($this->at(0)) - ->method('user') - ->with('id') - ->will($this->returnValue(1)); - $this->Users->Auth->staticExpects($this->at(1)) - ->method('user') - ->with('username') - ->will($this->returnValue('adminuser')); - $this->Users->Auth->expects($this->once()) - ->method('redirect') - ->with(null) - ->will($this->returnValue(Router::normalize('/'))); - $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); + $this->Users->Auth = $this->getMock('AuthComponent', array('login', 'user', 'redirect'), array($this->Collection)); + $this->Users->Auth->expects($this->once()) + ->method('login') + ->will($this->returnValue(true)); + $this->Users->Auth->staticExpects($this->at(0)) + ->method('user') + ->with('id') + ->will($this->returnValue(1)); + $this->Users->Auth->staticExpects($this->at(1)) + ->method('user') + ->with('username') + ->will($this->returnValue('adminuser')); + $this->Users->Auth->expects($this->once()) + ->method('redirect') + ->with(null) + ->will($this->returnValue(Router::normalize('/'))); + $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); $this->Users->Session->expects($this->any()) ->method('setFlash') ->with(__d('users', 'adminuser you have successfully logged in')); $this->Users->login(); - $this->assertEqual(Router::normalize($this->Users->redirectUrl), Router::normalize(Router::url($this->Users->Auth->loginRedirect))); + $this->assertEqual(Router::normalize($this->Users->redirectUrl), Router::normalize(Router::url($this->Users->Auth->loginRedirect))); } /** @@ -277,10 +277,10 @@ public function testUserLoginGet() { $this->__setGet(); $this->Users->login(); - $this->Collection = $this->getMock('ComponentCollection'); - $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); - $this->Users->Session->expects($this->never()) - ->method('setFlash'); + $this->Collection = $this->getMock('ComponentCollection'); + $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); + $this->Users->Session->expects($this->never()) + ->method('setFlash'); } /** @@ -292,15 +292,15 @@ public function testFailedUserLogin() { $this->Users->request->params['action'] = 'login'; $this->__setPost(array('User' => $this->usersData['invalidUser'])); - $this->Collection = $this->getMock('ComponentCollection'); - $this->Users->Auth = $this->getMock('AuthComponent', array('flash', 'login'), array($this->Collection)); - $this->Users->Auth->expects($this->once()) - ->method('login') - ->will($this->returnValue(false)); - $this->Users->Auth->expects($this->once()) - ->method('flash') - ->with(__d('users', 'Invalid e-mail / password combination. Please try again')); - $this->Users->login(); + $this->Collection = $this->getMock('ComponentCollection'); + $this->Users->Auth = $this->getMock('AuthComponent', array('flash', 'login'), array($this->Collection)); + $this->Users->Auth->expects($this->once()) + ->method('login') + ->will($this->returnValue(false)); + $this->Users->Auth->expects($this->once()) + ->method('flash') + ->with(__d('users', 'Invalid e-mail / password combination. Please try again')); + $this->Users->login(); } /** @@ -321,13 +321,13 @@ public function testAdd() { 'temppassword' => 'password', 'tos' => 1))); $this->Users->beforeFilter(); - $this->Collection = $this->getMock('ComponentCollection'); - $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); - $this->Users->Session->expects($this->once()) - ->method('setFlash') - ->with(__d('users', 'Your account has been created. You should receive an e-mail shortly to authenticate your account. Once validated you will be able to login.')); + $this->Collection = $this->getMock('ComponentCollection'); + $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); + $this->Users->Session->expects($this->once()) + ->method('setFlash') + ->with(__d('users', 'Your account has been created. You should receive an e-mail shortly to authenticate your account. Once validated you will be able to login.')); - $this->Users->add(); + $this->Users->add(); $this->__setPost(array( 'User' => array( @@ -337,11 +337,11 @@ public function testAdd() { 'temppassword' => '', 'tos' => 0))); $this->Users->beforeFilter(); - $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); - $this->Users->Session->expects($this->once()) - ->method('setFlash') - ->with(__d('users', 'Your account could not be created. Please, try again.')); - $this->Users->add(); + $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); + $this->Users->Session->expects($this->once()) + ->method('setFlash') + ->with(__d('users', 'Your account could not be created. Please, try again.')); + $this->Users->add(); } /** @@ -353,21 +353,21 @@ public function testVerify() { $this->Users->beforeFilter(); $this->Users->User->id = '37ea303a-3bdc-4251-b315-1316c0b300fa'; $this->Users->User->saveField('email_token_expires', date('Y-m-d H:i:s', strtotime('+1 year'))); - $this->Collection = $this->getMock('ComponentCollection'); - $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); - $this->Users->Session->expects($this->once()) - ->method('setFlash') - ->with(__d('users', 'Your e-mail has been validated!')); + $this->Collection = $this->getMock('ComponentCollection'); + $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); + $this->Users->Session->expects($this->once()) + ->method('setFlash') + ->with(__d('users', 'Your e-mail has been validated!')); - $this->Users->verify('email', 'testtoken2'); + $this->Users->verify('email', 'testtoken2'); $this->Users->beforeFilter(); - $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); - $this->Users->Session->expects($this->once()) - ->method('setFlash') - ->with(__d('users', 'Invalid token, please check the email you were sent, and retry the verification link.')); + $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); + $this->Users->Session->expects($this->once()) + ->method('setFlash') + ->with(__d('users', 'Invalid token, please check the email you were sent, and retry the verification link.')); - $this->Users->verify('email', 'invalid-token');; + $this->Users->verify('email', 'invalid-token');; } /** @@ -378,21 +378,21 @@ public function testVerify() { public function testLogout() { $this->Users->beforeFilter(); $this->Collection = $this->getMock('ComponentCollection'); - $this->Users->Cookie = $this->getMock('CookieComponent', array('destroy'), array($this->Collection)); - $this->Users->Cookie->expects($this->once()) - ->method('destroy'); - $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); - $this->Users->Session->expects($this->once()) - ->method('setFlash') - ->with(__d('users', 'testuser you have successfully logged out')); - $this->Users->Auth = $this->getMock('AuthComponent', array('logout', 'user'), array($this->Collection)); - $this->Users->Auth->expects($this->once()) - ->method('logout') - ->will($this->returnValue('/')); - $this->Users->Auth->staticExpects($this->at(0)) - ->method('user') - ->will($this->returnValue($this->usersData['validUser'])); - $this->Users->logout(); + $this->Users->Cookie = $this->getMock('CookieComponent', array('destroy'), array($this->Collection)); + $this->Users->Cookie->expects($this->once()) + ->method('destroy'); + $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); + $this->Users->Session->expects($this->once()) + ->method('setFlash') + ->with(__d('users', 'testuser you have successfully logged out')); + $this->Users->Auth = $this->getMock('AuthComponent', array('logout', 'user'), array($this->Collection)); + $this->Users->Auth->expects($this->once()) + ->method('logout') + ->will($this->returnValue('/')); + $this->Users->Auth->staticExpects($this->at(0)) + ->method('user') + ->will($this->returnValue($this->usersData['validUser'])); + $this->Users->logout(); $this->assertEqual($this->Users->redirectUrl, '/'); } @@ -537,21 +537,21 @@ public function testAdminDelete() { * @return void */ public function testSetCookie() { - $this->__setPost(array( - 'User' => array( - 'remember_me' => 1, - 'email' => 'testuser@cakedc.com', - 'username' => 'test', - 'password' => 'testtest') - )); + $this->__setPost(array( + 'User' => array( + 'remember_me' => 1, + 'email' => 'testuser@cakedc.com', + 'username' => 'test', + 'password' => 'testtest') + )); $this->Users->setCookie(array( 'name' => 'userTestCookie')); $this->Users->Cookie->name = 'userTestCookie'; $result = $this->Users->Cookie->read('User'); - $this->assertEqual($result, array( + $this->assertEqual($result, array( 'password' => 'testtest', - 'email' => 'testuser@cakedc.com', - )); + 'email' => 'testuser@cakedc.com', + )); } /** From 9db9da6c8bb9c38c8f4aa3ac825c9120e10a99f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 23 Oct 2012 00:07:40 +0200 Subject: [PATCH 12/33] Working on getting the UsersControllerTest to work with the RememberMeComponent changes --- Controller/Component/RememberMeComponent.php | 20 +++++++++------ Controller/UsersController.php | 8 +++--- .../Component/RememberMeComponentTest.php | 6 ++--- Test/Case/Controller/UsersControllerTest.php | 25 +++++++++++++------ 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index bf98d7c42..524c99c53 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -52,7 +52,7 @@ class RememberMeComponent extends Component { 'userModel' => 'User', 'cookieKey' => 'rememberMe', 'cookie' => array( - 'name' => 'Users'), + 'name' => 'User'), 'fields' => array( 'email', 'username', @@ -70,6 +70,17 @@ public function __construct(ComponentCollection $collection, $settings = array() $this->configureCookie($this->settings['cookie']); } +/** + * Initializes RememberMeComponent for use in the controller + * + * @param Controller $controller A reference to the instantiating controller object + * @return void + */ + public function initialize(Controller $controller) { + $this->request = $controller->request; + $this->Auth = $this->Controller->Auth; + } + /** * startup * @@ -77,11 +88,6 @@ public function __construct(ComponentCollection $collection, $settings = array() * @return void */ public function startup(Controller $controller) { - $this->Controller = $controller; - $this->request = $this->Controller->request; - $this->response = $this->Controller->response; - $this->Auth = $this->Controller->Auth; - if ($this->settings['autoLogin'] == true && !$this->Auth->loggedIn()) { $this->restoreLoginFromCookie(); } @@ -151,7 +157,7 @@ public function configureCookie($options = array()) { $validProperties = array('domain', 'key', 'name', 'path', 'secure', 'time'); $defaults = array( 'time' => '1 month', - 'name' => 'Users'); + 'name' => 'User'); $options = array_merge($defaults, $options); diff --git a/Controller/UsersController.php b/Controller/UsersController.php index f44246d8f..f5e254d22 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -654,16 +654,16 @@ protected function _sendPasswordReset($admin = null, $options = array()) { /** * Sets the cookie to remember the user * - * @param array Cookie component properties as array, like array('domain' => 'yourdomain.com') - * @param string $cookieKey + * @param array RememberMe (Cookie) component properties as array, like array('domain' => 'yourdomain.com') + * @param string Cookie data keyname for the userdata, its default is "User". This is set to User and NOT using the model alias to make sure it works with different apps with different user models across different (sub)domains. * @return void * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html * @deprecated Use the RememberMe Component */ - protected function _setCookie($options = array(), $cookieKey) { + protected function _setCookie($options = array(), $cookieKey = 'User') { $this->RememberMe->settings['cookieKey'] = $cookieKey; $this->RememberMe->configureCookie($options); - $this->RememberMe->setCookie($options); + $this->RememberMe->setCookie(); } /** diff --git a/Test/Case/Controller/Component/RememberMeComponentTest.php b/Test/Case/Controller/Component/RememberMeComponentTest.php index b95c79058..73ef1176f 100644 --- a/Test/Case/Controller/Component/RememberMeComponentTest.php +++ b/Test/Case/Controller/Component/RememberMeComponentTest.php @@ -66,8 +66,8 @@ public function testSetCookie() { $this->RememberMe->Cookie->expects($this->once()) ->method('write') ->with('rememberMe', array( - 'email' => 'email', - 'password' => 'password'), true); + 'email' => 'email', + 'password' => 'password'), true); $this->RememberMe->setCookie(array( 'User' => array( @@ -107,7 +107,7 @@ public function testRestoreLoginFromCookie() { public function testDestroyCookie() { $this->RememberMe->Cookie->expects($this->once()) ->method('destroy') - ->with($this->equalTo('Users')); + ->with($this->equalTo('User')); $this->RememberMe->destroyCookie(); } diff --git a/Test/Case/Controller/UsersControllerTest.php b/Test/Case/Controller/UsersControllerTest.php index 6434a93a8..36bdf7812 100644 --- a/Test/Case/Controller/UsersControllerTest.php +++ b/Test/Case/Controller/UsersControllerTest.php @@ -14,6 +14,7 @@ App::uses('AuthComponent', 'Controller/Component'); App::uses('CookieComponent', 'Controller/Component'); App::uses('SessionComponent', 'Controller/Component'); +App::uses('RememberMeComponent', 'Users.Controller/Component'); App::uses('Security', 'Utility'); app::uses('CakeEmail', 'Network/Email'); @@ -263,6 +264,10 @@ public function testUserLogin() { $this->Users->Session->expects($this->any()) ->method('setFlash') ->with(__d('users', 'adminuser you have successfully logged in')); + $this->Users->RememberMe = $this->getMock('RememberMeComponent', array(), array($this->Collection)); + $this->Users->RememberMe->expects($this->any()) + ->method('destroyCookie'); + $this->Users->login(); $this->assertEqual(Router::normalize($this->Users->redirectUrl), Router::normalize(Router::url($this->Users->Auth->loginRedirect))); } @@ -392,6 +397,10 @@ public function testLogout() { $this->Users->Auth->staticExpects($this->at(0)) ->method('user') ->will($this->returnValue($this->usersData['validUser'])); + $this->Users->RememberMe = $this->getMock('RememberMeComponent', array(), array($this->Collection)); + $this->Users->RememberMe->expects($this->any()) + ->method('destroyCookie'); + $this->Users->logout(); $this->assertEqual($this->Users->redirectUrl, '/'); } @@ -542,18 +551,19 @@ public function testSetCookie() { 'remember_me' => 1, 'email' => 'testuser@cakedc.com', 'username' => 'test', - 'password' => 'testtest') - )); + 'password' => 'testtest'))); + $this->Users->setCookie(array( 'name' => 'userTestCookie')); - $this->Users->Cookie->name = 'userTestCookie'; - $result = $this->Users->Cookie->read('User'); + + $this->Users->RememberMe->Cookie->name = 'userTestCookie'; + $result = $this->Users->RememberMe->Cookie->read('User'); + $this->assertEqual($result, array( 'password' => 'testtest', - 'email' => 'testuser@cakedc.com', - )); + 'email' => 'testuser@cakedc.com')); } - + /** * Test getting default and setted email instance config * @@ -570,7 +580,6 @@ public function testGetMailInstance() { $this->setExpectedException('ConfigureException'); Configure::write('Users.emailConfig', 'doesnotexist'); $anotherConfig = $this->Users->getMailInstance()->config(); - } /** From 10b3788419654685e22beddbd87c3dcb7f866b1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 23 Oct 2012 00:32:13 +0200 Subject: [PATCH 13/33] Fixing the UsersControllerTest::testSetCookie case to reflect and test the changes related to the cookie component --- Test/Case/Controller/UsersControllerTest.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Test/Case/Controller/UsersControllerTest.php b/Test/Case/Controller/UsersControllerTest.php index 36bdf7812..6e2954239 100644 --- a/Test/Case/Controller/UsersControllerTest.php +++ b/Test/Case/Controller/UsersControllerTest.php @@ -553,15 +553,18 @@ public function testSetCookie() { 'username' => 'test', 'password' => 'testtest'))); + $this->Collection = $this->getMock('ComponentCollection'); + $this->Users->RememberMe = $this->getMock('RememberMeComponent', array(), array($this->Collection)); + $this->Users->RememberMe->expects($this->once()) + ->method('configureCookie') + ->with(array('name' => 'userTestCookie')); + $this->Users->RememberMe->expects($this->once()) + ->method('setCookie'); + $this->Users->setCookie(array( 'name' => 'userTestCookie')); - $this->Users->RememberMe->Cookie->name = 'userTestCookie'; - $result = $this->Users->RememberMe->Cookie->read('User'); - - $this->assertEqual($result, array( - 'password' => 'testtest', - 'email' => 'testuser@cakedc.com')); + $this->assertEqual($this->Users->RememberMe->settings['cookieKey'], 'User'); } /** From abc6bd8452d924ab13f48728755c0213fddd0bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Wed, 28 Nov 2012 10:57:55 +0100 Subject: [PATCH 14/33] Changing some code back to work with 2.x versions before 2.2. https://github.com/CakeDC/users/issues/87 --- Controller/UsersController.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 28b74e556..5d6a72cf3 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -220,8 +220,8 @@ public function edit() { */ public function admin_index() { $this->Prg->commonProcess(); - $this->User->validator()->remove('username'); - $this->User->validator()->remove('email'); + unset($this->User->validate['username']); + unset($this->User->validate['email']); $this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs; if ($this->{$this->modelClass}->Behaviors->attached('Searchable')) { $parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs); From 1c8fcf022011eb88c918b9e6755cd076f2516147 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Thu, 29 Nov 2012 12:08:17 +0100 Subject: [PATCH 15/33] More fixes related to the RememberMeComponent --- Controller/Component/RememberMeComponent.php | 16 ++++++++++++---- Controller/UsersController.php | 5 ++--- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index 524c99c53..94c2bade2 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -26,7 +26,8 @@ class RememberMeComponent extends Component { * @var array */ public $components = array( - 'Cookie'); + 'Cookie', + 'Auth'); /** * Request object @@ -78,7 +79,6 @@ public function __construct(ComponentCollection $collection, $settings = array() */ public function initialize(Controller $controller) { $this->request = $controller->request; - $this->Auth = $this->Controller->Auth; } /** @@ -100,6 +100,7 @@ public function startup(Controller $controller) { */ public function restoreLoginFromCookie() { extract($this->settings); + $cookie = $this->Cookie->read($cookieKey); if (!empty($cookie)) { @@ -116,13 +117,20 @@ public function restoreLoginFromCookie() { * Sets the cookie with the specified fields * * @param array Optional, login credentials array in the form of Model.field, if empty this->request[''] will be used - * @return void + * @return boolean */ public function setCookie($data = array()) { extract($this->settings); if (empty($data)) { $data = $this->request->data; + if (empty($data)) { + $data = $this->Auth->user(); + } + } + + if (empty($data)) { + return false; } $cookieData = array(); @@ -133,7 +141,7 @@ public function setCookie($data = array()) { } } - $this->Cookie->write($cookieKey, $cookieData, true); + return $this->Cookie->write($cookieKey, $cookieData, true, '+99 years'); } /** diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 5d6a72cf3..b07f71d1e 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -57,8 +57,7 @@ class UsersController extends UsersAppController { 'Paginator', 'Security', 'Search.Prg', - 'Users.RememberMe', - ); + 'Users.RememberMe'); /** * Preset vars @@ -664,7 +663,7 @@ protected function _sendPasswordReset($admin = null, $options = array()) { * @link http://book.cakephp.org/2.0/en/core-libraries/components/cookie.html * @deprecated Use the RememberMe Component */ - protected function _setCookie($options = array(), $cookieKey = 'User') { + protected function _setCookie($options = array(), $cookieKey = 'rememberMe') { $this->RememberMe->settings['cookieKey'] = $cookieKey; $this->RememberMe->configureCookie($options); $this->RememberMe->setCookie(); From 824547dc40077ae559c0366670d3f42586deb14d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Fri, 30 Nov 2012 00:53:02 +0100 Subject: [PATCH 16/33] Updating the readme.md with the new infos about how to use the "remember me" feature --- readme.md | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/readme.md b/readme.md index 624cd3176..d787be12c 100644 --- a/readme.md +++ b/readme.md @@ -37,21 +37,31 @@ The plugin itself is already capable of: The default password reset process requires the user to enter his email address, an email is sent to the user with a link and a token. When the user accesses the URL with the token he can enter a new password. -### Using the "remember me" cookie ### +### Using the "remember me" functionality ### -To use the "remember me" checkbox which sets a cookie on the login page you will need to put this code or method call in your AppController::beforeFilter() method. +To use the "remember me" checkbox which sets a cookie on the login page you will need to add the RememberMe component to the AppController or the controllers you want to auto-login the user again based on the cookie. - public function restoreLoginFromCookie() { - $this->Cookie->name = 'Users'; - $cookie = $this->Cookie->read('rememberMe'); - if (!empty($cookie)) { - $this->request->data['User'][$this->Auth->fields['username']] = $cookie[$this->Auth->fields['username']]; - $this->request->data['User'][$this->Auth->fields['password']] = $cookie[$this->Auth->fields['password']]; - $this->Auth->login(); - } + public $components = array( + 'Users.RemembeMe'); + +If you are using another user model than 'User' you'll have to configure it: + + public $components = array( + 'Users.RemembeMe' => array( + 'userModel' => 'AppUser'); + +And add this line + + $this->RememberMe->restoreLoginFromCookie() + +to your controllers beforeFilter() callack + + public function beforeFilter() { + parent::beforeFilter(); + $this->RememberMe->restoreLoginFromCookie(); } -The code will read the login credentials from the cookie and log the user in based on that information. Do not forget to change the cookie name or fields to what you are using if you have changed them in your application! +The code will read the login credentials from the cookie and log the user in based on that information. Note that you have to use CakePHPs AuthComponent or an aliased Component implementing the same interface as AuthComponent. ## How to extend the plugin ## From ec1979dbf6d25e195b047b27d437e200e477feff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 2 Dec 2012 23:58:32 +0100 Subject: [PATCH 17/33] Changing RememberMe component to not always read the cookie --- Controller/Component/RememberMeComponent.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index 94c2bade2..5fb56f046 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -96,11 +96,15 @@ public function startup(Controller $controller) { /** * Logs the user again in based on the cookie data * + * @param boolean $checkLoginStatus * @return boolean True on login success, false on failure */ - public function restoreLoginFromCookie() { - extract($this->settings); + public function restoreLoginFromCookie($checkLoginStatus = true) { + if ($checkLoginStatus && $this->Auth->loggedIn()) { + return true; + } + extract($this->settings); $cookie = $this->Cookie->read($cookieKey); if (!empty($cookie)) { @@ -109,7 +113,10 @@ public function restoreLoginFromCookie() { $this->request->data[$userModel][$field] = $cookie[$field]; } } - return $this->Auth->login(); + + $result = $this->Auth->login(); + unset($this->request->data[$userModel]); + return $result; } } From e3d8a400d43152a16a696c27b8e1e2d64b5e58b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 4 Dec 2012 21:44:45 +0100 Subject: [PATCH 18/33] Fixing coding standards --- Controller/Component/RememberMeComponent.php | 1 + Controller/UserDetailsController.php | 12 +-- Controller/UsersController.php | 19 +++-- Model/User.php | 83 +++++++++++--------- Model/UserDetail.php | 15 ++-- 5 files changed, 69 insertions(+), 61 deletions(-) diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index 5fb56f046..ec12b940b 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -20,6 +20,7 @@ * @property AuthComponent $Auth */ class RememberMeComponent extends Component { + /** * Components * diff --git a/Controller/UserDetailsController.php b/Controller/UserDetailsController.php index 1881fa1a4..93cb0fe07 100644 --- a/Controller/UserDetailsController.php +++ b/Controller/UserDetailsController.php @@ -39,13 +39,13 @@ class UserDetailsController extends UsersAppController { * @return void */ public function index() { - $user_details = $this->UserDetail->find('all', array( + $userDetails = $this->UserDetail->find('all', array( 'contain' => array(), 'conditions' => array( 'UserDetail.user_id' => $this->Auth->user('id'), 'UserDetail.field LIKE' => 'user.%'), 'order' => 'UserDetail.position DESC')); - $this->set('user_details', $user_details); + $this->set('user_details', $userDetails); } /** @@ -70,8 +70,8 @@ public function view($id = null) { public function add() { if (!empty($this->request->data)) { $userId = $this->Auth->user('id'); - foreach($this->request->data as $group => $options) { - foreach($options as $key => $value) { + foreach ($this->request->data as $group => $options) { + foreach ($options as $key => $value) { $field = $group . '.' . $key; $this->UserDetail->updateAll( array('Detail.value' => "'$value'"), @@ -102,8 +102,8 @@ public function edit($section = 'user') { } if (empty($this->request->data)) { - $detail = $this->UserDetail->getSection($this->Auth->user('id'), $section); - $this->request->data['UserDetail'] = $detail[$section]; + $detail = $this->UserDetail->getSection($this->Auth->user('id'), $section); + $this->request->data['UserDetail'] = $detail[$section]; } $this->set('section', $section); diff --git a/Controller/UsersController.php b/Controller/UsersController.php index b07f71d1e..f5ea57e42 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -88,7 +88,7 @@ public function __construct($request, $response) { * * @return void * @link https://github.com/CakeDC/search - */ + */ protected function _setupComponents() { if (App::import('Component', 'Search.Prg')) { $this->components[] = 'Search.Prg'; @@ -99,7 +99,7 @@ protected function _setupComponents() { * Setup helpers based on plugin availability * * @return void - */ + */ protected function _setupHelpers() { if (App::import('Helper', 'Goodies.Gravatar')) { $this->helpers[] = 'Goodies.Gravatar'; @@ -141,7 +141,7 @@ protected function _setupAuth() { 'fields' => array( 'username' => 'email', 'password' => 'password'), - 'userModel' => 'Users.User', + 'userModel' => 'Users.User', 'scope' => array( 'User.active' => 1, 'User.email_verified' => 1))); @@ -160,7 +160,7 @@ public function index() { $this->paginate = array( 'limit' => 12, 'conditions' => array( - $this->modelClass . '.active' => 1, + $this->modelClass . '.active' => 1, $this->modelClass . '.email_verified' => 1)); $this->set('users', $this->paginate($this->modelClass)); } @@ -205,7 +205,7 @@ public function edit() { } } else { $data = $this->User->UserDetail->getSection($this->Auth->user('id'), 'User'); - if (!isset($data['User'])){ + if (!isset($data['User'])) { $data['User'] = array(); } $this->request->data['UserDetail'] = $data['User']; @@ -391,6 +391,7 @@ public function login() { /** * Search - Requires the CakeDC Search plugin to work * + * @throws MissingPluginException * @return void * @link https://github.com/CakeDC/search */ @@ -472,6 +473,7 @@ public function verify($type = 'email', $token = null) { * This method will send a new password to the user * * @param string $token Token + * @throws NotFoundException * @return void */ public function request_new_password($token = null) { @@ -561,7 +563,8 @@ public function reset_password($token = null, $user = null) { * Sets a list of languages to the view which can be used in selects * * @deprecated No fallback provided, use the Utils plugin in your app directly - * @param string View variable name, default is languages + * @param string $viewVar View variable name, default is languages + * @throws MissingPluginException * @return void * @link https://github.com/CakeDC/utils */ @@ -589,7 +592,7 @@ protected function _sendVerificationEmail($userData, $options = array()) { 'from' => Configure::read('App.defaultEmail'), 'subject' => __d('users', 'Account verification'), 'template' => 'Users.account_verification', - 'layout'=> 'default'); + 'layout' => 'default'); $options = array_merge($defaults, $options); @@ -617,7 +620,7 @@ protected function _sendPasswordReset($admin = null, $options = array()) { 'from' => Configure::read('App.defaultEmail'), 'subject' => __d('users', 'Password Reset'), 'template' => 'Users.password_reset_request', - 'layout'=> 'default'); + 'layout' => 'default'); $options = array_merge($defaults, $options); diff --git a/Model/User.php b/Model/User.php index a255e2a27..1e2f13447 100644 --- a/Model/User.php +++ b/Model/User.php @@ -83,7 +83,7 @@ class User extends UsersAppModel { 'rule' => array('alphaNumeric'), 'message' => 'The username must be alphanumeric.'), 'unique_username' => array( - 'rule'=>array('isUnique', 'username'), + 'rule' => array('isUnique', 'username'), 'message' => 'This username is already in use.'), 'username_min' => array( 'rule' => array('minLength', '3'), @@ -246,7 +246,7 @@ public function afterFind($results, $primary = false) { * @param string $string String to hash * @param string $type Method to use (sha1/sha256/md5) * @param boolean $salt If true, automatically appends the application's salt - * value to $string (Security.salt) + * value to $string (Security.salt) * @return string Hash */ public function hash($string, $type = null, $salt = false) { @@ -287,6 +287,7 @@ public function confirmEmail($email = null) { * Verifies a users email by a token that was sent to him via email and flags the user record as active * * @param string $token The token that wa sent to the user + * @throws RuntimeException * @return array On success it returns the user data record */ public function verifyEmail($token = null) { @@ -402,7 +403,7 @@ public function passwordReset($postData = array()) { $user = $this->save($user, false); $this->data = $user; return $user; - } elseif (!empty($user) && $user[$this->alias]['email_verified'] == 0){ + } elseif (!empty($user) && $user[$this->alias]['email_verified'] == 0) { $this->invalidate('email', __d('users', 'This Email Address exists but was never validated.')); } else { $this->invalidate('email', __d('users', 'This Email Address does not exist in the system.')); @@ -444,7 +445,7 @@ public function resetPassword($postData = array()) { 'new_password' => $tmp['password'], 'confirm_password' => array( 'required' => array( - 'rule' => array('compareFields', 'new_password', 'confirm_password'), + 'rule' => array('compareFields', 'new_password', 'confirm_password'), 'message' => __d('users', 'The passwords are not equal.')))); $this->set($postData); @@ -483,7 +484,8 @@ public function changePassword($postData = array()) { /** * Validation method to check the old password * - * @param array $password + * @param array $password + * @throws OutOfBoundsException * @return boolean True on success */ public function validateOldPassword($password) { @@ -508,7 +510,8 @@ public function compareFields($field1, $field2) { if (is_array($field1)) { $field1 = key($field1); } - if (isset($this->data[$this->alias][$field1]) && isset($this->data[$this->alias][$field2]) && + + if (isset($this->data[$this->alias][$field1]) && isset($this->data[$this->alias][$field2]) && $this->data[$this->alias][$field1] == $this->data[$this->alias][$field2]) { return true; } @@ -519,6 +522,7 @@ public function compareFields($field1, $field2) { * Returns all data about a user * * @param string $slug user slug or the uuid of a user + * @throws OutOfBoundsException * @return array */ public function view($slug = null) { @@ -673,14 +677,14 @@ protected function _beforeRegistration($postData = array(), $useEmailVerificatio } else { $postData[$this->alias]['email_verified'] = 1; } - $postData[$this->alias]['active'] = 1; - $defaultRole = Configure::read('Users.defaultRole'); - if ($defaultRole) { - $postData[$this->alias]['role'] = $defaultRole; - } else { - $postData[$this->alias]['role'] = 'registered'; - } - return $postData; + $postData[$this->alias]['active'] = 1; + $defaultRole = Configure::read('Users.defaultRole'); + if ($defaultRole) { + $postData[$this->alias]['role'] = $defaultRole; + } else { + $postData[$this->alias]['role'] = 'registered'; + } + return $postData; } /** @@ -688,7 +692,8 @@ protected function _beforeRegistration($postData = array(), $useEmailVerificatio * * @param string $state Find State * @param string $query Query options - * @param string $results Result data + * @param array|string $results Result data + * @throws MissingPluginException * @return array * @link https://github.com/CakeDC/search */ @@ -767,7 +772,7 @@ protected function _findSearch($state, $query, $results = array()) { * @param array $extra Extra options * @return array */ - function paginateCount($conditions = array(), $recursive = 0, $extra = array()) { + public function paginateCount($conditions = array(), $recursive = 0, $extra = array()) { $parameters = compact('conditions'); if ($recursive != $this->recursive) { $parameters['recursive'] = $recursive; @@ -788,29 +793,29 @@ function paginateCount($conditions = array(), $recursive = 0, $extra = array()) */ public function add($postData = null) { if (!empty($postData)) { - $this->data = $postData; - if ($this->validates()) { - if (empty($postData[$this->alias]['role'])) { - if (empty($postData[$this->alias]['is_admin'])) { - $defaultRole = Configure::read('Users.defaultRole'); - if ($defaultRole) { - $postData[$this->alias]['role'] = $defaultRole; - } else { - $postData[$this->alias]['role'] = 'registered'; - } - } else { - $postData[$this->alias]['role'] = 'admin'; - } - } - $postData[$this->alias]['password'] = $this->hash($postData[$this->alias]['password'], 'sha1', true); - $this->create(); - $result = $this->save($postData, false); - if ($result) { - $result[$this->alias][$this->primaryKey] = $this->id; - $this->data = $result; - return true; - } - } + $this->data = $postData; + if ($this->validates()) { + if (empty($postData[$this->alias]['role'])) { + if (empty($postData[$this->alias]['is_admin'])) { + $defaultRole = Configure::read('Users.defaultRole'); + if ($defaultRole) { + $postData[$this->alias]['role'] = $defaultRole; + } else { + $postData[$this->alias]['role'] = 'registered'; + } + } else { + $postData[$this->alias]['role'] = 'admin'; + } + } + $postData[$this->alias]['password'] = $this->hash($postData[$this->alias]['password'], 'sha1', true); + $this->create(); + $result = $this->save($postData, false); + if ($result) { + $result[$this->alias][$this->primaryKey] = $this->id; + $this->data = $result; + return true; + } + } } return false; } diff --git a/Model/UserDetail.php b/Model/UserDetail.php index f27ad0bbc..05a2e2871 100644 --- a/Model/UserDetail.php +++ b/Model/UserDetail.php @@ -147,7 +147,7 @@ public function getSection($userId = null, $section = null) { "{$this->alias}.user_id" => $userId); if (!is_null($section)) { - $conditions["{$this->alias}.field LIKE"] = $section . '.%'; + $conditions["{$this->alias}.field LIKE"] = $section . '.%'; } $results = $this->find('all', array( @@ -156,7 +156,7 @@ public function getSection($userId = null, $section = null) { 'fields' => array("{$this->alias}.field", "{$this->alias}.value"))); if (!empty($results)) { - foreach($results as $result) { + foreach ($results as $result) { list($prefix, $field) = explode('.', $result[$this->alias]['field']); $userDetails[$prefix][$field] = $result[$this->alias]['value']; } @@ -190,9 +190,9 @@ public function saveSection($userId = null, $data = null, $section = null) { if (!empty($this->sectionSchema[$section])) { $this->activeSectionSchema = $section; - foreach($data as $model => $userDetails) { + foreach ($data as $model => $userDetails) { if ($model == $this->alias) { - foreach($userDetails as $key => $value) { + foreach ($userDetails as $key => $value) { $data[$model][$key] = $this->deconstruct($key, $value); } } @@ -214,10 +214,10 @@ public function saveSection($userId = null, $data = null, $section = null) { } if (!empty($data) && is_array($data)) { - foreach($data as $model => $userDetails) { + foreach ($data as $model => $userDetails) { if ($model == $this->alias) { // Save the details - foreach($userDetails as $key => $value) { + foreach ($userDetails as $key => $value) { $newUserDetail = array(); $field = $section . '.' . $key; $userDetail = $this->find('first', array( @@ -256,5 +256,4 @@ public function saveSection($userId = null, $data = null, $section = null) { } return true; } -} -; \ No newline at end of file +} \ No newline at end of file From 38ce1965b79db323bdbb91c16673f32357b31a76 Mon Sep 17 00:00:00 2001 From: Callum Macdonald Date: Tue, 8 Jan 2013 17:52:14 +0700 Subject: [PATCH 19/33] Correct tos link The tos link was /users/pages/tos/ instead of /pages/tos/, adding `'plugin' => null` to the link array fixes. --- View/Users/add.ctp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/View/Users/add.ctp b/View/Users/add.ctp index 8a329ef9b..87f393ecd 100644 --- a/View/Users/add.ctp +++ b/View/Users/add.ctp @@ -26,11 +26,11 @@ echo $this->Form->input('temppassword', array( 'label' => __d('users', 'Password (confirm)'), 'type' => 'password')); - $tosLink = $this->Html->link(__d('users', 'Terms of Service'), array('controller' => 'pages', 'action' => 'tos')); + $tosLink = $this->Html->link(__d('users', 'Terms of Service'), array('controller' => 'pages', 'action' => 'tos', 'plugin' => null)); echo $this->Form->input('tos', array( 'label' => __d('users', 'I have read and agreed to ') . $tosLink)); echo $this->Form->end(__d('users', 'Submit')); ?> -element('Users/sidebar'); ?> \ No newline at end of file +element('Users/sidebar'); ?> From 0b6caad0cd80973a0d218de0a8bc8b0912ecc552 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 14 Jan 2013 11:11:51 +0100 Subject: [PATCH 20/33] Fixing notices coming up from the cookie component when calling RememberMe::destroy --- Controller/Component/RememberMeComponent.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index ec12b940b..baff22f95 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -159,7 +159,10 @@ public function setCookie($data = array()) { */ public function destroyCookie() { extract($this->settings); - $this->Cookie->destroy($cookie['name']); + if (isset($_COOKIE[$cookie['name']])) { + $this->Cookie->name = $cookie['name']; + $this->Cookie->destroy(); + } } /** From b556f2989ee772d60ff4e21b3177eb564ee4adeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 15 Jan 2013 17:20:53 +0100 Subject: [PATCH 21/33] Fixing unit tests --- Test/Case/AllUsersPluginTest.php | 11 +++++++++++ Test/Case/Controller/UsersControllerTest.php | 12 ++++++++---- Test/Case/Model/UserTest.php | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Test/Case/AllUsersPluginTest.php b/Test/Case/AllUsersPluginTest.php index fd51ab258..fbb56d448 100644 --- a/Test/Case/AllUsersPluginTest.php +++ b/Test/Case/AllUsersPluginTest.php @@ -1,4 +1,14 @@ addTestFile($basePath . 'Controller' . DS . 'UserDetailsControllerTest.php'); $suite->addTestFile($basePath . 'Controller' . DS . 'UsersControllerTest.php'); diff --git a/Test/Case/Controller/UsersControllerTest.php b/Test/Case/Controller/UsersControllerTest.php index 6e2954239..02e44b613 100644 --- a/Test/Case/Controller/UsersControllerTest.php +++ b/Test/Case/Controller/UsersControllerTest.php @@ -250,9 +250,13 @@ public function testUserLogin() { ->will($this->returnValue(true)); $this->Users->Auth->staticExpects($this->at(0)) ->method('user') - ->with('id') + ->with('last_login') ->will($this->returnValue(1)); $this->Users->Auth->staticExpects($this->at(1)) + ->method('user') + ->with('id') + ->will($this->returnValue(1)); + $this->Users->Auth->staticExpects($this->at(2)) ->method('user') ->with('username') ->will($this->returnValue('adminuser')); @@ -262,8 +266,8 @@ public function testUserLogin() { ->will($this->returnValue(Router::normalize('/'))); $this->Users->Session = $this->getMock('SessionComponent', array('setFlash'), array($this->Collection)); $this->Users->Session->expects($this->any()) - ->method('setFlash') - ->with(__d('users', 'adminuser you have successfully logged in')); + ->method('setFlash') + ->with(__d('users', 'adminuser you have successfully logged in')); $this->Users->RememberMe = $this->getMock('RememberMeComponent', array(), array($this->Collection)); $this->Users->RememberMe->expects($this->any()) ->method('destroyCookie'); @@ -564,7 +568,7 @@ public function testSetCookie() { $this->Users->setCookie(array( 'name' => 'userTestCookie')); - $this->assertEqual($this->Users->RememberMe->settings['cookieKey'], 'User'); + $this->assertEqual($this->Users->RememberMe->settings['cookieKey'], 'rememberMe'); } /** diff --git a/Test/Case/Model/UserTest.php b/Test/Case/Model/UserTest.php index 45f64f688..4250782be 100644 --- a/Test/Case/Model/UserTest.php +++ b/Test/Case/Model/UserTest.php @@ -60,6 +60,7 @@ public function tearDown() { unset($this->User); ClassRegistry::flush(); } + /** * * From 46664ba89989541048cc6c2092a50f040983f66d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 15 Jan 2013 17:24:43 +0100 Subject: [PATCH 22/33] Improving the users controller to make user of $this->plugin for loading elements and models, less overriding should be needed now for some methods --- Controller/UsersController.php | 38 +++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index f5ea57e42..b96a2f057 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -33,6 +33,13 @@ class UsersController extends UsersAppController { */ public $name = 'Users'; +/** + * If the controller is a plugin controller set the plugin name + * + * @var mixed + */ + public $plugin = null; + /** * Helpers * @@ -83,6 +90,18 @@ public function __construct($request, $response) { parent::__construct($request, $response); } +/** + * Returns $this->plugin with a dot, used for plugin loading using the dot notation + * + * @return mixed string|null + */ + protected function _pluginDot() { + if (is_string($this->plugin)) { + return $this->plugin . '.'; + } + return $this->plugin; + } + /** * Setup components based on plugin availability * @@ -141,14 +160,14 @@ protected function _setupAuth() { 'fields' => array( 'username' => 'email', 'password' => 'password'), - 'userModel' => 'Users.User', + 'userModel' => $this->_pluginDot() . $this->modelClass, 'scope' => array( - 'User.active' => 1, - 'User.email_verified' => 1))); + $this->modelClass . '.active' => 1, + $this->modelClass . '.email_verified' => 1))); $this->Auth->loginRedirect = '/'; - $this->Auth->logoutRedirect = array('plugin' => 'users', 'controller' => 'users', 'action' => 'login'); - $this->Auth->loginAction = array('admin' => false, 'plugin' => 'users', 'controller' => 'users', 'action' => 'login'); + $this->Auth->logoutRedirect = array('plugin' => $this->plugin, 'controller' => 'users', 'action' => 'login'); + $this->Auth->loginAction = array('admin' => false, 'plugin' => $this->plugin, 'controller' => 'users', 'action' => 'login'); } /** @@ -351,8 +370,7 @@ public function login() { if ($this->request->is('post')) { if ($this->Auth->login()) { $this->getEventManager()->dispatch(new CakeEvent('Users.afterLogin', $this, array( - 'isFirstLogin' => !$this->Auth->user('last_login') - ))); + 'isFirstLogin' => !$this->Auth->user('last_login')))); $this->User->id = $this->Auth->user('id'); $this->User->saveField('last_login', date('Y-m-d H:i:s')); @@ -514,7 +532,7 @@ protected function _sendNewPassword($userData) { ->replyTo(Configure::read('App.defaultEmail')) ->return(Configure::read('App.defaultEmail')) ->subject(env('HTTP_HOST') . ' ' . __d('users', 'Password Reset')) - ->template('new_password') + ->template($this->_pluginDot() . 'new_password') ->viewVars(array( 'model' => $this->modelClass, 'userData' => $userData)) @@ -591,7 +609,7 @@ protected function _sendVerificationEmail($userData, $options = array()) { $defaults = array( 'from' => Configure::read('App.defaultEmail'), 'subject' => __d('users', 'Account verification'), - 'template' => 'Users.account_verification', + 'template' => $this->_pluginDot() . 'account_verification', 'layout' => 'default'); $options = array_merge($defaults, $options); @@ -619,7 +637,7 @@ protected function _sendPasswordReset($admin = null, $options = array()) { $defaults = array( 'from' => Configure::read('App.defaultEmail'), 'subject' => __d('users', 'Password Reset'), - 'template' => 'Users.password_reset_request', + 'template' => $this->_pluginDot() . 'password_reset_request', 'layout' => 'default'); $options = array_merge($defaults, $options); From 6ddc83501a2526e92e1f2c0456236a8136d1a83d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Tue, 15 Jan 2013 17:28:55 +0100 Subject: [PATCH 23/33] Updating the copyright year in doc blocks --- Config/Migration/001_initialize_users_schema.php | 2 +- Config/Migration/002_renaming.php | 2 +- Config/Migration/map.php | 2 +- Config/Schema/schema.php | 2 +- Controller/Component/RememberMeComponent.php | 4 ++-- Controller/UserDetailsController.php | 4 ++-- Controller/UsersAppController.php | 4 ++-- Controller/UsersController.php | 4 ++-- Model/User.php | 4 ++-- Model/UserDetail.php | 4 ++-- Model/UsersAppModel.php | 4 ++-- Test/Case/AllUsersPluginTest.php | 4 ++-- Test/Case/Controller/Component/RememberMeComponentTest.php | 4 ++-- Test/Case/Controller/UserDetailsControllerTest.php | 4 ++-- Test/Case/Controller/UsersControllerTest.php | 4 ++-- Test/Case/Model/UserDetailTest.php | 4 ++-- Test/Case/Model/UserTest.php | 4 ++-- Test/Fixture/UserDetailFixture.php | 4 ++-- Test/Fixture/UserFixture.php | 4 ++-- View/Elements/pagination.ctp | 4 ++-- View/Emails/text/account_verification.ctp | 4 ++-- View/Emails/text/new_password.ctp | 4 ++-- View/Emails/text/password_reset_request.ctp | 4 ++-- View/UserDetails/add.ctp | 4 ++-- View/UserDetails/admin_add.ctp | 4 ++-- View/UserDetails/admin_edit.ctp | 4 ++-- View/UserDetails/admin_index.ctp | 4 ++-- View/UserDetails/admin_view.ctp | 4 ++-- View/UserDetails/edit.ctp | 4 ++-- View/UserDetails/index.ctp | 4 ++-- View/UserDetails/view.ctp | 4 ++-- View/Users/add.ctp | 4 ++-- View/Users/admin_add.ctp | 4 ++-- View/Users/admin_edit.ctp | 4 ++-- View/Users/admin_index.ctp | 4 ++-- View/Users/admin_view.ctp | 4 ++-- View/Users/change_password.ctp | 4 ++-- View/Users/dashboard.ctp | 4 ++-- View/Users/edit.ctp | 4 ++-- View/Users/index.ctp | 4 ++-- View/Users/login.ctp | 4 ++-- View/Users/request_password_change.ctp | 4 ++-- View/Users/search.ctp | 4 ++-- View/Users/view.ctp | 4 ++-- 44 files changed, 84 insertions(+), 84 deletions(-) diff --git a/Config/Migration/001_initialize_users_schema.php b/Config/Migration/001_initialize_users_schema.php index 612c7fbe0..8e2406413 100644 --- a/Config/Migration/001_initialize_users_schema.php +++ b/Config/Migration/001_initialize_users_schema.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2012, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Config/Migration/002_renaming.php b/Config/Migration/002_renaming.php index a975b078c..beabeec48 100644 --- a/Config/Migration/002_renaming.php +++ b/Config/Migration/002_renaming.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2012, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Config/Migration/map.php b/Config/Migration/map.php index 2881b9c44..c3d8d709f 100644 --- a/Config/Migration/map.php +++ b/Config/Migration/map.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2012, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Config/Schema/schema.php b/Config/Schema/schema.php index a58c54e8f..fdccd1747 100644 --- a/Config/Schema/schema.php +++ b/Config/Schema/schema.php @@ -2,7 +2,7 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2012, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index baff22f95..0bbc93916 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -1,11 +1,11 @@ diff --git a/View/Emails/text/account_verification.ctp b/View/Emails/text/account_verification.ctp index 15135681b..611eb687d 100644 --- a/View/Emails/text/account_verification.ctp +++ b/View/Emails/text/account_verification.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_add.ctp b/View/UserDetails/admin_add.ctp index fc5577fee..064854c91 100644 --- a/View/UserDetails/admin_add.ctp +++ b/View/UserDetails/admin_add.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_edit.ctp b/View/UserDetails/admin_edit.ctp index dc83e7d26..ab19c6261 100644 --- a/View/UserDetails/admin_edit.ctp +++ b/View/UserDetails/admin_edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_index.ctp b/View/UserDetails/admin_index.ctp index 0434adf68..aa80adc7e 100644 --- a/View/UserDetails/admin_index.ctp +++ b/View/UserDetails/admin_index.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_view.ctp b/View/UserDetails/admin_view.ctp index 35a900cc4..1b919b94a 100644 --- a/View/UserDetails/admin_view.ctp +++ b/View/UserDetails/admin_view.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/edit.ctp b/View/UserDetails/edit.ctp index eb1559d89..949db3f57 100644 --- a/View/UserDetails/edit.ctp +++ b/View/UserDetails/edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/index.ctp b/View/UserDetails/index.ctp index 9c0c563a4..c2ee3cfce 100644 --- a/View/UserDetails/index.ctp +++ b/View/UserDetails/index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/add.ctp b/View/Users/add.ctp index 8a329ef9b..20065e71b 100644 --- a/View/Users/add.ctp +++ b/View/Users/add.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_add.ctp b/View/Users/admin_add.ctp index 95d194160..c1f9e623f 100644 --- a/View/Users/admin_add.ctp +++ b/View/Users/admin_add.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_edit.ctp b/View/Users/admin_edit.ctp index 9d5214b5f..270139c6f 100644 --- a/View/Users/admin_edit.ctp +++ b/View/Users/admin_edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_index.ctp b/View/Users/admin_index.ctp index b3fb92649..bc534a510 100644 --- a/View/Users/admin_index.ctp +++ b/View/Users/admin_index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_view.ctp b/View/Users/admin_view.ctp index 629591fbe..dcacbdf44 100644 --- a/View/Users/admin_view.ctp +++ b/View/Users/admin_view.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/change_password.ctp b/View/Users/change_password.ctp index 08660ad39..bc4530440 100644 --- a/View/Users/change_password.ctp +++ b/View/Users/change_password.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/dashboard.ctp b/View/Users/dashboard.ctp index 54c915137..f510421a6 100644 --- a/View/Users/dashboard.ctp +++ b/View/Users/dashboard.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/edit.ctp b/View/Users/edit.ctp index 134539fb4..699bad37c 100644 --- a/View/Users/edit.ctp +++ b/View/Users/edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/index.ctp b/View/Users/index.ctp index 58e7ff085..1dbbcbc2d 100644 --- a/View/Users/index.ctp +++ b/View/Users/index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/login.ctp b/View/Users/login.ctp index ef12db24b..e803fe2b9 100644 --- a/View/Users/login.ctp +++ b/View/Users/login.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/request_password_change.ctp b/View/Users/request_password_change.ctp index 9b80201ca..5cf7b1ed7 100644 --- a/View/Users/request_password_change.ctp +++ b/View/Users/request_password_change.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/search.ctp b/View/Users/search.ctp index 11f7ffb69..b051e6d9b 100644 --- a/View/Users/search.ctp +++ b/View/Users/search.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/view.ctp b/View/Users/view.ctp index 991f1ed79..cc8edaf5d 100644 --- a/View/Users/view.ctp +++ b/View/Users/view.ctp @@ -1,11 +1,11 @@ From c50c6e5db3694c9743e49167dbc48aab71a4cd4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Wed, 16 Jan 2013 12:01:03 +0100 Subject: [PATCH 24/33] Fixing a case where the restoreFromCookie in the RememberMeComponent can cause wrong data in the request when the login via cookie failed. --- Controller/Component/RememberMeComponent.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Controller/Component/RememberMeComponent.php b/Controller/Component/RememberMeComponent.php index 0bbc93916..41d84fb73 100644 --- a/Controller/Component/RememberMeComponent.php +++ b/Controller/Component/RememberMeComponent.php @@ -109,6 +109,8 @@ public function restoreLoginFromCookie($checkLoginStatus = true) { $cookie = $this->Cookie->read($cookieKey); if (!empty($cookie)) { + $request = $this->request->data; + foreach ($fields as $field) { if (!empty($cookie[$field])) { $this->request->data[$userModel][$field] = $cookie[$field]; @@ -116,7 +118,11 @@ public function restoreLoginFromCookie($checkLoginStatus = true) { } $result = $this->Auth->login(); - unset($this->request->data[$userModel]); + + if (!$result) { + $this->request->data = $request; + } + return $result; } } From 0a2831c219b9673aaebf605285085c4783bc1703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Sun, 20 Jan 2013 23:46:19 +0100 Subject: [PATCH 25/33] Providing backward compatibility to a fix that was just made recently to the core for users that want to upgrade the plugin but not the core, see http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3550-inherited-controllers-get-wrong-property-names --- Controller/UsersController.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index e3266cbff..068623dd1 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -88,6 +88,23 @@ public function __construct($request, $response) { $this->_setupComponents(); $this->_setupHelpers(); parent::__construct($request, $response); + $this->_reInitControllerName(); + } + +/** + * Providing backward compatibility to a fix that was just made recently to the core + * for users that want to upgrade the plugin but not the core + * + * @link http://cakephp.lighthouseapp.com/projects/42648-cakephp/tickets/3550-inherited-controllers-get-wrong-property-names + * @return void + */ + protected function _reInitControllerName() { + $name = substr(get_class($this), 0, -10); + if ($this->name === null) { + $this->name = $name; + } elseif ($name !== $this->name) { + $this->name = $name; + } } /** From 2d1050207256beebd6b12a563bac71728f423a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Mon, 21 Jan 2013 00:00:04 +0100 Subject: [PATCH 26/33] Changing $this->User to $this->{$this->modelClass} so that everything works fine when the controller gets inherited and the model class changes --- Controller/UsersController.php | 62 +++++++++++++++++----------------- 1 file changed, 31 insertions(+), 31 deletions(-) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index 068623dd1..f4c1c31fe 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -207,7 +207,7 @@ public function index() { * @return void */ public function dashboard() { - $user = $this->User->read(null, $this->Auth->user('id')); + $user = $this->{$this->modelClass}->read(null, $this->Auth->user('id')); $this->set('user', $user); } @@ -219,7 +219,7 @@ public function dashboard() { */ public function view($slug = null) { try { - $this->set('user', $this->User->view($slug)); + $this->set('user', $this->{$this->modelClass}->view($slug)); } catch (Exception $e) { $this->Session->setFlash($e->getMessage()); $this->redirect('/'); @@ -234,17 +234,17 @@ public function view($slug = null) { */ public function edit() { if (!empty($this->request->data)) { - if ($this->User->UserDetail->saveSection($this->Auth->user('id'), $this->request->data, 'User')) { + if ($this->{$this->modelClass}->UserDetail->saveSection($this->Auth->user('id'), $this->request->data, 'User')) { $this->Session->setFlash(__d('users', 'Profile saved.')); } else { $this->Session->setFlash(__d('users', 'Could not save your profile.')); } } else { - $data = $this->User->UserDetail->getSection($this->Auth->user('id'), 'User'); - if (!isset($data['User'])) { - $data['User'] = array(); + $data = $this->{$this->modelClass}->UserDetail->getSection($this->Auth->user('id'), 'User'); + if (!isset($data[$this->modelClass])) { + $data[$this->modelClass] = array(); } - $this->request->data['UserDetail'] = $data['User']; + $this->request->data['UserDetail'] = $data[$this->modelClass]; } } @@ -255,8 +255,8 @@ public function edit() { */ public function admin_index() { $this->Prg->commonProcess(); - unset($this->User->validate['username']); - unset($this->User->validate['email']); + unset($this->{$this->modelClass}->validate['username']); + unset($this->{$this->modelClass}->validate['email']); $this->{$this->modelClass}->data[$this->modelClass] = $this->passedArgs; if ($this->{$this->modelClass}->Behaviors->attached('Searchable')) { $parsedConditions = $this->{$this->modelClass}->parseCriteria($this->passedArgs); @@ -281,7 +281,7 @@ public function admin_view($id = null) { $this->Session->setFlash(__d('users', 'Invalid User.')); $this->redirect(array('action' => 'index')); } - $this->set('user', $this->User->read(null, $id)); + $this->set('user', $this->{$this->modelClass}->read(null, $id)); } /** @@ -291,10 +291,10 @@ public function admin_view($id = null) { */ public function admin_add() { if (!empty($this->request->data)) { - $this->request->data['User']['tos'] = true; - $this->request->data['User']['email_verified'] = true; + $this->request->data[$this->modelClass]['tos'] = true; + $this->request->data[$this->modelClass]['email_verified'] = true; - if ($this->User->add($this->request->data)) { + if ($this->{$this->modelClass}->add($this->request->data)) { $this->Session->setFlash(__d('users', 'The User has been saved')); $this->redirect(array('action' => 'index')); } @@ -310,7 +310,7 @@ public function admin_add() { */ public function admin_edit($userId = null) { try { - $result = $this->User->edit($userId, $this->request->data); + $result = $this->{$this->modelClass}->edit($userId, $this->request->data); if ($result === true) { $this->Session->setFlash(__d('users', 'User saved')); $this->redirect(array('action' => 'index')); @@ -323,7 +323,7 @@ public function admin_edit($userId = null) { } if (empty($this->request->data)) { - $this->request->data = $this->User->read(null, $userId); + $this->request->data = $this->{$this->modelClass}->read(null, $userId); } $this->set('roles', Configure::read('Users.roles')); } @@ -335,7 +335,7 @@ public function admin_edit($userId = null) { * @return void */ public function admin_delete($userId = null) { - if ($this->User->delete($userId)) { + if ($this->{$this->modelClass}->delete($userId)) { $this->Session->setFlash(__d('users', 'User deleted')); } else { $this->Session->setFlash(__d('users', 'Invalid User')); @@ -365,9 +365,9 @@ public function add() { } if (!empty($this->request->data)) { - $user = $this->User->register($this->request->data); + $user = $this->{$this->modelClass}->register($this->request->data); if ($user !== false) { - $this->_sendVerificationEmail($this->User->data); + $this->_sendVerificationEmail($this->{$this->modelClass}->data); $this->Session->setFlash(__d('users', 'Your account has been created. You should receive an e-mail shortly to authenticate your account. Once validated you will be able to login.')); $this->redirect(array('action' => 'login')); } else { @@ -389,8 +389,8 @@ public function login() { $this->getEventManager()->dispatch(new CakeEvent('Users.afterLogin', $this, array( 'isFirstLogin' => !$this->Auth->user('last_login')))); - $this->User->id = $this->Auth->user('id'); - $this->User->saveField('last_login', date('Y-m-d H:i:s')); + $this->{$this->modelClass}->id = $this->Auth->user('id'); + $this->{$this->modelClass}->saveField('last_login', date('Y-m-d H:i:s')); if ($this->here == $this->Auth->loginRedirect) { $this->Auth->loginRedirect = '/'; @@ -495,7 +495,7 @@ public function verify($type = 'email', $token = null) { } try { - $this->User->verifyEmail($token); + $this->{$this->modelClass}->verifyEmail($token); $this->Session->setFlash(__d('users', 'Your e-mail has been validated!')); return $this->redirect(array('action' => 'login')); } catch (RuntimeException $e) { @@ -516,7 +516,7 @@ public function request_new_password($token = null) { throw new NotFoundException(); } - $data = $this->User->validateToken($token, true); + $data = $this->{$this->modelClass}->validateToken($token, true); if (!$data) { $this->Session->setFlash(__d('users', 'The url you accessed is not longer valid')); @@ -526,7 +526,7 @@ public function request_new_password($token = null) { $email = $data[$this->modelClass]['email']; unset($data[$this->modelClass]['email']); - if ($this->User->save($data, array('validate' => false))) { + if ($this->{$this->modelClass}->save($data, array('validate' => false))) { $this->_sendNewPassword($data); $this->Session->setFlash(__d('users', 'Your password was sent to your registered email account')); return $this->redirect(array('action' => 'login')); @@ -545,7 +545,7 @@ public function request_new_password($token = null) { protected function _sendNewPassword($userData) { $Email = $this->_getMailInstance(); $Email->from(Configure::read('App.defaultEmail')) - ->to($data[$this->modelClass]['email']) + ->to($userData[$this->modelClass]['email']) ->replyTo(Configure::read('App.defaultEmail')) ->return(Configure::read('App.defaultEmail')) ->subject(env('HTTP_HOST') . ' ' . __d('users', 'Password Reset')) @@ -553,7 +553,7 @@ protected function _sendNewPassword($userData) { ->viewVars(array( 'model' => $this->modelClass, 'userData' => $userData)) - ->send($content); + ->send(); } /** @@ -564,7 +564,7 @@ protected function _sendNewPassword($userData) { public function change_password() { if ($this->request->is('post')) { $this->request->data[$this->modelClass]['id'] = $this->Auth->user('id'); - if ($this->User->changePassword($this->request->data)) { + if ($this->{$this->modelClass}->changePassword($this->request->data)) { $this->Session->setFlash(__d('users', 'Password changed.')); $this->redirect('/'); } @@ -660,7 +660,7 @@ protected function _sendPasswordReset($admin = null, $options = array()) { $options = array_merge($defaults, $options); if (!empty($this->request->data)) { - $user = $this->User->passwordReset($this->request->data); + $user = $this->{$this->modelClass}->passwordReset($this->request->data); if (!empty($user)) { @@ -671,8 +671,8 @@ protected function _sendPasswordReset($admin = null, $options = array()) { ->template($options['template'], $options['layout']) ->viewVars(array( 'model' => $this->modelClass, - 'user' => $this->User->data, - 'token' => $this->User->data[$this->modelClass]['password_token'])) + 'user' => $this->{$this->modelClass}->data, + 'token' => $this->{$this->modelClass}->data[$this->modelClass]['password_token'])) ->send(); if ($admin) { @@ -714,13 +714,13 @@ protected function _setCookie($options = array(), $cookieKey = 'rememberMe') { * @return void */ protected function _resetPassword($token) { - $user = $this->User->checkPasswordToken($token); + $user = $this->{$this->modelClass}->checkPasswordToken($token); if (empty($user)) { $this->Session->setFlash(__d('users', 'Invalid password reset token, try again.')); $this->redirect(array('action' => 'reset_password')); } - if (!empty($this->request->data) && $this->User->resetPassword(Set::merge($user, $this->request->data))) { + if (!empty($this->request->data) && $this->{$this->modelClass}->resetPassword(Set::merge($user, $this->request->data))) { $this->Session->setFlash(__d('users', 'Password changed, you can now login with your new password.')); $this->redirect($this->Auth->loginAction); } From b3f4271103db7607704f61031c220b9ca5152b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Kr=C3=A4mer?= Date: Fri, 1 Feb 2013 19:45:24 +0100 Subject: [PATCH 27/33] Fixing a php 5.4 strict notice, made the signature of UsersAppController::isAuthorized match the parent --- Controller/UsersAppController.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Controller/UsersAppController.php b/Controller/UsersAppController.php index 16118af01..4d2dc1691 100644 --- a/Controller/UsersAppController.php +++ b/Controller/UsersAppController.php @@ -25,10 +25,11 @@ class UsersAppController extends AppController { * * This is called to see if a user (when logged in) is able to access an action * + * @param array $user * @return boolean True if allowed */ - public function isAuthorized() { - return parent::isAuthorized(); + public function isAuthorized($user) { + return parent::isAuthorized($user); } } From dd7c34b4902ecd378393e391b49e3c6a5026c949 Mon Sep 17 00:00:00 2001 From: dizyart Date: Sat, 2 Feb 2013 03:56:22 +0100 Subject: [PATCH 28/33] PHP5.4-strict inheritance in UsersControllerTestCase --- Test/Case/Controller/UsersControllerTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Test/Case/Controller/UsersControllerTest.php b/Test/Case/Controller/UsersControllerTest.php index 141076e00..f12200574 100644 --- a/Test/Case/Controller/UsersControllerTest.php +++ b/Test/Case/Controller/UsersControllerTest.php @@ -187,7 +187,7 @@ class UsersControllerTestCase extends CakeTestCase { * * @return void */ - public function startTest() { + public function startTest($method) { Configure::write('App.UserClass', null); $request = new CakeRequest(); @@ -597,7 +597,7 @@ private function __setGet() { * * @return void */ - public function endTest() { + public function endTest($method) { $this->Users->Session->destroy(); unset($this->Users); ClassRegistry::flush(); From 2ac27ed2e17ba2ccfca744f2bc20e7b9b18a8ba0 Mon Sep 17 00:00:00 2001 From: Yevgeny Tomenko Date: Tue, 12 Feb 2013 23:58:05 +0400 Subject: [PATCH 29/33] Update license blocks. --- Config/Migration/001_initialize_users_schema.php | 4 ++-- Config/Migration/002_renaming.php | 4 ++-- Config/Migration/map.php | 4 ++-- Config/Schema/schema.php | 4 ++-- Controller/UserDetailsController.php | 4 ++-- Controller/UsersAppController.php | 4 ++-- Controller/UsersController.php | 4 ++-- Model/User.php | 7 +++---- Model/UserDetail.php | 4 ++-- Model/UsersAppModel.php | 4 ++-- Test/Case/Controller/UserDetailsControllerTest.php | 4 ++-- Test/Case/Controller/UsersControllerTest.php | 4 ++-- Test/Case/Model/UserDetailTest.php | 4 ++-- Test/Case/Model/UserTest.php | 4 ++-- Test/Fixture/UserDetailFixture.php | 4 ++-- Test/Fixture/UserFixture.php | 4 ++-- View/Elements/pagination.ctp | 4 ++-- View/Emails/text/account_verification.ctp | 4 ++-- View/Emails/text/new_password.ctp | 4 ++-- View/Emails/text/password_reset_request.ctp | 4 ++-- View/UserDetails/add.ctp | 4 ++-- View/UserDetails/admin_add.ctp | 4 ++-- View/UserDetails/admin_edit.ctp | 4 ++-- View/UserDetails/admin_index.ctp | 4 ++-- View/UserDetails/admin_view.ctp | 4 ++-- View/UserDetails/edit.ctp | 4 ++-- View/UserDetails/index.ctp | 4 ++-- View/UserDetails/view.ctp | 4 ++-- View/Users/add.ctp | 4 ++-- View/Users/admin_add.ctp | 4 ++-- View/Users/admin_edit.ctp | 4 ++-- View/Users/admin_index.ctp | 4 ++-- View/Users/admin_view.ctp | 4 ++-- View/Users/change_password.ctp | 4 ++-- View/Users/dashboard.ctp | 4 ++-- View/Users/edit.ctp | 4 ++-- View/Users/index.ctp | 4 ++-- View/Users/login.ctp | 4 ++-- View/Users/request_password_change.ctp | 4 ++-- View/Users/search.ctp | 4 ++-- View/Users/view.ctp | 4 ++-- license.txt | 2 +- 42 files changed, 84 insertions(+), 85 deletions(-) diff --git a/Config/Migration/001_initialize_users_schema.php b/Config/Migration/001_initialize_users_schema.php index 8c6a7136e..3b1dc4f16 100644 --- a/Config/Migration/001_initialize_users_schema.php +++ b/Config/Migration/001_initialize_users_schema.php @@ -2,14 +2,14 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @Copyright 2010 - 2011, Cake Development Corporation + * @Copyright 2010 - 2013, Cake Development Corporation * @link http://github.com/CakeDC/users * @package plugins.users.config.migrations * @license MIT License (http://www.opensource.org/licenses/mit-license.php) diff --git a/Config/Migration/002_renaming.php b/Config/Migration/002_renaming.php index cd39be85e..d9874316b 100644 --- a/Config/Migration/002_renaming.php +++ b/Config/Migration/002_renaming.php @@ -2,14 +2,14 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @Copyright 2010 - 2011, Cake Development Corporation + * @Copyright 2010 - 2013, Cake Development Corporation * @link http://github.com/CakeDC/users * @package plugins.users.config.migrations * @license MIT License (http://www.opensource.org/licenses/mit-license.php) diff --git a/Config/Migration/map.php b/Config/Migration/map.php index 4ebb86efd..5d69f5f89 100644 --- a/Config/Migration/map.php +++ b/Config/Migration/map.php @@ -2,14 +2,14 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @Copyright 2010 - 2011, Cake Development Corporation + * @Copyright 2010 - 2013, Cake Development Corporation * @link http://github.com/CakeDC/users * @package plugins.users.config.migrations * @license MIT License (http://www.opensource.org/licenses/mit-license.php) diff --git a/Config/Schema/schema.php b/Config/Schema/schema.php index 6736df080..6bbacbd0e 100644 --- a/Config/Schema/schema.php +++ b/Config/Schema/schema.php @@ -2,14 +2,14 @@ /** * Users CakePHP Plugin * - * Copyright 2010 - 2011, Cake Development Corporation + * Copyright 2010 - 2013, Cake Development Corporation * 1785 E. Sahara Avenue, Suite 490-423 * Las Vegas, Nevada 89104 * * Licensed under The MIT License * Redistributions of files must retain the above copyright notice. * - * @Copyright 2010 - 2011, Cake Development Corporation + * @Copyright 2010 - 2013, Cake Development Corporation * @link http://github.com/CakeDC/users * @package plugins.users.config.schema * @license MIT License (http://www.opensource.org/licenses/mit-license.php) diff --git a/Controller/UserDetailsController.php b/Controller/UserDetailsController.php index 9dee9c783..50f7d6103 100644 --- a/Controller/UserDetailsController.php +++ b/Controller/UserDetailsController.php @@ -1,11 +1,11 @@ useDbConfig); + $db = ConnectionManager::getDataSource($this->useDbConfig); $by = $query['by']; $search = $query['search']; - $byQuoted = $db->value($search); $like = '%' . $query['search'] . '%'; switch ($by) { diff --git a/Model/UserDetail.php b/Model/UserDetail.php index 9ae7c9353..e12e7943c 100644 --- a/Model/UserDetail.php +++ b/Model/UserDetail.php @@ -1,11 +1,11 @@ diff --git a/View/Emails/text/account_verification.ctp b/View/Emails/text/account_verification.ctp index 057d6d62e..611eb687d 100644 --- a/View/Emails/text/account_verification.ctp +++ b/View/Emails/text/account_verification.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_add.ctp b/View/UserDetails/admin_add.ctp index cda182e8a..064854c91 100644 --- a/View/UserDetails/admin_add.ctp +++ b/View/UserDetails/admin_add.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_edit.ctp b/View/UserDetails/admin_edit.ctp index 29df7c009..ab19c6261 100644 --- a/View/UserDetails/admin_edit.ctp +++ b/View/UserDetails/admin_edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_index.ctp b/View/UserDetails/admin_index.ctp index a4f04f30b..aa80adc7e 100644 --- a/View/UserDetails/admin_index.ctp +++ b/View/UserDetails/admin_index.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/admin_view.ctp b/View/UserDetails/admin_view.ctp index f7c26d43f..1b919b94a 100644 --- a/View/UserDetails/admin_view.ctp +++ b/View/UserDetails/admin_view.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/edit.ctp b/View/UserDetails/edit.ctp index c9e9dea81..949db3f57 100644 --- a/View/UserDetails/edit.ctp +++ b/View/UserDetails/edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/UserDetails/index.ctp b/View/UserDetails/index.ctp index f2ad92870..c2ee3cfce 100644 --- a/View/UserDetails/index.ctp +++ b/View/UserDetails/index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/add.ctp b/View/Users/add.ctp index e85fbd86d..20065e71b 100644 --- a/View/Users/add.ctp +++ b/View/Users/add.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_add.ctp b/View/Users/admin_add.ctp index 37b523324..c1f9e623f 100644 --- a/View/Users/admin_add.ctp +++ b/View/Users/admin_add.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_edit.ctp b/View/Users/admin_edit.ctp index 569466f91..270139c6f 100644 --- a/View/Users/admin_edit.ctp +++ b/View/Users/admin_edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_index.ctp b/View/Users/admin_index.ctp index 4039e6e75..bc534a510 100644 --- a/View/Users/admin_index.ctp +++ b/View/Users/admin_index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/admin_view.ctp b/View/Users/admin_view.ctp index 6c98bce43..dcacbdf44 100644 --- a/View/Users/admin_view.ctp +++ b/View/Users/admin_view.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/change_password.ctp b/View/Users/change_password.ctp index 187d7a796..bc4530440 100644 --- a/View/Users/change_password.ctp +++ b/View/Users/change_password.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/dashboard.ctp b/View/Users/dashboard.ctp index 29b1c2819..f510421a6 100644 --- a/View/Users/dashboard.ctp +++ b/View/Users/dashboard.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/edit.ctp b/View/Users/edit.ctp index 912b4e355..699bad37c 100644 --- a/View/Users/edit.ctp +++ b/View/Users/edit.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/index.ctp b/View/Users/index.ctp index 84a00b8fd..1dbbcbc2d 100644 --- a/View/Users/index.ctp +++ b/View/Users/index.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/login.ctp b/View/Users/login.ctp index 460e1255d..dbb1ddaef 100644 --- a/View/Users/login.ctp +++ b/View/Users/login.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/request_password_change.ctp b/View/Users/request_password_change.ctp index 54d989701..5cf7b1ed7 100644 --- a/View/Users/request_password_change.ctp +++ b/View/Users/request_password_change.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/search.ctp b/View/Users/search.ctp index 019fc1714..b051e6d9b 100644 --- a/View/Users/search.ctp +++ b/View/Users/search.ctp @@ -1,11 +1,11 @@ diff --git a/View/Users/view.ctp b/View/Users/view.ctp index 4b1a98f18..cc8edaf5d 100644 --- a/View/Users/view.ctp +++ b/View/Users/view.ctp @@ -1,11 +1,11 @@ diff --git a/license.txt b/license.txt index 25e5ee24c..011c22241 100644 --- a/license.txt +++ b/license.txt @@ -1,6 +1,6 @@ The MIT License -Copyright 2009-2010 +Copyright 2009-2013 Cake Development Corporation 1785 E. Sahara Avenue, Suite 490-423 Las Vegas, Nevada 89104 From 4afd5d37d8412a79aa42cdb6897bdb09ce643a4c Mon Sep 17 00:00:00 2001 From: J Miller Date: Fri, 22 Feb 2013 21:58:01 -0800 Subject: [PATCH 30/33] Fix typo "RememberMe" not "RemembeMe" --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index d787be12c..a894fbcf9 100644 --- a/readme.md +++ b/readme.md @@ -42,12 +42,12 @@ The default password reset process requires the user to enter his email address, To use the "remember me" checkbox which sets a cookie on the login page you will need to add the RememberMe component to the AppController or the controllers you want to auto-login the user again based on the cookie. public $components = array( - 'Users.RemembeMe'); + 'Users.RememberMe'); If you are using another user model than 'User' you'll have to configure it: public $components = array( - 'Users.RemembeMe' => array( + 'Users.RememberMe' => array( 'userModel' => 'AppUser'); And add this line From d3d48d22dc776ffa814c3fac85a0a69ea13c0eaf Mon Sep 17 00:00:00 2001 From: Callum Macdonald Date: Sun, 3 Mar 2013 21:27:01 +0700 Subject: [PATCH 31/33] Add the missing prev / next links A view element exists for prev / next links but was not included in the admin users view. --- View/Users/admin_index.ctp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/View/Users/admin_index.ctp b/View/Users/admin_index.ctp index 4039e6e75..3f9c2f5e5 100644 --- a/View/Users/admin_index.ctp +++ b/View/Users/admin_index.ctp @@ -21,6 +21,7 @@ ?> element('paging'); ?> + element('pagination'); ?> @@ -62,5 +63,6 @@
Paginator->sort('username'); ?>
+ element('pagination'); ?> element('Users/admin_sidebar'); ?> From 06c663668e7db1f5bbafe773f8f76e69b9e6ad2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Gonz=C3=A1lez?= Date: Mon, 22 Apr 2013 13:40:19 +0100 Subject: [PATCH 32/33] destroying remember me cookie on password change for user, fixing tests --- Controller/UsersController.php | 2 + .../Component/RememberMeComponentTest.php | 69 ++++++++++++++++--- Test/Case/Controller/UsersControllerTest.php | 4 ++ 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/Controller/UsersController.php b/Controller/UsersController.php index f4c1c31fe..fa3af63e2 100755 --- a/Controller/UsersController.php +++ b/Controller/UsersController.php @@ -566,6 +566,8 @@ public function change_password() { $this->request->data[$this->modelClass]['id'] = $this->Auth->user('id'); if ($this->{$this->modelClass}->changePassword($this->request->data)) { $this->Session->setFlash(__d('users', 'Password changed.')); + // we don't want to keep the cookie with the old password around + $this->RememberMe->destroyCookie(); $this->redirect('/'); } } diff --git a/Test/Case/Controller/Component/RememberMeComponentTest.php b/Test/Case/Controller/Component/RememberMeComponentTest.php index d0c80d3a5..d45d003f5 100644 --- a/Test/Case/Controller/Component/RememberMeComponentTest.php +++ b/Test/Case/Controller/Component/RememberMeComponentTest.php @@ -38,6 +38,18 @@ class RememberMeComponentTest extends CakeTestCase { */ public $Controller; +/** + * User data + * @var array + */ + public $usersData = array( + 'test' => array( + 'email' => 'test@cakedc.com', + 'password' => 'test'), + 'admin' => array( + 'email' => 'admin@cakedc.com', + 'password' => 'admin')); + /** * start * @@ -84,31 +96,68 @@ public function testRestoreLoginFromCookie() { $this->RememberMe->Cookie->expects($this->once()) ->method('read') ->with($this->equalTo('rememberMe')) - ->will($this->returnValue(array( - 'email' => 'email', - 'password' => 'password'))); + ->will($this->returnValue($this->usersData['admin'])); $this->RememberMe->Auth->expects($this->once()) - ->method('login'); - + ->method('login') + ->will($this->returnValue(true)); + + $this->__setPostData(array('User' => $this->usersData['test'])); + $this->RememberMe->restoreLoginFromCookie(); + // even if we post "test" user, we have a remember me cookie set and will priorize the cookie over the post + // NOTE we check if the user is logged in in the startup method of the Component $this->assertEqual($this->RememberMe->request->data, array( - 'User' => array( - 'email' => 'email', - 'password' => 'password'))); + 'User' => $this->usersData['admin'])); } +/** + * testRestoreLoginFromCookieIncorrectLogin + * + * We check the post request data is not modified when the cookie holds incorrect login credentials + * + * @return void + */ + public function testRestoreLoginFromCookieIncorrectLogin() { + // cookie will hold "admin" data, and post request will have "test" + $this->RememberMe->Cookie->expects($this->once()) + ->method('read') + ->with($this->equalTo('rememberMe')) + ->will($this->returnValue($this->usersData['admin'])); + + // admin will not login + $this->RememberMe->Auth->expects($this->once()) + ->method('login') + ->will($this->returnValue(false)); + + // post has "test" data + $this->__setPostData(array('User' => $this->usersData['test'])); + + $this->RememberMe->restoreLoginFromCookie(); + + $this->assertEqual($this->RememberMe->request->data, array( + 'User' => $this->usersData['test'])); + } + /** * testDestroyCookie * * @return void */ public function testDestroyCookie() { + $_COOKIE['User'] = 'defined'; $this->RememberMe->Cookie->expects($this->once()) - ->method('destroy') - ->with($this->equalTo('User')); + ->method('destroy'); $this->RememberMe->destroyCookie(); } +/** + * Set post data to the test controller + * @param type $data + */ + private function __setPostData($data = array()) { + $_SERVER['REQUEST_METHOD'] = 'POST'; + $this->RememberMe->request->data = array_merge($data); + } } \ No newline at end of file diff --git a/Test/Case/Controller/UsersControllerTest.php b/Test/Case/Controller/UsersControllerTest.php index 3f696f5cb..462b17cbc 100644 --- a/Test/Case/Controller/UsersControllerTest.php +++ b/Test/Case/Controller/UsersControllerTest.php @@ -450,6 +450,10 @@ public function testChangePassword() { 'new_password' => 'newpassword', 'confirm_password' => 'newpassword', 'old_password' => 'test'))); + $this->Users->RememberMe = $this->getMock('RememberMeComponent', array(), array($this->Collection)); + $this->Users->RememberMe->expects($this->any()) + ->method('destroyCookie'); + $this->Users->change_password(); $this->assertEqual($this->Users->redirectUrl, '/'); } From 4cb60fed2948ecb613826d4a3b4568c7fafb9444 Mon Sep 17 00:00:00 2001 From: Guillermo Mansilla Date: Thu, 2 May 2013 14:43:56 -0400 Subject: [PATCH 33/33] Fix documentation to set controller name property and model name --- readme.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/readme.md b/readme.md index a894fbcf9..be4021668 100644 --- a/readme.md +++ b/readme.md @@ -79,6 +79,7 @@ Declare the controller class App::uses('UsersController', 'Users.Controller'); class AppUsersController extends UsersController { + public $name = 'AppUsers'; } In the case you want to extend also the user model it's required to set the right user class in the beforeFilter() because the controller will use the inherited model which would be Users.User. @@ -86,6 +87,7 @@ In the case you want to extend also the user model it's required to set the righ public function beforeFilter() { parent::beforeFilter(); $this->User = ClassRegistry::init('AppUser'); + $this->set('model', 'AppUser'); } You can overwrite the render() method to fall back to the plugin views in the case you want to use some of them @@ -103,6 +105,8 @@ You can overwrite the render() method to fall back to the plugin views in the ca return parent::render($view, $layout); } +Note: Depending on the CakePHP version you are using, you might need to bring a copy of the Views used in the plugin to your AppUsers view directory + ### Overwriting the default auth settings provided by the plugin To use the basics the plugin already offers but changing some of the settings overwrite the _setupAuth() method in the extending controller.