Commit bce4412f authored by surville's avatar surville
Browse files

[Evolution] Développement UnicaenLdap WIP

parent d8deca10
Loading
Loading
Loading
Loading
+274 −0
Changes for src/UnicaenLdap/Entity/Base/Group.php: 274 added lines, 0 removed lines.
Original line number Diff line number Diff line
<?php

namespace UnicaenLdap\Entity\Base;

use UnicaenLdap\Entity\Entity;
use UnicaenLdap\Entity\People as PeopleEntity;
use UnicaenLdap\Entity\Structure as StructureEntity;
use UnicaenLdap\Entity\System as SystemEntity;
use UnicaenLdap\Exception;
use Zend\Ldap\Dn;
use Zend\Ldap\Exception\LdapException;

/**
 * Classe mère des entités de la branche "groups" de l'annuaire LDAP.
 *
 * @author David Surville <david.surville@unicaen.fr>
 */
class Group extends Entity
{
    /**
     * Membre par défaut d'un groupe lorsque le groupe est vide
     */
    const MEMBER_NOBODY = 'nobody';

    /**
     * @var string
     */
    protected $type = 'Group';

    /**
     * Liste des classes d'objet nécessaires à la création d'un groupe
     *
     * @var string[]
     */
    protected $objectClass = [
        'top',
        'groupOfNames',
        'supannGroupe',
    ];

    /**
     * Liste des attributs autorisés pour une entité "Group"
     *
     * @var array
     */
    protected $authorizedAttributes = [
        // Attributes classes
        'objectClass',
        // Attributes
        'description',
        'member',
        'owner',
        'supannGroupeDateFin',
        'supannGroupeLecteurDN',
        'supannGroupeAdminDN',
        'supannRefId',
    ];

    /**
     * Liste des attributs contenant des dates
     *
     * @var string[]
     */
    protected $dateTimeAttributes = [
        'supannGroupeDateFin',
    ];

    /**
     * Liste des attributs monovalués
     *
     * @var array
     */
    protected $monoValuedAttributes = [
        'supannGroupeDateFin',
    ];


    /**
     * Retourne le DN du membre par défaut
     *
     * @return string
     */
    public function getMemberNobody()
    {
        return sprintf('uid=%s,%s',
            self::MEMBER_NOBODY,
            $this->getService()->getLdapSystemService()->getBranches()[0]
        );
    }

    /**
     * Attribut Ldap "description"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setDescription($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $this->appendOrNot('description', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "member"
     *
     * @param array|string|Dn|PeopleEntity|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setMember($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof PeopleEntity) {
                return $val->getDn();
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('member', array_filter($value), $append);

        return $this;
    }

    /**
     * Attribut Ldap "owner"
     *
     * @param array|string|Dn|PeopleEntity|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setOwner($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof PeopleEntity) {
                return $val->getDn();
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('owner', array_filter($value), $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannGroupeDateFin"
     *
     * @param array|string|DateTime|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannGroupeDateFin($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                $val = new DateTime($val, new \DateTimeZone('+0000')); // définition du timezone à +0h
            }
            return (int)$val->format('U');
        }, $value);

        $this->appendOrNot('supannGroupeDateFin', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannGroupeLecteurDN"
     *
     * @param array|string|Dn|PeopleEntity|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannGroupeLecteurDN($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof PeopleEntity) {
                return $val->getDn();
            } elseif ($val instanceof SystemEntity) {
                return $val->getDn();
            }
            else {
                return null;
            }
        }, $value);

        $this->appendOrNot('supannGroupeLecteurDN', array_filter($value), $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannGroupeAdminDN"
     *
     * @param array|string|Dn|PeopleEntity|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannGroupeAdminDN($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof PeopleEntity) {
                return $val->getDn();
            } elseif ($val instanceof SystemEntity) {
                return $val->getDn();
            }
            else {
                return null;
            }
        }, $value);

        $this->appendOrNot('supannGroupeAdminDN', array_filter($value), $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannRefId"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannRefId($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return preg_match(self::$attribute_with_label_pattern, $v);
        });

        $this->appendOrNot('supannRefId', $value, $append);

        return $this;
    }
}
 No newline at end of file
+2 −2
Changes for src/UnicaenLdap/Entity/Base/People.php: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -1043,14 +1043,14 @@ class People extends Entity
    {
        $universiteDomaine = $this->getService()->getLdapRootService()->getEtablissementDomain();
        $value = $this->preFormat($value);
        $value = array_map(function ($v) {
        $value = array_map(function ($v) use ($universiteDomaine) {
            list($identifiant, $domaine) = array_pad(explode('@', strtolower($v)), 2, null);
            if ($domaine != $universiteDomaine) {
                $v = sprintf('%s@%s', $v, $universiteDomaine);
            }
            return $v;
        }, $value);
        $value = array_filter($value, function ($v) {
        $value = array_filter($value, function ($v) use ($universiteDomaine) {
            return preg_match('/^[0-9a-z\-]+@' . $universiteDomaine . '$/', $v);
        });

+16 −0
Changes for src/UnicaenLdap/Entity/Entity.php: 16 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -102,6 +102,22 @@ abstract class Entity
        return $params[$type];
    }


    /**
     * Vérifie le format de l'identifiant Ldap selon le type de l'entité
     *
     * @param $type
     * @param $id
     * @return false|int
     */
    public static function checkIdFormat($type, $id)
    {
        switch($type) {
            default:
                return preg_match('/^[[:alnum:]_-]+$/', $id);
        }
    }

    /**
     * Entity constructor.
     *
+7 −29
Changes for src/UnicaenLdap/Entity/Group.php: 7 added lines, 29 removed lines.
Original line number Diff line number Diff line
@@ -3,37 +3,17 @@
namespace UnicaenLdap\Entity;

use DateTime;
use UnicaenLdap\Entity\Base\Group as BaseGroup;

/**
 * Classe mère des groupes de l'annuaire LDAP.
 * Classe de gestion des entités de la branche "groups" de l'annuaire LDAP.
 *
 * @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
 */
class Group extends Entity
class Group extends BaseGroup
{
    protected $type = 'Group';

    /**
     * Liste des classes d'objet nécessaires à la création d'un groupe
     *
     * @var string[]
     */
    protected $objectClass = [
        'groupOfNames',
        'supannGroupe',
    ];

    /**
     * Liste des attributs contenant des dates
     *
     * @var string[]
     */
    protected $dateTimeAttributes = [
        'supannGroupeDateFin',
    ];

    /**
     * Détermine si un groupe est valide ou non, c'est-à-dire si sa date de fin de validité n'est pas antérieure à la
     * Détermine si un groupe est valide ou non, c'est-à-dire si sa date de fin de validité est postérieure ou égal à la
     * date testée
     *
     * @param DateTime $dateObservation
@@ -64,14 +44,12 @@ class Group extends Entity
     *
     * @param string $orderBy Champ de tri (au besoin)
     * @return People[]
     * @throws \Zend\Ldap\Exception\LdapException
     */
    public function getPeople($orderBy = null)
    public function getMembres($orderBy = null)
    {
        /* @var $peopleService \UnicaenLdap\Service\People */
        $peopleService = $this->getService()->getLdapPeopleService();

        /** @var People[] $result */
        $result = $peopleService->getAllBy($this->get('member'), 'dn', $orderBy);
        $result = $peopleService->getAllBy($this->member, 'dn', $orderBy);

        return $result;
    }
+148 −94
Changes for src/UnicaenLdap/Entity/People.php: 148 added lines, 94 removed lines.
Original line number Diff line number Diff line
@@ -4,7 +4,8 @@ namespace UnicaenLdap\Entity;

use DateTime;
use UnicaenLdap\Entity\Base\People as BasePeople;
use UnicaenLdap\Entity\Base\Structure;
use UnicaenLdap\Entity\Structure as StructureEntity;
use UnicaenLdap\Exception;

/**
 * Classe de gestion des entités de la branche "people" de l'annuaire LDAP.
@@ -78,7 +79,7 @@ class People extends BasePeople
     * @param boolean $firstNameFirst prénom avant le nom de famille
     * @return string
     */
    public function getFullName($uppercaseName = false, $withCivility = false, $firstNameFirst = false)
    public function getNomComplet($uppercaseName = false, $withCivility = false, $firstNameFirst = false)
    {
        $sn = $this->sn;
        $sn = is_array($sn) ? current($sn) : $sn;
@@ -94,12 +95,32 @@ class People extends BasePeople
        return  $civilite . ($firstNameFirst ? $prenom . ' ' . $nom : $nom . ' ' . $prenom);
    }

    /**
     * Retourne le nom d'usage
     *
     * @return string|null
     */
    public function getNomUsage()
    {
        return is_array($this->sn) ? $this->sn[0] : $this->sn;
    }

    /**
     * Retourne le nom de famille s'il existe, sinon le nom d'usage
     *
     * @return string|null
     */
    public function getNomFamille()
    {
        return is_array($this->sn) ? $this->sn[1] : $this->sn;
    }

    /**
     * Retourne la date de naissance
     *
     * @return DateTime|false
     */
    public function getDateOfBirth()
    public function getDateNaissance()
    {
        return DateTime::createFromFormat('Ymd h:i:s', $this->schacDateOfBirth . ' 00:00:00');
    }
@@ -109,11 +130,26 @@ class People extends BasePeople
     *
     * @return DateTime|false
     */
    public function getExpiryDate()
    public function getDateExpiration()
    {
        return DateTime::createFromFormat('Ymd h:i:s', $this->schacExpiryDate . ' 00:00:00');
    }

    /**
     * Retourne l'adresse postale professionnelle
     *
     * @return array
     */
    public function getAdressePostale()
    {
        $value = $this->preFormat($this->postalAddress);
        $value = array_map(function ($val) {
            return explode('$', $val);
        }, $value);

        return 1 == count($value) ? $value[0] : $value;
    }

    /**
     * Test si une valeur est renseignée pour le champ "eduPersonAffiliation"
     *
@@ -122,7 +158,8 @@ class People extends BasePeople
     */
    protected function whichStatus(string $status): bool
    {
        return in_array($status, (array)$this->eduPersonAffiliation);
        $value = $this->preFormat($this->eduPersonAffiliation);
        return in_array($status, $value);
    }

    /**
@@ -244,32 +281,28 @@ class People extends BasePeople
        return $this->whichStatus(parent::STATUS_TEACHER);
    }

    public function getEduPersonOrg()
    {
        $structureService = $this->getService()->getLdapStructureService();
        $dn = $this->eduPersonOrgDN;
        if (empty($dn)) return null;

        return $structureService->getAllBy($dn, 'dn');
    }

    /**
     * Retourne l'identifiant Octopus de l'individu
     * Établissement auquel appartient la personne
     *
     * @return string|null
     * @return StructureEntity|null
     */
    public function getOctopusId()
    public function getEtablissement()
    {
        $attributeValues = $this->preFormat($this->supannRefId);
        $label = $this->getLabel('OCTOPUS', 'ID');

        $value = array_filter($attributeValues, function ($v) use ($label) {
            return preg_match("/^$label(?<identifiant>.+)$/", $v);
        });
        $rootService = $this->getService()->getLdapRootService();
        $structureService = $this->getService()->getLdapStructureService();
        $dn = $this->eduPersonOrgDN;

        return !empty($value)
            ? str_replace($label, '', array_values($value)[0])
            : null;
        if (empty($dn)) return null;
        switch($dn) {
            case $rootService->getEtablissementDN():
                return $rootService->getStructureEntity();
            default:
                try {
                    return $structureService->getBy($dn, 'dn');
                } catch (Exception $e) {
                    return null;
                }
        }
    }

    /**
@@ -311,124 +344,145 @@ class People extends BasePeople
    }

    /**
     * Retourne la liste des groupes dont l'utilisateur fait partie
     * Si le groupe n'est plus valide à la date d'observation, alors il n'est pas retourné dans la liste
     * Retourne le numéro d'étudiant
     *
     * @param DateTime $dateObservation
     * @param string   $orderBy Champ de tri (au besoin)
     * @return Group[]
     * @return string|null
     */
    public function getGroups(DateTime $dateObservation = null, $orderBy = null)
    public function getEtudiantId()
    {
        $groupService = $this->getService()->getLdapGroupService();
        return $this->supannEtuId;
    }

        return $groupService->filterValids($groupService->getAllBy($this->get('memberOf'), 'dn', $orderBy), $dateObservation);
    /**
     * Retourne le numéro d'employé
     *
     * @return string|null
     */
    public function getEmployeId()
    {
        return $this->supannEmpId;
    }

    /**
     * Retourne les structures auxquelles appartiennent la personne
     * Retourne l'identifiant de l'individu dans le référentiel
     *
     * @return Entity[]|Structure[]
     * @return string|null
     */
    public function getEduPersonOrgUnit()
    public function getReferentielId()
    {
        $structureService = $this->getService()->getLdapStructureService();
        $dn = $this->eduPersonOrgUnitDN;
        if (empty($dn)) return null;
        $attributeValues = $this->preFormat($this->supannRefId);
        $label = $this->getLabel('OCTOPUS', 'ID');

        return $structureService->getAllBy($dn, 'dn');
        $value = array_filter($attributeValues, function ($v) use ($label) {
            return preg_match("/^$label(?<identifiant>.+)$/", $v);
        });

        return !empty($value)
            ? str_replace($label, '', array_values($value)[0])
            : null;
    }

    /**
     * Retourne la structure principale à laquelle appartient la personne
     * Modifie l'ensemble des attributs liés au mot de passe
     *
     * @return Entity|Structure
     * @param string $value
     * @return $this
     * @throws \UnicaenLdap\Exception
     * @throws \Zend\Ldap\Exception\LdapException
     */
    public function getEduPersonPrimaryOrgUnit()
    public function setPassword(string $value)
    {
        $structureService = $this->getService()->getLdapStructureService();
        $dn = $this->eduPersonPrimaryOrgUnitDN;
        if (empty($dn)) return null;
        parent::setUserPassword($value);
        parent::setNtPassword($value);
        parent::setSambaNTPassword($value);
        parent::setUcbnSquidHash($value);

        return $structureService->getBy($dn, 'dn');
        return $this;
    }

    /**
     * Retourne la structure d'affectation de la personne
     * Retourne les structures auxquelles appartient la personne
     *
     * @todo à terminer
     * @return Entity|Structure
     * @throws \Exception
     * @return StructureEntity[]|null
     */
    public function getEntiteAffectation()
    public function getStructure()
    {
        throw new \Exception('Méthode pas finie');
        $structureService = $this->getService()->getLdapStructureService();
        $codes = $this->getNode()->getAttribute('supannEntiteAffectation');
        var_dump($codes);
        $value = $this->eduPersonOrgUnitDN;
        if (empty($value)) return null;

        return $structureService->getBy($dn, 'dn');
        $value = $structureService->getAllBy($value, 'dn');
        return 1 == count($value) ? array_shift($value) : $value;
    }

    /**
     * Retourne la structure d'affectation de la personne
     * Retourne la structure principale à laquelle appartient la personne
     *
     * @todo à terminer
     * @return Structure
     * @throws \Exception
     * @return StructureEntity|null
     */
    public function getEntiteAffectationPrincipale()
    public function getStructurePrincipale()
    {
        throw new \Exception('Méthode pas finie');
        $structureService = $this->getService()->getLdapStructureService();
        $value = $this->eduPersonPrimaryOrgUnitDN;
        if (empty($value)) return null;

        $codes = [];
        $affectations = $this->getNode()->getAttribute('supannAffectation');


        list($code, $description) = explode(';', $this->supannAffectation);
        $code = $this->supannAffectation;
        if (empty($dn)) return null;

        return $structureService->getBy($dn, 'dn');
        try {
            return $structureService->getBy($value, 'dn');
        } catch (Exception $e) {
            return null;
        }
    }

    /**
     * Retourne la structure d'affectation de la personne
     * Retourne les étapes auxquelles appartient la personne
     *
     * @todo à terminer
     * @return Structure
     * @throws \Exception
     * @return StructureEntity[]|null
     */
    public function getAffectationDescription()
    public function getInscriptionEtape()
    {
        throw new \Exception('Méthode pas finie');
        $structureService = $this->getService()->getLdapStructureService();
        $value = $this->preFormat($this->supannEtuEtape);
        if (empty($value)) return null;

        list($code, $description) = explode(';', $this->supannAffectation);
        $code = $this->supannAffectation;
        if (empty($dn)) return null;
        $label = $this->getLabel('UAI', $this->getService()->getLdapRootService()->getEtablissementUAI());
        $structureService = $this->getService()->getLdapStructureService();
        list($structureKey) = Entity::getNodeParams($structureService->getType());
        $structureBranch = $structureService->getBranches()[0];
        $value = array_map(function ($v) use ($label, $structureKey, $structureBranch) {
            return sprintf('%s=%s,%s',
                $structureKey,
                str_replace($label, '' , $v),
                $structureBranch
            );
        }, $value);

        return $structureService->getBy($dn, 'dn');
        $value = $structureService->getAllBy($value, 'dn');
        return 1 == count($value) ? array_shift($value) : $value;
    }

    /**
     * Modifie l'ensemble des attributs liés au mot de passe
     * Retourne les étapes auxquelles appartient la personne
     *
     * @param string $value
     * @return $this
     * @throws \UnicaenLdap\Exception
     * @throws \Zend\Ldap\Exception\LdapException
     * @return StructureEntity[]|null
     */
    public function setPassword(string $value)
    public function getInscriptionElementPedagogique()
    {
        parent::setUserPassword($value);
        parent::setNtPassword($value);
        parent::setSambaNTPassword($value);
        parent::setUcbnSquidHash($value);
        $value = $this->preFormat($this->supannEtuElementPedagogique);
        if (empty($value)) return null;

        return $this;
        $label = $this->getLabel('UAI', $this->getService()->getLdapRootService()->getEtablissementUAI());
        $structureService = $this->getService()->getLdapStructureService();
        list($structureKey) = Entity::getNodeParams($structureService->getType());
        $structureBranch = $structureService->getBranches()[0];
        $value = array_map(function ($v) use ($label, $structureKey, $structureBranch) {
            return sprintf('%s=%s,%s',
                $structureKey,
                str_replace($label, '' , $v),
                $structureBranch
            );
        }, $value);

        $value = $structureService->getAllBy($value, 'dn');
        return 1 == count($value) ? array_shift($value) : $value;
    }

    /**
Loading