From eed1a9e1318ae38b63b4a9200d6953d4404fde69 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ture=20Gj=C3=B8rup?= Date: Thu, 25 Jan 2024 13:56:29 +0100 Subject: [PATCH 1/4] #382: Allow setting user roles in easyadmin form --- config/packages/security.yaml | 2 +- src/Controller/Admin/UserCrudController.php | 6 ++++++ src/Entity/User.php | 5 +++-- src/Types/UserRoles.php | 14 ++++++++++++++ translations/messages+intl-icu.da.xlf | 8 ++++---- translations/messages+intl-icu.en.xlf | 4 ++++ 6 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 src/Types/UserRoles.php diff --git a/config/packages/security.yaml b/config/packages/security.yaml index eb9d4861..49d27501 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -15,9 +15,9 @@ security: security: false main: form_login: - # "app_login" is the name of the route created previously login_path: app_login check_path: app_login + enable_csrf: true logout: path: app_logout diff --git a/src/Controller/Admin/UserCrudController.php b/src/Controller/Admin/UserCrudController.php index 58128532..e2433a14 100644 --- a/src/Controller/Admin/UserCrudController.php +++ b/src/Controller/Admin/UserCrudController.php @@ -3,11 +3,13 @@ namespace App\Controller\Admin; use App\Entity\User; +use App\Types\UserRoles; use EasyCorp\Bundle\EasyAdminBundle\Config\Crud; use EasyCorp\Bundle\EasyAdminBundle\Config\KeyValueStore; use EasyCorp\Bundle\EasyAdminBundle\Context\AdminContext; use EasyCorp\Bundle\EasyAdminBundle\Dto\EntityDto; use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField; +use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField; use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField; use EasyCorp\Bundle\EasyAdminBundle\Field\FormField; @@ -44,6 +46,10 @@ public function configureFields(string $pageName): iterable ->setLabel(new TranslatableMessage('admin.user.name')), EmailField::new('mail') ->setLabel(new TranslatableMessage('admin.user.mail')), + ChoiceField::new('roles') + ->setChoices(UserRoles::array()) + ->allowMultipleChoices() + ->setLabel(new TranslatableMessage('admin.user.roles')), TextField::new('password') ->setFormType(RepeatedType::class) ->setFormTypeOptions([ diff --git a/src/Entity/User.php b/src/Entity/User.php index 2bd651a6..a777d8ac 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -34,7 +34,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface private array $roles = []; #[ORM\Column(length: 255)] - private ?bool $enabled = null; + private bool $enabled = true; #[ORM\Column(length: 255)] private ?string $password = null; @@ -80,7 +80,7 @@ public function setMail(string $mail): static return $this; } - public function isEnabled(): ?bool + public function isEnabled(): bool { return $this->enabled; } @@ -175,6 +175,7 @@ public function getRoles(): array public function setRoles(array $roles): self { + \sort($roles); $this->roles = $roles; return $this; diff --git a/src/Types/UserRoles.php b/src/Types/UserRoles.php new file mode 100644 index 00000000..fd669ef6 --- /dev/null +++ b/src/Types/UserRoles.php @@ -0,0 +1,14 @@ +admin.user.edited.updated __admin.user.edited.updated - - login.page.title - __login.page.title - login.user.mail __login.user.mail @@ -373,6 +369,10 @@ login.page.remember __login.page.remember + + admin.user.roles + __admin.user.roles + diff --git a/translations/messages+intl-icu.en.xlf b/translations/messages+intl-icu.en.xlf index 8c3d54f7..ce5a424d 100644 --- a/translations/messages+intl-icu.en.xlf +++ b/translations/messages+intl-icu.en.xlf @@ -369,6 +369,10 @@ login.page.remember Remember me + + admin.user.roles + Roles + From 9105797a85bf8bb8c73815d254c9f01bda953b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ture=20Gj=C3=B8rup?= Date: Thu, 25 Jan 2024 14:02:53 +0100 Subject: [PATCH 2/4] #382: Clarified sorting of roles --- src/Entity/User.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Entity/User.php b/src/Entity/User.php index a777d8ac..94e03d5d 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -175,6 +175,7 @@ public function getRoles(): array public function setRoles(array $roles): self { + // Sort the roles to ensure consistent display when shown in the UI. \sort($roles); $this->roles = $roles; From c6b00a943b85ce94e663b646e8daed08d03fb77c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ture=20Gj=C3=B8rup?= Date: Thu, 25 Jan 2024 14:04:30 +0100 Subject: [PATCH 3/4] #382: Updated Changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 066876ed..5b8c0327 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,7 @@ See [keep a changelog] for information about writing changes to this log. - Make data imported from feeds read-only in easy admin - Added event subscriber to index content created in the UI - Added command to dump index to json for API fixture generation +- Added "roles" to user create and edit forms [keep a changelog]: https://keepachangelog.com/en/1.1.0/ [unreleased]: https://github.com/itk-dev/event-database-imports/compare/main...develop From 7493d45e1de8c64af0f5c591ace50b1c8a5a1865 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ture=20Gj=C3=B8rup?= Date: Thu, 25 Jan 2024 14:27:31 +0100 Subject: [PATCH 4/4] #382: markdownlint Changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5b8c0327..85fb8d9e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,7 @@ See [keep a changelog] for information about writing changes to this log. - Make data imported from feeds read-only in easy admin - Added event subscriber to index content created in the UI - Added command to dump index to json for API fixture generation -- Added "roles" to user create and edit forms +- Added "roles" to user create and edit forms [keep a changelog]: https://keepachangelog.com/en/1.1.0/ [unreleased]: https://github.com/itk-dev/event-database-imports/compare/main...develop