diff --git a/server/controllers/user/recover-password.php b/server/controllers/user/recover-password.php index 7c79b239..c5cec698 100644 --- a/server/controllers/user/recover-password.php +++ b/server/controllers/user/recover-password.php @@ -8,6 +8,7 @@ class RecoverPasswordController extends Controller { private $email; private $token; private $password; + private $user; public function validations() { return [ @@ -37,19 +38,31 @@ class RecoverPasswordController extends Controller { } public function changePassword() { $recoverPassword = RecoverPassword::getDataStore($this->token, 'token'); - $user = User::getDataStore($this->email, 'email'); + $this->user = User::getDataStore($this->email, 'email'); - if (!$recoverPassword->isNull() && !$user->isNull()) { + if (!$recoverPassword->isNull() && !$this->user->isNull()) { $recoverPassword->delete(); - $user->setProperties([ + $this->user->setProperties([ 'password' => Hashing::hashPassword($this->password) ]); - $user->store(); + $this->user->store(); + + $this->sendMail(); Response::respondSuccess(); } else { Response::respondError(ERRORS::NO_PERMISSION); } } + public function sendMail() { + $mailSender = new MailSender(); + + $mailSender->setTemplate(MailTemplate::PASSWORD_RECOVERED, [ + 'to' => $this->user->email, + 'name' => $this->user->name, + ]); + + $mailSender->send(); + } } diff --git a/server/controllers/user/send-recover-password.php b/server/controllers/user/send-recover-password.php index bfb41d58..e92d1ae6 100644 --- a/server/controllers/user/send-recover-password.php +++ b/server/controllers/user/send-recover-password.php @@ -5,6 +5,9 @@ DataValidator::with('CustomValidations', true); class SendRecoverPasswordController extends Controller { const PATH = '/send-recover-password'; + private $token; + private $user; + public function validations() { return [ 'permission' => 'any', @@ -19,17 +22,36 @@ class SendRecoverPasswordController extends Controller { public function handler() { $email = Controller::request('email'); + $this->user = User::getUser($email,'email'); + + if(!$this->user->isNull()) { + $this->token = Hashing::generateRandomToken(); - $token = Hashing::generateRandomToken(); + $recoverPassword = new RecoverPassword(); + $recoverPassword->setProperties(array( + 'email' => $email, + 'token' => $this->token + )); + $recoverPassword->store(); - $recoverPassword = new RecoverPassword(); - $recoverPassword->setProperties(array( - 'email' => $email, - 'token' => $token - )); - $recoverPassword->store(); + $this->sendEmail(); - Response::respondSuccess(); - //TODO: mandar mail con token + Response::respondSuccess(); + } else { + Response::respondError(ERRORS::INVALID_EMAIL); + } + + } + + public function sendEmail() { + $mailSender = new MailSender(); + + $mailSender->setTemplate(MailTemplate::PASSWORD_FORGOT, [ + 'to' => $this->user->email, + 'name' => $this->user->name, + 'token' => $this->token + ]); + + $mailSender->send(); } } diff --git a/server/data/InitialMails.php b/server/data/InitialMails.php index 87ba71a6..899d9af3 100644 --- a/server/data/InitialMails.php +++ b/server/data/InitialMails.php @@ -32,7 +32,27 @@ class InitialMails { 'subject' => 'Tu correo electronico a sido cambiada - OpenSupports', 'body' => file_get_contents('data/mail-templates/user-edit-email-es.html') ] - ] + ], + 'PASSWORD_FORGOT' => [ + 'en' => [ + 'subject' => 'forgotten password - OpenSupports', + 'body' => file_get_contents('data/mail-templates/user-password-forgot-en.html') + ], + 'es' => [ + 'subject' => 'Contraseña olvidada - OpenSupports', + 'body' => file_get_contents('data/mail-templates/user-password-forgot-es.html') + ] + ], + 'PASSWORD_RECOVERED' => [ + 'en' => [ + 'subject' => 'Recover Password - OpenSupports', + 'body' => file_get_contents('data/mail-templates/user-recovered-password-en.html') + ], + 'es' => [ + 'subject' => 'Recuperación de contraseña - OpenSupports', + 'body' => file_get_contents('data/mail-templates/user-recovered-password-es.html') + ] + ], ]; } } \ No newline at end of file diff --git a/server/data/mail-templates/user-password-forgot-en.html b/server/data/mail-templates/user-password-forgot-en.html new file mode 100644 index 00000000..05708ec6 --- /dev/null +++ b/server/data/mail-templates/user-password-forgot-en.html @@ -0,0 +1,3 @@ +