Improved caching of schema

This commit is contained in:
Deon George 2023-02-19 00:32:46 +11:00
parent 8ec1d2b1fe
commit 92e5afd614
2 changed files with 223 additions and 211 deletions

View File

@ -4,7 +4,6 @@ namespace App\Classes\LDAP\Schema;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use LdapRecord\Connection;
use App\Classes\LDAP\Server;
use App\Exceptions\InvalidUsage;
@ -19,7 +18,6 @@ use App\Ldap\Entry;
class ObjectClass extends Base {
// The server ID that this objectclass belongs to.
private Server $server;
private Connection $connection;
// Array of objectClass names from which this objectClass inherits
private Collection $sup_classes;
@ -51,7 +49,7 @@ class ObjectClass extends Base {
*
* eg: ( 2.5.6.0 NAME 'top' DESC 'top of the superclass chain' ABSTRACT MUST objectClass )
*/
public function __construct(string $line,Entry $entry,Server $server)
public function __construct(string $line,Server $server)
{
parent::__construct($line);
@ -60,7 +58,6 @@ class ObjectClass extends Base {
$strings = preg_split('/[\s,]+/',$line,-1,PREG_SPLIT_DELIM_CAPTURE);
// Init
$this->connection = $entry->getConnection();
$this->server = $server;
$this->may_attrs = collect();
$this->may_force = collect();

View File

@ -32,6 +32,19 @@ class Server
'matchingrules',
];
public function __get(string $key): mixed
{
switch ($key) {
case 'attributetypes': return $this->attributetypes;
case 'ldapsyntaxes': return $this->ldapsyntaxes;
case 'matchingrules': return $this->matchingrules;
case 'objectclasses': return $this->objectclasses;
default:
throw new Exception('Unknown key:'.$key);
}
}
/**
* Query the server for a DN and return its children and if those children have children.
*
@ -151,11 +164,12 @@ class Server
if (! in_array($item,self::schema_types))
throw new InvalidUsage('Invalid request to fetch schema: '.$item);
$result = Cache::remember('schema'.$item,config('ldap.cache.time'),function() use ($item) {
// First pass if we have already retrieved the schema item
switch ($item) {
case 'attributetypes':
if (isset($this->attributetypes))
return is_null($key) ? $this->attributetypes : $this->attributetypes->get($key);
return $this->attributetypes;
else
$this->attributetypes = collect();
@ -163,7 +177,7 @@ class Server
case 'ldapsyntaxes':
if (isset($this->ldapsyntaxes))
return is_null($key) ? $this->ldapsyntaxes : $this->ldapsyntaxes->get($key);
return $this->ldapsyntaxes;
else
$this->ldapsyntaxes = collect();
@ -171,7 +185,7 @@ class Server
case 'matchingrules':
if (isset($this->matchingrules))
return is_null($key) ? $this->matchingrules : $this->matchingrules->get($key);
return $this->matchingrules;
else
$this->matchingrules = collect();
@ -189,7 +203,7 @@ class Server
case 'objectclasses':
if (isset($this->objectclasses))
return is_null($key) ? $this->objectclasses : $this->objectclasses->get($key);
return $this->objectclasses;
else
$this->objectclasses = collect();
@ -296,28 +310,7 @@ class Server
$this->attributetypes[strtolower($attr_name->name)]->setForceMay();
}
return is_null($key) ? $this->attributetypes : $this->attributetypes->get($key);
case 'objectclasses':
Log::debug('Object Classes');
foreach ($schema->{$item} as $line) {
if (is_null($line) || ! strlen($line))
continue;
$o = new ObjectClass($line,$schema,$this);
$this->objectclasses->put($o->name_lc,$o);
}
// Now go through and reference the parent/child relationships
foreach ($this->objectclasses as $o)
foreach ($o->getSupClasses() as $parent) {
$parent = strtolower($parent);
if ($this->objectclasses->has($parent) !== FALSE)
$this->objectclasses[$parent]->addChildObjectClass($o->name);
}
return is_null($key) ? $this->objectclasses : $this->objectclasses->get($key);
return $this->attributetypes;
case 'ldapsyntaxes':
Log::debug('LDAP Syntaxes');
@ -330,7 +323,7 @@ class Server
$this->ldapsyntaxes->put(strtolower($o->oid),$o);
}
return is_null($key) ? $this->ldapsyntaxes : $this->ldapsyntaxes->get($key);
return $this->ldapsyntaxes;
case 'matchingrules':
Log::debug('Matching Rules');
@ -371,10 +364,32 @@ class Server
}
}
return is_null($key) ? $this->matchingrules : $this->matchingrules->get($key);
return $this->matchingrules;
case 'objectclasses':
Log::debug('Object Classes');
foreach ($schema->{$item} as $line) {
if (is_null($line) || ! strlen($line))
continue;
$o = new ObjectClass($line,$this);
$this->objectclasses->put($o->name_lc,$o);
}
return NULL;
// Now go through and reference the parent/child relationships
foreach ($this->objectclasses as $o)
foreach ($o->getSupClasses() as $parent) {
$parent = strtolower($parent);
if ($this->objectclasses->has($parent) !== FALSE)
$this->objectclasses[$parent]->addChildObjectClass($o->name);
}
return $this->objectclasses;
}
});
return is_null($key) ? $result : $result->get($key);
}
public function schemaSyntaxName(string $oid): ?LDAPSyntax