mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-21 15:13:50 +00:00
continue CRUD user
This commit is contained in:
@@ -4,8 +4,10 @@ namespace Chill\MainBundle\Controller;
|
||||
|
||||
use Chill\MainBundle\CRUD\Controller\AbstractCRUDController;
|
||||
use Chill\MainBundle\CRUD\Controller\CRUDController;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\RedirectResponse;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
@@ -13,10 +15,10 @@ use Chill\MainBundle\Form\UserType;
|
||||
use Chill\MainBundle\Entity\GroupCenter;
|
||||
use Chill\MainBundle\Form\Type\ComposedGroupCenterType;
|
||||
use Chill\MainBundle\Form\UserPasswordType;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
|
||||
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
|
||||
use Symfony\Component\Validator\Validator\ValidatorInterface;
|
||||
use Symfony\Component\Routing\Annotation\Route;
|
||||
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter;
|
||||
|
||||
|
||||
/**
|
||||
@@ -48,63 +50,38 @@ class UserController extends CRUDController
|
||||
* @param ValidatorInterface $validator
|
||||
*/
|
||||
public function __construct(
|
||||
LoggerInterface $logger,
|
||||
LoggerInterface $chillLogger,
|
||||
ValidatorInterface $validator,
|
||||
UserPasswordEncoderInterface $passwordEncoder
|
||||
) {
|
||||
$this->logger = $logger;
|
||||
$this->logger = $chillLogger;
|
||||
$this->validator = $validator;
|
||||
$this->passwordEncoder = $passwordEncoder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new User entity.
|
||||
*
|
||||
*/
|
||||
public function new(Request $request): Response
|
||||
protected function createFormFor(string $action, $entity, string $formClass = null, array $formOptions = []): FormInterface
|
||||
{
|
||||
$user = new User();
|
||||
$form = $this->createCreateForm($user);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$user->setPassword($this->passwordEncoder
|
||||
->encodePassword($user, $form['plainPassword']->getData()));
|
||||
|
||||
$em->persist($user);
|
||||
$em->flush();
|
||||
|
||||
return $this->redirect($this->generateUrl('admin_user_show', array('id' => $user->getId())));
|
||||
} elseif ($form->isSubmitted() && !$form->isValid()){
|
||||
|
||||
// for "new", add special config
|
||||
if ('new' === $action) {
|
||||
return $this->createForm(UserType::class, $entity, array(
|
||||
'is_creation' => true
|
||||
));
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/User/new.html.twig', array(
|
||||
'entity' => $user,
|
||||
'form' => $form->createView(),
|
||||
));
|
||||
// default behaviour
|
||||
return parent::createFormFor($action, $entity, $formClass, $formOptions);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to create a User entity.
|
||||
*
|
||||
* @param User $entity The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createCreateForm(User $entity)
|
||||
protected function onPrePersist(string $action, $entity, FormInterface $form, Request $request)
|
||||
{
|
||||
$form = $this->createForm(UserType::class, $entity, array(
|
||||
'action' => $this->generateUrl('admin_user_create'),
|
||||
'method' => 'POST',
|
||||
'is_creation' => true
|
||||
));
|
||||
// for "new", encode the password
|
||||
if ('new' === $action) {
|
||||
$entity->setPassword($this->passwordEncoder
|
||||
->encodePassword($entity, $form['plainPassword']->getData()));
|
||||
}
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Create'));
|
||||
|
||||
return $form;
|
||||
// default behaviour
|
||||
parent::onPrePersist($action, $entity, $form, $request);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,55 +103,63 @@ class UserController extends CRUDController
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to edit an existing User entity.
|
||||
*
|
||||
*/
|
||||
public function editAction($id)
|
||||
|
||||
protected function generateTemplateParameter(string $action, $entity, Request $request, array $defaultTemplateParameters = [])
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$user = $em->getRepository('ChillMainBundle:User')->find($id);
|
||||
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException('Unable to find User entity.');
|
||||
// add mini-forms for edit action
|
||||
if ("edit" === $action) {
|
||||
return array_merge(
|
||||
$defaultTemplateParameters,
|
||||
[
|
||||
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($entity, $request)->createView(),
|
||||
'delete_groupcenter_form' => array_map(
|
||||
function(\Symfony\Component\Form\Form $form) {
|
||||
return $form->createView();
|
||||
},
|
||||
iterator_to_array($this->getDeleteLinkGroupCenterByUser($entity, $request), true)
|
||||
)
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
$editForm = $this->createEditForm($user);
|
||||
|
||||
return $this->render('@ChillMain/User/edit.html.twig', array(
|
||||
'entity' => $user,
|
||||
'edit_form' => $editForm->createView(),
|
||||
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($user)->createView(),
|
||||
'delete_groupcenter_form' => array_map(
|
||||
function(\Symfony\Component\Form\Form $form) {
|
||||
return $form->createView();
|
||||
|
||||
},
|
||||
iterator_to_array($this->getDeleteLinkGroupCenterByUser($user), true))
|
||||
));
|
||||
// default behaviour
|
||||
return parent::generateTemplateParameter($action, $entity, $request, $defaultTemplateParameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a form to edit the user password.
|
||||
*
|
||||
* @Route("/{_locale}/admin/user/{id}/edit_password", name="admin_user_edit_password")
|
||||
*/
|
||||
public function editPasswordAction($id)
|
||||
public function editPasswordAction(User $user, Request $request)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
$editForm = $this->createEditPasswordForm($user, $request);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
$user = $em->getRepository('ChillMainBundle:User')->find($id);
|
||||
if ($editForm->isSubmitted() && $editForm->isValid()) {
|
||||
$password = $editForm->get('new_password')->getData();
|
||||
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException('Unable to find User entity.');
|
||||
// logging for prod
|
||||
$this->logger->info('update password for an user', [
|
||||
'by' => $this->getUser()->getUsername(),
|
||||
'user' => $user->getUsername()
|
||||
]);
|
||||
|
||||
$user->setPassword($this->passwordEncoder->encodePassword($user, $password));
|
||||
|
||||
$this->getDoctrine()->getManager()->flush();
|
||||
$this->addFlash('success', $this->get('translator')->trans('Password successfully updated!'));
|
||||
|
||||
return $this->redirect(
|
||||
$request->query->has('returnPath') ? $request->query->get('returnPath') :
|
||||
$this->generateUrl('chill_crud_admin_user_edit', ['id' => $user->getId()])
|
||||
);
|
||||
}
|
||||
|
||||
$editForm = $this->createEditPasswordForm($user);
|
||||
|
||||
return $this->render('@ChillMain/User/edit_password.html.twig', array(
|
||||
return $this->render('@ChillMain/User/edit_password.html.twig', [
|
||||
'entity' => $user,
|
||||
'edit_form' => $editForm->createView()
|
||||
));
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -186,9 +171,6 @@ class UserController extends CRUDController
|
||||
private function createEditPasswordForm(User $user)
|
||||
{
|
||||
return $this->createForm(UserPasswordType::class, null, array(
|
||||
'action' =>
|
||||
$this->generateUrl('admin_user_update_password', array('id' => $user->getId())),
|
||||
'method' => 'PUT',
|
||||
'user' => $user
|
||||
))
|
||||
->add('submit', SubmitType::class, array('label' => 'Change password'))
|
||||
@@ -196,7 +178,11 @@ class UserController extends CRUDController
|
||||
;
|
||||
}
|
||||
|
||||
public function deleteLinkGroupCenterAction($uid, $gcid)
|
||||
/**
|
||||
* @Route("/{_locale}/admin/main/user/{uid}/delete_link_groupcenter/{gcid}",
|
||||
* name="admin_user_delete_groupcenter")
|
||||
*/
|
||||
public function deleteLinkGroupCenterAction($uid, $gcid, Request $request): RedirectResponse
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
@@ -218,7 +204,7 @@ class UserController extends CRUDController
|
||||
} catch (\RuntimeException $ex) {
|
||||
$this->addFlash('error', $this->get('translator')->trans($ex->getMessage()));
|
||||
|
||||
return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $uid)));
|
||||
return $this->redirect($this->generateUrl('chill_crud_admin_user_edit', array('id' => $uid)));
|
||||
}
|
||||
|
||||
$em->flush();
|
||||
@@ -226,11 +212,15 @@ class UserController extends CRUDController
|
||||
$this->addFlash('success', $this->get('translator')
|
||||
->trans('The permissions where removed.'));
|
||||
|
||||
return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $uid)));
|
||||
return $this->redirect($this->generateUrl('chill_crud_admin_user_edit', array('id' => $uid)));
|
||||
|
||||
}
|
||||
|
||||
public function addLinkGroupCenterAction(Request $request, $uid)
|
||||
/**
|
||||
* @Route("/{_locale}/admin/main/user/{uid}/add_link_groupcenter",
|
||||
* name="admin_user_add_groupcenter")
|
||||
*/
|
||||
public function addLinkGroupCenterAction(Request $request, $uid): RedirectResponse
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
@@ -240,7 +230,7 @@ class UserController extends CRUDController
|
||||
throw $this->createNotFoundException('Unable to find User entity.');
|
||||
}
|
||||
|
||||
$form = $this->createAddLinkGroupCenterForm($user);
|
||||
$form = $this->createAddLinkGroupCenterForm($user, $request);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isValid()) {
|
||||
@@ -254,8 +244,12 @@ class UserController extends CRUDController
|
||||
$this->addFlash('success', $this->get('translator')->trans('The '
|
||||
. 'permissions have been successfully added to the user'));
|
||||
|
||||
return $this->redirect($this->generateUrl('admin_user_edit',
|
||||
array('id' => $uid)));
|
||||
$returnPathParams = $request->query->has('returnPath') ?
|
||||
['returnPath' => $request->query->get('returnPath')] : [];
|
||||
|
||||
return $this->redirect($this->generateUrl('chill_crud_admin_user_edit',
|
||||
\array_merge(['id' => $uid], $returnPathParams)));
|
||||
|
||||
} else {
|
||||
foreach($this->validator->validate($user) as $error)
|
||||
$this->addFlash('error', $error->getMessage());
|
||||
@@ -293,104 +287,6 @@ class UserController extends CRUDController
|
||||
return $groupCenterManaged;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a form to edit a User entity.
|
||||
*
|
||||
* @param User $user The entity
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createEditForm(User $user)
|
||||
{
|
||||
$form = $this->createForm(UserType::class, $user, array(
|
||||
'action' => $this->generateUrl('admin_user_update', array('id' => $user->getId())),
|
||||
'method' => 'PUT',
|
||||
));
|
||||
|
||||
$form->add('submit', SubmitType::class, array('label' => 'Update'));
|
||||
|
||||
return $form;
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits an existing User entity.
|
||||
*
|
||||
*/
|
||||
public function updateAction(Request $request, $id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$user = $em->getRepository('ChillMainBundle:User')->find($id);
|
||||
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException('Unable to find User entity.');
|
||||
}
|
||||
|
||||
$editForm = $this->createEditForm($user);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isValid()) {
|
||||
$em->flush();
|
||||
|
||||
return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $id)));
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/User/edit.html.twig', array(
|
||||
'entity' => $user,
|
||||
'edit_form' => $editForm->createView(),
|
||||
'add_groupcenter_form' => $this->createAddLinkGroupCenterForm($user)->createView(),
|
||||
'delete_groupcenter_form' => array_map(
|
||||
function(\Symfony\Component\Form\Form $form) {
|
||||
return $form->createView();
|
||||
|
||||
},
|
||||
iterator_to_array($this->getDeleteLinkGroupCenterByUser($user), true))
|
||||
));
|
||||
}
|
||||
|
||||
/**
|
||||
* Edits the user password
|
||||
*
|
||||
*/
|
||||
public function updatePasswordAction(Request $request, $id)
|
||||
{
|
||||
$em = $this->getDoctrine()->getManager();
|
||||
|
||||
$user = $em->getRepository('ChillMainBundle:User')->find($id);
|
||||
|
||||
if (!$user) {
|
||||
throw $this->createNotFoundException('Unable to find User entity.');
|
||||
}
|
||||
|
||||
$editForm = $this->createEditPasswordForm($user);
|
||||
$editForm->handleRequest($request);
|
||||
|
||||
if ($editForm->isValid()) {
|
||||
$password = $editForm->get('new_password')->getData();
|
||||
|
||||
// logging for prod
|
||||
$this->logger->info('update password for an user', [
|
||||
'by' => $this->getUser()->getUsername(),
|
||||
'user' => $user->getUsername()
|
||||
]);
|
||||
|
||||
$user->setPassword($this->get('security.password_encoder')
|
||||
->encodePassword($user, $password));
|
||||
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', $this->get('translator')->trans('Password successfully updated!'));
|
||||
|
||||
return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $id)));
|
||||
}
|
||||
|
||||
return $this->render('@ChillMain/User/edit_password.html.twig', array(
|
||||
'entity' => $user,
|
||||
'edit_form' => $editForm->createView(),
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a form to delete a link to a GroupCenter
|
||||
*
|
||||
@@ -398,11 +294,13 @@ class UserController extends CRUDController
|
||||
*
|
||||
* @return \Symfony\Component\Form\Form The form
|
||||
*/
|
||||
private function createDeleteLinkGroupCenterForm(User $user, GroupCenter $groupCenter)
|
||||
private function createDeleteLinkGroupCenterForm(User $user, GroupCenter $groupCenter, $request)
|
||||
{
|
||||
$returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : [];
|
||||
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl('admin_user_delete_group_center',
|
||||
array('uid' => $user->getId(), 'gcid' => $groupCenter->getId())))
|
||||
->setAction($this->generateUrl('admin_user_delete_groupcenter',
|
||||
array_merge($returnPathParams, ['uid' => $user->getId(), 'gcid' => $groupCenter->getId()])))
|
||||
->setMethod('DELETE')
|
||||
->add('submit', SubmitType::class, array('label' => 'Delete'))
|
||||
->getForm()
|
||||
@@ -415,11 +313,13 @@ class UserController extends CRUDController
|
||||
* @param User $user
|
||||
* @return \Symfony\Component\Form\Form
|
||||
*/
|
||||
private function createAddLinkGroupCenterForm(User $user)
|
||||
private function createAddLinkGroupCenterForm(User $user, Request $request)
|
||||
{
|
||||
$returnPathParams = $request->query->has('returnPath') ? ['returnPath' => $request->query->get('returnPath')] : [];
|
||||
|
||||
return $this->createFormBuilder()
|
||||
->setAction($this->generateUrl('admin_user_add_group_center',
|
||||
array('uid' => $user->getId())))
|
||||
->setAction($this->generateUrl('admin_user_add_groupcenter',
|
||||
array_merge($returnPathParams, ['uid' => $user->getId()])))
|
||||
->setMethod('POST')
|
||||
->add(self::FORM_GROUP_CENTER_COMPOSED, ComposedGroupCenterType::class)
|
||||
->add('submit', SubmitType::class, array('label' => 'Add a new groupCenter'))
|
||||
@@ -431,11 +331,11 @@ class UserController extends CRUDController
|
||||
*
|
||||
* @param User $user
|
||||
*/
|
||||
private function getDeleteLinkGroupCenterByUser(User $user)
|
||||
private function getDeleteLinkGroupCenterByUser(User $user, Request $request)
|
||||
{
|
||||
foreach ($user->getGroupCenters() as $groupCenter) {
|
||||
yield $groupCenter->getId() => $this
|
||||
->createDeleteLinkGroupCenterForm($user, $groupCenter);
|
||||
->createDeleteLinkGroupCenterForm($user, $groupCenter, $request);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user