From 152fcccd4489378a8ed9391e3e191df4aeba6435 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Fri, 30 Sep 2016 20:09:06 +0200 Subject: Add users management UI MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - remove the “add a user” from the config page - add a CRUD on user - fix some missing translations (+ bad indentation) --- .../UserBundle/Controller/ManageController.php | 149 +++++++++++++++++++++ src/Wallabag/UserBundle/Entity/User.php | 2 +- src/Wallabag/UserBundle/Form/NewUserType.php | 58 ++++++++ src/Wallabag/UserBundle/Form/UserType.php | 61 +++++++++ .../Resources/views/Manage/edit.html.twig | 86 ++++++++++++ .../Resources/views/Manage/index.html.twig | 48 +++++++ .../Resources/views/Manage/new.html.twig | 61 +++++++++ .../UserBundle/Resources/views/manage.html.twig | 43 ++++++ 8 files changed, 507 insertions(+), 1 deletion(-) create mode 100644 src/Wallabag/UserBundle/Controller/ManageController.php create mode 100644 src/Wallabag/UserBundle/Form/NewUserType.php create mode 100644 src/Wallabag/UserBundle/Form/UserType.php create mode 100644 src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig create mode 100644 src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig create mode 100644 src/Wallabag/UserBundle/Resources/views/Manage/new.html.twig create mode 100644 src/Wallabag/UserBundle/Resources/views/manage.html.twig (limited to 'src/Wallabag/UserBundle') diff --git a/src/Wallabag/UserBundle/Controller/ManageController.php b/src/Wallabag/UserBundle/Controller/ManageController.php new file mode 100644 index 00000000..92ee2b41 --- /dev/null +++ b/src/Wallabag/UserBundle/Controller/ManageController.php @@ -0,0 +1,149 @@ +getDoctrine()->getManager(); + + $users = $em->getRepository('WallabagUserBundle:User')->findAll(); + + return $this->render('WallabagUserBundle:Manage:index.html.twig', array( + 'users' => $users, + )); + } + + /** + * Creates a new User entity. + * + * @Route("/new", name="user_new") + * @Method({"GET", "POST"}) + */ + public function newAction(Request $request) + { + $userManager = $this->container->get('fos_user.user_manager'); + + $user = $userManager->createUser(); + // enable created user by default + $user->setEnabled(true); + + $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [ + 'validation_groups' => ['Profile'], + ]); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $userManager->updateUser($user); + + // dispatch a created event so the associated config will be created + $event = new UserEvent($user, $request); + $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); + + $this->get('session')->getFlashBag()->add( + 'notice', + $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()]) + ); + + return $this->redirectToRoute('user_edit', array('id' => $user->getId())); + } + + return $this->render('WallabagUserBundle:Manage:new.html.twig', array( + 'user' => $user, + 'form' => $form->createView(), + )); + } + + /** + * Displays a form to edit an existing User entity. + * + * @Route("/{id}/edit", name="user_edit") + * @Method({"GET", "POST"}) + */ + public function editAction(Request $request, User $user) + { + $deleteForm = $this->createDeleteForm($user); + $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user); + $editForm->handleRequest($request); + + if ($editForm->isSubmitted() && $editForm->isValid()) { + $em = $this->getDoctrine()->getManager(); + $em->persist($user); + $em->flush(); + + $this->get('session')->getFlashBag()->add( + 'notice', + $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()]) + ); + + return $this->redirectToRoute('user_edit', array('id' => $user->getId())); + } + + return $this->render('WallabagUserBundle:Manage:edit.html.twig', array( + 'user' => $user, + 'edit_form' => $editForm->createView(), + 'delete_form' => $deleteForm->createView(), + 'twofactor_auth' => $this->getParameter('twofactor_auth'), + )); + } + + /** + * Deletes a User entity. + * + * @Route("/{id}", name="user_delete") + * @Method("DELETE") + */ + public function deleteAction(Request $request, User $user) + { + $form = $this->createDeleteForm($user); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $this->get('session')->getFlashBag()->add( + 'notice', + $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()]) + ); + + $em = $this->getDoctrine()->getManager(); + $em->remove($user); + $em->flush(); + } + + return $this->redirectToRoute('user_index'); + } + + /** + * Creates a form to delete a User entity. + * + * @param User $user The User entity + * + * @return \Symfony\Component\Form\Form The form + */ + private function createDeleteForm(User $user) + { + return $this->createFormBuilder() + ->setAction($this->generateUrl('user_delete', array('id' => $user->getId()))) + ->setMethod('DELETE') + ->getForm() + ; + } +} diff --git a/src/Wallabag/UserBundle/Entity/User.php b/src/Wallabag/UserBundle/Entity/User.php index dfed8e47..d98ae76a 100644 --- a/src/Wallabag/UserBundle/Entity/User.php +++ b/src/Wallabag/UserBundle/Entity/User.php @@ -64,7 +64,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf protected $entries; /** - * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user") + * @ORM\OneToOne(targetEntity="Wallabag\CoreBundle\Entity\Config", mappedBy="user", cascade={"remove"}) */ protected $config; diff --git a/src/Wallabag/UserBundle/Form/NewUserType.php b/src/Wallabag/UserBundle/Form/NewUserType.php new file mode 100644 index 00000000..ad5a2405 --- /dev/null +++ b/src/Wallabag/UserBundle/Form/NewUserType.php @@ -0,0 +1,58 @@ +add('username', TextType::class, [ + 'required' => true, + 'label' => 'user.form.username_label', + ]) + ->add('plainPassword', RepeatedType::class, [ + 'type' => PasswordType::class, + 'invalid_message' => 'validator.password_must_match', + 'first_options' => ['label' => 'user.form.password_label'], + 'second_options' => ['label' => 'user.form.repeat_new_password_label'], + 'constraints' => [ + new Constraints\Length([ + 'min' => 8, + 'minMessage' => 'validator.password_too_short', + ]), + new Constraints\NotBlank(), + ], + 'label' => 'user.form.plain_password_label', + ]) + ->add('email', EmailType::class, [ + 'label' => 'user.form.email_label', + ]) + ->add('save', SubmitType::class, [ + 'label' => 'user.form.save', + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => 'Wallabag\UserBundle\Entity\User', + ]); + } + + public function getBlockPrefix() + { + return 'new_user'; + } +} diff --git a/src/Wallabag/UserBundle/Form/UserType.php b/src/Wallabag/UserBundle/Form/UserType.php new file mode 100644 index 00000000..cfa67793 --- /dev/null +++ b/src/Wallabag/UserBundle/Form/UserType.php @@ -0,0 +1,61 @@ +add('name', TextType::class, [ + 'required' => false, + 'label' => 'user.form.name_label', + ]) + ->add('username', TextType::class, [ + 'required' => true, + 'label' => 'user.form.username_label', + ]) + ->add('email', EmailType::class, [ + 'required' => true, + 'label' => 'user.form.email_label', + ]) + ->add('enabled', CheckboxType::class, [ + 'required' => false, + 'label' => 'user.form.enabled_label', + ]) + ->add('locked', CheckboxType::class, [ + 'required' => false, + 'label' => 'user.form.locked_label', + ]) + ->add('twoFactorAuthentication', CheckboxType::class, [ + 'required' => false, + 'label' => 'user.form.twofactor_label', + ]) + ->add('save', SubmitType::class, [ + 'label' => 'user.form.save', + ]) + ; + } + + /** + * @param OptionsResolver $resolver + */ + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults(array( + 'data_class' => 'Wallabag\UserBundle\Entity\User', + )); + } +} diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig new file mode 100644 index 00000000..5292ba78 --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig @@ -0,0 +1,86 @@ +{% extends "WallabagCoreBundle::layout.html.twig" %} + +{% block title %}{{ 'user.page_title'|trans }}{% endblock %} + +{% block content %} + +
+
+
+
+
+

{{ 'user.edit_user'|trans }}

+ +
+ {{ form_start(edit_form) }} + {{ form_errors(edit_form) }} + +
+
+ {{ form_label(edit_form.name) }} + {{ form_errors(edit_form.name) }} + {{ form_widget(edit_form.name) }} +
+
+ +
+
+ {{ form_label(edit_form.username) }} + {{ form_errors(edit_form.username) }} + {{ form_widget(edit_form.username) }} +
+
+ +
+
+ {{ form_label(edit_form.email) }} + {{ form_errors(edit_form.email) }} + {{ form_widget(edit_form.email) }} +
+
+ +
+
+ {{ form_widget(edit_form.enabled) }} + {{ form_label(edit_form.enabled) }} + {{ form_errors(edit_form.enabled) }} +
+
+ +
+
+ {{ form_widget(edit_form.locked) }} + {{ form_label(edit_form.locked) }} + {{ form_errors(edit_form.locked) }} +
+
+ + {% if twofactor_auth %} +
+
+ {{ form_widget(edit_form.twoFactorAuthentication) }} + {{ form_label(edit_form.twoFactorAuthentication) }} + {{ form_errors(edit_form.twoFactorAuthentication) }} +
+
+ {% endif %} + +
+ + {{ form_widget(edit_form.save, {'attr': {'class': 'btn waves-effect waves-light'}}) }} + {{ form_rest(edit_form) }} + +

+ {{ form_start(delete_form) }} + + {{ form_end(delete_form) }} +

+

{{ 'user.form.back_to_list'|trans }}

+
+
+
+
+
+
+ +{% endblock %} diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig new file mode 100644 index 00000000..996bdb1a --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/views/Manage/index.html.twig @@ -0,0 +1,48 @@ +{% extends "WallabagCoreBundle::layout.html.twig" %} + +{% block title %}{{ 'user.page_title'|trans }}{% endblock %} + +{% block content %} + +
+
+
+
+
+

{{ 'user.description'|trans|raw }}

+ + + + + + + + + + + + + {% for user in users %} + + + + + + + + {% endfor %} + +
{{ 'user.form.username_label'|trans }}{{ 'user.form.email_label'|trans }}{{ 'user.form.last_login_label'|trans }}{{ 'user.form.locked_label'|trans }}{{ 'user.list.actions'|trans }}
{{ user.username }}{{ user.email }}{% if user.lastLogin %}{{ user.lastLogin|date('Y-m-d H:i:s') }}{% endif %}{% if user.locked %}{{ 'user.list.yes'|trans }}{% else %}{{ 'user.list.no'|trans }}{% endif %} + {{ 'user.list.edit_action'|trans }} +
+
+

+ {{ 'user.list.create_new_one'|trans }} +

+
+
+
+
+
+ +{% endblock %} diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/new.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/new.html.twig new file mode 100644 index 00000000..8c894c04 --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/views/Manage/new.html.twig @@ -0,0 +1,61 @@ +{% extends "WallabagCoreBundle::layout.html.twig" %} + +{% block title %}{{ 'user.page_title'|trans }}{% endblock %} + +{% block content %} + +
+
+
+
+
+

{{ 'user.new_user'|trans }}

+ +
+ {{ form_start(form) }} + {{ form_errors(form) }} + +
+
+ {{ form_label(form.username) }} + {{ form_errors(form.username) }} + {{ form_widget(form.username) }} +
+
+ +
+
+ {{ form_label(form.plainPassword.first) }} + {{ form_errors(form.plainPassword.first) }} + {{ form_widget(form.plainPassword.first) }} +
+
+ +
+
+ {{ form_label(form.plainPassword.second) }} + {{ form_errors(form.plainPassword.second) }} + {{ form_widget(form.plainPassword.second) }} +
+
+ +
+
+ {{ form_label(form.email) }} + {{ form_errors(form.email) }} + {{ form_widget(form.email) }} +
+
+ + {{ form_widget(form.save, {'attr': {'class': 'btn waves-effect waves-light'}}) }} + {{ form_rest(form) }} + +

{{ 'user.form.back_to_list'|trans }}

+
+
+
+
+
+
+ +{% endblock %} diff --git a/src/Wallabag/UserBundle/Resources/views/manage.html.twig b/src/Wallabag/UserBundle/Resources/views/manage.html.twig new file mode 100644 index 00000000..c614c55f --- /dev/null +++ b/src/Wallabag/UserBundle/Resources/views/manage.html.twig @@ -0,0 +1,43 @@ +{% extends "WallabagCoreBundle::layout.html.twig" %} + +{% block title %}{{ 'user.manage.page_title'|trans }}{% endblock %} + +{% block content %} + +
+
+
+
+
+

{{ 'user.manage.description'|trans|raw }}

+ + + + + + + + + + + + + + {% for user in users %} + + + + + + + + {% endfor %} + +
{{ 'user.manage.field.username'|trans }}{{ 'user.manage.field.email'|trans }}{{ 'user.manage.field.last_login'|trans }}{{ 'user.manage.field.locked'|trans }}{{ 'user.manage.action'|trans }}
{{ user.username }}{{ user.email }}{{ user.lastLogin|date('d/m/Y H:i:s') }}{{ user.locked ? 'yes' : 'no' }}edit - delete
+
+
+
+
+
+ +{% endblock %} -- cgit v1.2.3 From ccc7faec094387681a59faa5ca79a86e33f06972 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 2 Oct 2016 13:21:45 +0200 Subject: Disabled delete button for the logged user MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid some bad things to happen… --- src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/UserBundle') diff --git a/src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig b/src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig index 5292ba78..d5cf99c3 100644 --- a/src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig +++ b/src/Wallabag/UserBundle/Resources/views/Manage/edit.html.twig @@ -72,7 +72,7 @@

{{ form_start(delete_form) }} - + {{ form_end(delete_form) }}

{{ 'user.form.back_to_list'|trans }}

-- cgit v1.2.3