Skip to content
Snippets Groups Projects
Commit bce4412f authored by surville's avatar surville
Browse files

[Evolution] Développement UnicaenLdap WIP

parent d8deca10
Branches
Tags
No related merge requests found
<?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
......@@ -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);
});
......
......@@ -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.
*
......
......@@ -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;
}
......
......@@ -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;
}
/**
......
......@@ -21,8 +21,8 @@ class Structure extends BaseStructure
*/
public function getParents()
{
if (null !== $parentIds = $this->get('supannCodeEntiteParent')) {
return $this->service->getAll($parentIds);
if (null !== ($parentIds = $this->get('supannCodeEntiteParent'))) {
return $this->getService()->getAll($parentIds);
}
return null;
......@@ -46,18 +46,23 @@ class Structure extends BaseStructure
$filter = Filter::andFilter($childrenFilter, $filter);
}
return $this->service->search($filter, $orderBy, -1, 0);
return $this->getService()->search($filter, $orderBy, -1, 0);
}
/**
* Retourne le code de la base source
* Retourne le code dans la base source
*
* @return string
* @throws LdapException
* @throws \UnicaenLdap\Exception
*/
public function getCodeSource()
{
$code = $this->get('supannCodeEntite');
return (0 === strpos($code, 'HS_')) ? substr($code, 3) : $code;
$code = $this->getId();
return (
0 === strpos($code, $this->getService()->getCodeStructurePrefixe()) ||
0 === strpos($code, $this->getService()->getCodeModuleEnseignementPrefixe()))
? substr($code, 3)
: $code;
}
}
\ No newline at end of file
......@@ -267,7 +267,6 @@ abstract class AbstractService implements
}
/**
*
* @param string|AbstractFilter $filter Filtre Ldap à appliquer
* @param string[] $ou Liste des organisations dans lesquelles rechercher
* @param string[] $attributes Liste des attributs à retourner
......@@ -365,14 +364,18 @@ abstract class AbstractService implements
/**
* Retourne une entité du type correspondant au service courant
*
*
* @param string $id
* @return Entity
* @return Entity|null
* @throws Exception
*/
public function get($id)
{
list($key) = Entity::getNodeParams($this->type);
try {
return $this->getBy($id, $key);
} catch (Exception $e) {
return null;
}
}
/**
......@@ -440,7 +443,12 @@ abstract class AbstractService implements
$data = array();
$sortedData = array();
foreach ($values as $val) {
try {
$valRes = $this->getBy($val, $by);
}
catch (Exception $e) {
continue;
}
if (!empty($orderBy)) {
$sortedData[$valRes->getId()] = $valRes->get($orderBy);
}
......@@ -500,11 +508,15 @@ abstract class AbstractService implements
* @return Entity
* @throws Exception
*/
public function create($id, $ou = null)
public function create(string $id, $ou = null)
{
if(!Entity::checkIdFormat($id)) {
throw new Exception(sprintf("Le format de l'identifiant '%s' n'est pas correct.", $id));
}
if (empty($ou)) {
if (count($this->ou) > 1) {
throw new Exception('$ou non renseigné alors que le service couvre plusieurs Organizational Units (OU).');
throw new Exception('Le paramètre $ou doit être renseigné car le service couvre plusieurs Organizational Units (OU).');
}
$ou = $this->ou[0];
}
......
......@@ -26,4 +26,11 @@ class People extends AbstractService
'deactivated',
'blocked'
];
public function checkPassword($value)
{
/**
* @todo implémenter la fonction de vérification d'un mot de passe
*/
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@
namespace UnicaenLdap\Service;
use UnicaenLdap\Entity\Entity;
use UnicaenLdap\Entity\Root as RootEntity;
use UnicaenLdap\Entity\Structure as StructureEntity;
use UnicaenLdap\Ldap;
......@@ -78,7 +79,7 @@ class Root extends AbstractService
/**
* Retourne l'entité racine
*
* @return Entity
* @return RootEntity
*/
public function getEntity()
{
......@@ -89,7 +90,7 @@ class Root extends AbstractService
/**
* Retourne l'entité dans la branche "structures" liée à l'établissement
*
* @return Entity|StructureEntity
* @return StructureEntity
*/
public function getStructureEntity()
{
......
......@@ -73,7 +73,7 @@ class Structure extends AbstractService
/**
* Retourne la structure mère : Université
*
* @return StructureEntity|Entity
* @return StructureEntity
*/
public function getStructureMere()
{
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment