Skip to content

Commit

Permalink
feature to build validation url. Useful for api driven setups
Browse files Browse the repository at this point in the history
  • Loading branch information
skie committed Oct 18, 2023
1 parent 39b83c7 commit 3f8ce19
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
31 changes: 23 additions & 8 deletions src/Mailer/UsersMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@ class UsersMailer extends Mailer
* @param \Cake\Datasource\EntityInterface $user User entity
* @return void
*/
protected function validation(EntityInterface $user)
protected function validation(EntityInterface $user, $options = [])
{
$firstName = isset($user['first_name']) ? $user['first_name'] . ', ' : '';
// un-hide the token to be able to send it in the email content
$user->setHidden(['password', 'token_expires', 'api_token']);
$subject = __d('cake_d_c/users', 'Your account validation link');
$viewVars = [
'activationUrl' => UsersUrl::actionUrl('validateEmail', [

if (isset($options['linkGenerator']) && is_callable($options['linkGenerator'])) {
$generator = $options['linkGenerator'];
$link = $generator($user['token']);
} else {
$link = UsersUrl::actionUrl('validateEmail', [
'_full' => true,
$user['token'],
]),
]);
}

$viewVars = [
'activationUrl' => $link,
] + $user->toArray();

$this
Expand All @@ -57,18 +65,25 @@ protected function validation(EntityInterface $user)
* @param \Cake\Datasource\EntityInterface $user User entity
* @return void
*/
protected function resetPassword(EntityInterface $user)
protected function resetPassword(EntityInterface $user, $options = [])
{
$firstName = isset($user['first_name']) ? $user['first_name'] . ', ' : '';
$subject = __d('cake_d_c/users', '{0}Your reset password link', $firstName);
// un-hide the token to be able to send it in the email content
$user->setHidden(['password', 'token_expires', 'api_token']);

$viewVars = [
'activationUrl' => UsersUrl::actionUrl('resetPassword', [
if (isset($options['linkGenerator']) && is_callable($options['linkGenerator'])) {
$generator = $options['linkGenerator'];
$link = $generator($user['token']);
} else {
$link = UsersUrl::actionUrl('resetPassword', [
'_full' => true,
$user['token'],
]),
]);
}

$viewVars = [
'activationUrl' => $link,
] + $user->toArray();

$this
Expand Down
18 changes: 12 additions & 6 deletions src/Model/Behavior/PasswordBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@ public function resetToken($reference, array $options = [])
$user->updateToken($expiration);
$saveResult = $this->_table->save($user);
if ($options['sendEmail'] ?? false) {
$emailOptions = [];
if (isset($options['linkGenerator']) && is_callable($options['linkGenerator'])) {
$emailOptions['linkGenerator'] = $options['linkGenerator'];
}
switch ($options['type'] ?? null) {
case 'email':
$this->_sendValidationEmail($user);
$this->_sendValidationEmail($user, $emailOptions);
break;
case 'password':
$this->_sendResetPasswordEmail($user);
$this->_sendResetPasswordEmail($user, $emailOptions);
break;
}
}
Expand All @@ -85,27 +89,29 @@ public function resetToken($reference, array $options = [])
* Send the reset password related email link
*
* @param \Cake\Datasource\EntityInterface $user user
* @param array $options Options.
* @return void
*/
protected function _sendResetPasswordEmail($user)
protected function _sendResetPasswordEmail($user, $options = [])
{
$this
->getMailer(Configure::read('Users.Email.mailerClass') ?: 'CakeDC/Users.Users')
->send('resetPassword', [$user]);
->send('resetPassword', [$user, $options]);
}

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

/**
Expand Down

0 comments on commit 3f8ce19

Please sign in to comment.