<?php
namespace App\Controller;
use App\Controller\AppController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use App\Service\ApiCalls;
use App\Security\AppAuthenticator;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
use App\Security\PasswordRestoreAuthenticator;
use Symfony\Component\HttpFoundation\JsonResponse;
class ForgotPasswordController extends AppController
{
/*
* starting page and success on sending the link
*
*/
public function index(): Response
{
$textsRes = $this->ApiCalls->getPasswordAssistanceText();
if($textsRes[0]["apiReturnStatus"]===1){
$this->setData('texts', $textsRes[0]);
}
$check = $this->commonCalls(['getFooter','getHomeNav','getSearch']);
if(!$check){
return $check;
}
if($this->RedirectHandle!==NULL){
return $this->RedirectHandle;
}
if($this->Http->isMethod('POST')){
$res = $this->ApiCalls->forgotPassword($this->Http->request->get('userid'));
if($res[0]["apiReturnStatus"]===1){
$this->setData('success', TRUE);
if($res[0]["apiReturnMessage"]==="Success: password reset SMS sent"){
$this->setData('isSms', TRUE);
}
else{
$this->setData('isSms', FALSE);
}
}
else{
$this->setData('success', FALSE);
}
$this->setData('uiMsg', $res[0]["apiReturnMessage"]);
}
return $this->render('forgot_password/index.html.twig', $this->getData());
}
/*
* new password form page
* that is accessed after the link in the email is clicked
*
*/
public function restorePasswordAction(PasswordRestoreAuthenticator $PasswordRestoreAuthenticator): Response
{
$check = $this->commonCalls(['getFooter','getHomeNav','getSearch']);
if(!$check){
return $check;
}
if($this->RedirectHandle!==NULL){
return $this->RedirectHandle;
}
if(!$this->Http->getSession()->get('uiError')){
}
else{
$error = $this->Http->getSession()->get('uiError');
if(isset($error['message'])){
$this->setData('uiError', $this->Http->getSession()->get('uiError')['message']);
}
else{
$this->setData('uiError', $this->Http->getSession()->get('uiError'));
}
$this->Http->getSession()->remove('uiError');
}
return $this->render('forgot_password/restore_password_action.html.twig', $this->getData());
}
#response after the email or phone is provided
public function resetPassword(): Response
{
$isMobileFlag = FALSE;
if($this->Http->query->get('isMobile') && $this->Http->query->get('isMobile') === '1'){
$isMobileFlag = TRUE;
}
$res = $this->ApiCalls-> passwordAssistance($this->Http->request->get('userid'));
// var_dump($res);
if($res[0]["apiReturnStatus"]===1){
$ajaxMsg = [ 0 => [
'title'=> $res[0]["title"],
'html'=> $res[0]["html"],
'icon'=> $res[0]["icon"],
'footer'=> $res[0]["footer"],
'isAdmin'=> $res[0]["isAdmin"]
]];
if($res[0]["isAdmin"]){
$ajaxMsg['redirect'] = TRUE;
$ajaxMsg['url'] = 'https://'.$this->mode.'.learnertrack.net/user/password.asp';
$ajaxMsg['username'] = $res[0]["username"];
$ajaxMsg['firstpage'] = '/user/password.asp';
}
else{
if($isMobileFlag){
$ajaxMsg['redirect'] = TRUE;
$ajaxMsg['path'] = 'signin/code';
}
else{
$ajaxMsg['redirect'] = TRUE;
$ajaxMsg['path'] = 'signin';
}
}
$this->setData('password-restore', $ajaxMsg);
}
else{
// $msg = 'authentication failed, not authorized';
// $this->Http->getSession()->set('uiError', ['message'=>$msg]);
$ajaxMsg = [ 0 => [
'title'=> $res[0]["title"],
'html'=> $res[0]["html"],
'icon'=> $res[0]["icon"],
'footer'=> $res[0]["footer"],
'isAdmin'=> $res[0]["isAdmin"]
]];
$this->setData('password-restore', $ajaxMsg);
}
return new JsonResponse($this->getData());
}
#response after the email or phone is provided
public function resetPasswordFieldForm(): Response
{
if($this->Http->request->get('password')){
$password = $this->Http->request->get('password');
if($this->getUser()->getEmail()){
$res = $this->ApiCalls->passwordReset($this->getUser()->getEmail(), $password);
$this->setData('apiResponse', $res);
}
}
$ajaxMsg = [ 0 => [
'title'=> $res[0]["title"],
'html'=> $res[0]["html"],
'icon'=> $res[0]["icon"],
'footer'=> $res[0]["footer"]
]];
$this->setData('password-restore', $ajaxMsg);
return new JsonResponse($this->getData());
}
}