diff --git a/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php b/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php index adc0c071d..d135cda39 100644 --- a/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php +++ b/src/Bundle/ChillMainBundle/CRUD/Controller/CRUDController.php @@ -118,7 +118,7 @@ class CRUDController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $this->onFormValid($entity, $form, $request); + $this->onFormValid($action, $entity, $form, $request); $em = $this->getDoctrine()->getManager(); $this->onPreRemove($action, $entity, $form, $request); @@ -607,7 +607,7 @@ class CRUDController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $this->onFormValid($entity, $form, $request); + $this->onFormValid($action, $entity, $form, $request); $em = $this->getDoctrine()->getManager(); $this->onPreFlush($action, $entity, $form, $request); @@ -706,7 +706,7 @@ class CRUDController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { - $this->onFormValid($entity, $form, $request); + $this->onFormValid($action, $entity, $form, $request); $em = $this->getDoctrine()->getManager(); $this->onPrePersist($action, $entity, $form, $request); @@ -716,7 +716,7 @@ class CRUDController extends AbstractController $this->onPreFlush($action, $entity, $form, $request); $em->flush(); $this->onPostFlush($action, $entity, $form, $request); - $this->getPaginatorFactory(); + $this->addFlash('success', $this->generateFormSuccessMessage($action, $entity)); $result = $this->onBeforeRedirectAfterSubmission($action, $entity, $form, $request); @@ -1084,12 +1084,7 @@ class CRUDController extends AbstractController return null; } - /** - * @param object $entity - * @param FormInterface $form - * @param Request $request - */ - protected function onFormValid(object $entity, FormInterface $form, Request $request) + protected function onFormValid(string $action, object $entity, FormInterface $form, Request $request) { } diff --git a/src/Bundle/ChillMainBundle/Controller/UserController.php b/src/Bundle/ChillMainBundle/Controller/UserController.php index 62a28b8d6..99de70747 100644 --- a/src/Bundle/ChillMainBundle/Controller/UserController.php +++ b/src/Bundle/ChillMainBundle/Controller/UserController.php @@ -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); } } } diff --git a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php index 65b8ffdf6..e8c43e02d 100644 --- a/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php +++ b/src/Bundle/ChillMainBundle/DependencyInjection/ChillMainExtension.php @@ -307,9 +307,11 @@ class ChillMainExtension extends Extension implements PrependExtensionInterface, ], 'new' => [ 'role' => 'ROLE_ADMIN', + 'template' => '@ChillMain/User/new.html.twig' ], 'edit' => [ 'role' => 'ROLE_ADMIN', + 'template' => '@ChillMain/User/edit.html.twig' ] ] ] diff --git a/src/Bundle/ChillMainBundle/Form/UserType.php b/src/Bundle/ChillMainBundle/Form/UserType.php index 09e2d1391..bf056cec5 100644 --- a/src/Bundle/ChillMainBundle/Form/UserType.php +++ b/src/Bundle/ChillMainBundle/Form/UserType.php @@ -47,9 +47,9 @@ class UserType extends AbstractType ]) ->add('label', TextType::class) ->add('mainCenter', EntityType::class, [ - 'label' => 'main center', + 'label' => 'Main center', 'required' => false, - 'placeholder' => 'choose a main center', + 'placeholder' => 'Choose a main center', 'class' => Center::class, 'query_builder' => function (EntityRepository $er) { $qb = $er->createQueryBuilder('c'); @@ -59,16 +59,16 @@ class UserType extends AbstractType } ]) ->add('mainScope', EntityType::class, [ - 'label' => 'Choose a main scope', + 'label' => 'Main scope', 'required' => false, - 'placeholder' => 'choose a main scope', + 'placeholder' => 'Choose a main scope', 'class' => Scope::class, 'choice_label' => function (Scope $c) { return $this->translatableStringHelper->localize($c->getName()); }, ]) ->add('userJob', EntityType::class, [ - 'label' => 'Choose a job', + 'label' => 'user job', 'required' => false, 'placeholder' => 'choose a job', 'class' => UserJob::class, diff --git a/src/Bundle/ChillMainBundle/Resources/views/CRUD/_edit_content.html.twig b/src/Bundle/ChillMainBundle/Resources/views/CRUD/_edit_content.html.twig index cc2673960..5e338d5fd 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/CRUD/_edit_content.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/CRUD/_edit_content.html.twig @@ -1,17 +1,22 @@ +{% set formId = crudMainFormId|default('crud_main_form') %}
{% block crud_content_header %}

{{ ('crud.'~crud_name~'.title_edit')|trans }}

{% endblock crud_content_header %} {% block crud_content_form %} - {{ form_start(form) }} - + {{ form_start(form, { 'attr' : { 'id': formId } } ) }} + {% block crud_content_form_rows %} {% for f in form %} {{ form_row(f) }} {% endfor %} {% endblock crud_content_form_rows %} + {{ form_end(form) }} + + {% block crud_content_after_form %}{% endblock %} + {% block crud_content_form_actions %} {% endblock %} - {{ form_end(form) }} {% endblock %}
diff --git a/src/Bundle/ChillMainBundle/Resources/views/CRUD/_new_content.html.twig b/src/Bundle/ChillMainBundle/Resources/views/CRUD/_new_content.html.twig index ff5fe7263..4c2003617 100644 --- a/src/Bundle/ChillMainBundle/Resources/views/CRUD/_new_content.html.twig +++ b/src/Bundle/ChillMainBundle/Resources/views/CRUD/_new_content.html.twig @@ -5,14 +5,16 @@ {% block crud_content_form %} {{ form_start(form) }} - + {% block crud_content_form_rows %} {% for f in form %}{% if f.vars.name != 'submit' %} {{ form_row(f) }} {% endif %}{% endfor %} {% endblock crud_content_form_rows %} - {% block crud_content_form_actions %} + {% block crud_content_after_form %}{% endblock %} + + {% block crud_content_form_actions %}