Commit 0af6a8e2 authored by David Surville's avatar David Surville
Browse files

Reprise du développement de la librairie. Ajout notamment de fonctions pour chaque attribut Ldap

parent 78275982
<?php
namespace UnicaenLdap\Entity\Base;
use DateTime;
use UnicaenLdap\Entity\Entity;
use UnicaenLdap\Util;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère des entités de la branche "people" de l'annuaire LDAP.
*
* @author David Surville <david.surville at unicaen.fr>
*/
class People extends Entity
{
protected $type = 'People';
/**
* Liste des classes d'objet nécessaires à la création d'une personne
* Il est nécessaire d'ajouter la classe 'ucbnEtu' ou 'ucbnEmp' selon le
* statut de la personne.
*
* @var array
*/
protected $objectClass = [
'top',
'inetOrgPerson',
'organizationalPerson',
'person',
'eduPerson',
'supannPerson',
'sambaAccount',
'sambaSamAcount',
'posixAccount',
];
/**
* Liste des attributs contenant des dates
*
* @var array
*/
protected $dateTimeAttributes = [];
/**
* @param string $attrName
* @param mixed $value
* @param bool $append
* @throws \Zend\Ldap\Exception\LdapException
*/
private function _appendOrNot($attrName, $value, $append)
{
(!$append)
? $this->set($attrName, $value)
: $this->add($attrName, $value);
}
/**
* Attribut Ldap "sn"
*
* Nom d'un individu
* Doit contenir le nom d'usage. Il est possible d'ajouter le nom de famille en seconde valeur.
*
* Multivalué
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSn($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatName'], array_filter($data));
$this->_appendOrNot('sn', $values, $append);
return $this;
}
/**
* Attribut Ldap "givenName"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setGivenName($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatName'], array_filter($data));
$this->_appendOrNot('givenName', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "displayName"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setDisplayName($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatName'], array_filter($data));
$this->_appendOrNot('displayName', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "cn"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setCn($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatName'], array_filter($data));
$this->_appendOrNot('cn', Util::removeAccents($values[key($values)]), $append);
return $this;
}
/**
* Attribut Ldap "dateDeNaissance"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setDateDeNaissance($data = null, $append = false)
{
$data = (array)$data;
$values = array_map(function ($val) {
if (is_string($val)) {
$val = new DateTime($val);
}
return $val->format('Ymd');
}, array_filter($data));
$this->_appendOrNot('dateDeNaissance', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "schacDateOfBirth"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSchacDateOfBirth($data = null, $append = false)
{
$data = (array)$data;
$values = array_map(function ($val) {
if (is_string($val)) {
$val = new DateTime($val);
}
return $val->format('Ymd');
}, array_filter($data));
$this->_appendOrNot('schacDateOfBirth', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "sexe"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSexe($data = null, $append = false)
{
if (is_array($data)) {
$values = array_filter($data);
$data = $values[key($values)];
}
$value = (in_array($data, ['F', 'M'])) ? $data : null;
$this->_appendOrNot('sexe', $value, $append);
}
/**
* Attribut Ldap "postalAddress"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setPostalAddress($data = null, $append = false)
{
$data = (array)$data;
$values = array_filter($data, function($v) {
return preg_match('/.*\$.*\$.*\$.*\$.*\$.*/', $v);
});
$this->_appendOrNot('postalAddress', $values, $append);
return $this;
}
/**
* Attribut Ldap "telephoneNumber"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setTelephoneNumber($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatTel'], array_filter($data));
$this->_appendOrNot('telephoneNumber', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "supannAutreTelephone"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannAutreTelephone($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatTel'], array_filter($data));
$this->_appendOrNot('supannAutreTelephone', $values, $append);
return $this;
}
/**
* Attribut Ldap "mobile"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setMobile($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatTel'], array_filter($data));
$this->_appendOrNot('mobile', $values, $append);
return $this;
}
/**
* Attribut Ldap "preferredLanguage"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setPreferredLanguage($data = null, $append = false)
{
$data = (array)$data;
$values = array_map('strtolower', array_filter($data));
$this->_appendOrNot('preferredLanguage', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "leoCode"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setLeoCode($data = null, $append = false)
{
$data = (array)$data;
$values = array_filter($data, function($v) {
return preg_match('/^\d+$/', $v);
});
$this->_appendOrNot('leoCode', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "supannALiasLogin"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannAliasLogin($data = null, $append = false)
{
$data = (array)$data;
$values = array_map('strtolower', $data);
$values = array_filter($values, function($v) {
return preg_match('/^[0-9a-z\-]+$/', $v);
});
$this->_appendOrNot('supannAliasLogin', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "supannEmpId"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannEmpId($data = null, $append = false)
{
if (is_array($data)) {
$values = array_filter($data);
$data = $values[key($values)];
}
$this->_appendOrNot('supannEmpId', $data, $append);
}
/**
* Attribut Ldap "supannEtuId"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannEtuId($data = null, $append = false)
{
if (is_array($data)) {
$values = array_filter($data);
$data = $values[key($values)];
}
$this->_appendOrNot('supannEtuId', $data, $append);
}
/**
* Attribut Ldap "supannCodeINE"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannCodeINE($data = null, $append = false)
{
$data = (array)$data;
$values = array_map('strtoupper', $data);
$values = array_filter($values, function($v) {
return preg_match('/^[0-9]{10}[A-Z]{1}$/', $v);
});
$this->_appendOrNot('supannCodeINE', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "supannCivilite"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannCivilite($data = null, $append = false)
{
if (is_array($data)) {
$values = array_filter($data);
$data = $values[key($values)];
}
$value = (in_array($data, ['Mme', 'M.'])) ? $data : null;
$this->_appendOrNot('supannCivilite', $value, $append);
}
/**
* Attribut Ldap "supannListeRouge"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannListeRouge($data = null, $append = false)
{
$data = (array)$data;
$values = array_map([$this, 'formatBoolean'], array_filter($data));
$this->_appendOrNot('supannListeRouge', $values[key($values)], $append);
return $this;
}
/**
* Attribut Ldap "supannMailPerso"
*
* @param array|string|null $data
* @param bool $append
* @return self
* @throws LdapException
*/
public function setSupannMailPerso($data = null, $append = false)
{
$data = (array)$data;
$values = array_filter(filter_var_array($data, FILTER_VALIDATE_EMAIL));
$this->_appendOrNot('supannMailPerso', $values, $append);
return $this;
}
/**
* Retourne les structures auxquelles appartiennent la personne
*
* @return Structure[]
*/
public function getEduPersonOrgUnit()
{
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$dn = $this->eduPersonOrgUnitDN;
if (empty($dn)) return null;
return $structure->getAllBy($dn, 'dn');
}
/**
* Retourne la structure principale à laquelle appartient la personne
*
* @return Structure
*/
public function getEduPersonPrimaryOrgUnit()
{
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$dn = $this->eduPersonPrimaryOrgUnitDN;
if (empty($dn)) return null;
return $structure->getBy($dn, 'dn');
}
/**
* Retourne la structure d'affectation de la personne
*
* @todo à terminer
* @return Structure[]
* @throws \Exception
*/
public function getSupannEntiteAffectation()
{
throw new \Exception('Méthode pas finie');
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$codes = $this->getNode()->getAttribute('supannEntiteAffectation');
var_dump($codes);
return $structure->getBy($dn, 'dn');
}
/**
* Retourne la structure d'affectation de la personne
*
* @todo à terminer
* @return Structure
* @throws \Exception
*/
public function getSupannEntiteAffectationPrincipale()
{
throw new \Exception('Méthode pas finie');
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$codes = [];
$affectations = $this->getNode()->getAttribute('supannAffectation');
list($code, $description) = explode(';', $this->supannAffectation);
$code = $this->supannAffectation;
if (empty($dn)) return null;
return $structure->getBy($dn, 'dn');
}
/**
* Formate un nom ou un prénom
*
* @param string $name
* @return mixed|null|string|string[]
*/
protected function formatName($name)
{
$name = preg_replace('/[[:blank:]]+/', ' ', trim($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 $num;
}
$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 ($value) {
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;
}
}
}
\ No newline at end of file
......@@ -5,6 +5,8 @@ namespace UnicaenLdap\Entity;
use UnicaenLdap\Node;
use UnicaenLdap\Exception;
use UnicaenLdap\Service\AbstractService;
use Zend\Ldap\Dn;
use Zend\Ldap\Exception\LdapException;
/**
* Classe mère des entrées de l'annuaire LDAP.
......@@ -35,29 +37,57 @@ 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 labels utilisés dans les différents attributs
*
* @var array
*/
// protected $labels = [
// 'AUTRE' => 'AUTRE', // Texte libre
// 'BAP' => 'BAP', // Branche d'Activité Professionnelle REFERENS
// 'CNRS' => 'CNRS', // LABINTEL (CNRS)
// 'CNU' => 'CNU', // Section CNU
// 'INRA' => 'INRA', // INRA
// 'INRIA' => 'INRIA', // INRIA
// 'INSERM' => 'INSERM', // INSERM
// 'NCORPS' => 'NCORPS', // BCN: code issu de la colonne “CORPS” de la table “N_CORPS” du domaine “Gestion du personnel”
// 'REFERENS' => 'REFERENS', // Emploi-type REFERENS
// 'SAML' => 'SAML', // Fédération d'identités
// 'SILLAND' => 'SILLAND', // Fonction SILLAND
// 'SIRET' => 'SIRET', // SIRET (INSEE)
// 'SISE' => 'SISE', // BCN: valeur “DIPLOME SISE” de la table N_DIPLOME_SISE
// 'SUPANN' => 'SUPANN', // Label nomenclatures SupAnn
// 'TVA' => 'TVA', // Union Européenne
// 'UAI' => 'UAI', // Unité Administrative Immatriculée (UAI)
// 'UAI_ETABLISSEMENT' => 'UAI:%s',
//
// ];
/**
* Liste des attributs contenant des dates
*