From 228d5469a6e260b9bbf607acb7b17acfaf55269b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20M=2E=20Gonz=C3=A1lez=20Mart=C3=ADn?= Date: Mon, 22 Jan 2018 09:39:35 +0000 Subject: [PATCH 1/5] Update Extending-the-Plugin.md Improve docs thanks to @llincoln --- Docs/Documentation/Extending-the-Plugin.md | 2 ++ src/Controller/Component/UsersAuthComponent.php | 1 + src/Controller/Traits/PasswordManagementTrait.php | 8 ++++++-- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Docs/Documentation/Extending-the-Plugin.md b/Docs/Documentation/Extending-the-Plugin.md index 01a5b5b99..3da0705d6 100644 --- a/Docs/Documentation/Extending-the-Plugin.md +++ b/Docs/Documentation/Extending-the-Plugin.md @@ -180,6 +180,8 @@ Updating the Templates Use the standard CakePHP conventions to override Plugin views using your application views http://book.cakephp.org/3.0/en/plugins.html#overriding-plugin-templates-from-inside-your-application +`{project_dir}/src/Template/Plugin/CakeDC/Users/Users/{templates_in_here}` + Updating the Emails ------------------- diff --git a/src/Controller/Component/UsersAuthComponent.php b/src/Controller/Component/UsersAuthComponent.php index 9944abfe0..f32dff7ee 100644 --- a/src/Controller/Component/UsersAuthComponent.php +++ b/src/Controller/Component/UsersAuthComponent.php @@ -33,6 +33,7 @@ class UsersAuthComponent extends Component const EVENT_BEFORE_LOGOUT = 'Users.Component.UsersAuth.beforeLogout'; const EVENT_AFTER_LOGOUT = 'Users.Component.UsersAuth.afterLogout'; const EVENT_BEFORE_SOCIAL_LOGIN_USER_CREATE = 'Users.Component.UsersAuth.beforeSocialLoginUserCreate'; + const EVENT_AFTER_CHANGE_PASSWORD = 'Users.Component.UsersAuth.afterResetPassword'; /** * Initialize method, setup Auth if not already done passing the $config provided and diff --git a/src/Controller/Traits/PasswordManagementTrait.php b/src/Controller/Traits/PasswordManagementTrait.php index 653498906..a2f04d480 100644 --- a/src/Controller/Traits/PasswordManagementTrait.php +++ b/src/Controller/Traits/PasswordManagementTrait.php @@ -11,6 +11,7 @@ namespace CakeDC\Users\Controller\Traits; +use CakeDC\Users\Controller\Component\UsersAuthComponent; use CakeDC\Users\Exception\UserNotActiveException; use CakeDC\Users\Exception\UserNotFoundException; use CakeDC\Users\Exception\WrongPasswordException; @@ -71,8 +72,12 @@ public function changePassword() } else { $user = $this->getUsersTable()->changePassword($user); if ($user) { - $this->Flash->success(__d('CakeDC/Users', 'Password has been changed successfully')); + $event = $this->dispatchEvent(UsersAuthComponent::EVENT_AFTER_CHANGE_PASSWORD, ['user' => $user]); + if (is_array($event->result)) { + return $this->redirect($event->result); + } + $this->Flash->success(__d('CakeDC/Users', 'Password has been changed successfully')); return $this->redirect($redirect); } else { $this->Flash->error(__d('CakeDC/Users', 'Password could not be changed')); @@ -130,7 +135,6 @@ public function requestResetPassword() $msg = __d('CakeDC/Users', 'The password token could not be generated. Please try again'); $this->Flash->error($msg); } - return $this->redirect(['action' => 'login']); } catch (UserNotFoundException $exception) { $this->Flash->error(__d('CakeDC/Users', 'User {0} was not found', $reference)); From e0ee947a572303d6b116da7c9db5884ff4af27e2 Mon Sep 17 00:00:00 2001 From: Yelitza Parra Date: Wed, 11 Apr 2018 16:47:12 -0500 Subject: [PATCH 2/5] Adding after change password event --- src/Controller/Traits/PasswordManagementTrait.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Controller/Traits/PasswordManagementTrait.php b/src/Controller/Traits/PasswordManagementTrait.php index a2f04d480..96146c71f 100644 --- a/src/Controller/Traits/PasswordManagementTrait.php +++ b/src/Controller/Traits/PasswordManagementTrait.php @@ -73,11 +73,12 @@ public function changePassword() $user = $this->getUsersTable()->changePassword($user); if ($user) { $event = $this->dispatchEvent(UsersAuthComponent::EVENT_AFTER_CHANGE_PASSWORD, ['user' => $user]); - if (is_array($event->result)) { + if (!empty($event) && is_array($event->result)) { + return $this->redirect($event->result); } - $this->Flash->success(__d('CakeDC/Users', 'Password has been changed successfully')); + return $this->redirect($redirect); } else { $this->Flash->error(__d('CakeDC/Users', 'Password could not be changed')); From 0f79d15e3623b5e2383f15be75a4496a01969a0e Mon Sep 17 00:00:00 2001 From: Yelitza Parra Date: Wed, 11 Apr 2018 16:47:45 -0500 Subject: [PATCH 3/5] Adding unit test for after change password event --- .../Traits/PasswordManagementTraitTest.php | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php b/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php index a47d91e57..b246980f7 100644 --- a/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php +++ b/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php @@ -11,6 +11,7 @@ namespace CakeDC\Users\Test\TestCase\Controller\Traits; +use Cake\Event\Event; use CakeDC\Users\Test\TestCase\Controller\Traits\BaseTraitTest; use Cake\Auth\PasswordHasherFactory; use Cake\Core\Configure; @@ -27,7 +28,7 @@ class PasswordManagementTraitTest extends BaseTraitTest public function setUp() { $this->traitClassName = 'CakeDC\Users\Controller\Traits\PasswordManagementTrait'; - $this->traitMockMethods = ['set', 'redirect', 'validate', 'log']; + $this->traitMockMethods = ['set', 'redirect', 'validate', 'log', 'dispatchEvent']; $this->mockDefaultEmail = true; parent::setUp(); } @@ -93,6 +94,42 @@ public function testChangePasswordWithError() $this->Trait->changePassword(); } + /** + * test + * + * @return void + */ + public function testChangePasswordWithAfterChangeEvent() + { + $this->assertEquals('12345', $this->table->get('00000000-0000-0000-0000-000000000001')->password); + $this->_mockRequestPost(); + $this->_mockAuthLoggedIn(); + $this->_mockFlash(); + $this->Trait->request->expects($this->once()) + ->method('getData') + ->will($this->returnValue([ + 'password' => 'new', + 'password_confirm' => 'new', + ])); + $event = new Event('event'); + $event->result = [ + 'action' => 'newAction', + ]; + $this->Trait->expects($this->once()) + ->method('dispatchEvent') + ->will($this->returnValue($event)); + $this->Trait->expects($this->once()) + ->method('redirect') + ->with(['action' => 'newAction']); + $this->Trait->Flash->expects($this->any()) + ->method('success') + ->with('Password has been changed successfully'); + $this->Trait->changePassword(); + $hasher = PasswordHasherFactory::build('Default'); + $this->assertTrue($hasher->check('new', $this->table->get('00000000-0000-0000-0000-000000000001')->password)); + + } + /** * test * From 9bdd81d8335ba0fb5b1c5f8eb4f5380f2e0dddef Mon Sep 17 00:00:00 2001 From: Yelitza Parra Date: Wed, 11 Apr 2018 17:02:29 -0500 Subject: [PATCH 4/5] Fix cs --- .../TestCase/Controller/Traits/PasswordManagementTraitTest.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php b/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php index b246980f7..555c99386 100644 --- a/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php +++ b/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php @@ -12,7 +12,6 @@ namespace CakeDC\Users\Test\TestCase\Controller\Traits; use Cake\Event\Event; -use CakeDC\Users\Test\TestCase\Controller\Traits\BaseTraitTest; use Cake\Auth\PasswordHasherFactory; use Cake\Core\Configure; use Cake\ORM\TableRegistry; @@ -127,7 +126,6 @@ public function testChangePasswordWithAfterChangeEvent() $this->Trait->changePassword(); $hasher = PasswordHasherFactory::build('Default'); $this->assertTrue($hasher->check('new', $this->table->get('00000000-0000-0000-0000-000000000001')->password)); - } /** From c6e6458fcb9c96b39febe3a7804aaaeeb42dc909 Mon Sep 17 00:00:00 2001 From: Yelitza Parra Date: Wed, 11 Apr 2018 17:33:26 -0500 Subject: [PATCH 5/5] Fix cs --- src/Controller/Traits/PasswordManagementTrait.php | 2 +- .../TestCase/Controller/Traits/PasswordManagementTraitTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Controller/Traits/PasswordManagementTrait.php b/src/Controller/Traits/PasswordManagementTrait.php index 96146c71f..467c0a705 100644 --- a/src/Controller/Traits/PasswordManagementTrait.php +++ b/src/Controller/Traits/PasswordManagementTrait.php @@ -74,7 +74,6 @@ public function changePassword() if ($user) { $event = $this->dispatchEvent(UsersAuthComponent::EVENT_AFTER_CHANGE_PASSWORD, ['user' => $user]); if (!empty($event) && is_array($event->result)) { - return $this->redirect($event->result); } $this->Flash->success(__d('CakeDC/Users', 'Password has been changed successfully')); @@ -136,6 +135,7 @@ public function requestResetPassword() $msg = __d('CakeDC/Users', 'The password token could not be generated. Please try again'); $this->Flash->error($msg); } + return $this->redirect(['action' => 'login']); } catch (UserNotFoundException $exception) { $this->Flash->error(__d('CakeDC/Users', 'User {0} was not found', $reference)); diff --git a/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php b/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php index 555c99386..f4d292765 100644 --- a/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php +++ b/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php @@ -11,9 +11,9 @@ namespace CakeDC\Users\Test\TestCase\Controller\Traits; -use Cake\Event\Event; use Cake\Auth\PasswordHasherFactory; use Cake\Core\Configure; +use Cake\Event\Event; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase;