Commit 3a776412 authored by Laurent Lecluse's avatar Laurent Lecluse
Browse files

Correction de bugs et optimisation du chargement des roles

parent 12507291
Loading
Loading
Loading
Loading
+6 −57
Changes for module/Application/src/Provider/Role/RoleProvider.php: 6 added lines, 57 removed lines.
Original line number Diff line number Diff line
@@ -2,18 +2,17 @@

namespace Application\Provider\Role;

use Application\Cache\Traits\CacheContainerTrait;
use Application\Entity\Db\Affectation;
use Application\Entity\Db\Structure;
use Application\Service\Traits\ContextServiceAwareTrait;
use BjyAuthorize\Provider\Role\ProviderInterface;
use Intervenant\Entity\Db\Statut;
use UnicaenApp\Service\EntityManagerAwareInterface;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenAuth\Provider\Privilege\PrivilegeProviderAwareTrait;
use Laminas\Permissions\Acl\Role\RoleInterface;
use Application\Acl\Role;
use Intervenant\Service\StatutServiceAwareTrait;
use UnicaenApp\Traits\SessionContainerTrait;
use Application\Service\Traits\IntervenantServiceAwareTrait;

/**
@@ -26,27 +25,16 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface
{
    use EntityManagerAwareTrait;
    use StatutServiceAwareTrait;
    use SessionContainerTrait;
    use IntervenantServiceAwareTrait;
    use PrivilegeProviderAwareTrait;
    use ContextServiceAwareTrait;
    use CacheContainerTrait;

    /**
     * @var array
     */
    protected $config = [];

    /**
     * @var array
     */
    protected $roles;

    /**
     * @var Structure
     */
    protected $structureSelectionnee;

    /**
     * @var array
     */
@@ -54,18 +42,6 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface



    /**
     * Constructeur.
     *
     * @param array $config
     */
    public function __construct($config = [])
    {
        $this->config = $config;
    }



    /**
     * @return RoleInterface[]
     */
@@ -181,19 +157,17 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface


        // Chargement des rôles par statut d'intervenant
        $si = $this->getCacheContainer()->statutsInfo('getStatutsInfo');
        foreach ($si as $statut) {
            $role = new Role($statut['role-id'], 'user', $statut['role-name']);
        $statuts = $this->getServiceStatut()->getStatuts();
        foreach ($statuts as $statut) {
            $role = new Role($statut->getRoleId(), 'user', $statut->getTypeIntervenant()->getLibelle());

            if ($intervenant) {
                if ($intervenant->getStatut()->getId() == $statut['statut-id']) {
            if ($intervenant && $intervenant->getStatut() == $statut) {
                $role->setIntervenant($intervenant);
                if (isset($rolesPrivileges[$intervenant->getStatut()->getRoleId()])) {
                    $role->initPrivileges($rolesPrivileges[$intervenant->getStatut()->getRoleId()]);
                }
            }
            }
            $roles[$statut['role-id']] = $role;
            $roles[$statut->getRoleId()] = $role;
        }

        $this->getServiceContext()->setInInit(false);
@@ -201,29 +175,4 @@ class RoleProvider implements ProviderInterface, EntityManagerAwareInterface
        return $roles;
    }



    public function getStatutsInfo(): array
    {
        $si      = [];
        $statuts = $this->getServiceStatut()->getList();
        foreach ($statuts as $statut) {
            $si[] = [
                'statut-id' => $statut->getId(),
                'role-id'   => $statut->getRoleId(),
                'role-name' => $statut->getTypeIntervenant()->getLibelle(),
            ];
        }

        return $si;
    }



    public function setStructureSelectionnee(Structure $structureSelectionnee = null)
    {
        $this->structureSelectionnee = $structureSelectionnee;

        return $this;
    }
}
+1 −10
Changes for module/Application/src/Provider/Role/RoleProviderFactory.php: 1 added line, 10 removed lines.
Original line number Diff line number Diff line
@@ -20,19 +20,10 @@ class RoleProviderFactory

    public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
    {
        $config = $container->get('BjyAuthorize\Config');
        $em = $container->get(\Application\Constants::BDD);
        /* @var $em \Doctrine\ORM\EntityManager */

        if (!isset($config['role_providers'][RoleProvider::class])) {
            throw new InvalidArgumentException(
                'Config for "ApplicationRoleProvider" not set'
            );
        }

        $providerConfig = $config['role_providers'][RoleProvider::class];

        $roleProvider = new RoleProvider($providerConfig);
        $roleProvider = new RoleProvider();
        $roleProvider
            ->setEntityManager($em)
            ->setServiceStatut($container->get(StatutService::class))
−6
Changes for module/Application/src/Service/Factory/PrivilegeServiceFactory.php: 0 added lines, 6 removed lines.
Original line number Diff line number Diff line
@@ -36,12 +36,6 @@ class PrivilegeServiceFactory
        $service = new PrivilegeService($privilegesRolesConfig);
        $service->setEntityManager($container->get(Constants::BDD));


        if (!isset($config['unicaen-auth']['privilege_entity_class'])) {
            $config['unicaen-auth']['privilege_entity_class'] = Privilege::class;
        }
        $service->setPrivilegeEntityClass($config['unicaen-auth']['privilege_entity_class']);

        return $service;
    }
}
 No newline at end of file
+30 −9
Changes for module/Application/src/Service/ParametresService.php: 30 added lines, 9 removed lines.
Original line number Diff line number Diff line
@@ -12,7 +12,8 @@ use LogicException;
 *
 * @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
 */
class ParametresService extends AbstractService {
class ParametresService extends AbstractService
{

    /**
     *
@@ -20,22 +21,25 @@ class ParametresService extends AbstractService {
     */
    protected $cache = [];



    protected function getCache($param = null)
    {
        if (!$this->cache) {
            $repository = $this->getEntityManager()->getRepository(Parametre::class);
            /* @var $repository \Doctrine\ORM\EntityRepository */
            $list = $repository->findAll();

            foreach( $list as $entity ){ /* @var $entity \Application\Entity\Db\Parametre */
                $this->cache[$entity->getNom()] = $entity->getValeur();
            $sql  = 'SELECT nom, valeur FROM parametre';
            $list = $this->getEntityManager()->getConnection()->fetchAllAssociative($sql);
            foreach ($list as $p) {
                $this->cache[$p['NOM']] = $p['VALEUR'];
            }
        }
        if ($param)
        if ($param) {
            return isset($this->cache[$param]) ? $this->cache[$param] : null;
        else
        } else {
            return $this->cache;
        }
    }



    /**
     * Retourne la liste des paramètres de configuration de OSE
@@ -47,10 +51,13 @@ class ParametresService extends AbstractService {
        return $this->getCache();
    }



    /**
     * Retourne la description d'un paramètre
     *
     * @param string $param
     *
     * @return string
     * @throws LogicException
     */
@@ -63,13 +70,17 @@ class ParametresService extends AbstractService {
        if (empty($result)) {
            throw new LogicException('Le paramètre "' . $param . '" est invalide.');
        }

        return $result[0]->getDescription();
    }



    /**
     * Retourne un paramètre
     *
     * @param string $param
     *
     * @return string
     */
    public function get($param)
@@ -77,11 +88,14 @@ class ParametresService extends AbstractService {
        return $this->getCache($param);
    }



    /**
     * Affecte une valeur à un paramètre
     *
     * @param string $param
     * @param string $value
     *
     * @return self
     */
    public function set($param, $value)
@@ -96,13 +110,17 @@ class ParametresService extends AbstractService {
        $result[0]->setValeur($value);
        $this->cache[$param] = $value;
        $this->getEntityManager()->flush();

        return $this;
    }



    /**
     * Getter
     *
     * @param string $name
     *
     * @return string
     */
    public function __get($name)
@@ -110,11 +128,14 @@ class ParametresService extends AbstractService {
        return $this->get($name);
    }



    /**
     * Setter
     *
     * @param string $name
     * @param string $value
     *
     * @return self
     */
    public function __set($name, $value)
+27 −8
Changes for module/Application/src/Service/PrivilegeService.php: 27 added lines, 8 removed lines.
Original line number Diff line number Diff line
@@ -4,17 +4,24 @@ namespace Application\Service;

use Application\Entity\Db\Annee;
use Application\Entity\Db\Role;
use Application\Provider\Privilege\Privileges;
use Application\Service\Traits\ContextServiceAwareTrait;
use BjyAuthorize\Provider\Resource\ProviderInterface;
use Intervenant\Entity\Db\Statut;
use Intervenant\Service\StatutServiceAwareTrait;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenAuth\Provider\Privilege\PrivilegeProviderInterface;

/**
 * Description of Privilege
 *
 * @author Laurent LÉCLUSE <laurent.lecluse at unicaen.fr>
 */
class PrivilegeService extends \UnicaenAuth\Service\PrivilegeService
class PrivilegeService implements PrivilegeProviderInterface, ProviderInterface
{
    use EntityManagerAwareTrait;
    use ContextServiceAwareTrait;
    use StatutServiceAwareTrait;


    private array $privilegesCache       = [];
@@ -47,8 +54,7 @@ class PrivilegeService extends \UnicaenAuth\Service\PrivilegeService
    public function getPrivilegesRoles()
    {
        if (empty($this->privilegesCache)) {
            $annee                 = $this->getServiceContext()->getAnnee();
            $this->privilegesCache = $this->makePrivilegesRoles($annee);
            $this->privilegesCache = $this->makePrivilegesRoles();
        }

        return $this->privilegesCache;
@@ -56,7 +62,23 @@ class PrivilegeService extends \UnicaenAuth\Service\PrivilegeService



    public function makePrivilegesRoles(Annee $annee)
    /**
     * @return array
     */
    public function getResources()
    {
        $resources  = [];
        $privileges = array_keys($this->getPrivilegesRoles());
        foreach ($privileges as $privilege) {
            $resources[] = Privileges::getResourceId($privilege);
        }

        return $resources;
    }



    public function makePrivilegesRoles()
    {
        $privilegesRoles = $this->privilegesRolesConfig;

@@ -87,10 +109,7 @@ class PrivilegeService extends \UnicaenAuth\Service\PrivilegeService
            $privilegesRoles[$privilege][] = $role;
        }

        $dql   = "SELECT s FROM " . Statut::class . " s WHERE s.annee = :annee";
        $query = $this->getEntityManager()->createQuery($dql)->setParameter('annee', $annee);
        /** @var Statut[] $statuts */
        $statuts = $query->getResult();
        $statuts = $this->getServiceStatut()->getStatuts();
        foreach ($statuts as $statut) {
            $sp = $statut->getPrivileges();
            foreach ($sp as $privilege => $has) {
Loading