$filters | $filters | ||||
); | ); | ||||
/*dump(get_defined_vars());*/ | |||||
if ($this->isInstanceOf(TreeInterface::class)) { | if ($this->isInstanceOf(TreeInterface::class)) { | ||||
$entityId = $searchDto->getRequest()->get('entityId'); | $entityId = $searchDto->getRequest()->get('entityId'); | ||||
if ($entityId !== null) { | if ($entityId !== null) { |
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; | ||||
use Symfony\Component\Translation\TranslatableMessage; | use Symfony\Component\Translation\TranslatableMessage; | ||||
class UserController extends AbstractController | |||||
class AccountAdminController extends AbstractController | |||||
{ | { | ||||
protected $em; | protected $em; | ||||
$plainPassword = $form->get('plain_password')->getData(); | $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->update($user); | ||||
$this->em->flush(); | $this->em->flush(); |
namespace Lc\SovBundle\Controller\User; | namespace Lc\SovBundle\Controller\User; | ||||
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField; | |||||
use EasyCorp\Bundle\EasyAdminBundle\Field\EmailField; | |||||
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; | use EasyCorp\Bundle\EasyAdminBundle\Field\TextField; | ||||
use Lc\SovBundle\Controller\AbstractAdminController; | 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 | 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 | public function configureFields(string $pageName): iterable | ||||
{ | { | ||||
return [ | return [ | ||||
TextField::new('email') | |||||
EmailField::new('email'), | |||||
TextField::new('lastname'), | |||||
TextField::new('firstname'), | |||||
ChoiceField::new('roles') | |||||
->allowMultipleChoices() | |||||
->autocomplete() | |||||
->setChoices($this->rolesDefinition->getRolesList()) | |||||
]; | ]; | ||||
} | } | ||||
} | } |
<?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; | |||||
} | |||||
} |
<?php | |||||
namespace Lc\SovBundle\Definition; | |||||
interface RolesDefinitionInterface | |||||
{ | |||||
public function getRoles(): array; | |||||
public function getRole($role): ?array; | |||||
public function getRolesList(): array; | |||||
} |
public function create(EntityInterface $entity): self | public function create(EntityInterface $entity): self | ||||
{ | { | ||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_CREATE_EVENT); | |||||
$this->persist($entity); | $this->persist($entity); | ||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::CREATE_EVENT); | |||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_CREATE_EVENT); | |||||
return $this; | return $this; | ||||
} | } | ||||
public function update(EntityInterface $entity): self | public function update(EntityInterface $entity): self | ||||
{ | { | ||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_UPDATE_EVENT); | |||||
$this->persist($entity); | $this->persist($entity); | ||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::UPDATE_EVENT); | |||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_UPDATE_EVENT); | |||||
return $this; | return $this; | ||||
} | } | ||||
public function delete(EntityInterface $entity): self | public function delete(EntityInterface $entity): self | ||||
{ | { | ||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::PRE_DELETE_EVENT); | |||||
$this->remove($entity); | $this->remove($entity); | ||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::DELETE_EVENT); | |||||
$this->eventDispatcher->dispatch(new EntityManagerEvent($entity), EntityManagerEvent::POST_DELETE_EVENT); | |||||
return $this; | return $this; | ||||
} | } | ||||
public function flush($entity=null): self | |||||
public function flush($entity = null): self | |||||
{ | { | ||||
$this->wrapped->flush($entity); | $this->wrapped->flush($entity); | ||||
*/ | */ | ||||
class EntityManagerEvent extends 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; | protected EntityInterface $entity; | ||||
use Lc\SovBundle\Repository\AbstractRepositoryInterface; | use Lc\SovBundle\Repository\AbstractRepositoryInterface; | ||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||||
class CreateEntityEventSubscriber implements EventSubscriberInterface | |||||
class CommonPropertyEventSubscriber implements EventSubscriberInterface | |||||
{ | { | ||||
protected $em; | protected $em; | ||||
protected $adminUrlGenerator; | protected $adminUrlGenerator; | ||||
public static function getSubscribedEvents() | public static function getSubscribedEvents() | ||||
{ | { | ||||
return [ | return [ | ||||
EntityManagerEvent::CREATE_EVENT => ['createEntity'], | |||||
EntityManagerEvent::PRE_CREATE_EVENT => ['setCommonProperty'], | |||||
]; | ]; | ||||
} | } | ||||
public function createEntity(EntityManagerEvent $event) | |||||
public function setCommonProperty(EntityManagerEvent $event) | |||||
{ | { | ||||
$entity = $event->getEntity(); | $entity = $event->getEntity(); | ||||
$entityRepository = $this->em->getRepository(get_class($entity)); | $entityRepository = $this->em->getRepository(get_class($entity)); |
use Symfony\Component\HttpFoundation\Session\SessionInterface; | use Symfony\Component\HttpFoundation\Session\SessionInterface; | ||||
use Symfony\Component\Translation\TranslatableMessage; | use Symfony\Component\Translation\TranslatableMessage; | ||||
class EasyAdminEventSubscriber implements EventSubscriberInterface | |||||
class FlashMessageAdminEventSubscriber implements EventSubscriberInterface | |||||
{ | { | ||||
protected $session ; | protected $session ; | ||||
<?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); | |||||
} | |||||
} | |||||
} | |||||
} |
return $this; | 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 | * @see UserIn | ||||
*/ | */ | ||||
public function getName(): ?string | public function getName(): ?string | ||||
{ | { | ||||
return $this->getFirstname() . ' ' . strtoupper($this->getLastname()); | |||||
return $this->getFirstname().' '.strtoupper($this->getLastname()); | |||||
} | } | ||||
public function isVerified(): bool | public function isVerified(): bool |