2016-07-20 23:38:20 +02:00
|
|
|
<?php
|
2016-07-21 01:39:36 +02:00
|
|
|
use Respect\Validation\Validator as DataValidator;
|
2016-08-04 21:01:24 +02:00
|
|
|
DataValidator::with('CustomValidations', true);
|
2016-07-20 23:38:20 +02:00
|
|
|
|
2017-04-16 07:35:04 +02:00
|
|
|
/**
|
|
|
|
* @api {post} /user/send-recover-password send a token to the email of the user to change his password
|
|
|
|
*
|
|
|
|
* @apiName send recover password
|
|
|
|
*
|
|
|
|
* @apiGroup User
|
|
|
|
*
|
2017-04-17 04:59:11 +02:00
|
|
|
* @apiDescription This path send a token to the email of the user to change his password.
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
|
|
|
* @apiPermission Any
|
|
|
|
*
|
2017-04-17 04:59:11 +02:00
|
|
|
* @apiParam {string} email The email of the user who forgot the password.
|
2017-04-16 07:35:04 +02:00
|
|
|
*
|
|
|
|
* @apiError {String} message
|
|
|
|
*
|
|
|
|
* @apiSuccess {Object} data
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-07-20 23:38:20 +02:00
|
|
|
class SendRecoverPasswordController extends Controller {
|
2016-07-22 09:44:55 +02:00
|
|
|
const PATH = '/send-recover-password';
|
2017-02-08 19:09:15 +01:00
|
|
|
const METHOD = 'POST';
|
2016-07-20 23:38:20 +02:00
|
|
|
|
2016-08-16 01:15:09 +02:00
|
|
|
private $token;
|
|
|
|
private $user;
|
|
|
|
|
2016-07-20 23:38:20 +02:00
|
|
|
public function validations() {
|
|
|
|
return [
|
|
|
|
'permission' => 'any',
|
2016-07-21 01:39:36 +02:00
|
|
|
'requestData' => [
|
|
|
|
'email' => [
|
2016-08-04 21:01:24 +02:00
|
|
|
'validation' => DataValidator::email()->userEmail(),
|
2016-07-21 01:39:36 +02:00
|
|
|
'error' => ERRORS::INVALID_EMAIL
|
|
|
|
]
|
|
|
|
]
|
2016-07-20 23:38:20 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function handler() {
|
2017-01-16 20:07:53 +01:00
|
|
|
if(!Controller::isUserSystemEnabled()) {
|
|
|
|
throw new Exception(ERRORS::USER_SYSTEM_DISABLED);
|
|
|
|
}
|
|
|
|
|
2016-07-21 01:39:36 +02:00
|
|
|
$email = Controller::request('email');
|
2016-08-16 01:15:09 +02:00
|
|
|
$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
|
|
|
|
));
|
|
|
|
$recoverPassword->store();
|
|
|
|
|
|
|
|
$this->sendEmail();
|
|
|
|
|
|
|
|
Response::respondSuccess();
|
|
|
|
} else {
|
|
|
|
Response::respondError(ERRORS::INVALID_EMAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-07-20 23:38:20 +02:00
|
|
|
|
2016-08-16 01:15:09 +02:00
|
|
|
public function sendEmail() {
|
|
|
|
$mailSender = new MailSender();
|
2016-07-20 23:38:20 +02:00
|
|
|
|
2016-08-16 01:15:09 +02:00
|
|
|
$mailSender->setTemplate(MailTemplate::PASSWORD_FORGOT, [
|
|
|
|
'to' => $this->user->email,
|
|
|
|
'name' => $this->user->name,
|
|
|
|
'token' => $this->token
|
|
|
|
]);
|
2016-07-20 23:38:20 +02:00
|
|
|
|
2016-08-16 01:15:09 +02:00
|
|
|
$mailSender->send();
|
2016-07-20 23:38:20 +02:00
|
|
|
}
|
2016-07-21 01:39:36 +02:00
|
|
|
}
|