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

Gestion du nouveau système de privilèges pour les écoles doctorales

parent 1a4497f0
Loading
Loading
Loading
Loading
+56 −43
Changes for module/Application/config/others/ecole-doctorale.config.php: 56 added lines, 43 removed lines.
Original line number Diff line number Diff line
<?php

use Application\Controller\EcoleDoctoraleController;
use Application\Controller\Factory\EcoleDoctoraleControllerFactory;
use Application\Form\Factory\EcoleDoctoraleFormFactory;
use Application\Form\Factory\EcoleDoctoraleHydratorFactory;
use Application\Provider\Privilege\EcoleDoctoralePrivileges;
use Application\Provider\Privilege\StructurePrivileges;
use Application\Service\EcoleDoctorale\EcoleDoctoraleService;
use Application\View\Helper\EcoleDoctoraleHelper;
use UnicaenAuth\Guard\PrivilegeController;
use UnicaenAuth\Provider\Rule\PrivilegeRuleProvider;
use Zend\Mvc\Router\Http\Literal;
use Zend\Mvc\Router\Http\Segment;

return [
    'bjyauthorize'    => [
        'resource_providers' => [
            'BjyAuthorize\Provider\Resource\Config' => [
                'EcoleDoctorale' => [],
            ],
        ],
        'rule_providers'     => [
            PrivilegeRuleProvider::class => [
                'allow' => [
                    [
                        'privileges' => [
                            EcoleDoctoralePrivileges::ECOLE_DOCT_MODIFICATION,
                        ],
                        'resources'  => ['EcoleDoctorale'],
                        'assertion'  => 'Assertion\\EcoleDoctorale',
                    ],
                ],
            ],
        ],
//        'resource_providers' => [
//            'BjyAuthorize\Provider\Resource\Config' => [
//                'EcoleDoctorale' => [],
//            ],
//        ],
//        'rule_providers'     => [
//            PrivilegeRuleProvider::class => [
//                'allow' => [
//                    [
//                        'privileges' => [
//                            EcoleDoctoralePrivileges::ECOLE_DOCT_MODIFICATION,
//                        ],
//                        'resources'  => ['EcoleDoctorale'],
//                        'assertion'  => 'Assertion\\EcoleDoctorale',
//                    ],
//                ],
//            ],
//        ],
        'guards' => [
            PrivilegeController::class => [
                [
                    'controller' => 'Application\Controller\EcoleDoctorale',
                    'controller' => EcoleDoctoraleController::class,
                    'action'     => [
                        'index',
                        'information'
                    ],
                    'privileges' => EcoleDoctoralePrivileges::ECOLE_DOCT_CONSULTATION,
                    'privileges' => [
                        StructurePrivileges::STRUCTURE_CONSULTATION_TOUTES_STRUCTURES,
                        StructurePrivileges::STRUCTURE_CONSULTATION_SES_STRUCTURES,
                    ],
                ],
                [
                    'controller' => 'Application\Controller\EcoleDoctorale',
                    'controller' => EcoleDoctoraleController::class,
                    'action'     => [
                        'ajouter',
                        'supprimer',
                        'restaurer',
                    ],
                    'privileges' => EcoleDoctoralePrivileges::ECOLE_DOCT_CREATION,
                    'privileges' => [
                        StructurePrivileges::STRUCTURE_MODIFICATION_TOUTES_STRUCTURES,
                        StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES,
                    ],
                ],
                [
                    'controller' => 'Application\Controller\EcoleDoctorale',
                    'controller' => EcoleDoctoraleController::class,
                    'action'     => [
                        'modifier',
                        'ajouter-individu',
                        'retirer-individu',
                        'supprimer-logo',
                    ],
                    'privileges' => EcoleDoctoralePrivileges::ECOLE_DOCT_MODIFICATION,
                    'privileges' => [
                        StructurePrivileges::STRUCTURE_MODIFICATION_TOUTES_STRUCTURES,
                        StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES,
                    ],
                ],
            ],
        ],
@@ -65,14 +77,12 @@ return [
    'router'          => [
        'routes' => [
            'ecole-doctorale' => [
                'type'          => 'Segment',
                'type'          => Literal::class,
                'options'       => [
                    'route'    => '/[:language/]ecole-doctorale',
                    'route'    => '/ecole-doctorale',
                    'defaults' => [
                        '__NAMESPACE__' => 'Application\Controller',
                        'controller'    => 'EcoleDoctorale',
                        'controller'    => EcoleDoctoraleController::class,
                        'action'        => 'index',
                        'language'      => 'fr_FR',
                    ],
                ],
                'may_terminate' => true,
@@ -80,7 +90,7 @@ return [
                    'information' => [
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/information/:ecoleDoctorale',
                            'route'       => '/information/:structure',
                            'defaults'    => [
                                'action' => 'information',
                            ],
@@ -88,7 +98,7 @@ return [

                    ],
                    'ajouter' => [
                        'type'          => 'Segment',
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/ajouter',
                            'defaults'    => [
@@ -97,9 +107,9 @@ return [
                        ],
                    ],
                    'supprimer' => [
                        'type'          => 'Segment',
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/:ecoleDoctorale/supprimer',
                            'route'       => '/supprimer/:structure',
                            'constraints' => [
                                'ecoleDoctorale' => '\d+',
                            ],
@@ -109,9 +119,9 @@ return [
                        ],
                    ],
                    'restaurer' => [
                        'type'          => 'Segment',
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/:ecoleDoctorale/restaurer',
                            'route'       => '/restaurer/:structure',
                            'constraints' => [
                                'ecoleDoctorale' => '\d+',
                            ],
@@ -121,9 +131,9 @@ return [
                        ],
                    ],
                    'modifier' => [
                        'type'          => 'Segment',
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/:ecoleDoctorale/modifier',
                            'route'       => '/modifier/:structure',
                            'constraints' => [
                                'ecoleDoctorale' => '\d+',
                            ],
@@ -133,7 +143,7 @@ return [
                        ],
                    ],
                    'ajouter-individu' => [
                        'type'          => 'Segment',
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/:ecoleDoctorale/ajouter-individu',
                            'constraints' => [
@@ -145,7 +155,7 @@ return [
                        ],
                    ],
                    'retirer-individu' => [
                        'type'          => 'Segment',
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/:ecoleDoctorale/retirer-individu/:edi',
                            'constraints' => [
@@ -158,7 +168,7 @@ return [
                        ],
                    ],
                    'supprimer-logo' => [
                        'type'          => 'Segment',
                        'type'          => Segment::class,
                        'options'       => [
                            'route'       => '/supprimer-logo/:ecoleDoctorale',
                            'constraints' => [
@@ -182,14 +192,14 @@ return [
                            'ecole-doctorale' => [
                                'label'    => 'Écoles doctorales',
                                'route'    => 'ecole-doctorale',
                                'resource' => PrivilegeController::getResourceId('Application\Controller\EcoleDoctorale', 'index'),
                                'resource' => PrivilegeController::getResourceId(EcoleDoctoraleController::class, 'index'),

                                'order'    => 10,
                                'pages' => [
                                    'modification' => [
                                        'label'    => 'Modification',
                                        'route'    => 'ecole-doctorale/modifier',
                                        'resource' => PrivilegeController::getResourceId('Application\Controller\EcoleDoctorale', 'index'),
                                        'resource' => PrivilegeController::getResourceId(EcoleDoctoraleController::class, 'index'),

                                        'withtarget' => true,
                                        'paramsInject' => [
@@ -199,7 +209,7 @@ return [
                                    'information' => [
                                        'label'    => 'Détails',
                                        'route'    => 'ecole-doctorale/information',
                                        'resource' => PrivilegeController::getResourceId('Application\Controller\EcoleDoctorale', 'index'),
                                        'resource' => PrivilegeController::getResourceId(EcoleDoctoraleController::class, 'index'),

                                        'withtarget' => true,
                                        'paramsInject' => [
@@ -230,6 +240,9 @@ return [
        'factories' => [
            'Application\Controller\EcoleDoctorale' => EcoleDoctoraleControllerFactory::class,
        ],
        'aliases' => [
            EcoleDoctoraleController::class => 'Application\Controller\EcoleDoctorale',
        ]
    ],
    'form_elements'   => [
        'invokables' => [
+25 −9
Changes for module/Application/config/others/structure.config.php: 25 added lines, 9 removed lines.
Original line number Diff line number Diff line
<?php

use Application\Controller\Factory\EcoleDoctoraleControllerFactory;
use Application\Controller\Factory\StructureControllerFactory;
use Application\Controller\StructureController;
use Application\Form\Factory\EcoleDoctoraleFormFactory;
use Application\Form\Factory\EcoleDoctoraleHydratorFactory;
use Application\Provider\Privilege\EcoleDoctoralePrivileges;
use Application\Provider\Privilege\EtablissementPrivileges;
use Application\Service\EcoleDoctorale\EcoleDoctoraleService;
use Application\Provider\Privilege\StructurePrivileges;
use Application\Service\Structure\StructureService;
use Application\Service\Structure\StructureServiceFactory;
use Application\View\Helper\EcoleDoctoraleHelper;
use UnicaenAuth\Guard\PrivilegeController;
use Application\Entity\Db\StructureConcreteInterface;
use Application\View\Helper\StructureSubstitHelper;
use Application\View\Helper\StructureArrayHelper;
use UnicaenAuth\Guard\PrivilegeController;
use UnicaenAuth\Provider\Rule\PrivilegeRuleProvider;
use Zend\Mvc\Router\Http\Segment;

return [

    'bjyauthorize'    => [
        'resource_providers' => [
            'BjyAuthorize\Provider\Resource\Config' => [
                'structure' => [],
            ],
        ],
        'rule_providers'     => [
            PrivilegeRuleProvider::class => [
                'allow' => [
                    [
                        'privileges' => [
                            StructurePrivileges::STRUCTURE_CONSULTATION_TOUTES_STRUCTURES,
                            StructurePrivileges::STRUCTURE_CONSULTATION_SES_STRUCTURES,
                            StructurePrivileges::STRUCTURE_MODIFICATION_TOUTES_STRUCTURES,
                            StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES,
                        ],
                        'resources'  => ['structure'],
                        'assertion'  => 'Assertion\\Structure',
                    ],
                ],
            ],
        ],
        'guards' => [
            PrivilegeController::class => [
                [
+103
Changes for module/Application/src/Application/Assertion/Structure/StructureAssertion.php: 103 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Assertion\Structure;

use Application\Assertion\BaseAssertion;
use Application\Provider\Privilege\StructurePrivileges;
use Application\Service\UserContextService;
use Zend\Permissions\Acl\Resource\ResourceInterface;

/**
 * Class TheseAssertion
 *
 * @package Application\Assertion
 * @method UserContextService getServiceUserContext()
 */
class StructureAssertion extends BaseAssertion
{

    protected function assertEntity(ResourceInterface $structure, $privilege = null)
    {
        if (! parent::assertEntity($structure, $privilege)) {
            return false;
        }

        $role = $this->userContextService->getSelectedIdentityRole();
        switch (true) {
            case $privilege === StructurePrivileges::STRUCTURE_CONSULTATION_SES_STRUCTURES :
                return $role->getStructure() === $structure;
                break;
            case $privilege === StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES :
                return $role->getStructure() === $structure;
                break;
        }

        return true;
    }

    protected function assertController($controller, $action = null, $privilege = null)
    {
        if (! parent::assertController($controller, $action, $privilege)) {
            return false;
        }

        $structure = $this->getRouteMatch()->getStructure();

        switch (true) {
            case $privilege === StructurePrivileges::STRUCTURE_CONSULTATION_SES_STRUCTURES :
                return $this->isAllowed(
                    $structure,
                    StructurePrivileges::STRUCTURE_CONSULTATION_SES_STRUCTURES
                );
                break;
            case $privilege === StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES :
                return $this->isAllowed(
                    $structure,
                    StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES
                );
                break;
        }

        return true;
    }

    public function isAllowed($resource, $privilege = null)
    {
        $allowed = parent::isAllowed($resource, $privilege);

        return $allowed;
    }

    /**
     * @return static
     */
    protected function initControllerAssertion()
    {
        $this->controllerAssertion->setContext([
            'structure'     => $this->getRouteMatch()->getStructure(),
        ]);

        return $this;
    }

    /**
     * @return static
     */
    protected function initPageAssertion()
    {
        $this->pageAssertion->setContext(['structure' => $this->getRouteMatch()->getStructure()]);

        return $this;
    }

    /**
     * @param ResourceInterface $entity
     * @return static
     */
    protected function initEntityAssertion(ResourceInterface $entity)
    {
        $this->entityAssertion->setContext(['structure' => $entity]);

        return $this;
    }
}
 No newline at end of file
+50
Changes for module/Application/src/Application/Assertion/Structure/StructureControllerAssertion.php: 50 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Assertion\Structure;

use Application\Assertion\ControllerAssertion;
use Application\Entity\Db\Structure;
use Application\Provider\Privilege\StructurePrivileges;
use Application\Service\UserContextServiceAwareTrait;

class StructureControllerAssertion extends ControllerAssertion
{
    use UserContextServiceAwareTrait;

    const THESE_CONTROLLER = 'Application\Controller\These';
    const DOCTORANT_CONTROLLER = 'Application\Controller\Doctorant';

    /**
     * @var Structure
     */
    private $structure;

    /**
     * @param array $context
     */
    public function setContext(array $context)
    {
        parent::setContext($context);

        if (array_key_exists('structure', $context)) {
            $this->structure = $context['structure'];
        }
    }

    /**
     * @param string $privilege
     * @return boolean
     */
    public function assert($privilege = null)
    {
        switch($privilege) {
            case StructurePrivileges::STRUCTURE_CONSULTATION_SES_STRUCTURES :
                return true;
            case StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES :
                return true;
        }
        return true;
    }


}
 No newline at end of file
+57
Changes for module/Application/src/Application/Assertion/Structure/StructureEntityAssertion.php: 57 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace Application\Assertion\Structure;

use Application\Assertion\Exception\FailedAssertionException;
use Application\Entity\Db\Role;
use Application\Entity\Db\Structure;
use Application\Provider\Privilege\StructurePrivileges;
use Application\Service\UserContextServiceAwareInterface;
use Application\Service\UserContextServiceAwareTrait;

class StructureEntityAssertion implements UserContextServiceAwareInterface
{
    use UserContextServiceAwareTrait;

    /**
     * @var Structure
     */
    private $structure;

    /**
     * @param array $context
     */
    public function setContext(array $context)
    {
        $this->structure = $context['structure'];
    }

    /**
     * @param string $privilege
     * @return boolean
     * @throws FailedAssertionException
     */
    public function assert($privilege = null)
    {
        switch($privilege) {
            case StructurePrivileges::STRUCTURE_CONSULTATION_SES_STRUCTURES :
                return $this->isSesStructures();
            case StructurePrivileges::STRUCTURE_MODIFICATION_SES_STRUCTURES :
                return $this->isSesStructures();
        }
        return true;
    }


    /**
     * @return bool
     */
    protected function isSesStructures()
    {
        $connectedRole = $this->userContextService->getSelectedIdentityRole();
        switch($connectedRole->getCode()) {
            case Role::CODE_ADMIN_TECH : return true;
        }
        return true;
    }
}
 No newline at end of file
Loading