- A lot of work done to stabilize implemented features ; still a lot of thing to be done!

This commit is contained in:
Dave Mc Nicoll 2021-11-24 16:41:03 +00:00
parent a0363ebb71
commit b681f245c0
8 changed files with 193 additions and 18 deletions

View File

@ -2,7 +2,7 @@
namespace Ulmus\Ldap\Adapter; namespace Ulmus\Ldap\Adapter;
use Ulmus\{ Common\PdoObject, Exception\AdapterConfigurationException }; use Ulmus\{Common\PdoObject, Exception\AdapterConfigurationException, Ldap\Entity\User};
use Ulmus\Ldap\Common\LdapObject; use Ulmus\Ldap\Common\LdapObject;
@ -64,13 +64,11 @@ class Ldap implements \Ulmus\Adapter\AdapterInterface {
} }
} }
public function authenticate(string $username, string $password) : bool public function authenticate(string $dn, string $password) : bool
{ {
$this->ldapObject = $this->getLdapObject(); $this->ldapObject = $this->getLdapObject();
$bind = $this->ldapObject->bind($username, $password); return $this->ldapObject->bind($dn, $password);;
return $bind;
} }
public function connect() : object public function connect() : object

View File

@ -52,7 +52,9 @@ class LdapObject {
throw new \Exception("LdapObject is already binded with a user. Use the unbind() method to release it."); throw new \Exception("LdapObject is already binded with a user. Use the unbind() method to release it.");
} }
return $this->binded = ldap_bind($this->connection, $dn, $password); $this->binded = ldap_bind($this->connection, $dn, $password);
return $this->binded;
} }
public function unbind() : void public function unbind() : void
@ -69,6 +71,11 @@ class LdapObject {
return $this->bind($dn, $password); return $this->bind($dn, $password);
} }
public function compare(? string $dn, string $attribute, /* mixed */ $value) /*: bool|int*/
{
return ldap_compare($this->connection, $dn, $attribute, $value);
}
public function select(array $filter, array $fields = []) public function select(array $filter, array $fields = [])
{ {
static::$dump && call_user_func_array(static::$dump, [ $filter, $fields ]); static::$dump && call_user_func_array(static::$dump, [ $filter, $fields ]);
@ -108,8 +115,7 @@ class LdapObject {
if ( $attributes = ldap_get_attributes($this->connection, $result) ) { if ( $attributes = ldap_get_attributes($this->connection, $result) ) {
for ($i = 0; $i < $attributes['count']; $i++) { for ($i = 0; $i < $attributes['count']; $i++) {
$key = $attributes[$i]; $key = $attributes[$i];
$dataset[$key] = $attributes[$key]['count'] > 1 ? array_diff_key($attributes[$key], [ 'count' => "" ]) : $attributes[$key][0];
$dataset[$key] = $attributes[$key][0];
} }
} }
@ -142,6 +148,29 @@ class LdapObject {
return $this; return $this;
} }
public function runInsertQuery(array $filter, array $dataset)
{
return $this->runQuery($filter, $dataset);
}
public function runUpdateQuery(array $filter, array $dataset)
{
return $this->runQuery($filter, $dataset);
}
public function runDeleteQuery(array $filter, array $dataset)
{
static::$dump && call_user_func_array(static::$dump, [ $filter, $dataset ]);
if ( false === ( $queryResult = ldap_mod_del($this->connection, $filter['dn'], $dataset) ) ) {
$this->throwLdapException();
}
$this->rowCount = 1;
return $this;
}
protected function throwLdapException() : void protected function throwLdapException() : void
{ {
throw new \Exception(sprintf('LDAP error #%s `%s`', ldap_errno($this->connection), ldap_error($this->connection))); throw new \Exception(sprintf('LDAP error #%s `%s`', ldap_errno($this->connection), ldap_error($this->connection)));

View File

@ -8,5 +8,4 @@ class Datetime extends \Ulmus\Entity\Field\Datetime {
{ {
return new static(substr($arguments[0], 0, 8)); return new static(substr($arguments[0], 0, 8));
} }
} }

View File

@ -0,0 +1,41 @@
<?php
namespace Ulmus\Ldap\Entity\Field;
class LdapDatetime extends Datetime {
public const DATETIME_EPOCH_DIFF = 11644477200;
public function load(...$arguments)
{
if (is_numeric($arguments[0])) {
return static::createDateTimeFromSystemTime($arguments[0]);
}
return parent::load(...$arguments);
}
# https://stackoverflow.com/a/64918390
public static function createDateTimeFromSystemTime($time, $basis = '1601-01-01 00:00:00', $resolution = 1.E-7, $timeZone = 'UTC') : \DateTimeInterface
{
return new static((new \DateTime($basis.' UTC'))
->modify(round($time * $resolution ).' Seconds')
->setTimeZone(new \DateTimeZone($timeZone))
->format('Y-m-d H:i:s')
);
}
public function save()
{
return (string) $this->getSystemTime();
}
public function getSystemTime($basis = '1601-01-01 00:00:00', $resolution = 1.E7, $timeZone = 'UTC') : int
{
$interval = (new \DateTime('1970-01-01 00:00:00 UTC'))->diff(new \DateTime($basis.' UTC'));
$diff = $interval->days * 86400 + $interval->h * 3600 + $interval->i * 60 + $interval->s;
return ( $this->setTimeZone(new \DateTimeZone($timeZone))->getTimestamp() + $diff ) * $resolution;
}
}

View File

@ -46,6 +46,11 @@ class OrganizationalUnit
*/ */
public string $telephoneNumber; public string $telephoneNumber;
/**
* @Field('name' => 'objectGUID')
*/
public string $guid;
public function __toString() : string public function __toString() : string
{ {
return $this->ou; return $this->ou;

View File

@ -2,7 +2,7 @@
namespace Ulmus\Ldap\Entity; namespace Ulmus\Ldap\Entity;
use Ulmus\Ldap\Entity\Field\{ Datetime }; use Ulmus\Ldap\Entity\Field\{ Datetime, LdapDatetime };
/** /**
* @ObjectClass('user') * @ObjectClass('user')
@ -81,6 +81,11 @@ class User
*/ */
public string $title; public string $title;
/**
* @Field('readonly' => true)
*/
public ? array $memberOf;
/** /**
* @Field * @Field
*/ */
@ -106,6 +111,61 @@ class User
*/ */
public string $extensionAttribute4; public string $extensionAttribute4;
/**
* @Field
*/
public string $extensionAttribute5;
/**
* @Field
*/
public string $extensionAttribute6;
/**
* @Field
*/
public string $extensionAttribute7;
/**
* @Field
*/
public string $extensionAttribute8;
/**
* @Field
*/
public string $extensionAttribute9;
/**
* @Field
*/
public string $extensionAttribute10;
/**
* @Field
*/
public string $extensionAttribute11;
/**
* @Field
*/
public string $extensionAttribute12;
/**
* @Field
*/
public string $extensionAttribute13;
/**
* @Field
*/
public string $extensionAttribute14;
/**
* @Field
*/
public string $extensionAttribute15;
/** /**
* @Field * @Field
*/ */
@ -124,20 +184,38 @@ class User
/** /**
* @Field * @Field
*/ */
public Datetime $lastLogonDate; public string $scriptPath;
/** /**
* #Field('name' => 'createTimeStamp') * @Field
*/ */
# public Datetime $createdAt; public string $sid;
/** /**
* #Field('name' => 'modifyTimeStamp') * @Field('name' => "lastLogon", 'readonly' => true)
*/ */
# public Datetime $updatedAt; public LdapDatetime $lastLogon;
/**
* @Field('name' => "whenChanged", 'readonly' => true)
* /
public LdapDatetime $updatedAt;
/**
* @Field('name' => "whenCreated", 'readonly' => true)
* /
public LdapDatetime $createdAt; */
public function __toString() : string public function __toString() : string
{ {
return implode(' ', array_filter([ $this->firstname ?? "", $this->lastname ?? "" ])) ?: $this->displayName; return implode(' ', array_filter([ $this->firstname ?? "", $this->lastname ?? "" ])) ?: ( $this->displayName ?? "" );
}
public function memberOfGroup() : array
{
$arr = array_map(fn($e) => explode('=', explode(',', $e)[0])[1], $this->memberOf);
usort($arr, 'strcasecmp');
return $arr;
} }
} }

View File

@ -14,6 +14,11 @@ trait EntityTrait {
*/ */
public string $dn; public string $dn;
/**
* @Field
*/
public string $cn;
public static function resolveEntity() : EntityResolver public static function resolveEntity() : EntityResolver
{ {
return Ulmus::resolveEntity(static::class); return Ulmus::resolveEntity(static::class);

View File

@ -56,6 +56,19 @@ class Repository extends \Ulmus\Repository
return $this; return $this;
} }
public function loadAllFromOU(string $ou) : EntityCollection
{
$dn = $this->adapter->connector()->dn;
$this->adapter->connector()->dn = $ou;
$collection = $this->collectionFromQuery();
$this->adapter->connector()->dn = $dn;
return $collection;
}
public function update(string $dn, string $alias, ? string $schema) : self public function update(string $dn, string $alias, ? string $schema) : self
{ {
$this->queryBuilder->update($dn, "", ""); $this->queryBuilder->update($dn, "", "");
@ -63,9 +76,16 @@ class Repository extends \Ulmus\Repository
return $this; return $this;
} }
public function escapeValue(string $identifier) : string public function runDeleteQuery() /* : mixed */
{ {
return $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\Ldap::IDENTIFIER_FILTER); $this->finalizeQuery();
return Ulmus::runQuery($this->queryBuilder, $this->adapter);
}
public function escapeValue($identifier) : string
{
return is_object($identifier) ? $identifier : $this->adapter->adapter()->escapeIdentifier($identifier, Adapter\Ldap::IDENTIFIER_FILTER);
} }
public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest, bool $count = true) : self public function filterServerRequest(SearchRequest\SearchRequestInterface $searchRequest, bool $count = true) : self