2020-09-13 13:41:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Ldap;
|
|
|
|
|
2023-01-28 12:07:39 +00:00
|
|
|
use Carbon\Carbon;
|
2020-09-23 12:14:38 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2020-09-21 12:20:59 +00:00
|
|
|
use Illuminate\Support\Collection;
|
2023-01-28 12:07:39 +00:00
|
|
|
use Illuminate\Support\Facades\Config;
|
|
|
|
use LdapRecord\LdapRecordException;
|
2020-09-13 13:41:26 +00:00
|
|
|
use LdapRecord\Models\Model;
|
2023-01-27 08:59:31 +00:00
|
|
|
use LdapRecord\Query\ObjectNotFoundException;
|
2020-09-13 13:41:26 +00:00
|
|
|
|
2021-12-08 12:26:12 +00:00
|
|
|
use App\Classes\LDAP\Attribute\Factory;
|
|
|
|
|
2020-09-13 13:41:26 +00:00
|
|
|
class Entry extends Model
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The object classes of the LDAP model.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
public static $objectClasses = [];
|
2020-09-18 14:08:00 +00:00
|
|
|
|
2023-01-27 08:59:31 +00:00
|
|
|
/* OVERRIDES */
|
|
|
|
|
|
|
|
public function getAttributes(): array
|
|
|
|
{
|
|
|
|
$result = collect();
|
|
|
|
foreach (parent::getAttributes() as $attribute => $value) {
|
|
|
|
$result->put($attribute,Factory::create($attribute,$value));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result->toArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* STATIC METHODS */
|
|
|
|
|
2020-09-21 12:20:59 +00:00
|
|
|
/**
|
|
|
|
* Gets the root DN of the specified LDAPServer, or throws an exception if it
|
|
|
|
* can't find it.
|
|
|
|
*
|
|
|
|
* @param null $connection
|
|
|
|
* @return Collection
|
2023-01-27 08:59:31 +00:00
|
|
|
* @throws ObjectNotFoundException
|
2020-09-21 12:20:59 +00:00
|
|
|
* @testedin GetBaseDNTest::testBaseDNExists();
|
|
|
|
*/
|
2023-01-28 12:07:39 +00:00
|
|
|
public static function baseDNs($connection = NULL): ?Collection
|
2020-09-21 12:20:59 +00:00
|
|
|
{
|
2023-01-28 12:07:39 +00:00
|
|
|
$cachetime = Carbon::now()->addSeconds(Config::get('ldap.cache.time'));
|
2020-09-21 12:20:59 +00:00
|
|
|
|
2023-01-28 12:07:39 +00:00
|
|
|
try {
|
|
|
|
$base = static::on($connection ?? (new static)->getConnectionName())
|
|
|
|
->cache($cachetime)
|
|
|
|
->in(NULL)
|
|
|
|
->read()
|
|
|
|
->select(['namingcontexts'])
|
|
|
|
->whereHas('objectclass')
|
|
|
|
->firstOrFail();
|
|
|
|
|
|
|
|
// If we cannot get to our LDAP server we'll head straight to the error page
|
|
|
|
} catch (LdapRecordException $e) {
|
|
|
|
abort(597,$e->getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @note While we are caching our baseDNs, it seems if we have more than 1,
|
|
|
|
* our caching doesnt generate a hit on a subsequent call to this function (before the cache expires).
|
|
|
|
* IE: If we have 5 baseDNs, it takes 5 calls to this function to case them all.
|
|
|
|
* @todo Possibly a bug wtih ldaprecord, so need to investigate
|
|
|
|
*/
|
2020-09-23 12:14:38 +00:00
|
|
|
$result = collect();
|
|
|
|
foreach ($base->namingcontexts as $dn) {
|
2023-01-28 12:07:39 +00:00
|
|
|
$result->push((new self)->cache($cachetime)->findOrFail($dn));
|
2020-09-23 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2023-01-27 08:59:31 +00:00
|
|
|
/* ATTRIBUTES */
|
2021-12-08 12:26:12 +00:00
|
|
|
|
2023-01-27 08:59:31 +00:00
|
|
|
/**
|
|
|
|
* Return a key to use for sorting
|
|
|
|
*
|
|
|
|
* @todo This should be the DN in reverse order
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getSortKeyAttribute(): string
|
|
|
|
{
|
|
|
|
return $this->getDn();
|
2021-12-08 12:26:12 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 08:59:31 +00:00
|
|
|
/* METHODS */
|
|
|
|
|
2020-09-23 12:14:38 +00:00
|
|
|
/**
|
|
|
|
* Return an icon for a DN based on objectClass
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function icon(): string
|
|
|
|
{
|
|
|
|
$objectclasses = array_map('strtolower',$this->objectclass);
|
|
|
|
|
|
|
|
// Return icon based upon objectClass value
|
|
|
|
if (in_array('person',$objectclasses) ||
|
|
|
|
in_array('organizationalperson',$objectclasses) ||
|
|
|
|
in_array('inetorgperson',$objectclasses) ||
|
|
|
|
in_array('account',$objectclasses) ||
|
|
|
|
in_array('posixaccount',$objectclasses))
|
|
|
|
|
|
|
|
return 'fas fa-user';
|
|
|
|
|
|
|
|
elseif (in_array('organization',$objectclasses))
|
|
|
|
return 'fas fa-university';
|
|
|
|
|
|
|
|
elseif (in_array('organizationalunit',$objectclasses))
|
|
|
|
return 'fas fa-object-group';
|
|
|
|
|
|
|
|
elseif (in_array('posixgroup',$objectclasses) ||
|
|
|
|
in_array('groupofnames',$objectclasses) ||
|
|
|
|
in_array('groupofuniquenames',$objectclasses) ||
|
|
|
|
in_array('group',$objectclasses))
|
|
|
|
|
|
|
|
return 'fas fa-users';
|
|
|
|
|
|
|
|
elseif (in_array('dcobject',$objectclasses) ||
|
|
|
|
in_array('domainrelatedobject',$objectclasses) ||
|
|
|
|
in_array('domain',$objectclasses) ||
|
|
|
|
in_array('builtindomain',$objectclasses))
|
|
|
|
|
|
|
|
return 'fas fa-network-wired';
|
|
|
|
|
|
|
|
elseif (in_array('alias',$objectclasses))
|
|
|
|
return 'fas fa-theater-masks';
|
|
|
|
|
|
|
|
elseif (in_array('country',$objectclasses))
|
|
|
|
return sprintf('flag %s',strtolower(Arr::get($this->c,0)));
|
|
|
|
|
|
|
|
elseif (in_array('device',$objectclasses))
|
|
|
|
return 'fas fa-mobile-alt';
|
|
|
|
|
|
|
|
elseif (in_array('document',$objectclasses))
|
|
|
|
return 'fas fa-file-alt';
|
|
|
|
|
|
|
|
elseif (in_array('iphost',$objectclasses))
|
|
|
|
return 'fas fa-wifi';
|
|
|
|
|
|
|
|
elseif (in_array('room',$objectclasses))
|
|
|
|
return 'fas fa-door-open';
|
|
|
|
|
|
|
|
elseif (in_array('server',$objectclasses))
|
|
|
|
return 'fas fa-server';
|
|
|
|
|
2021-03-02 11:19:00 +00:00
|
|
|
elseif (in_array('openldaprootdse',$objectclasses))
|
|
|
|
return 'fas fa-info';
|
|
|
|
|
2020-09-23 12:14:38 +00:00
|
|
|
// Default
|
|
|
|
return 'fa-fw fas fa-cog';
|
2020-09-21 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Obtain the rootDSE for the server, that gives us server information
|
|
|
|
*
|
|
|
|
* @param null $connection
|
|
|
|
* @return Entry|null
|
2023-01-27 08:59:31 +00:00
|
|
|
* @throws ObjectNotFoundException
|
2020-09-21 12:20:59 +00:00
|
|
|
* @testedin TranslateOidTest::testRootDSE();
|
|
|
|
*/
|
|
|
|
public function rootDSE($connection = NULL): ?Entry
|
2020-09-18 14:08:00 +00:00
|
|
|
{
|
|
|
|
return static::on($connection ?? (new static)->getConnectionName())
|
2020-09-21 12:20:59 +00:00
|
|
|
->in(NULL)
|
2020-09-18 14:08:00 +00:00
|
|
|
->read()
|
|
|
|
->select(['+'])
|
|
|
|
->whereHas('objectclass')
|
|
|
|
->firstOrFail();
|
|
|
|
}
|
2020-09-13 13:41:26 +00:00
|
|
|
}
|