From f834fbe7a7f45dc117f207f6950a347617abcbb5 Mon Sep 17 00:00:00 2001 From: Steve Winter Date: Fri, 10 Apr 2020 18:11:12 +0100 Subject: [PATCH 1/3] Adding a UI for administrators --- config/packages/easy_admin.yaml | 21 ++++++++++ src/Controller/Admin/AdminController.php | 51 ++++++++++++++++++++++++ src/Entity/Admin.php | 32 +++++++++++++++ 3 files changed, 104 insertions(+) create mode 100644 src/Controller/Admin/AdminController.php diff --git a/config/packages/easy_admin.yaml b/config/packages/easy_admin.yaml index 7efd071..9cdfbfd 100644 --- a/config/packages/easy_admin.yaml +++ b/config/packages/easy_admin.yaml @@ -23,6 +23,9 @@ easy_admin: - { route: 'admin_export_helpers_by_zip_code', label: 'Export zip codes', icon: 'cloud-download', target: '_blank' } - { route: 'admin_export_unmatched', label: 'Export unmatched', icon: 'cloud-download', target: '_blank' } + - { label: 'Administration' } + - { entity: 'Admin', label: 'Administrators', icon: 'users' } + entities: Helper: class: App\Entity\Helper @@ -75,3 +78,21 @@ easy_admin: - { property: 'helper.firstName', label: 'Helper first name' } - { property: 'helper.lastName', label: 'Helper last name' } - { property: 'createdAt', label: 'Date' } + + Admin: + class: App\Entity\Admin + controller: App\Controller\Admin\AdminController + list: + title: Site administrators + max_results: 10 + sort: ['username', 'ASC'] + fields: + - { property: 'username', label: 'Administrator' } + form: + title: 'Add administrator' + help: "We're not going to check, but please ensure you use a suitably secure password!" + fields: + - { property: 'username', type: 'text', label: 'Username' } + - { property: 'plain_password', type: 'repeated', type_options: { type: Symfony\Component\Form\Extension\Core\Type\PasswordType, invalid_message: 'Both passwords must match', first_options: { label: 'Password' }, second_options: { label: 'Confirm password' }, required: true } } + edit: + title: 'Edit administrator' diff --git a/src/Controller/Admin/AdminController.php b/src/Controller/Admin/AdminController.php new file mode 100644 index 0000000..c387ad7 --- /dev/null +++ b/src/Controller/Admin/AdminController.php @@ -0,0 +1,51 @@ +passwordEncoder = $passwordEncoder; + } + + protected function createNewAdminEntity(): admin + { + return (new Admin()) + ->setId(1) + ->setUsername(''); + } + + protected function persistAdminEntity(Admin $admin): void + { + $admin->setPassword( + $this->passwordEncoder->encodePassword($admin, $admin->getPlainPassword()) + ); + parent::persistEntity($admin); + } + + protected function updateAdminEntity(Admin $admin): void + { + $admin->setPassword( + $this->passwordEncoder->encodePassword($admin, $admin->getPlainPassword()) + ); + parent::updateEntity($admin); + } + + protected function removeAdminEntity(Admin $admin) + { + if ($admin->getUsername() === $this->getUser()->getUsername()) { + $this->addFlash('error', 'You cannot delete yourself.'); + + return $this->redirectToRoute('easyadmin', ['action' => 'list', 'entity' => $this->entity['name']]); + } + + parent::removeEntity($admin); + } +} diff --git a/src/Entity/Admin.php b/src/Entity/Admin.php index bda97b4..e5e2e28 100644 --- a/src/Entity/Admin.php +++ b/src/Entity/Admin.php @@ -30,6 +30,8 @@ class Admin implements UserInterface */ private ?string $password; + private ?string $plainPassword; + public function __toString() { return $this->username; @@ -50,6 +52,13 @@ public function getUsername(): string return (string) $this->username; } + public function setUsername(?string $username): self + { + $this->username = $username; + + return $this; + } + /** * @see UserInterface */ @@ -84,4 +93,27 @@ public function getSalt() public function eraseCredentials() { } + + public function setId(int $id): self + { + $this->id = $id; + + return $this; + } + + public function getPlainPassword(): string + { + if (empty($this->plainPassword)) { + return ''; + } + + return $this->plainPassword; + } + + public function setPlainPassword(?string $plainPassword): self + { + $this->plainPassword = $plainPassword; + + return $this; + } } From eb086f924897306cc44175fa3a38b2894e75b7e6 Mon Sep 17 00:00:00 2001 From: Steve Winter Date: Mon, 13 Apr 2020 09:11:36 +0100 Subject: [PATCH 2/3] Addressing feedback on properties of the Admin entity. --- src/Controller/Admin/AdminController.php | 8 +++--- src/Entity/Admin.php | 34 +++++++----------------- 2 files changed, 13 insertions(+), 29 deletions(-) diff --git a/src/Controller/Admin/AdminController.php b/src/Controller/Admin/AdminController.php index c387ad7..660b64c 100644 --- a/src/Controller/Admin/AdminController.php +++ b/src/Controller/Admin/AdminController.php @@ -17,15 +17,13 @@ public function __construct(UserPasswordEncoderInterface $passwordEncoder) protected function createNewAdminEntity(): admin { - return (new Admin()) - ->setId(1) - ->setUsername(''); + return Admin::createForEasyAdminAdd(); } protected function persistAdminEntity(Admin $admin): void { $admin->setPassword( - $this->passwordEncoder->encodePassword($admin, $admin->getPlainPassword()) + $this->passwordEncoder->encodePassword($admin, $admin->plain_password) ); parent::persistEntity($admin); } @@ -33,7 +31,7 @@ protected function persistAdminEntity(Admin $admin): void protected function updateAdminEntity(Admin $admin): void { $admin->setPassword( - $this->passwordEncoder->encodePassword($admin, $admin->getPlainPassword()) + $this->passwordEncoder->encodePassword($admin, $admin->plain_password) ); parent::updateEntity($admin); } diff --git a/src/Entity/Admin.php b/src/Entity/Admin.php index e5e2e28..726f9e2 100644 --- a/src/Entity/Admin.php +++ b/src/Entity/Admin.php @@ -30,7 +30,16 @@ class Admin implements UserInterface */ private ?string $password; - private ?string $plainPassword; + public ?string $plain_password = ''; + + public static function createForEasyAdminAdd(): self + { + $admin = new static(); + $admin->id = 1; + $admin->username = ''; + + return $admin; + } public function __toString() { @@ -52,13 +61,6 @@ public function getUsername(): string return (string) $this->username; } - public function setUsername(?string $username): self - { - $this->username = $username; - - return $this; - } - /** * @see UserInterface */ @@ -100,20 +102,4 @@ public function setId(int $id): self return $this; } - - public function getPlainPassword(): string - { - if (empty($this->plainPassword)) { - return ''; - } - - return $this->plainPassword; - } - - public function setPlainPassword(?string $plainPassword): self - { - $this->plainPassword = $plainPassword; - - return $this; - } } From 21b604214d1cff77f2d22fb86b116efdb45d7819 Mon Sep 17 00:00:00 2001 From: Steve Winter Date: Mon, 13 Apr 2020 09:20:40 +0100 Subject: [PATCH 3/3] Addressing feedback on properties of the Admin entity. --- src/Entity/Admin.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/Entity/Admin.php b/src/Entity/Admin.php index 726f9e2..3810ef0 100644 --- a/src/Entity/Admin.php +++ b/src/Entity/Admin.php @@ -95,11 +95,4 @@ public function getSalt() public function eraseCredentials() { } - - public function setId(int $id): self - { - $this->id = $id; - - return $this; - } }