Commit 7099605c authored by Bertrand Gauthier's avatar Bertrand Gauthier
Browse files

WIP supressions profils

parent f120f82f
Loading
Loading
Loading
Loading
+0 −9
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@

use Application\Controller\DroitsController;
use Application\Controller\Factory\DroitsControllerFactory;
use Application\Controller\ProfilController;
use Application\Entity\Db\Privilege;
use Application\Entity\Db\Role;
use UnicaenPrivilege\Guard\PrivilegeController;
@@ -91,14 +90,6 @@ return [
                                        'resource' => PrivilegeController::getResourceId(RoleController::class, 'index'),
                                        'withtarget' => true,
                                    ],
                                    'profil' => [
                                        'label' => "Profils et privilèges",
                                        'title' => "Profils et privilèges",
                                        'route' => 'profil',
                                        'query' => ['depend' => ProfilController::PERIMETRE_Aucun],
                                        'resource' => PrivilegeController::getResourceId(ProfilController::class, 'index'),
                                        'withtarget' => true,
                                    ],
                                    'privileges' => [
                                        'label' => "Rôles et privilèges",
                                        'title' => "Rôles et privilèges",
+0 −170
Original line number Diff line number Diff line
<?php

namespace Application;

use Application\Controller\Factory\ProfilControllerFactory;
use Application\Controller\ProfilController;
use Application\Form\Factory\ProfilFormFactory;
use Application\Form\Factory\ProfilHydratorFactory;
use Application\Form\Hydrator\ProfilHydrator;
use Application\Form\ProfilForm;
use Application\Service\Profil\ProfilService;
use Application\Service\Profil\ProfilServiceFactory;
use UnicaenPrivilege\Guard\PrivilegeController;
use UnicaenPrivilege\Provider\Privilege\Privileges;
use Laminas\Router\Http\Literal;
use Laminas\Router\Http\Segment;

return [
    'bjyauthorize' => [
        'guards' => [
            PrivilegeController::class => [
                [
                    'controller' => ProfilController::class,
                    'action' => [
                        'index',
                    ],
                    'privileges' => [
                        \UnicaenPrivilege\Provider\Privilege\PrivilegePrivileges::PRIVILEGE_VOIR,
                        \UnicaenPrivilege\Provider\Privilege\PrivilegePrivileges::PRIVILEGE_AFFECTER,
                    ],
                ],
                [
                    'controller' => ProfilController::class,
                    'action' => [
                        'editer',
                        'supprimer',
                        'gerer-roles',
                        'ajouter-role',
                        'retirer-role',
                        'dupliquer-privileges',
                        'modifier-profil-privilege'
                    ],
                    'privileges' => [
                        \UnicaenPrivilege\Provider\Privilege\PrivilegePrivileges::PRIVILEGE_MODIFIER
                    ],
                ],
            ],
        ],
    ],
    'router' => [
        'routes' => [
            'profil' => [
                'type'          => Literal::class,
                'options'       => [
                    'route'    => '/profil',
                    'defaults' => [
                        'controller'    => ProfilController::class,
                        'action'        => 'index',
                    ],
                ],
                'may_terminate' => true,
                'child_routes' => [
                    'editer' => [
                        'type'          => Segment::class,
                        'options'       => [
                            'route'    => '/editer[/:profil]',
                            'defaults' => [
                                'controller'    => ProfilController::class,
                                'action'        => 'editer',
                            ],
                        ],
                        'may_terminate' => true,
                    ],
                    'supprimer' => [
                        'type'          => Segment::class,
                        'options'       => [
                            'route'    => '/supprimer/:profil',
                            'defaults' => [
                                'controller'    => ProfilController::class,
                                'action'        => 'supprimer',
                            ],
                        ],
                        'may_terminate' => true,
                    ],
                    'dupliquer' => [
                        'type'          => Segment::class,
                        'options'       => [
                            'route'    => '/dupliquer/:profil',
                            'defaults' => [
                                'controller'    => ProfilController::class,
                                'action'        => 'dupliquer-privileges',
                            ],
                        ],
                        'may_terminate' => true,
                    ],
                    'gerer-roles' => [
                        'type'          => Segment::class,
                        'options'       => [
                            'route'    => '/gerer-roles/:profil',
                            'defaults' => [
                                'controller'    => ProfilController::class,
                                'action'        => 'gerer-roles',
                            ],
                        ],
                        'may_terminate' => true,
                        'child_routes' => [
                            'retirer' => [
                                'type'          => Segment::class,
                                'options'       => [
                                    'route'    => '/retirer/:role',
                                    'defaults' => [
                                        'controller'    => ProfilController::class,
                                        'action'        => 'retirer-role',
                                    ],
                                ],
                                'may_terminate' => true,
                            ],
                            'ajouter' => [
                                'type'          => Literal::class,
                                'options'       => [
                                    'route'    => '/ajouter',
                                    'defaults' => [
                                        'controller'    => ProfilController::class,
                                        'action'        => 'ajouter-role',
                                    ],
                                ],
                                'may_terminate' => true,
                            ],
                        ],
                    ],
                ],
            ],
            'modifier-profil-privilege' => [
                'type'          => Segment::class,
                'options'       => [
                    'route'    => '/modifier-profil-privilege/:profil/:privilege',
                    'defaults' => [
                        /** @see ProfilController::modifierProfilPrivilegeAction() */
                        'controller'    => ProfilController::class,
                        'action'        => 'modifier-profil-privilege',
                    ],
                ],
                'may_terminate' => true,
            ],
        ],
    ],
    'navigation' => [
    ],
    'service_manager' => [
        'factories' => [
            ProfilService::class => ProfilServiceFactory::class,
        ],
    ],
    'controllers'     => [
        'factories' => [
            ProfilController::class => ProfilControllerFactory::class,
        ],
    ],

    'form_elements'   => [
        'factories' => [
            ProfilForm::class => ProfilFormFactory::class,
        ],
    ],
    'hydrators' => [
        'factories' => [
            ProfilHydrator::class => ProfilHydratorFactory::class,
        ]
    ],
];
 No newline at end of file
+0 −45
Original line number Diff line number Diff line
<?php

namespace Application\Controller\Factory;

use Application\Controller\ProfilController;
use Application\Form\ProfilForm;
use Application\Service\Profil\ProfilService;
use Application\Service\Role\RoleService;
use Interop\Container\ContainerInterface;
use UnicaenPrivilege\Service\Privilege\PrivilegeCategorieService;
use UnicaenPrivilege\Service\Privilege\PrivilegeService;

class ProfilControllerFactory {

    /**
     * @throws \Psr\Container\NotFoundExceptionInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     */
    public function __invoke(ContainerInterface $container): ProfilController
    {
        /**
         * @var PrivilegeService $privilegeService
         * @var ProfilService $profilService
         * @var RoleService $roleService
         */
        $privilegeService = $container->get(PrivilegeService::class);
        $profilService = $container->get(ProfilService::class);
        $roleService = $container->get(RoleService::class);

        /** @var ProfilForm $profilForm */
        $profilForm = $container->get('FormElementManager')->get(ProfilForm::class);

        $controller = new ProfilController();
        $controller->setPrivilegeService($privilegeService);
        $controller->setProfilService($profilService);
        $controller->setApplicationRoleService($roleService);
        $controller->setProfilForm($profilForm);

        /** @var PrivilegeCategorieService $categoriesPrivilegeService */
        $categoriesPrivilegeService = $container->get(PrivilegeCategorieService::class);
        $controller->setprivilegeCategorieService($categoriesPrivilegeService);

        return $controller;
    }
}
 No newline at end of file
+1 −11
Original line number Diff line number Diff line
@@ -39,13 +39,11 @@ class PrivilegeController extends AbstractController

        $qbRoles = $this->applicationRoleService->getRepository()->createQueryBuilder("r");
        $qbRoles
            ->addSelect('p')
            ->leftJoin('r.profils', 'p')
            ->addSelect('s')
            ->leftJoin('r.structure', 's')
            ->addSelect('ts')
            ->leftJoin('s.typeStructure', 'ts')
            ->orderBy("r.typeStructureDependant, r.ordreAffichage, r.structure", 'asc');
            ->orderBy("r.code, s.libelle", 'asc');
        $this->applyFilterDependance($qbRoles, $depend);
        $this->applyFilterRole($qbRoles, $role);
        /** @var Role[] $roles */
@@ -95,7 +93,6 @@ class PrivilegeController extends AbstractController
    public function modifierAction(): JsonModel
    {
        $qb = $this->applicationRoleService->getRepository()->createQueryBuilder('r')
            ->leftJoin('r.profils', 'p')->addSelect('p')
            ->where('r = :role')->setParameter('role', $this->params()->fromRoute('role'));
        try {
            /** @var Role $role */
@@ -107,13 +104,6 @@ class PrivilegeController extends AbstractController
            ]);
        }

        if ($role->getProfil()) {
            return new JsonModel([
                'success' => false,
                'error' => "Seuls les rôles sans profil sont concernés !",
            ]);
        }

        $qb = $this->privilegeService->getRepo()->createQueryBuilder('p')
            ->leftJoin('p.roles', 'r')->addSelect('r')
            ->where('p = :privilege')->setParameter('privilege', $this->params()->fromRoute('privilege'));
+0 −293
Original line number Diff line number Diff line
<?php

namespace Application\Controller;

use Application\Entity\Db\Privilege;
use Application\Entity\Db\Profil;
use Application\Entity\Db\Role;
use Application\Form\ProfilForm;
use Application\Service\Profil\ProfilServiceAwareTrait;
use Application\Service\Role\ApplicationRoleServiceAwareTrait;
use Doctrine\ORM\QueryBuilder;
use Exception;
use UnicaenPrivilege\Service\Privilege\PrivilegeCategorieServiceAwareTrait;
use UnicaenPrivilege\Service\Privilege\PrivilegeServiceAwareTrait;
use Laminas\Http\Request;
use Laminas\Http\Response;
use Laminas\Mvc\Controller\AbstractActionController;
use Laminas\View\Model\JsonModel;
use Laminas\View\Model\ViewModel;

class ProfilController extends AbstractActionController
{
    use PrivilegeCategorieServiceAwareTrait;
    use PrivilegeServiceAwareTrait;
    use ProfilServiceAwareTrait;
    use ApplicationRoleServiceAwareTrait;

    public const PERIMETRE_ED = "ED";
    public const PERIMETRE_UR = "UR";
    public const PERIMETRE_Etab = "Etab";
    public const PERIMETRE_Aucun = "Aucun";

    /**
     * @var ProfilForm
     */
    private $profilForm;

    /**
     * @param ProfilForm $profilForm
     */
    public function setProfilForm(ProfilForm $profilForm)
    {
        $this->profilForm = $profilForm;
    }

    public function indexAction(): ViewModel
    {
        $depend = $this->params()->fromQuery("depend");
        $categorie = $this->params()->fromQuery("categorie");
        $profil = $this->params()->fromQuery("profil");

        $qbProfils = $this->profilService->getRepository()->createQueryBuilder("profil");
        $qbProfils
            ->addSelect('ts')->leftJoin('profil.structureType', 'ts')
            ->orderBy("profil.structureType, profil.libelle", 'asc');
        $this->applyFilterDependance($qbProfils, $depend);
        $this->applyFilterProfil($qbProfils, $profil);
        /** @var Profil[] $profils */
        $profils = $qbProfils->getQuery()->execute();

        $qbPrivileges = $this->getPrivilegeService()->getRepo()->createQueryBuilder("p");
        $qbPrivileges
            ->addSelect('profil')->leftJoin('p.profils', 'profil')
            ->orderBy("p.categorie, p.ordre", "ASC");
        $this->applyFilterCategorie($qbPrivileges, $categorie);
        /** @var Privilege[] $privileges */
        $privileges = $qbPrivileges->getQuery()->execute();

        return new ViewModel([
            'profils' => $profils,
            'privileges' => $privileges,
            'categoriesPrivilegesForFilter' => $this->fetchCategoriesPrivilegesForFilter(),
            'profilsForFilter' => $this->fetchProfilsForFilter($depend),
            'params' => $this->params()->fromQuery(),
        ]);
    }

    private function applyFilterDependance(QueryBuilder $qb, $depend): QueryBuilder
    {
        switch ($depend) {
            case self::PERIMETRE_ED:
                $qb->andWhere("profil.structureType = :type")->setParameter("type", "2");
                break;
            case self::PERIMETRE_UR:
                $qb->andWhere("profil.structureType = :type")->setParameter("type", "3");
                break;
            case self::PERIMETRE_Etab:
                $qb->andWhere("profil.structureType = :type")->setParameter("type", "1");
                break;
            case self::PERIMETRE_Aucun:
                $qb->andWhere("profil.structureType IS NULL");
                break;
            default:
                break;
        }

        return $qb;
    }

    private function applyFilterCategorie(QueryBuilder $qb, $categorie): QueryBuilder
    {
        $qb->leftJoin('p.categorie', "cp");
        if ($categorie !== null && $categorie !== "") {
            $qb
                ->andWhere("cp.code = :type")
                ->setParameter("type", $categorie);
        }

        return $qb;
    }

    private function applyFilterProfil(QueryBuilder $qb, $profil): QueryBuilder
    {
        if ($profil !== null && $profil !== "") {
            $qb
                ->andWhere("profil.libelle = :profil")
                ->setParameter("profil", $profil);
        }

        return $qb;
    }

    private function fetchCategoriesPrivilegesForFilter(): array
    {
        return $this->privilegeCategorieService->findAll(['libelle' => 'asc']);
    }

    private function fetchProfilsForFilter($depend): array
    {
        // pour filtre par (libellé de) rôle
        $qb = $this->profilService->getRepository()->createQueryBuilder("profil");
        $qb
            ->orderBy('profil.libelle');

        $this->applyFilterDependance($qb, $depend);

        /** @var Profil[] $profils */
        $profils = $qb->getQuery()->getResult();

        return $profils;
    }

    public function editerAction() {
        /** @var Profil $profil */
        $profilId = $this->params()->fromRoute('profil');

        $profil = null;
        if($profilId)   $profil = $this->getProfilService()->getProfil($profilId);
        else            $profil = new Profil();

        /** @var ProfilForm $form */
        $form = $this->profilForm;
        $form->setAttribute('action', $this->url()->fromRoute('profil/editer', ['profil' => $profil->getId()], [], true));
        $form->bind($profil);

        /** @var Request $request */
        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = $request->getPost();
            $form->setData($data);
            if ($form->isValid()) {
                if ($profilId)  $this->getProfilService()->update($profil);
                else            $this->getProfilService()->create($profil);
                $this->redirect()->toRoute('profil', [], [], true);
            }
        }

        return new ViewModel([
            'title' => 'Édition d\'un profil',
            'form' => $form,
        ]);
    }

    public function supprimerAction() {
        /** @var Profil $profil */
        $profilId = $this->params()->fromRoute('profil');
        $profil = $this->getProfilService()->getProfil($profilId);

        if ($profil) {
            $this->getProfilService()->delete($profil);
        }

        $this->redirect()->toRoute('profil', [], [], true);

    }

    public function modifierProfilPrivilegeAction(): JsonModel
    {
        $privilegeId = $this->params()->fromRoute("privilege");
        $profilId = $this->params()->fromRoute("profil");

        /** @var Profil $profil */
        $profil = $this->getProfilService()->getRepository()->find($profilId);
        /** @var Privilege $privilege */
        $privilege = $this->getPrivilegeService()->getRepo()->find($privilegeId);

        try {
            $value = $this->profilService->inverserPrivilegeForProfil($profil, $privilege);
        } catch (Exception $e) {
            return new JsonModel([
                'success' => false,
                'error' => "Un problème lors du changement de rôle s'est produit : " . $e->getMessage(),
            ]);
        }

        return new JsonModel([
            'success' => true,
            'value' => $value,
        ]);
    }

    public function gererRolesAction(): ViewModel
    {
        /** @var Profil $profil */
        $profilId   = $this->params()->fromRoute('profil');
        $profil     = $this->getProfilService()->getProfil($profilId);

        $roles      = $this->getApplicationRoleService()->findRolesSansProfil();

        return new ViewModel([
            'profil' => $profil,
            'rolesDisponibles' => $roles,
        ]);
    }

    public function ajouterRoleAction(): Response
    {
        /** @var Profil $profil */
        $profilId = $this->params()->fromRoute('profil');
        $profil = $this->getProfilService()->getProfil($profilId);

        /** @var Request $request */
        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = $request->getPost();
            /** @var Role $role */
            $roleId = $data['role'] ?? null;
            if ($roleId) {
                $role = $this->getApplicationRoleService()->getRepository()->find($roleId);

                if (!$profil->hasRole($role)) {
                    $profil->addRole($role);
                    $this->getProfilService()->update($profil);
                    $this->getProfilService()->applyProfilToRole($profil, $role);
                }
            }
        }

        return $this->redirect()->toRoute('profil/gerer-roles', ['profil' => $profil->getId()], [], true);
    }

    public function retirerRoleAction()
    {
        /** @var Profil $profil */
        $profilId = $this->params()->fromRoute('profil');
        $profil = $this->getProfilService()->getProfil($profilId);

        /** @var Role $role */
        $roleId = $this->params()->fromRoute('role');
        $role = $this->getApplicationRoleService()->getRepository()->find($roleId);

        if ($profil->hasRole($role)) {
            $profil->removeRole($role);
            $this->getProfilService()->update($profil);
        }

        $this->redirect()->toRoute('profil/gerer-roles', ['profil' => $profil->getId()], [], true);
    }

    public function dupliquerPrivilegesAction()
    {
        /** @var Profil $profil */
        $profilId = $this->params()->fromRoute('profil');
        $profil = $this->getProfilService()->getProfil($profilId);

        /** @var Request $request */
        $request = $this->getRequest();
        if ($request->isPost()) {
            $data = $request->getPost();
            $profilFrom = $this->getProfilService()->getProfil($data['profil']);

            $this->getProfilService()->copyPrivilegeFrom($profilFrom, $profil);
            $this->redirect()->toRoute('profil', [], [], true);
        }

        $profils = $this->getProfilService()->getProfils();
        return new ViewModel([
            'title' => 'Sélectionner un profil pour dupliquer les privilèges',
            'profil' => $profil,
            'profils' => $profils,
        ]);
    }
}
 No newline at end of file
Loading