<?php
namespace Menke\UserBundle\Controller;
use Menke\UserBundle\Entity\User;
use Menke\UserBundle\Form\UserType;
use Doctrine\Persistence\ManagerRegistry;
use Menke\UserBundle\Service\MailerService;
use Menke\UserBundle\Form\PasswordResetType;
use Menke\UserBundle\Form\PasswordForgotType;
use Symfony\Component\HttpFoundation\Request;
use Menke\UserBundle\Event\UserActivatedEvent;
use Symfony\Component\HttpFoundation\Response;
use Menke\UserBundle\Repository\UserRepository;
use Symfony\Component\Routing\Annotation\Route;
use Menke\UserBundle\Security\TokenGeneratorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
class UserController extends AbstractClientableController
{
/**
* @Route("/user/", name="user_index", methods="GET")
*/
public function index(UserRepository $userRepository): Response
{
return $this->render('@MenkeUser/user/index.html.twig', [
'users' => $userRepository->findBy(['client' => $this->getCurrentClient(), 'role' => [User::ROLE_ADMIN, User::ROLE_MANAGER]])
]);
}
/**
* @Route("/user/new", name="user_new", methods="GET|POST")
*/
public function new(Request $request, UserPasswordHasherInterface $passwordHasher, ManagerRegistry $managerRegistry): Response
{
$user = new User();
$form = $this->createForm(UserType::class, $user, [
'be_role_selectable' => true,
'pw_editable' => true,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$hash = $passwordHasher->hashPassword($user, $user->getPassword());
$user->setPassword($hash);
$em = $managerRegistry->getManager();
// ToDo: remove this assignment when client system is expanded
$client = $this->getCurrentClient();
$user->setClient($client);
$user->setActivatedAt(new \DateTime());
$em->persist($user);
$em->flush();
$this->addFlash('notice', 'Benutzer angelegt');
return $this->redirectToRoute('user_index');
}
return $this->render('@MenkeUser/user/new.html.twig', [
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @Route("/user/{id}", name="user_show", methods="GET")
*/
public function show(User $user): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN', $user);
return $this->render('@MenkeUser/user/show.html.twig', ['user' => $user]);
}
/**
* @Route("/user/{id}/edit", name="user_edit", methods="GET|POST")
*/
public function edit(Request $request, User $user, UserPasswordHasherInterface $passwordHasher, ManagerRegistry $managerRegistry): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN', $user);
$password = $user->getPassword();
$form = $this->createForm(UserType::class, $user, [
'required_pw' => false,
'pw_editable' => true,
'be_role_selectable' => true
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
if (!empty($user->getPassword())) {
$hash = $passwordHasher->hashPassword($user, $user->getPassword());
$user->setPassword($hash);
} else {
$user->setPassword($password);
}
$managerRegistry->getManager()->flush();
$this->addFlash('notice', 'Benutzer gespeichert');
return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
}
return $this->render('@MenkeUser/user/edit.html.twig', [
'user' => $user,
'form' => $form->createView(),
]);
}
/**
* @Route("/user/{id}", name="user_delete", methods="DELETE", requirements={"id"="\d+"})
*/
public function delete(Request $request, User $user, ManagerRegistry $managerRegistry): Response
{
$this->denyAccessUnlessGranted('ROLE_ADMIN', $user);
if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
$em = $managerRegistry->getManager();
$em->remove($user);
$em->flush();
}
return $this->redirectToRoute('user_index');
}
/**
* @Route("/user/multiple", name="user_delete-multiple", methods="DELETE")
*/
public function deleteMultiple(Request $request, UserRepository $userRepo, ManagerRegistry $managerRegistry): Response
{
if ($this->isCsrfTokenValid('delete_users', $request->request->get('_token'))) {
$em = $managerRegistry->getManager();
/** @var array $deleteIds */
$deleteIds = $request->request->get('delete');
foreach ($deleteIds as $id => $value) {
if ($value) {
$user = $userRepo->find($id);
$this->denyAccessUnlessGranted('ROLE_ADMIN', $user);
$em->remove($user);
}
}
$em->flush();
}
return $this->redirectToRoute('user_index');
}
/**
* @Route("/forgot-password", name="user_forgot", methods="GET|POST")
*/
public function forgotPassword(
Request $request,
TokenGeneratorInterface $generator,
UserRepository $repo,
MailerService $mailer,
ManagerRegistry $managerRegistry
) {
$form = $this->createForm(PasswordForgotType::class);
$form->handleRequest($request);
$userNotFound = false;
$emailSent = false;
if ($form->isSubmitted() && $form->isValid()) {
$em = $managerRegistry->getManager();
$data = $form->getData();
$user = $repo->findOneBy(array('username' => $data['email']));
if ($user) {
$user->setPasswordResetToken($generator->generateToken());
$user->setPasswordRequestedAt(new \DateTime());
$em->flush();
$mailer->sendPasswordForgotMessage($user, $user->getPasswordResetToken());
$emailSent = true;
} else {
$userNotFound = true;
}
}
return $this->render('@MenkeUser/user/forgot.html.twig', array(
'form' => $form->createView(),
'userNotFound' => $userNotFound,
'emailSent' => $emailSent,
));
}
/**
* @Route("/reset-password/{token}", name="user_reset", methods="GET|POST")
*/
public function resetPasswordAction(
Request $request,
UserRepository $repo,
MailerService $mailer,
UserPasswordHasherInterface $passwordHasher,
$token,
ManagerRegistry $managerRegistry
) {
$user = $repo->findOneBy(array('passwordResetToken' => $token));
$passwordReset = false;
if (!$user) {
$response = new Response();
$response->setStatusCode(Response::HTTP_NOT_FOUND);
return $response;
}
$form = $this->createForm(PasswordResetType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$em = $managerRegistry->getManager();
$hash = $passwordHasher->hashPassword($user, $data['password']);
$user->setPassword($hash);
$user->setPasswordResetToken(null);
$user->setPasswordResetAt(new \DateTime());
$em->flush();
$mailer->sendPasswordResetMessage($user);
$passwordReset = true;
}
return $this->render('@MenkeUser/user/reset.html.twig', array(
'form' => $form->createView(),
'passwordReset' => $passwordReset
));
}
/**
* @Route("/activate/{token}", name="user_activate", methods="GET")
*/
public function activateAccountAction($token, UserRepository $repo, EventDispatcherInterface $dispatcher, ManagerRegistry $managerRegistry)
{
$user = $repo->findOneBy(array('activateToken' => $token));
if (!$user) {
$response = new Response();
$response->setStatusCode(Response::HTTP_NOT_FOUND);
return $response;
}
$user->setActivateToken(null);
$user->setActivatedAt(new \DateTime());
$event = new UserActivatedEvent($user);
$dispatcher->dispatch($event, UserActivatedEvent::NAME);
$em = $managerRegistry->getManager();
$em->flush();
return $this->render('@MenkeUser/user/activated.html.twig');
}
}