Added delete user feature #225 (#249)

This commit is contained in:
Kevin Papst
2018-07-31 13:16:40 +02:00
committed by GitHub
parent 0996eca3c5
commit a8a8424ced
29 changed files with 879 additions and 49 deletions

View File

@@ -122,7 +122,7 @@ class ActivityController extends AbstractController
$this->flashSuccess('action.deleted_successfully');
return $this->redirectToRoute('admin_activity', ['id' => $activity->getId()]);
return $this->redirectToRoute('admin_activity');
}
return $this->render(
@@ -160,7 +160,7 @@ class ActivityController extends AbstractController
$editForm->get('create_more')->setData(true);
$activity = $newActivity;
} else {
return $this->redirectToRoute('admin_activity', ['id' => $activity->getId()]);
return $this->redirectToRoute('admin_activity');
}
}

View File

@@ -101,7 +101,7 @@ class CustomerController extends AbstractController
$this->flashSuccess('action.updated_successfully');
return $this->redirectToRoute('admin_customer', ['id' => $customer->getId()]);
return $this->redirectToRoute('admin_customer');
}
return $this->render('admin/customer_edit.html.twig', [
@@ -139,7 +139,7 @@ class CustomerController extends AbstractController
$this->flashSuccess('action.deleted_successfully');
return $this->redirectToRoute('admin_customer', ['id' => $customer->getId()]);
return $this->redirectToRoute('admin_customer');
}
return $this->render('admin/customer_delete.html.twig', [

View File

@@ -116,7 +116,7 @@ class ProjectController extends AbstractController
$this->flashSuccess('action.deleted_successfully');
return $this->redirectToRoute('admin_project', ['id' => $project->getId()]);
return $this->redirectToRoute('admin_project');
}
return $this->render('admin/project_delete.html.twig', [
@@ -151,7 +151,7 @@ class ProjectController extends AbstractController
$editForm->get('create_more')->setData(true);
$project = $newProject;
} else {
return $this->redirectToRoute('admin_project', ['id' => $project->getId()]);
return $this->redirectToRoute('admin_project');
}
}

View File

@@ -10,6 +10,7 @@
namespace App\Controller\Admin;
use App\Controller\AbstractController;
use App\Entity\Timesheet;
use App\Entity\User;
use App\Form\Toolbar\UserToolbarForm;
use App\Form\UserCreateType;
@@ -19,6 +20,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* Controller used to manage users in the admin part of the site.
@@ -29,6 +31,27 @@ use Symfony\Component\HttpFoundation\Request;
*/
class UserController extends AbstractController
{
/**
* @var UserPasswordEncoderInterface
*/
protected $encoder;
/**
* @param UserPasswordEncoderInterface $encoder
*/
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
/**
* @return \App\Repository\UserRepository
*/
protected function getRepository()
{
return $this->getDoctrine()->getRepository(User::class);
}
/**
* @Route("/", defaults={"page": 1}, name="admin_user")
* @Route("/page/{page}", requirements={"page": "[1-9]\d*"}, name="admin_user_paginated")
@@ -47,7 +70,7 @@ class UserController extends AbstractController
}
/* @var $entries Pagerfanta */
$entries = $this->getDoctrine()->getRepository(User::class)->findByQuery($query);
$entries = $this->getRepository()->findByQuery($query);
return $this->render('admin/user.html.twig', [
'entries' => $entries,
@@ -69,8 +92,7 @@ class UserController extends AbstractController
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$password = $this->get('security.password_encoder')
->encodePassword($user, $user->getPlainPassword());
$password = $this->encoder->encodePassword($user, $user->getPlainPassword());
$user->setPassword($password);
$user->setEnabled(true);
$user->setRoles([User::DEFAULT_ROLE]);
@@ -99,6 +121,49 @@ class UserController extends AbstractController
);
}
/**
* The route to delete an existing user.
*
* @Route("/{id}/delete", name="admin_user_delete")
* @Method({"GET", "POST"})
* @Security("is_granted('delete', userToDelete)")
*
* @param User $userToDelete
* @param Request $request
* @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
* @throws \Doctrine\ORM\NonUniqueResultException
*/
public function deleteAction(User $userToDelete, Request $request)
{
$stats = $this->getDoctrine()->getRepository(Timesheet::class)->getUserStatistics($userToDelete);
$deleteForm = $this->createFormBuilder()
->setAction($this->generateUrl('admin_user_delete', ['id' => $userToDelete->getId()]))
->setMethod('POST')
->getForm();
$deleteForm->handleRequest($request);
if (0 == $stats->getRecordsTotal() || ($deleteForm->isSubmitted() && $deleteForm->isValid())) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->remove($userToDelete);
$entityManager->flush();
$this->flashSuccess('action.deleted_successfully');
return $this->redirectToRoute('admin_user');
}
return $this->render(
'admin/user_delete.html.twig',
[
'user' => $userToDelete,
'stats' => $stats,
'form' => $deleteForm->createView(),
]
);
}
/**
* @param UserQuery $query
* @return \Symfony\Component\Form\FormInterface

View File

@@ -22,6 +22,7 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* User profile controller
@@ -31,6 +32,19 @@ use Symfony\Component\HttpFoundation\Request;
*/
class ProfileController extends AbstractController
{
/**
* @var UserPasswordEncoderInterface
*/
protected $encoder;
/**
* @param UserPasswordEncoderInterface $encoder
*/
public function __construct(UserPasswordEncoderInterface $encoder)
{
$this->encoder = $encoder;
}
/**
* @Route("/{username}", name="user_profile")
* @Method("GET")
@@ -75,8 +89,7 @@ class ProfileController extends AbstractController
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$password = $this->get('security.password_encoder')
->encodePassword($profile, $profile->getPlainPassword());
$password = $this->encoder->encodePassword($profile, $profile->getPlainPassword());
$profile->setPassword($password);
$entityManager = $this->getDoctrine()->getManager();
@@ -147,11 +160,9 @@ class ProfileController extends AbstractController
}
}
foreach ($preferences as $preference) {
$preference->setUser($profile);
$entityManager->persist($preference);
$entityManager->flush();
}
$profile->setPreferences($preferences);
$entityManager->persist($profile);
$entityManager->flush();
$this->flashSuccess('action.updated_successfully');