From 84947b0798620203ed79f7e0260250f8836e4f2b Mon Sep 17 00:00:00 2001 From: Michel NAUD Date: Wed, 10 Feb 2021 21:12:44 +0100 Subject: [PATCH 1/2] add user view --- composer.json | 1 + composer.lock | 68 +++++++++++++- config/bundles.php | 1 + src/Controller/Admin/UserController.php | 21 +++++ src/Controller/ResetPasswordController.php | 17 ++-- src/Entity/User.php | 24 ++++- src/Form/UserType.php | 51 ++++++++++ symfony.lock | 3 + templates/admin/layout.html.twig | 2 +- templates/admin/user/_list.html.twig | 2 +- templates/admin/user/view.html.twig | 104 +++++++++++++++++++++ 11 files changed, 284 insertions(+), 10 deletions(-) create mode 100644 src/Form/UserType.php create mode 100644 templates/admin/user/view.html.twig diff --git a/composer.json b/composer.json index a274897..d285680 100644 --- a/composer.json +++ b/composer.json @@ -12,6 +12,7 @@ "doctrine/orm": "^2.8", "karser/karser-recaptcha3-bundle": "^0.1.15", "kilik/table": "^2.5", + "knplabs/knp-time-bundle": "^1.15", "phpdocumentor/reflection-docblock": "^5.2", "sensio/framework-extra-bundle": "^5.1", "symfony/asset": "4.4.*", diff --git a/composer.lock b/composer.lock index 6f34ff6..2adbb57 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "36bc382df52477340676c9cd355d7f7c", + "content-hash": "c993b23ecd2575cf2488ca278b0431d8", "packages": [ { "name": "composer/package-versions-deprecated", @@ -1707,6 +1707,72 @@ ], "time": "2021-02-03T10:36:28+00:00" }, + { + "name": "knplabs/knp-time-bundle", + "version": "v1.15.0", + "source": { + "type": "git", + "url": "https://github.com/KnpLabs/KnpTimeBundle.git", + "reference": "96bc6523babd1a85988f8b4ecd2730c3aa064b8c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/KnpLabs/KnpTimeBundle/zipball/96bc6523babd1a85988f8b4ecd2730c3aa064b8c", + "reference": "96bc6523babd1a85988f8b4ecd2730c3aa064b8c", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "symfony/config": "~3.4|^4.3|^5.0", + "symfony/dependency-injection": "~3.4|^4.3|^5.0", + "symfony/translation": "^4.3|^5.0" + }, + "require-dev": { + "symfony/framework-bundle": "^4.3|^5.0", + "symfony/phpunit-bridge": "^4.3|^5.0", + "symfony/templating": "~3.4|^4.3|^5.0", + "symfony/twig-bundle": "^4.3|^5.0" + }, + "suggest": { + "symfony/twig-bundle": "to use the Twig `time_diff()` function or `|ago` filter" + }, + "type": "symfony-bundle", + "extra": { + "branch-alias": { + "dev-master": "1.1.x-dev" + } + }, + "autoload": { + "psr-4": { + "Knp\\Bundle\\TimeBundle\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "KnpLabs Team", + "homepage": "http://knplabs.com" + }, + { + "name": "Symfony Community", + "homepage": "http://github.com/KnpLabs/KnpTimeBundle/contributors" + } + ], + "description": "Making your dates look sensible and descriptive", + "homepage": "http://github.com/KnpLabs/KnpTimeBundle", + "keywords": [ + "bundle", + "date", + "descriptive time", + "knp", + "knplabs", + "time" + ], + "time": "2021-01-15T18:15:43+00:00" + }, { "name": "laminas/laminas-code", "version": "4.0.0", diff --git a/config/bundles.php b/config/bundles.php index 7044f19..69091ca 100644 --- a/config/bundles.php +++ b/config/bundles.php @@ -18,4 +18,5 @@ Kilik\TableBundle\KilikTableBundle::class => ['all' => true], Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle::class => ['all' => true], Karser\Recaptcha3Bundle\KarserRecaptcha3Bundle::class => ['all' => true], + Knp\Bundle\TimeBundle\KnpTimeBundle::class => ['all' => true], ]; diff --git a/src/Controller/Admin/UserController.php b/src/Controller/Admin/UserController.php index e6c089a..0b4d9f2 100644 --- a/src/Controller/Admin/UserController.php +++ b/src/Controller/Admin/UserController.php @@ -129,4 +129,25 @@ public function _listAction(TableService $tableService, Request $request) { return $tableService->handleRequest($this->getTable(), $request); } + + /** + * @Route("/{user}", name="admin_user_view") + */ + public function view(User $user): Response + { + return $this->render('admin/user/view.html.twig', [ + 'user' => $user, + ]); + } + + /** + * @Route("/{user}/edit", name="admin_user_edit") + */ + public function edit(User $user): Response + { + return $this->render('admin/user/edit.html.twig', [ + 'user' => $user, + ]); + } + } diff --git a/src/Controller/ResetPasswordController.php b/src/Controller/ResetPasswordController.php index bf787bd..8bf3069 100644 --- a/src/Controller/ResetPasswordController.php +++ b/src/Controller/ResetPasswordController.php @@ -5,6 +5,7 @@ use App\Entity\User; use App\Form\NewPasswordType; use App\Form\PasswordRequestType; +use App\Manager\UserManager; use Doctrine\ORM\EntityManagerInterface; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\Request; @@ -33,8 +34,10 @@ public function __construct(string $mailFrom) public function reset( Request $request, EntityManagerInterface $entityManager, - \Swift_Mailer $mailer - ) { + \Swift_Mailer $mailer, + UserManager $userManager + ) + { $form = $this->createForm(PasswordRequestType::class); $form->handleRequest($request); @@ -45,7 +48,7 @@ public function reset( if ($user instanceof User) { $token = bin2hex(random_bytes(32)); $user->setPasswordRequestToken($token); - $entityManager->flush(); + $userManager->save($user, true, true); // send your email with SwiftMailer or anything else here $this->addFlash('success', 'Si l\'adresse email est correcte, vous allez recevoir un email'); @@ -95,8 +98,10 @@ public function confirm( EntityManagerInterface $entityManager, UserPasswordEncoderInterface $encoder, TokenStorageInterface $tokenStorage, - SessionInterface $session - ) { + SessionInterface $session, + UserManager $userManager + ) + { $user = $entityManager->getRepository(User::class)->findOneBy(['passwordRequestToken' => $token]); if (!$token || !$user instanceof User) { @@ -113,7 +118,7 @@ public function confirm( $password = $encoder->encodePassword($user, $plainPassword); $user->setPassword($password); $user->setPasswordRequestToken(null); - $entityManager->flush(); + $userManager->save($user, true, true); $token = new UsernamePasswordToken($user, $password, 'main'); $tokenStorage->setToken($token); diff --git a/src/Entity/User.php b/src/Entity/User.php index 30a6eb6..c7fd3f1 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -48,6 +48,23 @@ class User implements UserInterface self::STATUS_PRESIDENT, ]; + const STATUSES_OFFICE = [ + self::STATUS_SECRETARY_DEPUTY, + self::STATUS_SECRETARY, + self::STATUS_TREASURER_DEPUTY, + self::STATUS_TREASURER, + self::STATUS_PRESIDENT_DEPUTY, + self::STATUS_PRESIDENT, + ]; + + const ROLE_USER = 'user'; + const ROLE_ADMIN = 'admin'; + + const ROLES = [ + self::ROLE_USER, + self::ROLE_ADMIN, + ]; + /** * @ORM\Id * @ORM\GeneratedValue @@ -301,7 +318,7 @@ public function getStatus(): ?int public function getStatusAsString(): string { - $statuses=self::STATUSES; + $statuses = self::STATUSES; if (isset($statuses[$this->status])) { return $statuses[$this->status]; } @@ -319,4 +336,9 @@ public function isMember(): ?bool { return in_array($this->status, self::STATUSES_MEMBER); } + + public function isOffice(): ?bool + { + return in_array($this->status, self::STATUSES_OFFICE); + } } diff --git a/src/Form/UserType.php b/src/Form/UserType.php new file mode 100644 index 0000000..c92b90f --- /dev/null +++ b/src/Form/UserType.php @@ -0,0 +1,51 @@ +add('email', EmailType::class) + ->add('roles', ChoiceType::class, + [ + 'choices' => $roles, + ]) + ->add('password') + ->add('passwordRequestToken') + ->add('nickname') + ->add('createdAt') + ->add('updatedAt') + ->add('simBms', CheckboxType::class) + ->add('simDcs', CheckboxType::class) + ->add('status')// ->add('player') + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => User::class, + ]); + } +} diff --git a/symfony.lock b/symfony.lock index 6c22941..66dadb5 100644 --- a/symfony.lock +++ b/symfony.lock @@ -146,6 +146,9 @@ "kilik/table": { "version": "2.5.0" }, + "knplabs/knp-time-bundle": { + "version": "v1.15.0" + }, "laminas/laminas-code": { "version": "4.0.0" }, diff --git a/templates/admin/layout.html.twig b/templates/admin/layout.html.twig index c61077a..c1cdca7 100644 --- a/templates/admin/layout.html.twig +++ b/templates/admin/layout.html.twig @@ -343,7 +343,7 @@ diff --git a/templates/admin/user/edit.html.twig b/templates/admin/user/edit.html.twig new file mode 100644 index 0000000..44911fb --- /dev/null +++ b/templates/admin/user/edit.html.twig @@ -0,0 +1,34 @@ +{% extends 'admin/layout.html.twig' %} +{% set libKilikTable=true %} +{% set section="admin" %} +{% set subSection="user" %} + +{% block pageContent %} +
+
+
+
+

Modifier l'utilisateur

+
+ {{ form_start(form) }} + {{ form_rest(form) }} + {{ form_errors(form) }} +
+ +
+ +
+
+ {{ form_end(form) }} +
+
+
+
+ {% endblock pageContent %} diff --git a/templates/admin/user/view.html.twig b/templates/admin/user/view.html.twig index 9cd9751..f36f96c 100644 --- a/templates/admin/user/view.html.twig +++ b/templates/admin/user/view.html.twig @@ -34,6 +34,10 @@ {% if user.simDcs %} + {% if user.player is not null %} + {# @todo add link to DCS player #} + détails + {% endif %} {% else %} {% endif %} @@ -45,7 +49,7 @@ {% if user.simBms %} - + {% else %} {% endif %} @@ -67,20 +71,22 @@ - Créé le: + Créé: -
+
{{ user.createdAt | ago }}
- Modifié le: + Modifié: -
+
{{ user.updatedAt | ago }}
diff --git a/templates/base.html.twig b/templates/base.html.twig index 88648cc..d6966d0 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -33,45 +33,44 @@ {% block header %} {% endblock header %} - {% block pageContent %} - {% endblock pageContent %} +
+ {% block pageContent %} + {% endblock pageContent %} +
{% block footer %} - {% endblock footer %} - - {% block javascript %} - -{% endblock %} -{% endblock body %} +{% endblock %} {% endblock body %} diff --git a/templates/layout.html.twig b/templates/layout.html.twig index 28195be..ea648e5 100644 --- a/templates/layout.html.twig +++ b/templates/layout.html.twig @@ -33,10 +33,12 @@ Les nouvelles {% else %} {% endif %} @@ -101,29 +105,29 @@ {% endblock pageContent %} {% block footer %} -
- + {#
#} {% endblock footer %} diff --git a/templates/security/login.html.twig b/templates/security/login.html.twig index 00e939f..674623e 100644 --- a/templates/security/login.html.twig +++ b/templates/security/login.html.twig @@ -16,11 +16,10 @@ {% if app.user %}
- You are logged in as {{ app.user.username }}, Logout + Vous êtes déjà connecté en tant que {{ app.user.username }}, me déconnecter
{% endif %} -

Please sign in

- +