From 52323fcd24c1b566141a4d8324e2c15bdbf65bc8 Mon Sep 17 00:00:00 2001 From: PapsOu Date: Mon, 14 May 2018 11:10:08 +0200 Subject: [PATCH 1/6] Add User and Client (Application) association --- src/Controller/UserController.php | 2 + src/Entity/Oauth/Client.php | 19 +++++++ src/Entity/Oauth/User.php | 27 +++++++++- .../Transformer/ApplicationTransformer.php | 42 +++++++++++++++ src/Form/User/CreateUserForm.php | 52 +++++++++++++++++-- .../Applications/manageApplications.html.twig | 1 + templates/User/createForm.html.twig | 1 + templates/User/manageUsers.html.twig | 11 ++++ templates/base.html.twig | 1 + 9 files changed, 152 insertions(+), 4 deletions(-) create mode 100644 src/Form/Applications/Transformer/ApplicationTransformer.php diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 5a11c53..30bd6ca 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -156,6 +156,8 @@ public function addUserAction(Request $request) $encoder = $this->encoderFactory->getEncoder($user); $user->setPassword($encoder->encodePassword($userData['password'], $user->getSalt())); + $user->setApplication($userData['application']); + $this->getDoctrine()->getManager()->persist($user); $this->getDoctrine()->getManager()->flush(); diff --git a/src/Entity/Oauth/Client.php b/src/Entity/Oauth/Client.php index f4667db..4bb50bc 100644 --- a/src/Entity/Oauth/Client.php +++ b/src/Entity/Oauth/Client.php @@ -1,9 +1,19 @@ users = new ArrayCollection(); } /** diff --git a/src/Entity/Oauth/User.php b/src/Entity/Oauth/User.php index ebed677..bfce237 100644 --- a/src/Entity/Oauth/User.php +++ b/src/Entity/Oauth/User.php @@ -84,6 +84,13 @@ class User implements AdvancedUserInterface, \Serializable */ protected $locked = false; + /** + * @ORM\ManyToOne(targetEntity="Client", inversedBy="users") + * + * @var Client + */ + protected $application; + public function __construct(?string $email = null, ?string $password) { $this->email = $email; @@ -196,6 +203,7 @@ public function serialize() $this->expirationDate, $this->enabled, $this->locked, + $this->application, )); } @@ -211,7 +219,8 @@ public function unserialize($serialized) $this->creationDate, $this->expirationDate, $this->enabled, - $this->locked) = unserialize($serialized); + $this->locked, + $this->application) = unserialize($serialized); } /** @@ -295,4 +304,20 @@ public function isCredentialsNonExpired(): bool { return true; } + + /** + * @return Client + */ + public function getApplication(): ?Client + { + return $this->application; + } + + /** + * @param Client $application + */ + public function setApplication(?Client $application): void + { + $this->application = $application; + } } diff --git a/src/Form/Applications/Transformer/ApplicationTransformer.php b/src/Form/Applications/Transformer/ApplicationTransformer.php new file mode 100644 index 0000000..80a071c --- /dev/null +++ b/src/Form/Applications/Transformer/ApplicationTransformer.php @@ -0,0 +1,42 @@ +doctrine = $doctrine; + } + + public function transform($value) + { + if ($value instanceof Client) { + return $value->getId(); + } + + return -1; + } + + public function reverseTransform($value) + { + return $this->doctrine->getManager('default')->getRepository(Client::class)->find($value); + } +} diff --git a/src/Form/User/CreateUserForm.php b/src/Form/User/CreateUserForm.php index 525e24d..a721841 100644 --- a/src/Form/User/CreateUserForm.php +++ b/src/Form/User/CreateUserForm.php @@ -1,5 +1,13 @@ 'ROLE_USER', 'ROLE_SUPER_ADMIN' => 'ROLE_SUPER_ADMIN', ]; + public function __construct(RegistryInterface $doctrine, ApplicationTransformer $applicationTransformer) + { + $this->doctrine = $doctrine; + $this->applicationTransformer = $applicationTransformer; + } + public function buildForm(FormBuilderInterface $builder, array $options) { $builder + ->add('application', ChoiceType::class, [ + 'required' => true, + 'multiple' => false, + 'expanded' => false, + 'choices' => $this->getApplications(), + ]) ->add('email', EmailType::class) ->add('password', PasswordType::class) ->add('roles', ChoiceType::class, [ 'required' => false, 'multiple' => true, 'expanded' => true, - 'choices' => $this->userRoles + 'choices' => $this->userRoles, ]) ->add('submit', SubmitType::class, [ 'attr' => [ - 'class' => 'fluid' + 'class' => 'fluid', ], - 'label' => 'Créer l\'utilisateur' + 'label' => 'Créer l\'utilisateur', ]) ; + + $builder->get('application')->addModelTransformer($this->applicationTransformer); + } + + private function getApplications(): array + { + $applications = []; + + foreach ($this->doctrine->getManager('default')->getRepository(Client::class)->findAll() as $application) { + $applications[$application->getId()] = $application->getName(); + } + + return array_flip($applications); } } diff --git a/templates/Applications/manageApplications.html.twig b/templates/Applications/manageApplications.html.twig index 99991e7..ec1693b 100644 --- a/templates/Applications/manageApplications.html.twig +++ b/templates/Applications/manageApplications.html.twig @@ -158,6 +158,7 @@ modal.find('.actions').remove(); $('.ui.checkbox').checkbox(); + $('.ui.dropdown').dropdown(); modal.modal('show'); } diff --git a/templates/User/createForm.html.twig b/templates/User/createForm.html.twig index a85a7d3..1b90202 100644 --- a/templates/User/createForm.html.twig +++ b/templates/User/createForm.html.twig @@ -2,6 +2,7 @@ {{ form_row(form.username) }} {{ form_row(form.email) }} +{{ form_row(form.application) }} {{ form_row(form.roles) }} {{ form_row(form.expirationDate) }} {{ form_row(form.enabled) }} diff --git a/templates/User/manageUsers.html.twig b/templates/User/manageUsers.html.twig index b680129..74a6876 100644 --- a/templates/User/manageUsers.html.twig +++ b/templates/User/manageUsers.html.twig @@ -39,6 +39,7 @@ # + Application Username Email Roles @@ -56,6 +57,15 @@ {% for user in users %} {{ user.id }} + + {% if user.application is not null %} + + {{ user.application.name }} + + {% else %} + N/A + {% endif %} + {{ user.username }} {{ user.email }} @@ -136,6 +146,7 @@ modal.find('.actions').remove(); $('.ui.checkbox').checkbox(); + $('.ui.dropdown').dropdown(); modal.modal('show'); } diff --git a/templates/base.html.twig b/templates/base.html.twig index cf5a7ca..2dc50ea 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -26,6 +26,7 @@