forked from civicrm/civicrm-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
standaloneusers - split User.PasswordResetEmail into public and priva…
…te actions
- Loading branch information
Showing
10 changed files
with
234 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
ext/standaloneusers/Civi/Api4/Action/User/SendPasswordResetEmail.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
namespace Civi\Api4\Action\User; | ||
|
||
// @todo | ||
// URL is (a) just theh path in the emails. | ||
// clicking button on form with proper token does nothing. | ||
// should redirect to login on success | ||
|
||
use Civi; | ||
use Civi\Api4\Generic\BasicBatchAction; | ||
use Civi\Api4\MessageTemplate; | ||
use CRM_Standaloneusers_WorkflowMessage_PasswordReset; | ||
|
||
/** | ||
* @class API_Exception | ||
*/ | ||
|
||
/** | ||
* This is designed to be an internal API | ||
* | ||
* @method static setIdentifier(string $identifier) | ||
* @method static setTimeout(int $minutes) | ||
*/ | ||
class SendPasswordResetEmail extends BasicBatchAction { | ||
|
||
/** | ||
* Timeout for the reset token in minutes | ||
* | ||
* @var int | ||
*/ | ||
protected $timeout = 60; | ||
|
||
/** | ||
* @inheritdoc | ||
* | ||
* Data we need from the User record | ||
*/ | ||
protected function getSelect() { | ||
|
||
return ['id', 'username', 'uf_name', 'contact_id']; | ||
} | ||
|
||
public function doTask($user) { | ||
// (Re)generate token and store on User. | ||
$token = PasswordReset::updateToken($user['id'], $this->timeout); | ||
|
||
$workflowMessage = self::preparePasswordResetWorkflow($user, $token); | ||
if ($workflowMessage) { | ||
// The template_params are used in the template like {$resetUrlHtml} and {$resetUrlHtml} {$usernamePlaintext} {$usernameHtml} | ||
try { | ||
[$sent, /*$subject, $text, $html*/] = $workflowMessage->sendTemplate(); | ||
if (!$sent) { | ||
throw new \RuntimeException("sendTemplate() returned unsent."); | ||
} | ||
Civi::log()->info("Successfully sent password reset to user {$user['id']} ({$user['username']}) to {$user['uf_name']}"); | ||
} | ||
catch (\Exception $e) { | ||
Civi::log()->error("Failed to send password reset to user {$user['id']} ({$user['username']}) to {$user['uf_name']}"); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Prepare a password reset workflow email for a user | ||
* | ||
* Includes some checks that we have all the necessary pieces | ||
* in place | ||
* | ||
* @internal (only public for use in SecurityTest) | ||
* | ||
* @return \CRM_Standaloneusers_WorkflowMessage_PasswordReset|null | ||
*/ | ||
public static function preparePasswordResetWorkflow(array $user, string $token): ?CRM_Standaloneusers_WorkflowMessage_PasswordReset { | ||
// Find the message template | ||
$tplID = MessageTemplate::get(FALSE) | ||
->setSelect(['id']) | ||
->addWhere('workflow_name', '=', 'password_reset') | ||
->addWhere('is_default', '=', TRUE) | ||
->addWhere('is_reserved', '=', FALSE) | ||
->addWhere('is_active', '=', TRUE) | ||
->execute()->first()['id']; | ||
if (!$tplID) { | ||
// Some sites may deliberately disable this, but it's unusual, so leave a notice in the log. | ||
Civi::log()->notice("There is no active, default password_reset message template, which has prevented emailing a reset to {username}", ['username' => $user['username']]); | ||
return NULL; | ||
} | ||
if (!filter_var($user['uf_name'] ?? '', \FILTER_VALIDATE_EMAIL)) { | ||
Civi::log()->warning("User {$user['id']} has an invalid email. Failed to send password reset."); | ||
return NULL; | ||
} | ||
|
||
// get the required params from the user record | ||
$username = $user['username']; | ||
$email = $user['uf_name']; | ||
$contactId = $user['contact_id']; | ||
|
||
// The template_params are used in the template like {$resetUrlHtml} and {$resetUrlHtml} {$usernamePlaintext} {$usernameHtml} | ||
[$domainFromName, $domainFromEmail] = \CRM_Core_BAO_Domain::getNameAndEmail(TRUE); | ||
$workflowMessage = (new CRM_Standaloneusers_WorkflowMessage_PasswordReset()) | ||
->setRequiredParams($username, $email, $contactId, $token) | ||
->setFrom("\"$domainFromName\" <$domainFromEmail>"); | ||
|
||
return $workflowMessage; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.