2020-08-22 12:26:06 +00:00
|
|
|
<?php
|
|
|
|
|
2020-08-23 02:30:18 +00:00
|
|
|
namespace App\Classes\LDAP;
|
2020-08-22 12:26:06 +00:00
|
|
|
|
2020-09-13 13:41:26 +00:00
|
|
|
use App\Ldap\Entry;
|
2020-08-27 12:46:07 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2020-08-22 12:26:06 +00:00
|
|
|
|
|
|
|
class Server
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Gets the root DN of the specified LDAPServer, or NULL if it
|
|
|
|
* can't find it (ie, the server won't give it to us, or it isnt
|
|
|
|
* specified in the configuration file).
|
|
|
|
*
|
|
|
|
* @return array array|NULL The root DN(s) of the server on success (string) or NULL if it cannot be determine.
|
|
|
|
* @todo Sort the entries, so that they are in the correct DN order.
|
|
|
|
*/
|
2020-08-27 12:46:07 +00:00
|
|
|
public function getBaseDN(): ?Collection
|
2020-08-22 12:26:06 +00:00
|
|
|
{
|
2020-08-31 11:41:45 +00:00
|
|
|
//findBaseDn()?
|
2020-08-22 12:26:06 +00:00
|
|
|
// If the base is set in the configuration file, then just return that after validating it exists.
|
|
|
|
// @todo
|
|
|
|
if (false) {
|
|
|
|
|
|
|
|
// We need to work out the baseDN
|
|
|
|
} else {
|
|
|
|
$result = $this->getDNAttrValues('',['namingcontexts']);
|
|
|
|
|
2020-08-27 12:46:07 +00:00
|
|
|
return $result ? collect($result->namingcontexts) : NULL;
|
2020-08-22 12:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Search for a DN and return its attributes
|
|
|
|
*
|
|
|
|
* @param $dn
|
|
|
|
* @param array $attrs
|
|
|
|
* @param int $deref // @todo
|
|
|
|
* @return Entry|bool
|
|
|
|
*/
|
|
|
|
protected function getDNAttrValues(string $dn,array $attrs=['*','+'],int $deref=LDAP_DEREF_NEVER): ?Entry
|
|
|
|
{
|
|
|
|
try {
|
2020-09-13 13:41:26 +00:00
|
|
|
return ($x=(new Entry)
|
|
|
|
->query()
|
2020-08-22 12:26:06 +00:00
|
|
|
->select($attrs)
|
2020-09-13 13:41:26 +00:00
|
|
|
->find($dn)) ? $x : NULL;
|
2020-08-22 12:26:06 +00:00
|
|
|
|
|
|
|
// @todo Tidy up this exception
|
|
|
|
} catch (\Exception $e) {
|
2020-08-23 02:30:18 +00:00
|
|
|
dd(['e'=>$e]);
|
2020-08-22 12:26:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-31 11:41:45 +00:00
|
|
|
|
2020-09-13 11:30:04 +00:00
|
|
|
/**
|
|
|
|
* Fetch a DN from the server
|
|
|
|
*
|
|
|
|
* @param $dn
|
|
|
|
* @return |null
|
|
|
|
*/
|
2020-09-13 13:41:26 +00:00
|
|
|
public function fetch(string $dn,array $attrs=['*','+'])
|
2020-09-13 11:30:04 +00:00
|
|
|
{
|
|
|
|
try {
|
2020-09-13 13:41:26 +00:00
|
|
|
return ($x=(new Entry)
|
|
|
|
->query()
|
|
|
|
->select($attrs)
|
|
|
|
->find($dn)) ? $x : NULL;
|
2020-09-13 11:30:04 +00:00
|
|
|
|
|
|
|
// @todo Tidy up this exception
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
dd(['e'=>$e]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 11:41:45 +00:00
|
|
|
/**
|
|
|
|
* Query the server for a DN
|
|
|
|
*
|
|
|
|
* @param string $dn
|
|
|
|
* @return |null
|
|
|
|
*/
|
2020-09-14 11:08:55 +00:00
|
|
|
public function children(string $dn)
|
2020-08-31 11:41:45 +00:00
|
|
|
{
|
|
|
|
try {
|
2020-09-13 13:41:26 +00:00
|
|
|
return ($x=(new Entry)
|
|
|
|
->query()
|
2020-09-14 11:08:55 +00:00
|
|
|
->select(['dn','hassubordinates'])
|
2020-09-13 13:41:26 +00:00
|
|
|
->setDn($dn)
|
2020-08-31 11:41:45 +00:00
|
|
|
->listing()
|
|
|
|
->get()) ? $x : NULL;
|
|
|
|
|
2020-09-14 11:08:55 +00:00
|
|
|
// @todo Tidy up this exception
|
2020-08-31 11:41:45 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
dd(['e'=>$e]);
|
|
|
|
}
|
|
|
|
}
|
2020-08-23 02:30:18 +00:00
|
|
|
}
|