5use Ajax\semantic\html\collections\form\HtmlForm;
30 abstract protected function authLoadView($viewName, $vars = [ ]):void;
32 abstract protected function useAjax():bool;
44 abstract public function info($force =
null);
124 return
'Forgot your password?';
132 return new \DateInterval(
'PT30M');
141 return new
AuthTokens(self::$TOKENS_RECOVERY_ACCOUNT,10,$this->accountRecoveryDuration()->s,true);
177 $href=$this->getBaseUrl().
'/recoveryInit';
178 $target=$this->_getBodySelector();
179 $caption=$this->recoveryAccountCaption();
180 return "<a class='_recovery' href='$href' data-target='$target'>$caption</a>";
184 $fMessage =
new FlashMessage(
'Enter the email associated with your account to receive a password reset link.',
'Account recovery',
'info',
'user' );
185 $this->recoveryInitMessage ( $fMessage );
186 $message = $this->fMessage ( $fMessage );
187 if($this->useAjax()){
188 $frm=$this->jquery->semantic()->htmlForm(
'frm-account-recovery');
189 $frm->addExtraFieldRules(
'email',[
'empty',
'email']);
190 $frm->setValidationParams([
'inline'=>
true,
'on'=>
'blur']);
192 $this->authLoadView ( $this->_getFiles ()->getViewInitRecovery(), [
'_message' => $message,
'submitURL' => $this->getBaseUrl ().
'/recoveryInfo',
'bodySelector' => $this->_getBodySelector()] );
198 #[\Ubiquity\attributes\items\router\Post]
200 if(URequest::isPost()){
201 if($this->isValidEmailForRecovery($email=URequest::filterPost(
'email',FILTER_VALIDATE_EMAIL))) {
202 $this->prepareEmailAccountRecovery($email);
203 $fMessage =
new FlashMessage (sprintf(
'A password reset email has been sent to <b>%s</b>.<br>You can only use this link temporarily, from the same machine, on this browser.',$email),
'Account recovery',
'success',
'email');
204 $this->recoveryEmailSendMessage($fMessage);
206 $fMessage =
new FlashMessage (sprintf(
'No account is associated with the email address <b>%s</b>.<br><a href="%s" data-target="%s">Try again.</a>.',$email,$this->getBaseUrl().
'/recoveryInit',$this->_getBodySelector()),
'Account recovery',
'error',
'user');
207 $this->recoveryEmailErrorMessage($fMessage);
209 echo $this->fMessage ( $fMessage );
213 public function recovery(
string $key,
string $hashMail) {
214 $tokens = $this->getAuthTokensAccountRecovery();
215 if ($tokens->exists($key)) {
216 if (!$tokens->expired($key)) {
217 $data = $tokens->fetch($key);
218 if(\is_array($data)) {
219 $email = $data[
'email'];
220 if (\md5($email) === $hashMail && $this->validateEmail($email)) {
221 $fMessage =
new FlashMessage (
"Enter a new password associated to the account <b>$email</b>.",
'Account recovery',
'success',
'user');
222 $this->emailAccountRecoverySuccess($fMessage);
223 $message=$this->fMessage($fMessage);
224 if($this->useAjax()) {
225 $frm = $this->_addFrmAjaxBehavior(
'frm-account-recovery');
226 $passwordInputName = $this->_getPasswordInputName();
227 $frm->addExtraFieldRules($passwordInputName .
'-conf', [
'empty',
"match[$passwordInputName]"]);
229 $this->authLoadView ( $this->_getFiles ()->getViewRecovery(), [
'key'=>$key,
'email'=>$email,
'_message' => $message,
'submitURL' => $this->getBaseUrl ().
'/recoverySubmit',
'bodySelector' => $this->_getBodySelector(),
'passwordInputName' => $this->_getPasswordInputName (),
'passwordLabel' => $this->passwordLabel (),
'passwordConfLabel'=>$this->passwordConfLabel()] );
233 $msg =
'This recovery link was not generated on this device!';
235 $msg =
'This recovery link is no longer active!';
238 $fMessage =
new FlashMessage ($msg ??
'This account recovery link is not valid!',
'Account recovery',
'error',
'user');
239 $this->emailAccountRecoveryError($fMessage);
240 echo $this->fMessage($fMessage);
244 $duration=$this->accountRecoveryDuration();
245 $tokens=$this->getAuthTokensAccountRecovery();
247 $dExpire=$d->add($duration);
248 $key=$tokens->store([
'email'=>$email]);
249 return [
'url'=>$key.
'/'.\md5($email),
'expire'=>$dExpire];
253 $data=$this->generateEmailAccountRecoveryUrl($email);
254 $validationURL=$this->getBaseUrl().
'/recovery/'.$data[
'url'];
255 $this->_sendEmailAccountRecovery($email, $validationURL,UDateTime::elapsed($data[
'expire']));
261 #[\Ubiquity\attributes\items\router\Post]
263 if(URequest::isPost() && URequest::has(
'key')){
265 $msg=
'This account recovery link is invalid!';
266 $tokens = $this->getAuthTokensAccountRecovery();
267 $key=URequest::post(
'key');
268 if ($tokens->exists($key)) {
269 if(!$tokens->expired($key)){
270 $data=$tokens->fetch($key);
271 $email=$data[
'email'];
272 if($email===URequest::post(
'email')){
273 if($this->passwordResetAction($email,URequest::password_hash(
'password'))){
274 $fMessage =
new FlashMessage (
"Your password has been updated correctly for the account associated with <b>$email</b>.",
'Account recovery',
'success',
'user');
275 $this->resetPasswordSuccessMessage($fMessage);
276 echo $this->info(
true);
279 $msg=
'An error occurs when updating your password!';
283 $msg=
'This account recovery link is expired!';
285 $tokens->remove($key);
288 $fMessage =
new FlashMessage ($msg,
'Account recovery',
'error',
'user');
289 $this->resetPasswordErrorMessage($fMessage);
291 echo $this->fMessage($fMessage);
Ubiquity\controllers\auth$AuthFiles This class is part of Ubiquity.
Ubiquity\controllers\auth\traits$AuthAccountRecoveryTrait This class is part of Ubiquity.
fMessage(FlashMessage $fMessage, $id=null)
emailAccountRecoveryError(FlashMessage $fMessage)
To override Displayed when the account recovery link is not valid.
recoveryEmailSendMessage(FlashMessage $fMessage)
To override Displayed when email is sent for a recovery account operation.
isValidEmailForRecovery(string $email)
To override Checks if a valid account matches this email.
validateEmail(string $mail)
resetPasswordSuccessMessage(FlashMessage $fMessage)
To override Displayed when a new password is set with recovery account.
authLoadView($viewName, $vars=[])
recoveryInitMessage(FlashMessage $fMessage)
To override Displayed when an account recovery operation is initiated.
recoveryAccountCaption()
Returns the recovery account link caption.
prepareEmailAccountRecovery(string $email)
static string $TOKENS_RECOVERY_ACCOUNT
generateEmailAccountRecoveryUrl($email)
recoveryEmailErrorMessage(FlashMessage $fMessage)
To override Displayed when email is not associated with an existing account.
_sendEmailAccountRecovery(string $email, string $validationURL, string $expire)
Sends an email for account recovery (password reset).
recovery(string $key, string $hashMail)
emailAccountRecoverySuccess(FlashMessage $fMessage)
To override Displayed when the account recovery link is valid.
getAuthTokensAccountRecovery()
To override Returns the AuthTokens instance used for tokens generation for a recovery account.
accountRecoveryDuration()
Returns the default validity duration for an email account recovery.
passwordResetAction(string $email, string $newPasswordHash)
To override Changes the active password associated with the account corresponding to this email.
resetPasswordErrorMessage(FlashMessage $fMessage)
To override Displayed when an error occurs when a new password is set with recovery account.
DateTime utilities Ubiquity\utils\base$UDateTime This class is part of Ubiquity.
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.