- A lot of small bugfixes done on this version

This commit is contained in:
Dave Mc Nicoll 2022-04-13 03:30:50 +00:00
parent b681f245c0
commit 4d20cd50cc
10 changed files with 189 additions and 33 deletions

View File

@ -2,7 +2,7 @@
namespace Ulmus\Ldap\Adapter; namespace Ulmus\Ldap\Adapter;
use Ulmus\{Common\PdoObject, Exception\AdapterConfigurationException, Ldap\Entity\User}; use Ulmus\{Common\PdoObject, Exception\AdapterConfigurationException, Ldap\Entity\User, Ulmus};
use Ulmus\Ldap\Common\LdapObject; use Ulmus\Ldap\Common\LdapObject;
@ -37,14 +37,14 @@ class Ldap implements \Ulmus\Adapter\AdapterInterface {
public LdapObject $ldapObject; public LdapObject $ldapObject;
public function __construct( public function __construct(
? string $host = null, ? string/*|array*/ $host = null,
? string $baseDn = null, ? string $baseDn = null,
? string $username = null, ? string $username = null,
? string $password = null, ? string $password = null,
? string $accountSuffix = null ? string $accountSuffix = null
) { ) {
if ($host) { if ($host) {
$this->hosts = [ $host ]; $this->hosts = is_array($host) ? $host : [ $host ];
} }
if ($baseDn) { if ($baseDn) {
@ -146,6 +146,17 @@ class Ldap implements \Ulmus\Adapter\AdapterInterface {
ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7); ldap_set_option(NULL, LDAP_OPT_DEBUG_LEVEL, 7);
} }
} }
public function writableValue(/* mixed */ $value) /*: mixed*/
{
switch (true) {
case is_object($value):
return Ulmus::convertObject($value);
}
return $value;
}
public function escapeIdentifier(string $segment, int $type, string $ignore = "") : string public function escapeIdentifier(string $segment, int $type, string $ignore = "") : string
{ {

View File

@ -16,6 +16,8 @@ class LdapObject {
public int $bufferedRows = 0; public int $bufferedRows = 0;
public int $lastInsertId = 0;
public string $dn; public string $dn;
public bool $binded = false; public bool $binded = false;
@ -135,10 +137,44 @@ class LdapObject {
return $this->rowCount; return $this->rowCount;
} }
/**
* @deprecated
*/
public function runQuery(array $filter, array $dataset) public function runQuery(array $filter, array $dataset)
{
throw new \Exception("Method runQuery() cannot be run on this connector");
}
public function runInsertQuery(array $filter, array $dataset)
{ {
static::$dump && call_user_func_array(static::$dump, [ $filter, $dataset ]); static::$dump && call_user_func_array(static::$dump, [ $filter, $dataset ]);
$dn = $filter['dn'] ?? $this->dn;
$combine = array_combine($filter['fields'], $dataset);
if ( $combine['cn'] ?? false ) {
$dn = "cn={$combine['cn']}," . $dn;
}
try {
if (false === ($queryResult = ldap_add($this->connection, $dn, $combine))) {
$this->throwLdapException();
}
}
catch(\Throwable $e) {
$this->throwLdapException();
}
$this->rowCount = 1;
return $this;
}
public function runUpdateQuery(array $filter, array $dataset)
{
static::$dump && call_user_func_array(static::$dump, [ $filter, $dataset ]);
if ( false === ( $queryResult = ldap_mod_replace($this->connection, $filter['dn'], $dataset) ) ) { if ( false === ( $queryResult = ldap_mod_replace($this->connection, $filter['dn'], $dataset) ) ) {
$this->throwLdapException(); $this->throwLdapException();
} }
@ -148,21 +184,11 @@ class LdapObject {
return $this; return $this;
} }
public function runInsertQuery(array $filter, array $dataset) public function runDeleteQuery(array $filter, array $control)
{
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 ]); static::$dump && call_user_func_array(static::$dump, [ $filter, $dataset ]);
if ( false === ( $queryResult = ldap_mod_del($this->connection, $filter['dn'], $dataset) ) ) { if ( false === ( $queryResult = ldap_delete($this->connection, $filter['dn']) ) ) {
$this->throwLdapException(); $this->throwLdapException();
} }

View File

@ -46,6 +46,11 @@ class User
*/ */
public string $name; public string $name;
/**
* @Field
*/
public string $canonicalName;
/** /**
* @Field * @Field
*/ */
@ -82,14 +87,14 @@ class User
public string $title; public string $title;
/** /**
* @Field('readonly' => true) * @Virtual
*/ */
public ? array $memberOf; public ? array $memberOf;
/** /**
* @Field * @Field
*/ */
public string $userAccountControl; public int $userAccountControl;
/** /**
* @Field * @Field
@ -191,6 +196,11 @@ class User
*/ */
public string $sid; public string $sid;
/**
* @Field
*/
public string $targetAddress;
/** /**
* @Field('name' => "lastLogon", 'readonly' => true) * @Field('name' => "lastLogon", 'readonly' => true)
*/ */

View File

@ -10,7 +10,7 @@ trait EntityTrait {
use \Ulmus\EntityTrait; use \Ulmus\EntityTrait;
/** /**
* @Id * @Id('readonly' => true)
*/ */
public string $dn; public string $dn;
@ -19,6 +19,11 @@ trait EntityTrait {
*/ */
public string $cn; public string $cn;
/**
* @Field
*/
public array $objectClass;
public static function resolveEntity() : EntityResolver public static function resolveEntity() : EntityResolver
{ {
return Ulmus::resolveEntity(static::class); return Ulmus::resolveEntity(static::class);

17
src/Query/Delete.php Normal file
View File

@ -0,0 +1,17 @@
<?php
namespace Ulmus\Ldap\Query;
class Delete extends \Ulmus\Query\Fragment {
public int $order = -100;
public string $dn;
public function render() : array
{
return [
'dn' => $this->dn,
];
}
}

24
src/Query/Insert.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Ulmus\Ldap\Query;
class Insert extends \Ulmus\Query\Fragment {
public int $order = -100;
public array $fields = [];
public function set($dataset) : self
{
$this->fields = $dataset;
return $this;
}
public function render() : array
{
return [
'fields' => $this->fields
];
}
}

View File

@ -4,10 +4,6 @@ namespace Ulmus\Ldap\Query;
class Select extends \Ulmus\Query\Fragment { class Select extends \Ulmus\Query\Fragment {
public bool $distinct = false;
public bool $union = false;
public ? int $top = null; public ? int $top = null;
protected array $fields = []; protected array $fields = [];

View File

@ -1,9 +1,7 @@
<?php <?php
namespace Ulmus\Ldap\Query; namespace Ulmus\Ldap\Query;
class Set extends \Ulmus\Query\Set class Set extends \Ulmus\Query\Set
{ {
public function render() /* : mixed */ public function render() /* : mixed */

View File

@ -43,6 +43,25 @@ class QueryBuilder implements Ulmus\Query\QueryBuilderInterface
return $this; return $this;
} }
public function insert(array $dataset) : self
{
if ( null === $this->getFragment(Query\Insert::class) ) {
$insert = new Query\Insert();
$this->push($insert);
}
$insert->set($dataset);
return $this;
}
public function values(array $dataset) : self
{
$this->addValues(array_values($dataset));
return $this;
}
public function set(array $dataset, ? array $escapedFields = null) : self public function set(array $dataset, ? array $escapedFields = null) : self
{ {
if ( null === ( $set = $this->getFragment(Query\Set::class) ) ) { if ( null === ( $set = $this->getFragment(Query\Set::class) ) ) {
@ -72,12 +91,15 @@ class QueryBuilder implements Ulmus\Query\QueryBuilderInterface
return $this; return $this;
} }
public function delete() : self public function delete(string $dn) : self
{ {
if ( ! $this->getFragment(Ulmus\Query\Delete::class) ) { if ( null === ( $delete = $this->getFragment(Query\Delete::class) ) ) {
$this->push(new Ulmus\Query\Delete()); $delete = new Query\Delete($this);
$this->push($delete);
} }
$delete->dn = $dn;
return $this; return $this;
} }

View File

@ -38,13 +38,26 @@ class Repository extends \Ulmus\Repository
return $this; return $this;
} }
protected function insertSqlQuery(array $dataset, bool $replace = false) : self
{
if ( null === $insert = $this->queryBuilder->getFragment(Query\Insert::class) ) {
$fields = array_map([ $this, 'escapeField' ] , array_keys($dataset));
$this->insert($fields, "", "", null);
}
$this->values($dataset);
return $this;
}
protected function updateSqlQuery(array $dataset) : self protected function updateSqlQuery(array $dataset) : self
{ {
$condition = array_pop($this->queryBuilder->where->conditionList); $condition = array_pop($this->queryBuilder->where->conditionList);
if ($condition[0] === 'dn') { if ($condition[0] === 'dn') {
if ( null === $this->queryBuilder->getFragment(Query\Update::class) ) { if ( null === $this->queryBuilder->getFragment(Query\Update::class) ) {
$this->update($condition[1], "", ""); $this->update($condition[1], "", null);
} }
} }
else { else {
@ -56,6 +69,33 @@ class Repository extends \Ulmus\Repository
return $this; return $this;
} }
protected function deleteSqlQuery() : self
{
/*$condition = array_pop($this->queryBuilder->where->conditionList);
if ($condition[0] === 'dn') {
if ( null === $this->queryBuilder->getFragment(Query\Delete::class) ) {
$this->delete($condition[1], "", null);
}
}
else {
array_push($this->queryBuilder->where->conditionList, $condition);
}
*/
return $this;
}
public function destroy(object $entity) : bool
{
if ( ! $this->matchEntity($entity) ) {
throw new \Exception("Your entity class `" . get_class($entity) . "` cannot match entity type of repository `{$this->entityClass}`");
}
$this->delete($entity->dn)->deleteAll()->rowCount();
return false;
}
public function loadAllFromOU(string $ou) : EntityCollection public function loadAllFromOU(string $ou) : EntityCollection
{ {
$dn = $this->adapter->connector()->dn; $dn = $this->adapter->connector()->dn;
@ -69,18 +109,25 @@ class Repository extends \Ulmus\Repository
return $collection; return $collection;
} }
public function update(string $dn, string $alias, ? string $schema) : self public function insert(array $dataset, string $table, string $alias, ? string $schema, bool $replace = false) : self
{ {
$this->queryBuilder->update($dn, "", ""); $this->queryBuilder->insert($dataset);
return $this; return $this;
} }
public function runDeleteQuery() /* : mixed */ public function update(string $dn, string $alias, ? string $schema) : self
{ {
$this->finalizeQuery(); $this->queryBuilder->update($dn);
return Ulmus::runQuery($this->queryBuilder, $this->adapter); return $this;
}
public function delete(...$args) : self
{
$this->queryBuilder->delete($args[0]);
return $this;
} }
public function escapeValue($identifier) : string public function escapeValue($identifier) : string