<?php
namespace App\Controller\ING;
use App\Controller\BaseController;
use Doctrine\DBAL\Connection;
use Empire\Core\Core;
use Empire\Core\Login;
use Empire\Core\News;
use Empire\Core\Password;
use Exception;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class MainController extends BaseController
{
/**
* @Route("/")
*/
public function main() {
return $this->render('ins/main.twig', [
'title' => 'Home',
'gejs' => ['ajax'],
'news' => News::getLatestNews(),
]);
}
/**
* @Route("/login", name="login")
* @return Response
*/
public function login(Request $request) {
$redirectUrl = $request->query->get('returnUrl');
if(null === Core::user()) {
if(is_null($redirectUrl)){
return $this->render('login.twig', ['gejs' => ['ajax']]);
} else {
$client = $request->query->get('client');
return $this->render('login.twig', ['redirectUrl' => $redirectUrl, 'client' => $client, 'gejs' => ['ajax']]);
}
} else {
if(is_null($redirectUrl)){
return $this->redirect('/irms');
} else {
return $this->redirect($redirectUrl);
}
}
}
/**
* @Route("/logout")
* @return RedirectResponse
*/
public function logout() {
Login::logout();
$this->addFlash('success', 'You have been logged out.');
return $this->redirect('/');
}
/**
* @Route("/lostpass", name="lostpass", methods={"GET"})
* @return Response
*/
public function lostpass() {
return $this->render('lostpass.twig', ['gejs' => ['ajax']]);
}
/**
* @Route("/lostpass", methods={"POST"})
*/
public function lostpass_post(Request $request, \Swift_Mailer $mailer) {
$reset = Password::resetInitial($request->request->get('handle', ''), $request->server->get('REMOTE_ADDR'), $mailer, $this);
if($reset) {
return $this->redirect('/');
} else {
return $this->redirect('/lostpass');
}
}
/**
* @Route("/lostpass/{token}/{id}/{signature}", methods={"GET"})
* @param $token
* @param $id
* @param $signature
* @return Response
*/
public function lostpass2 ($token, $id, $signature) {
try {
if(Password::checkToken($token, $id, $signature)) {
return $this->render('lostpass2.twig', ['gejs' => ['ajax']]);
} else {
$this->addFlash('error', 'Link is invalid.');
return $this->render('lostpass.twig', ['gejs' => ['ajax']]);
}
} catch (Exception $e) {
$this->addFlash('error', $e->getMessage());
return $this->render('lostpass.twig', ['gejs' => ['ajax']]);
}
}
/**
* @Route("/lostpass/{token}/{id}/{signature}", methods={"POST"})
* @param Request $request
* @param $token
* @param $id
* @param $signature
* @return Response
*/
public function lostpass2_post(Request $request, $token, $id, $signature) {
$passwords = [$request->request->get('password'), $request->request->get('password2')];
if ($passwords[0] !== $passwords[1]) {
$this->addFlash('error', 'Your passwords do not match. Try again.');
return $this->render('lostpass2.twig', ['gejs' => ['ajax']]);
}
try {
$reset = Password::resetFinal(null, $passwords, [$token, $id, $signature]);
if($reset) {
$this->addFlash('success', 'Your password has been reset, you may now log in using your new password.');
return $this->redirect('/irms');
} else {
$this->addFlash('error', 'An error occurrred reseetting your password.');
return $this->redirect('/irms/%token/$id/$signature');
}
} catch (Exception $e) {
$this->addFlash('error', $e->getMessage());
return $this->render('lostpass2.twig', ['gejs' => ['ajax']]);
}
}
}