Commit e32e2493 authored by David Surville's avatar David Surville
Browse files

Merge zf-3.x

parents 2d285705 5ca3ac88
.idea
vendor
\ No newline at end of file
......@@ -3,17 +3,17 @@
"description": "Module Ldap des applications Unicaen",
"require": {
"php": "^5.6 || ^7.0",
"zendframework/zend-servicemanager": "~2.4 || ^2.7",
"zendframework/zend-modulemanager": "~2.4 || ^2.7",
"zendframework/zend-config": "~2.4 || ^2.7",
"zendframework/zend-serializer": "~2.4 || ^2.7",
"zendframework/zend-log": "~2.4 || ^2.7",
"zendframework/zend-i18n": "~2.4 || ^2.7",
"zendframework/zend-stdlib": "~2.4 || ^2.7",
"zendframework/zend-ldap": "~2.4 || ^2.7"
"zendframework/zend-config": "^3.2",
"zendframework/zend-i18n": "^2.9",
"zendframework/zend-ldap": "^2.10",
"zendframework/zend-log": "^2.10",
"zendframework/zend-modulemanager": "^2.8",
"zendframework/zend-serializer": "^2.9",
"zendframework/zend-servicemanager": "^3.4",
"zendframework/zend-stdlib": "^3.2"
},
"require-dev": {
"phpunit/PHPUnit": ">=3.7",
"phpunit/phpunit": ">=3.7",
"zendframework/zend-test": ">=2.2"
},
"autoload": {
......
<?php
use UnicaenLdap\LdapFactory;
namespace UnicaenLdap;
use UnicaenLdap\Entity\Generic;
use UnicaenLdap\Entity\Group;
use UnicaenLdap\Entity\People;
use UnicaenLdap\Entity\Structure;
use UnicaenLdap\Entity\System;
use UnicaenLdap\Options\ModuleOptionsFactory;
use UnicaenLdap\Service\Generic as LdapGenericService;
use UnicaenLdap\Service\GenericFactory as GenericServiceFactory;
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\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' => [
......@@ -14,14 +28,54 @@ return array(
],
'service_manager' => [
'factories' => [
'Ldap' => LdapFactory::class,
'LdapOptions' => ModuleOptionsFactory::class,
'LdapServiceGeneric' => GenericServiceFactory::class,
'LdapServiceGroup' => GroupServiceFactory::class,
'LdapServicePeople' => PeopleServiceFactory::class,
'LdapServiceStructure' => StructureServiceFactory::class,
'LdapServiceSystem' => SystemServiceFactory::class,
'Ldap' => LdapFactory::class,
'LdapOptions' => ModuleOptionsFactory::class,
'LdapServiceGeneric' => GenericServiceFactory::class,
'LdapServiceGroup' => GroupServiceFactory::class,
'LdapServicePeople' => PeopleServiceFactory::class,
'LdapServiceStructure' => StructureServiceFactory::class,
'LdapServiceSystem' => SystemServiceFactory::class,
],
'aliases' => [
'ldap' => 'Ldap',
'ldapOptions' => 'LdapOptions',
'ldapServiceGeneric' => 'LdapServiceGeneric',
'ldapServiceGroup' => 'LdapServiceGroup',
'ldapServicePeople' => 'LdapServicePeople',
'ldapServiceStructure' => 'LdapServiceStructure',
'ldapServiceSystem' => 'LdapServiceSystem',
LdapGenericService::class => 'LdapServiceGeneric',
LdapPeopleService::class => 'LdapServicePeople',
LdapGroupService ::class => 'LdapServiceGroup',
LdapStructureService::class => 'LdapServiceStructure',
LdapSystemService::class => 'LdapServiceSystem',
],
'lazy_services' => [
// Mapping services to their class names is required since the ServiceManager is not a declarative DIC.
'class_map' => [
'LdapServicePeople' => LdapPeopleService::class,
'LdapServiceGeneric' => LdapGenericService::class,
'LdapServiceGroup' => LdapGroupService::class,
'LdapServiceStructure' => LdapStructureService::class,
'LdapServiceSystem' => LdapSystemService::class,
],
],
'delegators' => [
'LdapServicePeople' => [
LazyServiceFactory::class,
],
'LdapServiceGeneric' => [
LazyServiceFactory::class,
],
'LdapServiceGroup' => [
LazyServiceFactory::class,
],
'LdapServiceStructure' => [
LazyServiceFactory::class,
],
'LdapServiceSystem' => [
LazyServiceFactory::class,
],
],
],
);
......@@ -68,7 +68,7 @@ class Group extends Entity
public function getPeople($orderBy = null)
{
/* @var $people \UnicaenLdap\Service\People */
$people = $this->getService()->getServiceLocator()->get('ldapServicePeople');
$people = $this->getService()->getLdapServiceForEntityClass(\UnicaenLdap\Service\People::class);
/** @var People[] $result */
$result = $people->getAllBy($this->get('member'), 'dn', $orderBy);
......
......@@ -103,7 +103,7 @@ class People extends BasePeople
*/
public function getGroups(DateTime $dateObservation = null, $orderBy = null)
{
$group = $this->getService()->getServiceLocator()->get('ldapServiceGroup');
$group = $this->getService()->getLdapServiceForEntityClass(\UnicaenLdap\Service\Group::class);
return $group->filterValids($group->getAllBy($this->get('memberOf'), 'dn', $orderBy), $dateObservation);
}
......@@ -138,6 +138,24 @@ class People extends BasePeople
return 0 === strpos($this->uid, 'i');
}
/**
* @var \UnicaenLdap\Service\Structure
*/
protected $ldapStructureService;
/**
* @return \UnicaenLdap\Service\Structure
*/
protected function getLdapStructureService()
{
if (null === $this->ldapStructureService) {
$this->ldapStructureService =
$this->getService()->getLdapServiceForEntityClass(\UnicaenLdap\Service\Structure::class);
}
return $this->ldapStructureService;
}
/**
* Retourne les structures auxquelles appartiennent la personne
*
......@@ -145,7 +163,7 @@ class People extends BasePeople
*/
public function getEduPersonOrgUnit()
{
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$structure = $this->getLdapStructureService();
$dn = $this->eduPersonOrgUnitDN;
if (empty($dn)) return null;
......@@ -159,7 +177,7 @@ class People extends BasePeople
*/
public function getEduPersonPrimaryOrgUnit()
{
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$structure = $this->getLdapStructureService();
$dn = $this->eduPersonPrimaryOrgUnitDN;
if (empty($dn)) return null;
......@@ -176,7 +194,7 @@ class People extends BasePeople
public function getEntiteAffectation()
{
throw new \Exception('Méthode pas finie');
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$structure = $this->getLdapStructureService();
$codes = $this->getNode()->getAttribute('supannEntiteAffectation');
var_dump($codes);
......@@ -193,7 +211,7 @@ class People extends BasePeople
public function getEntiteAffectationPrincipale()
{
throw new \Exception('Méthode pas finie');
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$structure = $this->getLdapStructureService();
$codes = [];
$affectations = $this->getNode()->getAttribute('supannAffectation');
......@@ -216,7 +234,7 @@ class People extends BasePeople
public function getAffectationDescription()
{
throw new \Exception('Méthode pas finie');
$structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
$structure = $this->getLdapStructureService();
list($code, $description) = explode(';', $this->supannAffectation);
$code = $this->supannAffectation;
......
......@@ -2,6 +2,7 @@
namespace UnicaenLdap;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
......@@ -12,16 +13,15 @@ use Zend\ServiceManager\ServiceLocatorInterface;
*/
class LdapFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return Ldap
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
return $this->__invoke($serviceLocator, '?');
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
/* @var $options \UnicaenLdap\Options\ModuleOptions */
$options = $serviceLocator->get('ldapOptions');
$options = $container->get('ldapOptions');
return new Ldap( $options->getLdap() );
}
......
......@@ -2,6 +2,7 @@
namespace UnicaenLdap\Options;
use Interop\Container\ContainerInterface;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
......@@ -12,15 +13,14 @@ use Zend\ServiceManager\ServiceLocatorInterface;
*/
class ModuleOptionsFactory implements FactoryInterface
{
/**
* Create service
*
* @param ServiceLocatorInterface $serviceLocator
* @return ModuleOptions
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$config = $serviceLocator->get('Configuration');
return $this->__invoke($serviceLocator, '?');
}
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$config = $container->get('Configuration');
$moduleConfig = isset($config['unicaen-ldap']) ? $config['unicaen-ldap'] : array();
return new ModuleOptions($moduleConfig);
......
......@@ -2,12 +2,12 @@
namespace UnicaenLdap\Service;
use UnicaenLdap\Ldap;
use UnicaenApp\Exception\RuntimeException;
use UnicaenLdap\Collection;
use UnicaenLdap\Entity\Entity;
use UnicaenLdap\Exception;
use UnicaenLdap\Collection;
use UnicaenLdap\Filter\Filter;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use UnicaenLdap\Ldap;
use Zend\Ldap\Filter\AbstractFilter;
use Zend\Ldap\Dn;
use Zend\Stdlib\ErrorHandler;
......@@ -19,8 +19,6 @@ use Zend\Stdlib\ErrorHandler;
*/
abstract class AbstractService
{
use ServiceLocatorAwareTrait;
/**
* Limite de recherche par défaut
*/
......@@ -45,7 +43,7 @@ abstract class AbstractService
/**
* Organizational Units
*
*
* @var array
*/
protected $ou = [];
......@@ -57,6 +55,38 @@ abstract class AbstractService
*/
protected $code_structure_prefixe = 'HS_';
/**
* @var array
*/
protected $ldapServicesByEntityClass = [
\UnicaenLdap\Entity\People::class => null,
\UnicaenLdap\Entity\Structure::class => null,
\UnicaenLdap\Entity\System::class => null,
\UnicaenLdap\Entity\Group::class => null,
\UnicaenLdap\Entity\Generic::class => null,
];
/**
* @param array
*/
public function setLdapServicesByEntityClass(array $ldapServicesByEntityClass)
{
$this->ldapServicesByEntityClass = $ldapServicesByEntityClass;
}
/**
* @param string $entityClass
* @return mixed
*/
public function getLdapServiceForEntityClass($entityClass)
{
if (!isset($this->ldapServicesByEntityClass[$entityClass])) {
throw new RuntimeException("Le service de recherche LDAP pour la classe d'entité $entityClass n'a pas été injecté.");
}
return $this->ldapServicesByEntityClass[$entityClass];
}
/**
* Compteur utilisé dans la recherche
*
......@@ -72,9 +102,6 @@ abstract class AbstractService
*/
public function getLdap()
{
if (empty($this->ldap)){
$this->ldap = $this->getServiceLocator()->get('ldap');
}
return $this->ldap;
}
......@@ -135,39 +162,39 @@ abstract class AbstractService
/**
* Retourne la liste de toutes les entités correspondantes
*
*
* @param string $orderBy Propriété de référence pour le tri
* @return Collection
*/
public function getList( $orderBy=null, $limit=self::DEFAULT_LIMIT, $offset=self::DEFAULT_OFFSET )
public function getList($orderBy = null, $limit = self::DEFAULT_LIMIT, $offset = self::DEFAULT_OFFSET)
{
list($key) = Entity::getNodeParams($this->type);
return $this->search( "($key=*)", $orderBy, $limit, $offset );
return $this->search("($key=*)", $orderBy, $limit, $offset);
}
/**
* Recherche une liste d'entités correspondantes
*
* @param string|AbstractFilter $filter Valeur de recherche
* @param string $orderBy Attribut de référence pour le tri
* @param integer $limit Nombre maximum d'occurences renvoyées (-1 = infini)
* @param integer $offset Renvoi les entités à partir de $offset uniquement
* @param string|AbstractFilter $filter Valeur de recherche
* @param string $orderBy Attribut de référence pour le tri
* @param integer $limit Nombre maximum d'occurences renvoyées (-1 = infini)
* @param integer $offset Renvoi les entités à partir de $offset uniquement
* @return Collection
* @throws Exception
*/
public function search( $filter, $orderBy=null, $limit=self::DEFAULT_LIMIT, $offset=self::DEFAULT_OFFSET )
public function search($filter, $orderBy = null, $limit = self::DEFAULT_LIMIT, $offset = self::DEFAULT_OFFSET)
{
list($key) = Entity::getNodeParams($this->type);
if ($limit < 0) $limit = 3999999999; // Limite maximum à 4 milliard...
if ($offset < 0) $offset = 0; // Moins de zéro = impossible
list( $resource, $search ) = $this->__searchBegin($filter, $this->ou, [$key,$orderBy]);
list($resource, $search) = $this->__searchBegin($filter, $this->ou, [$key, $orderBy]);
ErrorHandler::start(E_WARNING);
$this->count = ldap_count_entries($resource, $search);
ErrorHandler::stop();
if ($this->count > 0){
if ($this->count > 0) {
if ($orderBy !== null && is_string($orderBy)) {
ErrorHandler::start(E_WARNING);
......@@ -181,21 +208,21 @@ abstract class AbstractService
$result = array();
$i = 0;
ErrorHandler::start(E_WARNING);
for ($entry=ldap_first_entry($resource,$search); $entry; $entry=ldap_next_entry($resource,$entry)) {
list($value) = ldap_get_values_len($resource,$entry,$key);
if (null !== $value){
for ($entry = ldap_first_entry($resource, $search); $entry; $entry = ldap_next_entry($resource, $entry)) {
list($value) = ldap_get_values_len($resource, $entry, $key);
if (null !== $value) {
$result[] = $value;
$i++;
}
if ($i > $limit + $offset - 1) break; // Pas besoin d'aller plus loin...
}
ErrorHandler::stop();
}else{
} else {
$result = array();
}
$this->__searchEnd( $search );
$this->__searchEnd($search);
$this->count = count($result);
return new Collection( $this, array_slice( $result, $offset, $limit ) );
return new Collection($this, array_slice($result, $offset, $limit));
}
/**
......@@ -205,9 +232,9 @@ abstract class AbstractService
* @return integer
* @throws Exception
*/
public function searchCount( $filter )
public function searchCount($filter)
{
list( $resource, $search ) = $this->__searchBegin($filter, $this->ou);
list($resource, $search) = $this->__searchBegin($filter, $this->ou);
if ($search === false) {
throw new Exception('searching: ' . $filter);
......@@ -220,29 +247,29 @@ abstract class AbstractService
/**
* Recherche une liste d'entités correspondantes
*
* @param string|AbstractFilter $filter Valeur de recherche
* @param array $attributes Liste des attributs à retourner
* @param string $orderBy Champ de référence pour le tri
* @param integer $limit Nombre maximum d'occurences renvoyées (-1 = infini)
* @param integer $offset Renvoi les entités à partir de $offset uniquement
* @param string|AbstractFilter $filter Valeur de recherche
* @param array $attributes Liste des attributs à retourner
* @param string $orderBy Champ de référence pour le tri
* @param integer $limit Nombre maximum d'occurences renvoyées (-1 = infini)
* @param integer $offset Renvoi les entités à partir de $offset uniquement
* @return Collection
* @throws Exception
*/
public function searchAttributes( $filter, array $attributes, $orderBy=null, $limit=self::DEFAULT_LIMIT, $offset=self::DEFAULT_OFFSET )
public function searchAttributes($filter, array $attributes, $orderBy = null, $limit = self::DEFAULT_LIMIT, $offset = self::DEFAULT_OFFSET)
{
list($key) = Entity::getNodeParams($this->type);
if ($limit < 0) $limit = 3999999999; // Limite maximum à 4 milliard...
if ($offset < 0) $offset = 0; // Moins de zéro = impossible
$searchAttributes = $attributes;
if (! in_array($key, $searchAttributes)) $searchAttributes[] = $key;
if (null !== $orderBy && ! in_array($orderBy, $searchAttributes)) $searchAttributes[] = $orderBy;
list( $resource, $search ) = $this->__searchBegin($filter, $this->ou, $searchAttributes);
if (!in_array($key, $searchAttributes)) $searchAttributes[] = $key;
if (null !== $orderBy && !in_array($orderBy, $searchAttributes)) $searchAttributes[] = $orderBy;
list($resource, $search) = $this->__searchBegin($filter, $this->ou, $searchAttributes);
ErrorHandler::start(E_WARNING);
$this->count = ldap_count_entries($resource, $search);
ErrorHandler::stop();
if ($this->count > 0){
if ($this->count > 0) {
if ($orderBy !== null && is_string($orderBy)) {
ErrorHandler::start(E_WARNING);
$isSorted = ldap_sort($resource, $search, $orderBy);
......@@ -255,11 +282,11 @@ abstract class AbstractService
$result = array();
$i = 0;
ErrorHandler::start(E_WARNING);
for ($entry=ldap_first_entry($resource,$search); $entry; $entry=ldap_next_entry($resource,$entry)) {
list($id) = ldap_get_values_len($resource,$entry,$key);
for ($entry = ldap_first_entry($resource, $search); $entry; $entry = ldap_next_entry($resource, $entry)) {
list($id) = ldap_get_values_len($resource, $entry, $key);
$data = array();
foreach( $attributes as $attribute ){
$attrValue = ldap_get_values_len($resource,$entry,$attribute);
foreach ($attributes as $attribute) {
$attrValue = ldap_get_values_len($resource, $entry, $attribute);
if (1 == $attrValue['count']) $attrValue = $attrValue[0];
$data[$attribute] = $attrValue;
}
......@@ -268,22 +295,22 @@ abstract class AbstractService
if ($i > $limit + $offset - 1) break; // Pas besoin d'aller plus loin...
}
ErrorHandler::stop();
}else{
} else {
$result = array();
}
$this->__searchEnd( $search );
return array_slice( $result, $offset, $limit );
$this->__searchEnd($search);
return array_slice($result, $offset, $limit);
}
/**
*
* @param string|AbstractFilter $filter Filtre Ldap à appliquer
* @param string[] $ou Liste des organisations dans lesquelles rechercher
* @param string[] $attributes Liste des attributs à retourner
* @param string|AbstractFilter $filter Filtre Ldap à appliquer
* @param string[] $ou Liste des organisations dans lesquelles rechercher
* @param string[] $attributes Liste des attributs à retourner
* @return array
* @throws Exception
*/
private function __searchBegin( $filter, array $ou=null, array $attributes=null )
private function __searchBegin($filter, array $ou = null, array $attributes = null)
{
/* Initialisation $basedn et $filter */
if ($filter instanceof AbstractFilter) {
......@@ -293,15 +320,15 @@ abstract class AbstractService
if (is_string($ou)) $ou = array($ou);
elseif (null === $ou) $ou = $this->ou;
if (1 == count($ou)){
$basedn = "ou=".$ou[0].",".$this->getLdap()->getBaseDn();
}else{
if (1 == count($ou)) {
$basedn = "ou=" . $ou[0] . "," . $this->getLdap()->getBaseDn();
} else {
$basedn = $this->getLdap()->getBaseDn();
$ouFilter = '(&(|';
foreach( $ou as $ouItem ){
foreach ($ou as $ouItem) {
$ouFilter .= "(ou:dn:=$ouItem)";
}
$filter = $ouFilter.")$filter)";
$filter = $ouFilter . ")$filter)";
}
$resource = $this->getLdap()->getResource();
if (null === $attributes) $search = ldap_search($resource, $basedn, $filter);
......@@ -309,7 +336,7 @@ abstract class AbstractService
if ($search === false) {
throw new Exception('searching: ' . $filter);
}
return array( $resource, $search );
return array($resource, $search);
}
/**
......@@ -317,7 +344,7 @@ abstract class AbstractService
*
* @param resource $search
*/
private function __searchEnd( $search )
private function __searchEnd($search)
{
ldap_free_result($search);
}
......@@ -329,7 +356,7 @@ abstract class AbstractService
* @param resource $entry
* @return null|array
*/
private function __getEntryAttributes( $resource, $entry )
private function __getEntryAttributes($resource, $entry)
{
if (!is_resource($resource)) {
return null;
......@@ -341,7 +368,7 @@ abstract class AbstractService
$resource, $entry, $berIdentifier
);
$attributes = array();
$attributes = array();
while ($name) {
ErrorHandler::start(E_WARNING);
......@@ -371,14 +398,14 @@ abstract class AbstractService
/**
* Retourne une entité du type correspondant au service courant
*
*
*
* @param string $id
* @return Entity
*/
public function get( $id )
public function get($id)
{
list($key) = Entity::getNodeParams($this->type);
return $this->getBy( $id, $key );
return $this->getBy($id, $key);
}
/**
......@@ -390,42 +417,42 @@ abstract class AbstractService