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..467c0a705 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,6 +72,10 @@ public function changePassword() } else { $user = $this->getUsersTable()->changePassword($user); 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')); return $this->redirect($redirect); diff --git a/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php b/tests/TestCase/Controller/Traits/PasswordManagementTraitTest.php index a47d91e57..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 CakeDC\Users\Test\TestCase\Controller\Traits\BaseTraitTest; use Cake\Auth\PasswordHasherFactory; use Cake\Core\Configure; +use Cake\Event\Event; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; @@ -27,7 +27,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 +93,41 @@ 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 *