/
var
/
www
/
html
/
boaz2
/
src
/
Controller
/
Upload File
HOME
<?php namespace App\Controller; use App\Entity\Loading; use App\Entity\ReleaseOrder; use App\Entity\User; use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\Session; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; use App\Security\LoginFormAuthenticator; class SecurityController extends AbstractController { private $encoder; private $guardHandler; private $authenticator; function __construct(UserPasswordEncoderInterface $encoder, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $authenticator) { // parent::__construct(); $this->encoder = $encoder; $this->guardHandler = $guardHandler; $this->authenticator = $authenticator; } /** * @Route("/login", name="login") */ public function login(AuthenticationUtils $authenticationUtils) { $user = $this->getUser(); if($user){ return $this->redirectToRoute('home'); } else{ $error = $authenticationUtils->getLastAuthenticationError(); // last username entered by the user $lastUsername = $authenticationUtils->getLastUsername(); return $this->render('users/login.html.twig', [ 'last_username' => $lastUsername, 'error' => $error, 'title' => 'login' ]); // last username entered by the user } } /** * @Route("/register", name="register") */ public function register(){ $em = $this->getDoctrine()->getManager(); $user = $this->getUser(); // if($user->getRole() !== 'MANAGEMENT'){ // return $this->redirectToRoute('home'); // } if(isset($_POST['submit'])) { $user = new User(); $user->setUsername($_POST['username']); $user->setPassword($this->encoder->encodePassword($user, $_POST['password'])); $user->setName($_POST['fullname']); $user->setEmail($_POST['email']); $user->setTelephone($_POST['telephone']); $user->setRole($_POST['role']); $user->setEnabled('true'); $role = 'ROLE_' . $_POST['role']; $user->setRoles(array($role)); // save the User! $em->persist($user); $em->flush(); return $this->redirectToRoute('login'); } return $this->render('users/register.html.twig', ['title' => 'Registration']); } /** * @Route("/changepassword", name="change") */ public function change() { if(isset($_POST['submit'])){ $user = $this->getUser(); $user = $this->getDoctrine()->getRepository(User::class)->find($user->getId()); $user->setPassword($this->encoder->encodePassword($user, $_POST['password'])); // make more modifications to the database $this->getDoctrine()->getManager()->flush(); return $this->redirectToRoute('home'); } return $this->render('users/change.html.twig', ['title' => 'Change Password']); } /** * @Route("/reset", name="reset") */ public function reset() { if(isset($_POST['submit'])){ $user = $this->getDoctrine()->getRepository(User::class)->findByUsername($_POST['username'])[0]; $user->setPassword($this->encoder->encodePassword($user, $_POST['password'])); // make more modifications to the database $this->getDoctrine()->getManager()->flush(); return $this->redirectToRoute('home'); } $users = $this->getDoctrine()->getRepository(User::class)->findAll(); return $this->render('users/reset.html.twig', ['title' => 'Reset Password', 'users' => $users]); } /** * @Route("/logout", name="logout") */ public function logout() { throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.'); } /** * @Route("/user/profile", name="profile") */ public function updateProfile(Request $request) { $em = $this->getDoctrine()->getManager(); $user = $this->getUser(); if($request->getMethod() === 'POST') { $user->setEmail($_POST['email']); $user->setTelephone($_POST['telephone']); $em->persist($user); $em->flush(); return $this->redirectToRoute('profile'); } return $this->render('users/profile.html.twig', ['title' => 'Profile', 'user' => $user]); } }