opensupports/server/controllers/user/send-recover-password.php

92 lines
2.4 KiB
PHP
Raw Normal View History

<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
2017-04-16 07:35:04 +02:00
/**
* @api {post} /user/send-recover-password Send password recovery
* @apiVersion 4.10.0
2017-04-16 07:35:04 +02:00
*
* @apiName Send password recovery
2017-04-16 07:35:04 +02:00
*
* @apiGroup User
*
* @apiDescription This path sends a token to the email of the user/staff to change his password.
2017-04-16 07:35:04 +02:00
*
* @apiPermission any
2017-04-16 07:35:04 +02:00
*
* @apiParam {String} email The email of the user/staff who forgot the password.
* @apiParam {Boolean} staff Indicates if the user is a staff member.
2017-04-16 07:35:04 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiUse INVALID_EMAIL
2017-04-16 07:35:04 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiSuccess {Object} data Empty object.
2017-04-16 07:35:04 +02:00
*
*/
class SendRecoverPasswordController extends Controller {
2016-07-22 09:44:55 +02:00
const PATH = '/send-recover-password';
const METHOD = 'POST';
private $token;
private $user;
private $staff;
public function validations() {
return [
'permission' => 'any',
'requestData' => [
'email' => [
'validation' => DataValidator::oneOf(
DataValidator::email()->userEmail(),
DataValidator::email()->staffEmail()
),
'error' => ERRORS::INVALID_EMAIL
]
]
];
}
public function handler() {
$this->staff = Controller::request('staff');
$email = Controller::request('email');
if($this->staff){
2019-10-29 22:23:20 +01:00
$this->user = Staff::getUser($email, 'email');
} else {
$this->user = User::getUser($email, 'email');
}
if(!$this->user->isNull()) {
$this->token = Hashing::generateRandomToken();
$recoverPassword = new RecoverPassword();
$recoverPassword->setProperties(array(
'email' => $email,
'token' => $this->token,
'staff' => !!$this->staff
));
$recoverPassword->store();
$this->sendEmail();
Response::respondSuccess();
} else {
2018-11-20 23:41:00 +01:00
throw new RequestException(ERRORS::INVALID_EMAIL);
}
}
public function sendEmail() {
$mailSender = MailSender::getInstance();
$mailSender->setTemplate(MailTemplate::PASSWORD_FORGOT, [
'to' => $this->user->email,
'name' => $this->user->name,
2017-07-11 08:39:17 +02:00
'url' => Setting::getSetting('url')->getValue(),
'token' => $this->token
]);
$mailSender->send();
}
}