Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a UI for administrators #54

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions config/packages/easy_admin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
49 changes: 49 additions & 0 deletions src/Controller/Admin/AdminController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Controller\Admin;

use App\Entity\Admin;
use EasyCorp\Bundle\EasyAdminBundle\Controller\EasyAdminController;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class AdminController extends EasyAdminController
{
protected UserPasswordEncoderInterface $passwordEncoder;

public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->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);
}
}
11 changes: 11 additions & 0 deletions src/Entity/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down