Skip to content
Snippets Groups Projects
Commit 9f0cf3ea authored by David Surville's avatar David Surville
Browse files

Merge branch 'zf-3.0'

parents 617a8566 84b1adb6
No related branches found
No related tags found
No related merge requests found
Showing
with 4383 additions and 482 deletions
CHANGELOG
=========
3.0.0 (01/10/2020)
------------------
- Adaptation du code pour le passage à ZF3.
- Scission des classes liées aux entités ("Generic", "Group", "People", "Root", "Structure" et "System") en deux classes :
- classe de base avec un setter pour (presque) chaque attribut Ldap
- classe qui hérite de la classe de base avec des fonctions spécifiques à l'entité
- Gestions des attributs "supannActivite", "ucbnSecteurDisciplinaire" et "supannEmpCorps"
- Ajout des getters pour les attributs de la classe "People" : "rid", "sambaSID", "uidNumber", "gidNumber", "loginShell" et "homeDirectory"
- Ajout des setters pour les différents attributs liés aux structures de la classe "People"
- Ajout des attributs cachés "createTimestamp" et "modifyTimestamp" comme DateTimeAttributes
- Ajout des setters pour les attributs de la classe "People" : "supannEtablissement", "supannEtuAnneeInscription", "supannEtuCursusAnnee", "supannEtuDiplome" et "supannEtuEtape"
- Ajout des setters pour les attributs de la classe "People" : "supannEtuInscription", "ucbnSiteLocalisation", "ucbnAnneePostBac", "ucbnCodeEtape", "ucbnEtuComplementInscription",
"ucbnPrivateAddress", "ucbnPrivateAddresseBis", "supannEtuElementPedagogique", "supannEtuRegimeInscription", "supannEtuSecteurDisciplinaire"
et "supannEtuTypeDiplome"
- Gestion de la modification de l'attribut "supannRefId" par étiquette
- Ajout de fonctions de vérification du statut d'une personne dans la classe "People"
- Fonction de récupération de champs date au format Php DateTime
- Mise en place d'une entité "Root" pour gérer la racine de l'annuaire Ldap et d'un service associé
- Ajout des setters pour les attributs de la classe "People" : "supannAutreMail" et "mailForwardingAddress"
# UnicaenLdap
* [Introduction](#introduction)
* [Installation](#installation)
* [Configuration](#configuration)
## Introduction
Ce module permet de se connecter à l'annuaire Ldap de l'université et de consulter/modifier ses données.
## Pré-requis
L'utilisation de ce module nécessite l'installation de l'extension `ext-ldap` de PHP.
## Installation
```bash
$ composer require unicaen/ldap
```
## Configuration
> Récupérer les fichiers de config du module
```bash
$ cp -n vendor/unicaen/zimbra/config/unicaen-ldap.local.php.dist config/autoload/unicaen-ldap.local.php
```
> Adapter le contenu à vos besoins en configurant notamment les paramètres de connexion au serveur Ldap.
```php
'unicaen-ldap' => [
'host' => 'ldap-test.unicaen.fr',
'port' => 389,
'version' => 3,
'baseDn' => "dc=unicaen,dc=fr", // racine de l'annuaire
'bindRequiresDn' => true,
'username' => "uid=xxxx,ou=system,dc=unicaen,dc=fr",
'password' => "xxxx",
'accountFilterFormat' => "(&(objectClass=supannPerson)(supannAliasLogin=%s))",
]
```
\ No newline at end of file
......@@ -14,14 +14,14 @@ use UnicaenLdap\Service\Group as LdapGroupService;
use UnicaenLdap\Service\GroupFactory as GroupServiceFactory;
use UnicaenLdap\Service\People as LdapPeopleService;
use UnicaenLdap\Service\PeopleFactory as PeopleServiceFactory;
use UnicaenLdap\Service\Root as LdapRootService;
use UnicaenLdap\Service\RootFactory as RootServiceFactory;
use UnicaenLdap\Service\Structure as LdapStructureService;
use UnicaenLdap\Service\StructureFactory as StructureServiceFactory;
use UnicaenLdap\Service\System as LdapSystemService;
use UnicaenLdap\Service\SystemFactory as SystemServiceFactory;
use Zend\ServiceManager\Proxy\LazyServiceFactory;
;;
return array(
'unicaen-ldap' => [
......@@ -30,10 +30,10 @@ return array(
'factories' => [
'Ldap' => LdapFactory::class,
'LdapOptions' => ModuleOptionsFactory::class,
'LdapServiceGeneric' => GenericServiceFactory::class,
'LdapServiceGroup' => GroupServiceFactory::class,
'LdapServicePeople' => PeopleServiceFactory::class,
'LdapServiceRoot' => RootServiceFactory::class,
'LdapServiceStructure' => StructureServiceFactory::class,
'LdapServiceSystem' => SystemServiceFactory::class,
],
......@@ -43,11 +43,13 @@ return array(
'ldapServiceGeneric' => 'LdapServiceGeneric',
'ldapServiceGroup' => 'LdapServiceGroup',
'ldapServicePeople' => 'LdapServicePeople',
'ldapServiceRoot' => 'LdapServiceRoot',
'ldapServiceStructure' => 'LdapServiceStructure',
'ldapServiceSystem' => 'LdapServiceSystem',
LdapGenericService::class => 'LdapServiceGeneric',
LdapPeopleService::class => 'LdapServicePeople',
LdapGroupService ::class => 'LdapServiceGroup',
LdapPeopleService::class => 'LdapServicePeople',
LdapRootService::class => 'LdapServiceRoot',
LdapStructureService::class => 'LdapServiceStructure',
LdapSystemService::class => 'LdapServiceSystem',
],
......@@ -57,6 +59,7 @@ return array(
'LdapServicePeople' => LdapPeopleService::class,
'LdapServiceGeneric' => LdapGenericService::class,
'LdapServiceGroup' => LdapGroupService::class,
'LdapServiceRoot' => LdapRootService::class,
'LdapServiceStructure' => LdapStructureService::class,
'LdapServiceSystem' => LdapSystemService::class,
],
......@@ -71,14 +74,15 @@ return array(
'LdapServiceGroup' => [
LazyServiceFactory::class,
],
'LdapServiceRoot' => [
LazyServiceFactory::class,
],
'LdapServiceStructure' => [
LazyServiceFactory::class,
],
'LdapServiceSystem' => [
LazyServiceFactory::class,
],
],
],
);
<?php
return array(
'unicaen-ldap' => array(
return [
'unicaen-ldap' => [
'host' => 'host.domain.fr',
'port' => 389,
'version' => 3,
'baseDn' => "ou=xxxxxxxxxxx,dc=domain,dc=fr",
'baseDn' => "dc=domain,dc=fr", // racine de l'annuaire
'bindRequiresDn' => true,
'username' => "uid=xxxxxxxxx,ou=xxxxxxxxxx,dc=domain,dc=fr",
'password' => "xxxxxxxxxxxx",
'accountFilterFormat' => "(&(objectClass=posixAccount)(supannAliasLogin=%s))",
)
);
]
];
<?php
namespace UnicaenLdap\Entity\Base;
use UnicaenLdap\Entity\Entity;
use UnicaenLdap\Exception;
use Zend\Ldap\Attribute;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère des entités de la branche "generic" de l'annuaire LDAP.
*
* @author David Surville <david.surville@unicaen.fr>
*/
class Generic extends Entity
{
/**
* @var string
*/
protected $type = 'Generic';
/**
* Liste des classes d'objet nécessaires à la création d'une adresse générique
*
* @var array
*/
protected $objectClass = [
'top',
'inetOrgPerson',
'organizationalPerson',
'person',
];
/**
* Liste des attributs autorisés pour une entité "Generic"
*
* @var array
*/
protected $authorizedAttributes = [
// Attributes classes
'objectClass',
// Attributes
'cn',
'description',
'mail',
'sn',
'supannAliasLogin',
'ucbnServiceIMAP',
'userPassword',
];
/**
* Liste des attributs contenant des dates
*
* @var string[]
*/
protected $dateTimeAttributes = [
];
/**
* Liste des attributs monovalués
*
* @var array
*/
protected $monoValuedAttributes = [
'supannALiasLogin',
'ucbnServiceIMAP',
'userPassword',
];
/**
* Attribut Ldap "cn"
*
* @param array|string|null $value
* @param bool $append
* @return self
*/
public function setCn($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('cn', $value, $append);
return $this;
}
/**
* Attribut Ldap "description"
*
* @param array|string|null $value
* @param bool $append
* @return self
*/
public function setDescription($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('description', $value, $append);
return $this;
}
/**
* Attribut Ldap "mail"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setMail($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_filter(filter_var_array($value, FILTER_VALIDATE_EMAIL));
$this->appendOrNot('mail', $value, $append);
return $this;
}
/**
* Attribut Ldap "sn"
*
* @param array|string|null $value
* @param bool $append
* @return self
*/
public function setSn($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('sn', $value, $append);
return $this;
}
/**
* Attribut Ldap "supannALiasLogin"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setSupannAliasLogin($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_map('strtolower', $value);
$value = array_filter($value, function ($v) {
return preg_match('/^[0-9a-z\-]+$/', $v);
});
$this->appendOrNot('supannAliasLogin', $value, $append);
return $this;
}
/**
* Attribut Ldap "ucbnServiceIMAP"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setUcbnServiceIMAP($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_map([$this, 'formatBoolean'], $value);
$this->appendOrNot('ucbnServiceIMAP', $value, $append);
return $this;
}
/**
* Attribut Ldap "userPassword"
*
* @param string $value
* @return self
* @throws LdapException
*/
public function setUserPassword(string $value)
{
$this->getNode()->setPasswordAttribute($value, Attribute::PASSWORD_HASH_SHA, 'userPassword');
return $this;
}
}
\ No newline at end of file
<?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->service->getLdapSystemService()->getBranches()[0]
);
}
/**
* Attribut Ldap "description"
*
* @param array|string|null $value
* @param bool $append
* @return self
*/
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
This diff is collapsed.
<?php
namespace UnicaenLdap\Entity\Base;
use UnicaenLdap\Entity\Entity;
use UnicaenLdap\Entity\Structure as StructureEntity;
use UnicaenLdap\Exception;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère de l'entité racine de l'annuaire LDAP.
*
* @author David Surville <david.surville@unicaen.fr>
*/
class Root extends Entity
{
protected $type = 'Root';
/**
* Liste des classes d'objet nécessaires à la création d'une structure
*
* @var string[]
*/
protected $objectClass = [
'top',
'dcObject',
'organization',
'eduOrg',
'supannOrg',
];
/**
* Liste des attributs autorisés pour l'entité "Root"
*
* @var array
*/
protected $authorizedAttributes = [
// Attributes classes
'objectClass',
// Attributes
'description',
'eduOrgHomePageURI',
'eduOrgLegalName',
'eduOrgSuperiorURI',
'eduOrgWhitePagesURI',
'facsimileTelephoneNumber',
'l',
'o',
'postalAddress',
'supannEtablissement',
'telephoneNumber',
];
/**
* Liste des attributs contenant des dates
*
* @var array
*/
protected $dateTimeAttributes = [];
/**
* Liste des attributs monovalués
*
* @var array
*/
protected $monoValuedAttributes = [
'eduOrgLegalName',
];
/**
* 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 "eduOrgHomePageURI"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setEduOrgHomePageURI($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_filter(filter_var_array($value, FILTER_VALIDATE_URL));
$this->appendOrNot('eduOrgHomePageURI', $value, $append);
return $this;
}
/**
* Attribut Ldap "eduOrgLegalName"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setEduOrgLegalName($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('eduOrgLegalName', $value, $append);
return $this;
}
/**
* Attribut Ldap "eduOrgSuperiorURI"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setEduOrgSuperiorURI($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_filter(filter_var_array($value, FILTER_VALIDATE_URL));
$this->appendOrNot('eduOrgSuperiorURI', $value, $append);
return $this;
}
/**
* Attribut Ldap "eduOrgWhitePagesURI"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setEduOrgWhitePagesURI($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_filter(filter_var_array($value, FILTER_VALIDATE_URL));
$this->appendOrNot('eduOrgWhitePagesURI', $value, $append);
return $this;
}
/**
* Attribut Ldap "facsimileTelephoneNumber"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setFacsimileTelephoneNumber($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_map([$this, 'formatTel'], $value);
$this->appendOrNot('facsimileTelephoneNumber', $value, $append);
return $this;
}
/**
* Attribut Ldap "l"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setL($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('l', $value, $append);
return $this;
}
/**
* Attribut Ldap "o"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setO($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('o', $value, $append);
return $this;
}
/**
* Attribut Ldap "postalAddress"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setPostalAddress($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_filter($value, function ($v) {
return preg_match(self::$postal_address_pattern, $v);
});
$this->appendOrNot('postalAddress', $value, $append);
return $this;
}
/**
* Attribut Ldap "supannEtablissement"
*
* @param array|string|null $value
* @param bool $append
* @return $this
* @throws Exception
* @throws LdapException
*/
public function setSupannEtablissement($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('supannEtablissement', $value, $append);
return $this;
}
/**
* Attribut Ldap "telephoneNumber"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setTelephoneNumber($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_map([$this, 'formatTel'], $value);
$this->appendOrNot('telephoneNumber', $value, $append);
return $this;
}
}
\ No newline at end of file
<?php
namespace UnicaenLdap\Entity\Base;
use UnicaenLdap\Entity\Entity;
use UnicaenLdap\Entity\Structure as StructureEntity;
use UnicaenLdap\Exception;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère des entités de la branche "structures" de l'annuaire LDAP.
*
* @author David Surville <david.surville@unicaen.fr>
*/
class Structure extends Entity
{
protected $type = 'Structure';
/**
* Liste des classes d'objet nécessaires à la création d'une structure
*
* @var string[]
*/
protected $objectClass = [
'top',
'organizationalUnit',
'supannEntite',
'ucbnEntite',
];
/**
* Liste des attributs autorisés pour une entité "People"
*
* @var array
*/
protected $authorizedAttributes = [
// Attributes classes
'objectClass',
// Attributes
'description',
'facsimileTelephoneNumber',
'ou',
'postalAddress',
'supannCodeEntiteParent',
'supannRefId',
'supannTypeEntite',
'telephoneNumber',
];
/**
* Liste des attributs contenant des dates
*
* @var array
*/
protected $dateTimeAttributes = [];
/**
* Liste des attributs monovalués
*
* @var array
*/
protected $monoValuedAttributes = [];
/**
* 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 "ou"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setOu($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('ou', $value, $append);
return $this;
}
/**
* Attribut Ldap "supannCodeEntiteParent"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setSupannCodeEntiteParent($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_map(function ($val) {
if (is_string($val)) {
if (0 !== strpos($val, $this->service->getCodeStructurePrefixe())) {
$val = $this->service->getCodeStructurePrefixe() . $val;
}
return $val;
} elseif ($val instanceof StructureEntity) {
return $val->get('supannCodeEntite');
} else {
return null;
}
}, $value);
$this->appendOrNot('supannCodeEntiteParent', array_filter($value), $append);
return $this;
}
/**
* Attribut Ldap "supannTypeEntite"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setSupannTypeEntite($value = null, $append = false)
{
$value = $this->preFormat($value);
$supannLabel = $this->getLabel('SUPANN');
$value = array_map(function ($val) use ($supannLabel) {
if (is_string($val)) {
return preg_match("/^$supannLabel.+$/", $val) ? $val : sprintf('%s%s', $supannLabel, $val);
} else {
return null;
}
}, $value);
$this->appendOrNot('supannTypeEntite', array_filter($value), $append);
return $this;
}
/**
* Attribut Ldap "telephoneNumber"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setTelephoneNumber($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_map([$this, 'formatTel'], $value);
$this->appendOrNot('telephoneNumber', $value, $append);
return $this;
}
/**
* Attribut Ldap "facsimileTelephoneNumber"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setFacsimileTelephoneNumber($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_map([$this, 'formatTel'], $value);
$this->appendOrNot('facsimileTelephoneNumber', $value, $append);
return $this;
}
/**
* Attribut Ldap "postalAddress"
*
* @param array|string|null $value
* @param bool $append
* @return self
* @throws Exception
* @throws LdapException
*/
public function setPostalAddress($value = null, $append = false)
{
$value = $this->preFormat($value);
$value = array_filter($value, function ($v) {
return preg_match(self::$postal_address_pattern, $v);
});
$this->appendOrNot('postalAddress', $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
<?php
namespace UnicaenLdap\Entity\Base;
use UnicaenLdap\Entity\Entity;
use Zend\Ldap\Attribute;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère des entités de la branche "system" de l'annuaire LDAP.
*
* @author David Surville <david.surville@unicaen.fr>
*/
class System extends Entity
{
protected $type = 'System';
/**
* Liste des classes d'objet nécessaires à la création d'un compte système
*
* @var string[]
*/
protected $objectClass = [
'top',
'inetOrgPerson',
'organizationalPerson',
'person',
'supannPerson',
'ucbnEmp',
];
/**
* Liste des attributs autorisés pour une entité "Generic"
*
* @var array
*/
protected $authorizedAttributes = [
// Attributes classes
'objectClass',
// Attributes
'cn',
'sn',
'userPassword',
];
/**
* Liste des attributs contenant des dates
*
* @var string[]
*/
protected $dateTimeAttributes = [
];
/**
* Attribut Ldap "cn"
*
* @param array|string|null $value
* @param bool $append
* @return self
*/
public function setCn($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('cn', $value, $append);
return $this;
}
/**
* Attribut Ldap "sn"
*
* @param array|string|null $value
* @param bool $append
* @return self
*/
public function setSn($value = null, $append = false)
{
$value = $this->preFormat($value);
$this->appendOrNot('sn', $value, $append);
return $this;
}
/**
* Attribut Ldap "userPassword"
*
* @param string $value
* @return self
* @throws LdapException
*/
public function setUserPassword(string $value)
{
$this->getNode()->setPasswordAttribute($value, Attribute::PASSWORD_HASH_SHA, 'userPassword');
return $this;
}
}
\ No newline at end of file
......@@ -5,6 +5,9 @@ namespace UnicaenLdap\Entity;
use UnicaenLdap\Node;
use UnicaenLdap\Exception;
use UnicaenLdap\Service\AbstractService;
use UnicaenLdap\Util;
use Zend\Ldap\Dn;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère des entrées de l'annuaire LDAP.
......@@ -35,27 +38,62 @@ abstract class Entity
/**
* Liste des classes d'objet nécessaires à la création de l'entité
*
* @var string[]
* @var array
*/
protected $objectClass = [
protected $objectClass = [];
/**
* Liste des attributs autorisés pour une entité
*
* @var array
*/
protected $authorizedAttributes = [];
/**
* Liste des attributs contenant des dates communs à toutes les entités
*/
private $sharedDateTimeAttributes = [
'createTimestamp',
'modifyTimestamp',
];
/**
* Liste des attributs contenant des dates
*
* @var string[]
* @var array
*/
protected $dateTimeAttributes = [
protected $dateTimeAttributes = [];
];
/**
* Liste des attributs monovalués
*
* @var array
*/
protected $monoValuedAttributes = [];
/**
* Liste des patterns génériques utilisés pour différents attributs
*/
static protected $attribute_with_label_pattern = '/^(?<etiquette>\{[\w\-:]+\})(?<identifiant>.+)$/';
/**
* Liste des patterns spécifiques utilisés pour différents attributs
*/
static protected $postal_address_pattern = '/^(.*)\$(.*)\$(.*)\$(.*)\$(.*)\$(.*)$/';
/**
* @param string $type type d'entité
* @return array
* @throws Exception
*/
public static function getNodeParams($type)
{
$params = [
'Generic' => ['uid', 'UnicaenLdap\\Entity\\Generic'],
'Group' => ['cn', 'UnicaenLdap\\Entity\\Group'],
'People' => ['uid', 'UnicaenLdap\\Entity\\People'],
'Root' => ['dc', 'UnicaenLdap\\Entity\\Root'],
'Structure' => ['supannCodeEntite', 'UnicaenLdap\\Entity\\Structure'],
'System' => ['uid', 'UnicaenLdap\\Entity\\System'],
];
......@@ -64,11 +102,28 @@ 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);
}
}
/**
* Construit une entrée.
* Entity constructor.
*
* @param AbstractService $service Service qui gèrera la future entité
* @param Node|Dn|array|string $data Noeud si Node, sinon DN
* @throws LdapException
*/
public function __construct(AbstractService $service, $data)
{
......@@ -82,7 +137,7 @@ abstract class Entity
}
/**
* Retourne le type du service
* Retourne le type de l'entité
*
* @return string
*/
......@@ -132,98 +187,113 @@ abstract class Entity
}
/**
* Retourne la clé primaire correspondant à l'entité
* Retourne l'Organizational Unit (OU) de l'entité
*
* @return string
*/
public function getId()
public function getOu()
{
list($key) = self::getNodeParams($this->type);
if ($result = $this->getNode()->getDn()->get(1)) {
return $result['ou'];
}
return $this->get($key);
return null;
}
/**
* Retourne la clé primaire correspondant à l'entité
* Retourne le nom de la clé primaire correspondant à l'entité
*
* @return string
* @return mixed
* @throws Exception
*/
public function getKey()
{
return reset(self::getNodeParams($this->type));
$params = self::getNodeParams($this->type);
return reset($params);
}
/**
* Retourne la liste des attributs de l'entité
* Retourne la valeur de la clé primaire correspondant à l'entité
*
* @return string[]
* @return string
* @throws Exception
* @throws LdapException
*/
public function getAttributesList()
public function getId()
{
return array_keys($this->getNode()->getAttributes());
return $this->get($this->getKey());
}
/**
* Exporte sous forme de tableau le contenu de l'entité
* Retourne l'attribut "objectClass"
*
* @return array
* @return array|int|mixed|null
* @throws LdapException
*/
public function toArray()
public function getObjectClass()
{
$result = [];
$attrsList = $this->getAttributesList();
foreach ($attrsList as $attrName) {
$result[$attrName] = $this->get($attrName);
}
return $result;
return $this->get('objectClass');
}
/**
* Mise à jour de l'entité
* Retourne la liste des attributs de l'entité
*
* @return self
* @param bool $includeSystemAttributes
* @return string[]
*/
public function update()
public function getAttributesList($includeSystemAttributes = true)
{
$this->getNode()->update();
return $this;
return array_keys($this->getNode()->getAttributes($includeSystemAttributes));
}
/**
* Suppression de l'entité
* Retourne un label constitué des différentes clés passées en paramètre
* Format : {$key1:$key2:$key3:...}
*
* @return self
* @param string ...$key
* @return string
*/
public function delete()
public function getLabel(...$key)
{
$this->getNode()->delete();
$label = '';
$c = 0;
foreach($key as $k) {
if(is_string($k)) {
$label .= ($c == 0) ? $k : ":$k";
}
$c++;
}
return $this;
return sprintf('{%s}', $label);
}
/**
* Insertion de l'entité
* Exporte sous forme de tableau le contenu de l'entité
*
* @return self
* @return array
* @throws LdapException
*/
public function insert()
public function toArray()
{
$this->getNode()->attachLdap($this->service->getLdap());
$result = [];
$attrsList = $this->getAttributesList();
foreach ($attrsList as $attrName) {
$result[$attrName] = $this->get($attrName);
}
return $this;
return $result;
}
/**
* Retourne un attribut
*
* @param string $attrName
* @return mixed
* @return array|int|mixed|null
* @throws LdapException
*/
public function get($attrName)
{
if (in_array($attrName, $this->dateTimeAttributes)) {
if (in_array($attrName, array_merge($this->dateTimeAttributes, $this->sharedDateTimeAttributes))) {
$value = $this->getNode()->getDateTimeAttribute($attrName);
} else {
$value = $this->getNode()->getAttribute($attrName);
......@@ -240,13 +310,28 @@ abstract class Entity
/**
* Affecte une nouvelle valeur à un attribut
*
* This is an offline method.
* Node will be updated on calling update().
*
* @param string $attrName
* @param mixed $value
* @return self
* @throws Exception
* @throws LdapException
*/
public function set($attrName, $value)
{
if (in_array($attrName, $this->dateTimeAttributes)) {
if(!in_array($attrName, $this->authorizedAttributes)) {
throw new Exception(sprintf("L'attribut Ldap '%s' n'est pas autorisé pour une entité '%s'.", $attrName, get_class($this)));
}
if(is_array($value)
&& count($value) > 1
&& in_array($attrName, $this->monoValuedAttributes)) {
throw new Exception(sprintf("L'attribut Ldap '%s' est monovalué et ne doit contenir qu'une seule valeur.", $attrName));
}
if (in_array($attrName, array_merge($this->dateTimeAttributes, $this->sharedDateTimeAttributes))) {
$this->getNode()->setDateTimeAttribute($attrName, $value, true);
} else {
$this->getNode()->setAttribute($attrName, $value);
......@@ -269,14 +354,28 @@ abstract class Entity
/**
* Ajoute une valeur à un attribut
* Si l'attribut est monovalué, la valeur actuelle est remplacée par la nouvelle valeur
*
* This is an offline method.
* Node will be updated on calling update().
*
* @param string $attrName
* @param mixed $value
* @return self
* @throws Exception
* @throws LdapException
*/
public function add($attrName, $value)
{
if (in_array($attrName, $this->dateTimeAttributes)) {
if(!in_array($attrName, $this->authorizedAttributes)) {
throw new Exception(sprintf("L'attribut Ldap '%s' n'est pas autorisé pour une entité '%s'.", $attrName, get_class($this)));
}
if(in_array($attrName, $this->monoValuedAttributes)) {
$this->set($attrName, $value);
}
else {
if (in_array($attrName, array_merge($this->dateTimeAttributes, $this->sharedDateTimeAttributes))) {
$this->getNode()->appendToDateTimeAttribute($attrName, $value);
} else {
$this->getNode()->appendToAttribute($attrName, $value);
......@@ -284,12 +383,16 @@ abstract class Entity
return $this;
}
}
/**
* Retire une valeur à un attribut
*
* @param type $attrName
* @param type $value
* This is an offline method.
* Node will be updated on calling update().
*
* @param string $attrName
* @param mixed|array $value
* @return self
*/
public function remove($attrName, $value)
......@@ -299,11 +402,31 @@ abstract class Entity
return $this;
}
/**
* Ajoute ou affecte une valeur à un attribut Ldap
*
* This is an offline method.
* Node will be updated on calling update().
*
* @param string $attrName
* @param mixed $value
* @param bool $append
* @throws Exception
* @throws LdapException
*/
protected function appendOrNot($attrName, $value, $append)
{
(!$append)
? $this->set($attrName, $value)
: $this->add($attrName, $value);
}
/**
* Méthode magique...
*
* @param string $attrName
* @return mixed
* @throws LdapException
*/
public function __get($attrName)
{
......@@ -316,6 +439,7 @@ abstract class Entity
* @param string $attrName
* @param mixed $value
* @return self
* @throws LdapException
*/
public function __set($attrName, $value)
{
......@@ -341,4 +465,175 @@ abstract class Entity
}
}
}
/**
* Déplacement de l'entité dans un autre OU (Organizational Unit)
*
* This is an offline method.
* Node will be moved on calling update().
*
* @param string | Dn $newDn
* @throws \Zend\Ldap\Exception\LdapException
*/
public function move(string $newOu)
{
$arrayOu = $this->getService()->getOu();
if(empty($arrayOu) || !in_array($newOu, $arrayOu)) {
throw new Exception(sprintf("L'entité ne peut être déplacée dans l'OU '%s'.", $newOu));
}
if($newOu == $this->getOu()) {
throw new Exception(sprintf("L'entité est déjà présente dans l'OU '%s'.", $newOu));
}
$newDn = sprintf('%s=%s,ou=%s,%s', $this->getKey(), $this->getId(), $newOu, $this->getNode()->getLdap()->getBaseDn());
$this->getNode()->move($newDn);
return $this;
}
/**
* Renommage de l'entité
*
* This is an offline method. Node will be renamed on calling update().
*
* @param string | Dn $newDn
* @throws \Zend\Ldap\Exception\LdapException
*/
public function rename($newId)
{
$newDn = sprintf('%s=%s,ou=%s,%s', $this->getKey(), $newId, $this->getOu(), $this->getNode()->getLdap()->getBaseDn());
$this->getNode()->move($newDn);
}
/**
* Suppression de l'entité
*
* This is an offline method.
* Node will be deleted on calling update().
*
* @return self
*/
public function delete()
{
$this->getNode()->delete();
return $this;
}
/**
* Mise à jour de l'entité
*
* @return self
* @throws LdapException
*/
public function update()
{
$this->getNode()->update();
return $this;
}
/**
* Attache une connexion
*
* @return self
* @throws LdapException
*/
public function attach()
{
$this->getNode()->attachLdap($this->service->getLdap());
return $this;
}
/**
* Formate un nom ou un prénom
*
* @param string $name
* @param bool $removeAccents remplace les caractères avec un signe diacritique
* @return mixed|null|string|string[]
*/
protected function formatName($name, $removeAccents = false)
{
$name = preg_replace('/[[:blank:]]+/', ' ', trim($name));
if ($removeAccents) {
$name = Util::removeAccents($name);
}
return mb_convert_case($name, MB_CASE_TITLE, "UTF-8");
}
/**
* Formate un numéro de téléphone (0XXXXXXXXX) au format international (+33 X XX XX XX XX)
*
* @param string $num
* @return string
*/
protected function formatTel($num)
{
if (is_null($num)) {
return null;
}
$num = preg_replace('/[\.\-\s]+/', '', $num);
return preg_match("/0\d{9}/", $num)
? preg_replace("/0(\d{1})(\d{2})(\d{2})(\d{2})(\d{2})/", "+33 $1 $2 $3 $4 $5", $num)
: $num;
}
/**
* Formate les données sources correspondant à un booléen
*
* @param mixed $value
* @return string
*/
protected function formatBoolean($value)
{
if (!is_bool($value) && $value === null) {
return null;
}
if (is_string($value)) {
$value = strtolower($value);
}
switch (true) {
case $value === false;
case $value === 0:
case $value === 'n':
return 'FALSE';
case $value === true:
case $value === 1:
case $value === 'o':
case $value === 'y':
return 'TRUE';
default:
return null;
}
}
/**
* Vérifie une valeur et supprime les valeurs nulles
*
* @param mixed $value
* @return array
*/
protected function preFormat($value)
{
if (is_scalar($value)
|| is_null($value)
|| is_a($value, 'DateTime')) {
$value = [$value];
}
$value = array_filter($value, 'strlen'); // Delete NULL, '' and FALSE values
return $value;
}
}
\ No newline at end of file
......@@ -2,34 +2,12 @@
namespace UnicaenLdap\Entity;
/**
* Classe mère des adresses génériques de l'annuaire LDAP.
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class Generic extends Entity
{
protected $type = 'Generic';
use UnicaenLdap\Entity\Base\Generic as BaseGeneric;
/**
* Liste des classes d'objet nécessaires à la création d'une adresse générique
* Classe de gestion des entités de la branche "generic" de l'annuaire LDAP.
*
* @var string[]
*/
protected $objectClass = [
'top',
'inetOrgPerson',
'organizationalPerson',
'person',
'supannPerson',
'ucbnEmp',
];
/**
* Liste des attributs contenant des dates
*
* @var string[]
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
protected $dateTimeAttributes = [
];
}
\ No newline at end of file
class Generic extends BaseGeneric
{}
\ No newline at end of file
......@@ -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);
$peopleService = $this->service->getLdapPeopleService();
$result = $peopleService->getAllBy($this->member, 'dn', $orderBy);
return $result;
}
......
This diff is collapsed.
<?php
namespace UnicaenLdap\Entity;
use UnicaenLdap\Entity\Base\Root as BaseRoot;
use UnicaenLdap\Filter\Filter;
use Zend\Ldap\Exception\LdapException;
/**
* Classe de gestion de l'entité racine de l'annuaire LDAP.
*
* @author David SURVILLE <david.surville@unicaen.fr>
*/
class Root extends BaseRoot
{}
\ No newline at end of file
......@@ -2,38 +2,18 @@
namespace UnicaenLdap\Entity;
use UnicaenLdap\Entity\Base\Structure as BaseStructure;
use UnicaenLdap\Filter\Filter;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère des structures de l'annuaire LDAP.
* Classe de gestion des entités de la branche "structures" de l'annuaire LDAP.
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
* @author David SURVILLE <david.surville@unicaen.fr>
*/
class Structure extends Entity
class Structure extends BaseStructure
{
protected $type = 'Structure';
/**
* Liste des classes d'objet nécessaires à la création d'une structure
*
* @var string[]
*/
protected $objectClass = [
'top',
'organizationalUnit',
'supannEntite',
'ucbnEntite',
];
/**
* Liste des attributs contenant des dates
*
* @var string[]
*/
protected $dateTimeAttributes = [
];
/**
* Retourne la structure parente, si elle existe
*
......@@ -41,7 +21,7 @@ class Structure extends Entity
*/
public function getParents()
{
if (null !== $parentIds = $this->get('supannCodeEntiteParent')) {
if (null !== ($parentIds = $this->get('supannCodeEntiteParent'))) {
return $this->service->getAll($parentIds);
}
......@@ -70,17 +50,19 @@ class Structure extends Entity
}
/**
* Retourne le code Harpège
* Retourne le code dans la base source
*
* @return string
* @throws LdapException
* @throws \UnicaenLdap\Exception
*/
public function getCodeHarpege()
public function getCodeSource()
{
$code = $this->get('supannCodeEntite');
if (0 === strpos($code, 'HS_')) {
return substr($code, 3);
} else {
return null; // Ne retourne rien si le code ne correspond pas à la nomenclature Harpège
}
$code = $this->getId();
return (
0 === strpos($code, $this->service->getCodeStructurePrefixe()) ||
0 === strpos($code, $this->service->getCodeModuleEnseignementPrefixe()))
? substr($code, 3)
: $code;
}
}
\ No newline at end of file
......@@ -2,32 +2,12 @@
namespace UnicaenLdap\Entity;
use UnicaenLdap\Entity\Base\System as BaseSystem;
/**
* Classe mère des utilisateurs système de l'annuaire LDAP.
* Classe de gestion des entités de la branche "system" de l'annuaire LDAP.
*
* @author Laurent Lécluse <laurent.lecluse at unicaen.fr>
*/
class System extends Entity
{
protected $type = 'System';
/**
* Liste des classes d'objet nécessaires à la création d'un compte système
*
* @var string[]
*/
protected $objectClass = [
'top',
'inetOrgPerson',
'organizationalPerson',
'person',
];
/**
* Liste des attributs contenant des dates
*
* @var string[]
*/
protected $dateTimeAttributes = [
];
}
\ No newline at end of file
{}
\ No newline at end of file
This diff is collapsed.
......@@ -9,7 +9,19 @@ namespace UnicaenLdap\Service;
*/
class Generic extends AbstractService
{
/**
* Type de l'entité
*
* @var string
*/
protected $type = 'Generic';
protected $ou = array('generic');
/**
* Organizational Units
*
* @var array
*/
protected $ou = [
'generic'
];
}
\ No newline at end of file
......@@ -27,22 +27,25 @@ class GenericFactory implements FactoryInterface
{
/**
* @var Ldap $ldap
* @var Service\Group $ldapGroupService
* @var Service\People $ldapPeopleService
* @var Service\Root $ldapRootService
* @var Service\Structure $ldapStructureService
* @var Service\Group $ldapGroupService
* @var Service\System $ldapSystemService
*/
$ldap = $container->get('ldap');
$ldapGroupService = $container->get('ldapServiceGroup');
$ldapPeopleService = $container->get('ldapServicePeople');
$ldapRootService = $container->get('ldapServiceRoot');
$ldapStructureService = $container->get('ldapServiceStructure');
$ldapGroupService = $container->get('ldapServiceGroup');
$ldapSystemService = $container->get('ldapServiceSystem');
$service = new Generic();
$service->setLdap($ldap);
$service->setLdapGroupService($ldapGroupService);
$service->setLdapPeopleService($ldapPeopleService);
$service->setLdapRootService($ldapRootService);
$service->setLdapStructureService($ldapStructureService);
$service->setLdapGroupService($ldapGroupService);
$service->setLdapSystemService($ldapSystemService);
return $service;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment