Commit 719b9ca4 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Ajustement des privilèges d'un rôle lorsque celui-ci reçoit un profil

parent dd4805ad
Loading
Loading
Loading
Loading
+1 −1
Changes for module/Application/src/Application/Controller/ProfilController.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -133,7 +133,7 @@ class ProfilController extends AbstractActionController {
            if (! $profil->hasRole($role)) {
                $profil->addRole($role);
                $this->getProfilService()->update($profil);
//                $this->getProfilService()->applyProfilToRole($profil, $role);
                $this->getProfilService()->applyProfilToRole($profil, $role);
            }
        }

+1 −1
Changes for module/Application/src/Application/Entity/Db/Mapping/Application.Entity.Db.Privilege.dcm.xml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@
            </join-table>
        </many-to-many>

        <many-to-many target-entity="Application\Entity\Db\Profil" field="modeles">
        <many-to-many target-entity="Application\Entity\Db\Profil" field="profils">
            <join-table name="PROFIL_PRIVILEGE">
                <join-columns>
                    <join-column name="PRIVILEGE_ID" referenced-column-name="ID"/>
+25 −7
Changes for module/Application/src/Application/Entity/Db/Privilege.php: 25 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -4,24 +4,25 @@ namespace Application\Entity\Db;

use Doctrine\Common\Collections\ArrayCollection;
use UnicaenAuth\Entity\Db\AbstractPrivilege;
use UnicaenAuth\Entity\Db\RoleInterface;

class Privilege extends AbstractPrivilege
{
    /** @var ArrayCollection */
    private $modeles;
    private $profils;

    public function __construct()
    {
        parent::__construct();
        $this->modeles = new ArrayCollection();
        $this->profils = new ArrayCollection();
    }

    /**
     * @return ArrayCollection
     */
    public function getRolesModeles()
    public function getProfils()
    {
        return $this->modeles;
        return $this->profils;
    }

    /**
@@ -30,7 +31,7 @@ class Privilege extends AbstractPrivilege
     */
    public function addProfil($profil)
    {
        $this->modeles->add($profil);
        $this->profils->add($profil);
        return $this;
    }

@@ -40,7 +41,7 @@ class Privilege extends AbstractPrivilege
     */
    public function removeProfil($profil)
    {
        $this->modeles->removeElement($profil);
        $this->profils->removeElement($profil);
        return $this;
    }

@@ -50,6 +51,23 @@ class Privilege extends AbstractPrivilege
     */
    public function hasProfil($profil)
    {
        return $this->modeles->contains($profil);
        return $this->profils->contains($profil);
    }

    /**
     * @return ArrayCollection
     */
    public function getRoles()
    {
        return $this->role;
    }

    /**
     * @param RoleInterface $role
     * @return bool
     */
    public function hasRole($role)
    {
        return $this->role->contains($role);
    }
}
+39 −0
Changes for module/Application/src/Application/Service/Profil/ProfilService.php: 39 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -2,14 +2,18 @@

namespace Application\Service\Profil;

use Application\Entity\Db\Privilege;
use Application\Entity\Db\Profil;
use Application\Entity\Db\Role;
use Doctrine\ORM\NonUniqueResultException;
use Doctrine\ORM\OptimisticLockException;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenAuth\Service\Traits\PrivilegeServiceAwareTrait;

class ProfilService {
    use EntityManagerAwareTrait;
    use PrivilegeServiceAwareTrait;

    /**
     * @return Profil[]
@@ -83,4 +87,39 @@ class ProfilService {
            throw new RuntimeException("Un problème s'est produit lors de la suppression d'un Profil", $e);
        }
    }

    /**
     * Lors de l'affectation d'un profil à un rôle :
     * 1 - retrait des anciens privilèges accordés ;
     * 2 - affectation des privilèges du nouveau profil.
     * @param Profil $profil
     * @param Role $role
     */
    public function applyProfilToRole($profil, $role)
    {
        // Comment accéder à la liste des privilèges d'un rôle ??? (la relation est unidirectionnelle est c'est génant ici) ...
//        foreach ($role->getPrivileges() as $privilege) {

        $privileges = $this->getServicePrivilege()->getRepo()->findAll();
        /** @var Privilege $privilege */
        foreach ($privileges as $privilege) {
            if ($privilege->hasRole($role)) {
                $privilege->removeRole($role);
                try {
                    $this->getServicePrivilege()->getEntityManager()->flush($privilege);
                } catch (OptimisticLockException $e) {
                    throw new RuntimeException("Un problème est survenu lors du retrait d'un privilège", $e);
                }
            }
        }

        foreach ($profil->getPrivileges() as $privilege) {
            $privilege->addRole($role);
            try {
                $this->getServicePrivilege()->getEntityManager()->flush($privilege);
            } catch (OptimisticLockException $e) {
                throw new RuntimeException("Un problème est survenu lors de l'ajout d'un privilège", $e);
            }
        }
    }
}
 No newline at end of file
+7 −1
Changes for module/Application/src/Application/Service/Profil/ProfilServiceFactory.php: 7 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -3,18 +3,24 @@
namespace Application\Service\Profil;

use Doctrine\ORM\EntityManager;
use UnicaenAuth\Service\PrivilegeService;
use Zend\ServiceManager\ServiceLocatorInterface;

class ProfilServiceFactory {

    public function __invoke(ServiceLocatorInterface $serviceLocator)
    {
        /** @var EntityManager $entityManager */
        /**
         * @var EntityManager $entityManager
         * @var PrivilegeService $privilegeService
         */
        $entityManager = $serviceLocator->get('doctrine.entitymanager.orm_default');
        $privilegeService = $serviceLocator->get('UnicaenAuth\Service\Privilege');

        /** @var ProfilService $service */
        $service = new ProfilService();
        $service->setEntityManager($entityManager);
        $service->setServicePrivilege($privilegeService);
        return $service;
    }
}
 No newline at end of file