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..660b64c --- /dev/null +++ b/src/Controller/Admin/AdminController.php @@ -0,0 +1,49 @@ +passwordEncoder = $passwordEncoder; + } + + protected function createNewAdminEntity(): admin + { + return Admin::createForEasyAdminAdd(); + } + + protected function persistAdminEntity(Admin $admin): void + { + $admin->setPassword( + $this->passwordEncoder->encodePassword($admin, $admin->plain_password) + ); + parent::persistEntity($admin); + } + + protected function updateAdminEntity(Admin $admin): void + { + $admin->setPassword( + $this->passwordEncoder->encodePassword($admin, $admin->plain_password) + ); + 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..3810ef0 100644 --- a/src/Entity/Admin.php +++ b/src/Entity/Admin.php @@ -30,6 +30,17 @@ class Admin implements UserInterface */ private ?string $password; + public ?string $plain_password = ''; + + public static function createForEasyAdminAdd(): self + { + $admin = new static(); + $admin->id = 1; + $admin->username = ''; + + return $admin; + } + public function __toString() { return $this->username;