Browse Source

Merge branch 'develop' of https://gitea.laclic.fr/Laclic/SovBundle into develop

feature/ticket
Guillaume 3 years ago
parent
commit
85b8bfee8c
11 changed files with 174 additions and 19 deletions
  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 View File

$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) {

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

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();

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



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())
]; ];
} }

} }

+ 47
- 0
Definition/RolesDefinition.php View File

<?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 View File

<?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 View File



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);



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

*/ */
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;



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

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));

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

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 ;



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

<?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 View File

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

Loading…
Cancel
Save