Commit 56b82fd8 authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Desaffection du profil si le role evolue de son coté

parent 719b9ca4
Loading
Loading
Loading
Loading
+19 −2
Changes for module/Application/src/Application/Controller/PrivilegeController.php: 19 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -7,7 +7,9 @@ use Application\Entity\Db\Role;
use Application\Service\Etablissement\EtablissementServiceAwareTrait;
use Application\Service\Role\RoleServiceAwareTrait;
use Application\Service\Structure\StructureServiceAwareTrait;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\QueryBuilder;
use UnicaenApp\Exception\RuntimeException;
use UnicaenApp\Service\EntityManagerAwareTrait;
use UnicaenAuth\Entity\Db\CategoriePrivilege;
use UnicaenAuth\Service\Traits\PrivilegeServiceAwareTrait;
@@ -58,19 +60,34 @@ class PrivilegeController extends AbstractController
    {
        $privilege_id = $this->params()->fromRoute("privilege");
        $role_id = $this->params()->fromRoute("role");
        /**
         * @var Privilege $privilege
         * @var Role $role
         */
        $privilege = $this->entityManager->getRepository(Privilege::class)->find($privilege_id);
        $role = $this->entityManager->getRepository(Role::class)->find($role_id);

        $value = null;
        if( array_search($role, $privilege->getRole()->toArray()) !== false) {
            $privilege->removeRole($role);
            $this->entityManager->flush($privilege);
            try {
                $this->getEntityManager()->flush($privilege);
            } catch (OptimisticLockException $e) {
                throw new RuntimeException("Un problème est survenu lors de la suppression du privilège.",$e);
            }
            $value = 0;
        } else {
            $privilege->addRole($role);
            $this->entityManager->flush($privilege);
            try {
                $this->getEntityManager()->flush($privilege);
            } catch (OptimisticLockException $e) {
                throw new RuntimeException("Un problème est survenu lors de l'ajout du privilège.",$e);
            }
            $value = 1;
        }
        // retrait des profils associés à un role
        $this->getRoleService()->removeProfils($role);


        $queryParams = $this->params()->fromQuery();
        return new ViewModel([
+3 −2
Changes for module/Application/src/Application/Service/Profil/ProfilService.php: 3 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -89,6 +89,7 @@ class ProfilService {
    }

    /**
     * // TODO : /?\ DQL plutôt que cela /?\
     * 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.
@@ -106,7 +107,7 @@ class ProfilService {
            if ($privilege->hasRole($role)) {
                $privilege->removeRole($role);
                try {
                    $this->getServicePrivilege()->getEntityManager()->flush($privilege);
                    $this->getEntityManager()->flush($privilege);
                } catch (OptimisticLockException $e) {
                    throw new RuntimeException("Un problème est survenu lors du retrait d'un privilège", $e);
                }
@@ -116,7 +117,7 @@ class ProfilService {
        foreach ($profil->getPrivileges() as $privilege) {
            $privilege->addRole($role);
            try {
                $this->getServicePrivilege()->getEntityManager()->flush($privilege);
                $this->getEntityManager()->flush($privilege);
            } catch (OptimisticLockException $e) {
                throw new RuntimeException("Un problème est survenu lors de l'ajout d'un privilège", $e);
            }
+15 −0
Changes for module/Application/src/Application/Service/Role/RoleService.php: 15 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -323,6 +323,21 @@ class RoleService extends BaseService
        return $roles;
    }

    /**
     * @param Role $role
     */
    public function removeProfils($role)
    {
        foreach ($role->getProfils() as $profil) {
            $role->removeProfil($profil);
            try {
                $this->getEntityManager()->flush($role);
            } catch (OptimisticLockException $e) {
                throw new RuntimeException("Un problème est survenu lors de la déassociation entre le role et le profil.",$e);
            }
        }
    }

//SELECT * FROM ROLE R
//JOIN STRUCTURE S on R.STRUCTURE_ID = S.ID
//LEFT JOIN STRUCTURE_SUBSTIT SS on S.ID = SS.FROM_STRUCTURE_ID