Commit 76cd148c authored by Jean-Philippe Metivier's avatar Jean-Philippe Metivier
Browse files

Création d'action pour les ajouts et suppressions Role/Profil (pour simplifier...

Création d'action pour les ajouts et suppressions Role/Profil (pour simplifier les démarches suivantes)
parent 53231343
Loading
Loading
Loading
Loading
+27 −0
Changes for module/Application/config/others/profil.config.php: 27 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ return [
                        'editer',
                        'supprimer',
                        'gerer-roles',
                        'ajouter-role',
                        'retirer-role',
                        'modifier-profil-privilege'
                    ],
                    'privileges' => [
@@ -79,6 +81,7 @@ return [
                        ],
                        'may_terminate' => true,
                    ],

                    'gerer-roles' => [
                        'type'          => Segment::class,
                        'options'       => [
@@ -89,6 +92,30 @@ return [
                            ],
                        ],
                        '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,
                            ],
                        ],
                    ],
                ],
            ],
+4 −0
Changes for module/Application/src/Application/Controller/Factory/ProfilControllerFactory.php: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ namespace Application\Controller\Factory;

use Application\Controller\ProfilController;
use Application\Service\Profil\ProfilService;
use Application\Service\Role\RoleService;
use UnicaenAuth\Service\PrivilegeService;
use Zend\Mvc\Controller\ControllerManager;

@@ -14,14 +15,17 @@ class ProfilControllerFactory {
        /**
         * @var PrivilegeService $privilegeService
         * @var ProfilService $profilService
         * @var RoleService $roleService
         */
        $privilegeService = $controllerManager->getServiceLocator()->get('UnicaenAuth\Service\Privilege');
        $profilService = $controllerManager->getServiceLocator()->get(ProfilService::class);
        $roleService = $controllerManager->getServiceLocator()->get(RoleService::class);

        /** @var ProfilController $controller */
        $controller = new ProfilController();
        $controller->setServicePrivilege($privilegeService);
        $controller->setProfilService($profilService);
        $controller->setRoleService($roleService);
        return $controller;
    }
}
 No newline at end of file
+47 −0
Changes for module/Application/src/Application/Controller/ProfilController.php: 47 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -4,8 +4,10 @@ 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\RoleServiceAwareTrait;
use Doctrine\ORM\OptimisticLockException;
use UnicaenApp\Exception\RuntimeException;
use UnicaenAuth\Service\Traits\PrivilegeServiceAwareTrait;
@@ -17,6 +19,7 @@ use Zend\View\Model\ViewModel;
class ProfilController extends AbstractActionController {
    use PrivilegeServiceAwareTrait;
    use ProfilServiceAwareTrait;
    use RoleServiceAwareTrait;

    public function indexAction()
    {
@@ -105,8 +108,52 @@ class ProfilController extends AbstractActionController {
        $profilId   = $this->params()->fromRoute('profil');
        $profil     = $this->getProfilService()->getProfil($profilId);

        $roles      = $this->getRoleService()->getRoles();

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

    public function ajouterRoleAction()
    {
        /** @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'];
            $role = $this->getRoleService()->getRepository()->find($roleId);

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

        $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->getRoleService()->getRepository()->find($roleId);

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

        $this->redirect()->toRoute('profil/gerer-roles', ['profil' => $profil->getId()], [], true);
    }
}
 No newline at end of file
+41 −14
Changes for module/Application/view/application/profil/gerer-roles.phtml: 41 added lines, 14 removed lines.
Original line number Diff line number Diff line
<?php

/** @var \Application\Entity\Db\Profil $profil */
/**
 * @var \Application\Entity\Db\Profil $profil
 * @var \Application\Entity\Db\Role[] $rolesDisponibles
 * @var $form
 */

?>

<h1>
    Gestion des rôles associées au profil (<?php echo $profil->getRoleCode(); ?>) <?php echo $profil->getLibelle(); ?>
    Gestion des rôles associées au profil
</h1>

<div class="panel panel-info">
    <div class="panel-heading">
        Information sur le profil sélectionné
    </div>
    <div class="panel-body">
        <dl class="dl-horizontal">
            <dt> Libellé </dt>
            <dd> <?php echo $profil->getLibelle(); ?> </dd>
            <dt> Code </dt>
            <dd> <?php echo $profil->getRoleCode(); ?> </dd>
            <dt> Nombre d'affectation</dt>
            <dd> <?php echo count($profil->getRoles()); ?> </dd>
        </dl>
    </div>
</div>

    <a href="" class="btn btn-primary">
        <span class="glyphicon glyphicon-plus"></span> Ajouter un rôle
    </a>

<br/>
<br/>



<div class="row">
    <div class="col-md-6">
        <table class="table table-condensed">
            <tr>
                <th> Libelle du rôle </th>
@@ -26,12 +38,27 @@
            <?php foreach ($profil->getRoles() as $role) : ?>
                <tr>
                    <td>
                <?php echo $role->getLibelle(); ?>
                        <?php echo $role->getRoleId(); ?>
                    </td>
                    <td>
                VOIR
                SUPPRIMER
                        <a href="<?php echo $this->url('profil/gerer-roles/retirer', ['profil' => $profil->getId(), 'role' => $role->getId()], [], true); ?>">
                            <span class="glyphicon glyphicon-remove"></span>
                        </a>
                    </td>
                </tr>
            <?php endforeach; ?>
        </table>
    </div>
    <div class="col-md-6">
        <form method="POST" action="<?php echo $this->url('profil/gerer-roles/ajouter', ['profil' => $profil->getId()], [], true); ?>">
            <select name="role">
                <?php foreach ($rolesDisponibles as $role) : ?>
                    <option value="<?php echo $role->getId(); ?>"> <?php echo $role->getRoleId(); ?></option>
                <?php endforeach; ?>
            </select>
            <br/>
            <input type="submit" class="btn btn-primary" value="Ajouter un rôle"/>
        </form>

    </div>
</div>
 No newline at end of file