Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #652, #683 #685

Merged
merged 1 commit into from
Apr 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Controller/Traits/PasswordManagementTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ public function requestResetPassword()
'expiration' => Configure::read('Users.Token.expiration'),
'checkActive' => false,
'sendEmail' => true,
'ensureActive' => Configure::read('Users.Registration.ensureActive')
'ensureActive' => Configure::read('Users.Registration.ensureActive'),
'type' => 'password'
]);
if ($resetUser) {
$msg = __d('CakeDC/Users', 'Please check your email to continue with password reset process');
Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Traits/UserValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function resendTokenValidation()
'expiration' => Configure::read('Users.Token.expiration'),
'checkActive' => true,
'sendEmail' => true,
'emailTemplate' => 'CakeDC/Users.validation'
'type' => 'email'
])) {
$this->Flash->success(__d(
'CakeDC/Users',
Expand Down
25 changes: 23 additions & 2 deletions src/Model/Behavior/PasswordBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,14 @@ public function resetToken($reference, array $options = [])
$user->updateToken($expiration);
$saveResult = $this->_table->save($user);
if (Hash::get($options, 'sendEmail')) {
$this->sendResetPasswordEmail($user);
switch (Hash::get($options, 'type')) {
case 'email':
$this->_sendValidationEmail($user);
break;
case 'password':
$this->_sendResetPasswordEmail($user);
break;
}
}

return $saveResult;
Expand All @@ -82,13 +89,27 @@ public function resetToken($reference, array $options = [])
* @param EntityInterface $user user
* @return void
*/
protected function sendResetPasswordEmail($user)
protected function _sendResetPasswordEmail($user)
{
$this
->getMailer(Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users')
->send('resetPassword', [$user]);
}

/**
* Wrapper for mailer
*
* @param EntityInterface $user user
* @return void
*/
protected function _sendValidationEmail($user)
{
$mailer = Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users';
$this
->getMailer($mailer)
->send('validation', [$user]);
}

/**
* Get the user by email or username
*
Expand Down
21 changes: 0 additions & 21 deletions src/Model/Behavior/RegisterBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,6 @@ public function getRegisterValidators($options)
return $validator;
}

/**
* @param EntityInterface $user User information
* @param array $options ['tokenExpiration]
* @return bool|EntityInterface
*/
public function resendValidationEmail($user, $options)
{
if ($user->active) {
throw new UserAlreadyActiveException(__d('CakeDC/Users', "User account already validated"));
}

$tokenExpiration = Hash::get($options, 'token_expiration');
$user = $this->_updateActive($user, true, $tokenExpiration);
$userSaved = $this->_table->saveOrFail($user);
if ($userSaved) {
$this->_sendValidationEmail($userSaved);
}

return $userSaved;
}

/**
* Wrapper for mailer
*
Expand Down
14 changes: 8 additions & 6 deletions tests/TestCase/Model/Behavior/PasswordBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function setUp()
parent::setUp();
$this->table = TableRegistry::getTableLocator()->get('CakeDC/Users.Users');
$this->Behavior = $this->getMockBuilder('CakeDC\Users\Model\Behavior\PasswordBehavior')
->setMethods(['sendResetPasswordEmail'])
->setMethods(['_sendResetPasswordEmail'])
->setConstructorArgs([$this->table])
->getMock();
Email::setConfigTransport('test', [
Expand Down Expand Up @@ -79,7 +79,7 @@ public function testResetToken()
$user = $this->table->findByUsername('user-1')->first();
$token = $user->token;
$this->Behavior->expects($this->never())
->method('sendResetPasswordEmail')
->method('_sendResetPasswordEmail')
->with($user);
$result = $this->Behavior->resetToken('user-1', [
'expiration' => 3600,
Expand All @@ -100,11 +100,12 @@ public function testResetTokenSendEmail()
$token = $user->token;
$tokenExpires = $user->token_expires;
$this->Behavior->expects($this->once())
->method('sendResetPasswordEmail');
->method('_sendResetPasswordEmail');
$result = $this->Behavior->resetToken('user-1', [
'expiration' => 3600,
'checkActive' => true,
'sendEmail' => true
'sendEmail' => true,
'type' => 'password'
]);
$this->assertNotEquals($token, $result->token);
$this->assertNotEquals($tokenExpires, $result->token_expires);
Expand Down Expand Up @@ -158,7 +159,7 @@ public function testResetTokenUserAlreadyActive()
$this->table->expects($this->never())
->method('save');
$this->Behavior->expects($this->never())
->method('sendResetPasswordEmail');
->method('_sendResetPasswordEmail');
$this->Behavior->resetToken('user-4', [
'expiration' => 3600,
'checkActive' => true,
Expand Down Expand Up @@ -229,7 +230,8 @@ public function testEmailOverride()
$this->Behavior->resetToken('user-1', [
'expiration' => 3600,
'checkActive' => true,
'sendEmail' => true
'sendEmail' => true,
'type' => 'password'
]);
}
}
48 changes: 0 additions & 48 deletions tests/TestCase/Model/Behavior/RegisterBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,52 +317,4 @@ public function testRegisterUsingCustomRole()
]);
$this->assertSame('emperor', $result['role']);
}

/**
* Test resendValidationEmail method
*
* @return void
*/
public function testResendValidationEmail()
{
$user = [
'username' => 'testuser',
'email' => 'testuser@test.com',
'password' => 'password',
'password_confirm' => 'password',
'first_name' => 'test',
'last_name' => 'user',
'tos' => 1
];
$result = $this->Table->register($this->Table->newEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]);
$this->assertFalse($result->active);
$originalExpiration = $result->token_expires;
$updatedResult = $this->Table->resendValidationEmail($result, ['token_expiration' => 4000]);
$this->assertNotEmpty($updatedResult);
$this->assertFalse($updatedResult->active);
$newExpiration = $updatedResult->token_expires;
$this->assertNotEquals($originalExpiration, $newExpiration);
}

/**
* Test resendValidationEmail method throw exception on active user
*
* @return void
*/
public function testResendValidationEmailThrows()
{
$user = [
'username' => 'testuser',
'email' => 'testuser@test.com',
'password' => 'password',
'password_confirm' => 'password',
'first_name' => 'test',
'last_name' => 'user',
'tos' => 1
];
$result = $this->Table->register($this->Table->newEntity(), $user, ['token_expiration' => 3600, 'validate_email' => 1]);
$activeUser = $this->Table->activateUser($result);
$this->expectException(UserAlreadyActiveException::class);
$updatedResult = $this->Table->resendValidationEmail($activeUser, ['token_expiration' => 4000]);
}
}