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

57 lines
1.5 KiB
PHP
Raw Normal View History

2016-07-20 06:38:34 +02:00
<?php
use Respect\Validation\Validator as DataValidator;
2016-07-20 06:38:34 +02:00
class RecoverPasswordController extends Controller {
2016-07-22 09:44:55 +02:00
const PATH = '/recover-password';
2016-07-20 06:38:34 +02:00
private $email;
private $token;
private $password;
public function validations() {
return [
'permission' => 'any',
'requestData' => [
'email' => [
'validation' => DataValidator::email() ,
'error' => ERRORS::INVALID_EMAIL
],
'password' => [
'validation' => DataValidator::length(5, 200),
'error' => ERRORS::INVALID_PASSWORD
]
]
2016-07-20 06:38:34 +02:00
];
}
public function handler(){
$this->requestData();
$this->changePassword();
}
public function requestData(){
$this->email = Controller::request('email');
$this->token = Controller::request('token');
$this->password = Controller::request('password');
}
public function changePassword(){
2016-07-22 09:44:55 +02:00
$recoverPassword = RecoverPassword::getDatastore($this->token, 'token');
$user = User::getDataStore($this->email, 'email');
2016-07-22 09:44:55 +02:00
if($recoverPassword && $user) {
$recoverPassword->trash();
2016-07-20 06:38:34 +02:00
2016-07-22 09:44:55 +02:00
$user->setProperties([
'password' => Hashing::hashPassword($this->password)
]);
2016-07-20 06:38:34 +02:00
2016-07-22 09:44:55 +02:00
$user->store();
Response::respondSuccess('password changed');
return;
2016-07-20 06:38:34 +02:00
}
Response::respondError(ERRORS::NO_PERMISSION);
2016-07-20 06:38:34 +02:00
}
}