Commit a6a235a4 authored by Stéphane Bouvry's avatar Stéphane Bouvry
Browse files

Vue 'Personnel' qui permet à un utilisateur de voir une liste de personne en...

Vue 'Personnel' qui permet à un utilisateur de voir une liste de personne en fonction de la configuration Oscar et de son profil (permettra au N+1 / responsable d'organisme de gérer les attributions hiérarchiques des personnes)
parent 6ed80be0
Loading
Loading
Loading
Loading
+7 −0
Changes for config/autoload/global.php: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -115,6 +115,13 @@ return array(
        'userSubmitScheduleValidateByNp1' => true,
        'userSubmitScheduleValidateByPrivilege' => \Oscar\Provider\Privileges::PERSON_MANAGE_SCHEDULE,

        // Niveau d'accès à la liste du personnel
        //  - 0
        //  - 1 = N=1
        //  - 2 = Personnel dans l'oganisation
        //  - 3 = Person dans l'organisation, et dans les activités
        'listPersonnel' => 0,

        'scheduleModeles' => [
            'fulltime' => [
                'label' => 'Plein temps',
+1 −1
Changes for module/Oscar/config/module.config.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -238,7 +238,7 @@ return array(
                ],
                [ 'controller' =>   'Person',
                    'action' => ['show'],
                    'privileges' => \Oscar\Provider\Privileges::PERSON_SHOW
                    'roles' => ['user']
                ],
                [ 'controller' =>   'Person',
                    'action' => ['edit', 'new', 'syncLdap', 'merge', 'organizationRole', 'notificationPerson', 'notificationPersonGenerate'],
+54 −1
Changes for module/Oscar/src/Oscar/Controller/PersonController.php: 54 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ namespace Oscar\Controller;

use Doctrine\ORM\AbstractQuery;
use Doctrine\ORM\Query;
use Elasticsearch\Common\Exceptions\BadRequest400Exception;
use Elasticsearch\Common\Exceptions\Unauthorized401Exception;
use Oscar\Entity\Activity;
use Oscar\Entity\ActivityPerson;
use Oscar\Entity\Authentification;
@@ -19,7 +21,10 @@ use Oscar\Entity\ActivityLogRepository;
use Oscar\Entity\NotificationPerson;
use Oscar\Entity\Organization;
use Oscar\Entity\OrganizationPerson;
use Oscar\Entity\OrganizationRepository;
use Oscar\Entity\Person;
use Oscar\Entity\PersonRepository;
use Oscar\Entity\Privilege;
use Oscar\Entity\ProjectMember;
use Oscar\Entity\Referent;
use Oscar\Entity\Role;
@@ -54,6 +59,11 @@ class PersonController extends AbstractOscarController

    public function personnelAction(){

        $access = $this->getConfiguration('oscar.listPersonnel');
        if( $access == 0 ){
            throw new BadRequest400Exception("Cette fonctionnalité n'est pas activé");
        }

        if( $this->isAjax() || $this->params()->fromQuery('format') == 'json' ){

            $q = $this->params()->fromQuery('q');
@@ -65,11 +75,21 @@ class PersonController extends AbstractOscarController
                'leader' => null
            ];

            $idCoWorkers = $this->getPersonService()->getCoWorkerIds($this->getCurrentPerson()->getId());

            $idCoWorkers = [];

            $idSubordinates = $this->getPersonService()->getSubordinateIds($this->getCurrentPerson()->getId());


            if( $access > 1 ){
                $idCoWorkers = $this->getPersonService()->getCoWorkerIds($this->getCurrentPerson()->getId());
            }


            if( !$this->getOscarUserContext()->hasPrivileges(Privileges::PERSON_INDEX) ){
                $params['ids'] = array_merge($idCoWorkers, $idSubordinates);
//                var_dump($params);
//                die();
            }

            $datas = $this->getPersonService()->getPersonsSearchPaged($q, $page, $params);
@@ -581,6 +601,35 @@ class PersonController extends AbstractOscarController
        $id = $this->params()->fromRoute('id');
        $page = $this->params()->fromQuery('page', 1);
        $person = $this->getPersonService()->getPerson($id);


        $manageHierarchie = $this->getOscarUserContext()->hasPrivileges(Privileges::PERSON_EDIT);


        if( !$this->getOscarUserContext()->hasPrivileges(Privileges::PERSON_SHOW) ){

            // N+1 ?
            /** @var PersonRepository $personRepository */
            $personRepository = $this->getEntityManager()->getRepository(Person::class);

            // Subordonnées de la personne connectée
            $subordinatesIds = $personRepository->getSubordinatesIds($this->getCurrentPerson()->getId());

            if( !in_array($person->getId(), $subordinatesIds) ){
                /** @var OrganizationRepository $organizationRepository */
                $organizationRepository = $this->getEntityManager()->getRepository(Organization::class);
                $organizationIds = $organizationRepository->getOrganizationsIdsForPerson($this->getCurrentPerson()->getId(), true);

                $coworkerIds = $this->getPersonService()->getCoWorkerIds($this->getCurrentPerson()->getId());
                if( ! in_array($id, $coworkerIds) ){
                    throw new Unauthorized401Exception("Vous n'avez pas accès à la fiche de cette personne");
                }
            }
        }




        $auth = null;
        $activities = null;
        $traces = null;
@@ -619,6 +668,9 @@ class PersonController extends AbstractOscarController
        }

        if( $method == "POST" ){
            if( !$manageHierarchie ){
                return $this->getResponseUnauthorized();
            }
            $action = $this->params()->fromPost('action', null);
            switch( $action ) {
                case 'referent' :
@@ -676,6 +728,7 @@ class PersonController extends AbstractOscarController
            'ldapRoles' => $roles,
            'scheduleEditable' => $this->getOscarUserContext()->hasPrivileges(Privileges::PERSON_MANAGE_SCHEDULE),
            'referents' => $referents,
            'manageHierarchie' => $manageHierarchie,
            'subordinates' => $subordinates,
            'authentification' => $this->getEntityManager()->getRepository(Authentification::class)->findOneBy(['username' => $person->getLadapLogin()]),
            'auth' => $auth,
+68 −0
Changes for module/Oscar/src/Oscar/Entity/PersonRepository.php: 68 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,74 @@ class PersonRepository extends EntityRepository implements IConnectedRepository
{
    private $_cacheSelectebleRolesOrganisation;

    /**
     * Retourne la liste des IDS des personnes identifiées directement comme membre dans la/les organisations.
     *
     * @param $organisationIds
     * @return array
     */
    public function getPersonIdsInOrganizations( $organisationIds ){
        $result = $this->createQueryBuilder('p')
            ->select('p.id')
            ->innerJoin('p.organizations', 'op')
            ->where('op.organization IN(:organisationsIds)')
            ->setParameter('organisationsIds', $organisationIds)
            ->getQuery()
            ->getResult();
        return array_map('current', $result);
    }

    public function getPersonIdsForOrganizationsActivities( $organizationIds ){
        $activityMembers = $this->getEntityManager()->getRepository(Person::class)->createQueryBuilder('p')
            ->select('DISTINCT p.id')
            ->innerJoin('p.activities', 'ap')
            ->innerJoin('ap.activity', 'a')
            ->innerJoin('a.organizations', 'o')
            ->innerJoin('o.roleObj', 'r')
            ->where('r.principal = \'true\' AND o.organization IN(:organizationIds)')
            ->setParameter('organizationIds', $organizationIds)
            ->getQuery();

        $projectMembers = $this->getEntityManager()->getRepository(Person::class)->createQueryBuilder('p')
            ->select('DISTINCT p.id')
            ->innerJoin('p.projectAffectations', 'p_p')
            ->innerJoin('p_p.project', 'prj')
            ->innerJoin('prj.partners', 'partner')
            ->innerJoin('partner.organization', 'org')
            ->innerJoin('partner.roleObj', 'r')
            ->where('r.principal = \'true\' AND org.id IN(:organizationIds)')
            ->setParameter('organizationIds', $organizationIds)
            ->getQuery();

        $idsFromActivities = array_map('current', $activityMembers->getResult());
        $idsFromProjects = array_map('current', $projectMembers->getResult());

        return array_unique(array_merge($idsFromActivities, $idsFromProjects));
    }

    public function getSubordinates( $idReferent ){
        $ids = $this->getSubordinatesIds($idReferent);
        return $this->createQueryBuilder('p')
            ->where('p.id IN(:ids)')
            ->setParameter('ids', $ids)
            ->getQuery()
            ->getResult()
        ;

    }

    public function getSubordinatesIds( $referentId ){
        $result = $this->getEntityManager()->getRepository(Referent::class)->createQueryBuilder('r')
            ->select('p.id')
            ->innerJoin('r.person', 'p')
            ->where('r.referent = :idReferent')
            ->setParameter('idReferent', $referentId)
            ->getQuery()
            ->getResult();
        return array_map('current', $result);

    }

    /**
     * Retourne la liste des personnes qui ont des notifications non-lues et ayant un compte d'authentification.
     *
+31 −0
Changes for module/Oscar/src/Oscar/Service/OscarUserContext.php: 31 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -17,8 +17,10 @@ use Oscar\Entity\ActivityPerson;
use Oscar\Entity\Authentification;
use Oscar\Entity\Organization;
use Oscar\Entity\OrganizationPerson;
use Oscar\Entity\OrganizationRepository;
use Oscar\Entity\OrganizationRole;
use Oscar\Entity\Person;
use Oscar\Entity\PersonRepository;
use Oscar\Entity\Privilege;
use Oscar\Entity\Project;
use Oscar\Entity\ProjectMember;
@@ -42,6 +44,35 @@ use Zend\ServiceManager\ServiceLocatorAwareTrait;
class OscarUserContext extends UserContext
{

    public function hasPersonnelAccess(){
        if( !$this->getCurrentPerson() ){
            return false;
        }
        $access = $this->getServiceLocator()->get('OscarConfig')->getConfiguration('listPersonnel');

        if( $access == 0 ) return false;

        /** @var PersonRepository $personRepository */
        $personRepository = $this->getEntityManager()->getRepository(Person::class);

        /** @var OrganizationRepository $organisationRepository */
        $organisationRepository = $this->getEntityManager()->getRepository(Organization::class);

        $subodinates = $personRepository->getSubordinatesIds($this->getCurrentPerson()->getId());

        if( $access > 0 && count($subodinates) > 0 ) return true;

        $idsOrga = $organisationRepository->getOrganizationsIdsForPerson($this->getCurrentPerson()->getId());
        $coworkers = $personRepository->getPersonIdsInOrganizations($idsOrga);

        if( $access > 1 && count($coworkers) > 0 ) return true;

        $cocoworkers = $personRepository->getPersonIdsForOrganizationsActivities($idsOrga);
        if( $access > 1 && count($cocoworkers) > 0 ) return true;

        return false;
    }

    /**
     * @return null|Authentification
     */
Loading