- First commit, User loading and Authentication are working
This commit is contained in:
commit
8d1a310921
15 files changed
+1274
No files matched your search
@@ -0,0 +1,174 @@
|
||||
<?php
|
||||
|
||||
namespace Ulmus\Ldap\Common;
|
||||
|
||||
use function ldap_set_option, ldap_start_tls, ldap_bind, ldap_unbind, ldap_connect, ldap_close, ldap_get_entries, ldap_count_entries;
|
||||
|
||||
class LdapObject {
|
||||
|
||||
public static ? string $dump = null;
|
||||
|
||||
public $connection;
|
||||
|
||||
public $search;
|
||||
|
||||
public int $rowCount = 0;
|
||||
|
||||
public int $bufferedRows = 0;
|
||||
|
||||
public string $dn;
|
||||
|
||||
public bool $binded = false;
|
||||
|
||||
public function __destruct()
|
||||
{
|
||||
isset($this->connection) and ldap_close($this->connection);
|
||||
}
|
||||
|
||||
public function connect(string $host, string $baseDn) : bool
|
||||
{
|
||||
$this->connection = ldap_connect($host);
|
||||
|
||||
$this->dn = $baseDn;
|
||||
|
||||
return $this->connection !== false;
|
||||
}
|
||||
|
||||
public function setOptions(array $options, $useConnectionRessource = true) : void
|
||||
{
|
||||
foreach($options as $field => $value) {
|
||||
ldap_set_option($useConnectionRessource ? $this->connection : null, $field, $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function startTLS() : void
|
||||
{
|
||||
ldap_start_tls($this->connection);
|
||||
}
|
||||
|
||||
public function bind(? string $dn, ? string $password) : bool
|
||||
{
|
||||
if ($this->binded) {
|
||||
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);
|
||||
}
|
||||
|
||||
public function unbind() : void
|
||||
{
|
||||
if ($this->binded) {
|
||||
$this->binded = ! ldap_unbind($this->connection);
|
||||
}
|
||||
}
|
||||
|
||||
public function select(array $filter, array $fields = [])
|
||||
{
|
||||
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
|
||||
|
||||
$this->search = ldap_search($this->connection, $this->dn, $filter['filters'], $filter['fields'], 0, $filter['limit'] ?? -1);
|
||||
|
||||
$this->rowCount = $this->bufferedRows = ldap_count_entries($this->connection, $this->search);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function fetch() /* : bool|array */
|
||||
{
|
||||
if (! $this->search ) {
|
||||
throw new \Exception('Impossible to fetch from LdapObject from which select() was not called first.');
|
||||
}
|
||||
|
||||
if ( ! $this->bufferedRows ) {
|
||||
return false;
|
||||
}
|
||||
if ( $this->rowCount === $this->bufferedRows ) {
|
||||
$result = ldap_first_entry($this->connection, $this->search);
|
||||
}
|
||||
else {
|
||||
$result = ldap_next_entry($this->connection, $this->search);
|
||||
}
|
||||
|
||||
if ($result) {
|
||||
$this->bufferedRows--;
|
||||
|
||||
$dataset = [];
|
||||
|
||||
if ( $attributes = ldap_get_attributes($this->connection, $result) ) {
|
||||
for ($i = 0; $i < $attributes['count']; $i++) {
|
||||
$key = $attributes[$i];
|
||||
|
||||
$dataset[strtolower($key)] = $attributes[$key][0];
|
||||
}
|
||||
}
|
||||
|
||||
return $dataset;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function rowCount() : int
|
||||
{
|
||||
return $this->rowCount;
|
||||
}
|
||||
|
||||
public function fetchAll()
|
||||
{
|
||||
return ldap_get_entries($this->connection, $result);
|
||||
}
|
||||
|
||||
public function closeCursor() : void {}
|
||||
|
||||
public function runQuery(string $sql, array $parameters = [])
|
||||
{
|
||||
static::$dump && call_user_func_array(static::$dump, [ $sql, $parameters ]);
|
||||
|
||||
try {
|
||||
if (false !== ( $statement = $this->prepare($sql) )) {
|
||||
return $this->execute($statement, $parameters, true);
|
||||
}
|
||||
}
|
||||
catch (\PDOException $e) {
|
||||
throw new \PdoException($e->getMessage() . " `$sql` with data:" . json_encode($parameters));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public function execute(PDOStatement $statement, array $parameters = [], bool $commit = true)
|
||||
{
|
||||
try {
|
||||
if ( ! $this->inTransaction() ) {
|
||||
$this->beginTransaction();
|
||||
}
|
||||
|
||||
if (empty($parameters) ? $statement->execute() : $statement->execute($parameters)) {
|
||||
$statement->lastInsertId = $this->lastInsertId();
|
||||
|
||||
if ( $commit ) {
|
||||
$this->commit();
|
||||
}
|
||||
|
||||
return $statement;
|
||||
}
|
||||
else {
|
||||
throw new \PDOException($statement->errorCode() . " - " . json_encode($statement->errorInfo()));
|
||||
}
|
||||
}
|
||||
catch (\PDOException $e) {
|
||||
$this->rollback();
|
||||
|
||||
throw $e;
|
||||
}
|
||||
catch (\Throwable $e) {
|
||||
if ( function_exists("debogueur") ) {
|
||||
debogueur($statement, $parameters, $commit);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user