Sfoglia il codice sorgente

Gestion des roles et form user create

feature/ticket
Fab 2 anni fa
parent
commit
0821dc1bb9
11 ha cambiato i file con 174 aggiunte e 19 eliminazioni
  1. +0
    -1
      Controller/AbstractAdminController.php
  2. +2
    -4
      Controller/User/AccountAdminController.php
  3. +29
    -2
      Controller/User/UserAdminController.php
  4. +47
    -0
      Definition/RolesDefinition.php
  5. +16
    -0
      Definition/RolesDefinitionInterface.php
  6. +8
    -4
      Doctrine/EntityManager.php
  7. +6
    -3
      Event/EntityManager/EntityManagerEvent.php
  8. +3
    -3
      EventSubscriber/CommonPropertyEventSubscriber.php
  9. +1
    -1
      EventSubscriber/FlashMessageAdminEventSubscriber.php
  10. +47
    -0
      EventSubscriber/User/UserPasswordEventSubscriber.php
  11. +15
    -1
      Model/User/User.php

+ 0
- 1
Controller/AbstractAdminController.php Vedi File

@@ -402,7 +402,6 @@ abstract class AbstractAdminController extends EaAbstractCrudController
$filters
);

dump(get_defined_vars());
if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) {

Controller/User/UserController.php → Controller/User/AccountAdminController.php Vedi File

@@ -14,7 +14,7 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Symfony\Component\Translation\TranslatableMessage;

class UserController extends AbstractController
class AccountAdminController extends AbstractController
{
protected $em;

@@ -59,9 +59,7 @@ class UserController extends AbstractController

$plainPassword = $form->get('plain_password')->getData();

// @TODO : créer UserManager
$newPasswordEncoded = $passwordEncoder->encodePassword($user, $plainPassword);
$user->setPassword($newPasswordEncoded);
$user->setPassword($passwordEncoder->encodePassword($user, $plainPassword));

$this->em->update($user);
$this->em->flush();

+ 29
- 2
Controller/User/UserAdminController.php Vedi File

@@ -2,17 +2,44 @@

namespace Lc\SovBundle\Controller\User;

use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
use Lc\SovBundle\Controller\AbstractAdminController;
use Lc\SovBundle\Definition\RolesDefinition;
use Lc\SovBundle\Definition\RolesDefinitionInterface;
use Lc\SovBundle\Doctrine\EntityManager;
use Lc\SovBundle\Translation\TranslatorAdmin;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;

use function Symfony\Component\Translation\t;

abstract class UserAdminController extends AbstractAdminController
{
protected $rolesDefinition;
public function __construct(
SessionInterface $session,
RequestStack $request,
EntityManager $em,
TranslatorAdmin $translatorAdmin,
RolesDefinitionInterface $rolesDefinition
) {
parent::__construct($session, $request, $em, $translatorAdmin);
$this->rolesDefinition = $rolesDefinition;
}

public function configureFields(string $pageName): iterable
{

return [
TextField::new('email')
EmailField::new('email'),
TextField::new('lastname'),
TextField::new('firstname'),
ChoiceField::new('roles')
->allowMultipleChoices()
->autocomplete()
->setChoices($this->rolesDefinition->getRolesList())
];
}

}

+ 47
- 0
Definition/RolesDefinition.php Vedi File

@@ -0,0 +1,47 @@
<?php


namespace Lc\SovBundle\Definition;


class RolesDefinition implements RolesDefinitionInterface
{
protected $roles = array(
'ROLE_USER' => [
'label' => 'Utilisateurs',
'role' => 'ROLE_USER',
],
'ROLE_ADMIN' => [
'label' => 'Administrateurs',
'role' => 'ROLE_ADMIN',
],
'ROLE_SUPER_ADMIN' => [
'label' => 'SuperAdmin',
'role' => 'ROLE_SUPER_ADMIN',
],
);

public function getRoles(): array
{
return $this->roles;
}

public function getRole($role): ?array
{
if (isset($this->roles[$role])) {
return $this->roles[$role];
} else {
return null;
}
}

public function getRolesList(): array
{
$rolesList = array();
foreach ($this->roles as $role) {
$rolesList[$role['label']] = $role['role'];
}

return $rolesList;
}
}

+ 16
- 0
Definition/RolesDefinitionInterface.php Vedi File

@@ -0,0 +1,16 @@
<?php


namespace Lc\SovBundle\Definition;


interface RolesDefinitionInterface
{

public function getRoles(): array;

public function getRole($role): ?array;

public function getRolesList(): array;

}

+ 8
- 4
Doctrine/EntityManager.php Vedi File

@@ -41,29 +41,33 @@ class EntityManager extends EntityManagerDecorator

public function create(EntityInterface $entity): self
{
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT);
$this->persist($entity);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_CREATE_EVENT);

return $this;
}

public function update(EntityInterface $entity): self
{
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT);
$this->persist($entity);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_UPDATE_EVENT);

return $this;
}

public function delete(EntityInterface $entity): self
{
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT);

$this->remove($entity);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT);
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_DELETE_EVENT);

return $this;
}

public function flush($entity=null): self
public function flush($entity = null): self
{
$this->wrapped->flush($entity);


+ 6
- 3
Event/EntityManager/EntityManagerEvent.php Vedi File

@@ -12,9 +12,12 @@ use Symfony\Contracts\EventDispatcher\Event;
*/
class EntityManagerEvent extends Event
{
const CREATE_EVENT = 'entity_manager_event.create';
const UPDATE_EVENT = 'entity_manager_event.update';
const DELETE_EVENT = 'entity_manager_event.delete';
const PRE_CREATE_EVENT = 'entity_manager_event.pre_create';
const POST_CREATE_EVENT = 'entity_manager_event.post_create';
const PRE_UPDATE_EVENT = 'entity_manager_event.pre_update';
const POST_UPDATE_EVENT = 'entity_manager_event.post_update';
const PRE_DELETE_EVENT = 'entity_manager_event.pre_delete';
const POST_DELETE_EVENT = 'entity_manager_event.post_delete';

protected EntityInterface $entity;


EventSubscriber/CreateEntityEventSubscriber.php → EventSubscriber/CommonPropertyEventSubscriber.php Vedi File

@@ -12,7 +12,7 @@ use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
use Lc\SovBundle\Repository\AbstractRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class CreateEntityEventSubscriber implements EventSubscriberInterface
class CommonPropertyEventSubscriber implements EventSubscriberInterface
{
protected $em;
protected $adminUrlGenerator;
@@ -25,11 +25,11 @@ class CreateEntityEventSubscriber implements EventSubscriberInterface
public static function getSubscribedEvents()
{
return [
EntityManagerEvent::CREATE_EVENT => ['createEntity'],
EntityManagerEvent::PRE_CREATE_EVENT => ['setCommonProperty'],
];
}

public function createEntity(EntityManagerEvent $event)
public function setCommonProperty(EntityManagerEvent $event)
{
$entity = $event->getEntity();
$entityRepository = $this->em->getRepository(get_class($entity));

EventSubscriber/EasyAdminEventSubscriber.php → EventSubscriber/FlashMessageAdminEventSubscriber.php Vedi File

@@ -9,7 +9,7 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Translation\TranslatableMessage;

class EasyAdminEventSubscriber implements EventSubscriberInterface
class FlashMessageAdminEventSubscriber implements EventSubscriberInterface
{
protected $session ;


+ 47
- 0
EventSubscriber/User/UserPasswordEventSubscriber.php Vedi File

@@ -0,0 +1,47 @@
<?php

namespace Lc\SovBundle\EventSubscriber\User;

use Doctrine\ORM\EntityManagerInterface;

use Lc\SovBundle\Doctrine\EntityInterface;
use Lc\SovBundle\Doctrine\Extension\SortableInterface;
use Lc\SovBundle\Doctrine\Extension\StatusInterface;
use Lc\SovBundle\Doctrine\Extension\TreeInterface;
use Lc\SovBundle\Event\EntityManager\EntityManagerEvent;
use Lc\SovBundle\Model\User\UserInterface;
use Lc\SovBundle\Repository\AbstractRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;

class UserPasswordEventSubscriber implements EventSubscriberInterface
{
protected $passwordEncoder;

public function __construct(UserPasswordEncoderInterface $passwordEncoder)
{
$this->passwordEncoder = $passwordEncoder;
}

public static function getSubscribedEvents()
{
return [
EntityManagerEvent::PRE_CREATE_EVENT => ['setUserPasswordIfNull'],
];
}

public function setUserPasswordIfNull(EntityManagerEvent $event)
{
$entity = $event->getEntity();

if ($entity instanceof UserInterface) {
if ($entity->getPassword() == null) {
$password = $this->passwordEncoder->encodePassword($entity, $entity->generatePassword());
$entity->setPassword($password);
}
}
}


}

+ 15
- 1
Model/User/User.php Vedi File

@@ -104,6 +104,20 @@ abstract class User implements EntityInterface, UserInterface
return $this;
}


public function generatePassword($length = 8): string
{
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$count = mb_strlen($chars);

for ($i = 0, $password = ''; $i < $length; $i++) {
$index = rand(0, $count - 1);
$password .= mb_substr($chars, $index, 1);
}

return $password;
}

/**
* @see UserIn
*/
@@ -147,7 +161,7 @@ abstract class User implements EntityInterface, UserInterface

public function getName(): ?string
{
return $this->getFirstname() . ' ' . strtoupper($this->getLastname());
return $this->getFirstname().' '.strtoupper($this->getLastname());
}

public function isVerified(): bool

Loading…
Annulla
Salva