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

#382: Allow setting user roles in easyadmin form #32

Merged
merged 4 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion config/packages/security.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/Controller/Admin/UserCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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([
Expand Down
5 changes: 3 additions & 2 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,7 +80,7 @@ public function setMail(string $mail): static
return $this;
}

public function isEnabled(): ?bool
public function isEnabled(): bool
{
return $this->enabled;
}
Expand Down Expand Up @@ -175,6 +175,7 @@ public function getRoles(): array

public function setRoles(array $roles): self
{
\sort($roles);
turegjorup marked this conversation as resolved.
Show resolved Hide resolved
$this->roles = $roles;

return $this;
Expand Down
14 changes: 14 additions & 0 deletions src/Types/UserRoles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace App\Types;

enum UserRoles: string
{
case ROLE_USER = 'USER';
case ROLE_ADMIN = 'ADMIN';

public static function array(): array
{
return array_combine(array_column(self::cases(), 'value'), array_column(self::cases(), 'name'));
}
}
8 changes: 4 additions & 4 deletions translations/messages+intl-icu.da.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -353,10 +353,6 @@
<source>admin.user.edited.updated</source>
<target state="needs-l10n">__admin.user.edited.updated</target>
</trans-unit>
<trans-unit id="SapGUZS" resname="login.page.title">
<source>login.page.title</source>
<target state="needs-l10n">__login.page.title</target>
</trans-unit>
<trans-unit id="zPof34." resname="login.user.mail">
<source>login.user.mail</source>
<target state="needs-l10n">__login.user.mail</target>
Expand All @@ -373,6 +369,10 @@
<source>login.page.remember</source>
<target state="needs-l10n">__login.page.remember</target>
</trans-unit>
<trans-unit id=".hsboMJ" resname="admin.user.roles">
<source>admin.user.roles</source>
<target state="needs-l10n">__admin.user.roles</target>
</trans-unit>
</body>
</file>
</xliff>
4 changes: 4 additions & 0 deletions translations/messages+intl-icu.en.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,10 @@
<source>login.page.remember</source>
<target>Remember me</target>
</trans-unit>
<trans-unit id=".hsboMJ" resname="admin.user.roles">
<source>admin.user.roles</source>
<target>Roles</target>
</trans-unit>
</body>
</file>
</xliff>
Loading