Import and Export work with attribute tags

This commit is contained in:
Deon George 2025-03-17 20:45:49 +11:00
parent ce66dcb2b5
commit b35b44b2b8
3 changed files with 39 additions and 20 deletions

View File

@ -178,9 +178,13 @@ class Attribute implements \Countable, \ArrayAccess, \Iterator
return $this->name; return $this->name;
} }
public function addValue(string $value): void public function addValue(string $tag,string $value): void
{ {
$this->values->push($value); $this->_values->put(
$tag,
$this->_values
->get($tag,collect())
->push($value));
} }
public function current(): mixed public function current(): mixed

View File

@ -59,8 +59,6 @@ class LDIF extends Import
$base64encoded = FALSE; $base64encoded = FALSE;
$attribute = NULL; $attribute = NULL;
$value = ''; $value = '';
// Else its a blank line
} }
continue; continue;
@ -69,7 +67,7 @@ class LDIF extends Import
$m = []; $m = [];
preg_match('/^([a-zA-Z0-9;-]+)(:+)\s+(.*)$/',$line,$m); preg_match('/^([a-zA-Z0-9;-]+)(:+)\s+(.*)$/',$line,$m);
switch ($x=Arr::get($m,1)) { switch (Arr::get($m,1)) {
case 'changetype': case 'changetype':
if ($m[2] !== ':') if ($m[2] !== ':')
throw new GeneralException(sprintf('ChangeType cannot be base64 encoded set at [%d]. (line %d)',$version,$c)); throw new GeneralException(sprintf('ChangeType cannot be base64 encoded set at [%d]. (line %d)',$version,$c));
@ -133,7 +131,6 @@ class LDIF extends Import
// Start of a new attribute // Start of a new attribute
$base64encoded = ($m[2] === '::'); $base64encoded = ($m[2] === '::');
// @todo Need to parse attributes with ';' options
$attribute = $m[1]; $attribute = $m[1];
$value = $m[3]; $value = $m[3];
@ -160,7 +157,7 @@ class LDIF extends Import
} }
public function readEntry() { public function readEntry() {
static $haveVersion = false; static $haveVersion = FALSE;
if ($lines = $this->nextLines()) { if ($lines = $this->nextLines()) {
@ -179,7 +176,7 @@ class LDIF extends Import
} else } else
$changetype = 'add'; $changetype = 'add';
$this->template = new Template($this->server_id,null,null,$changetype); $this->template = new Template($this->server_id,NULL,NULL,$changetype);
switch ($changetype) { switch ($changetype) {
case 'add': case 'add':
@ -201,7 +198,7 @@ class LDIF extends Import
return $this->error(sprintf('%s %s',_('DN does not exist'),$dn),$lines); return $this->error(sprintf('%s %s',_('DN does not exist'),$dn),$lines);
$this->template->setDN($dn); $this->template->setDN($dn);
$this->template->accept(false,true); $this->template->accept(FALSE,TRUE);
return $this->getModifyDetails($lines); return $this->getModifyDetails($lines);
@ -228,6 +225,6 @@ class LDIF extends Import
return $this->error(_('A valid dn line is required'),$lines); return $this->error(_('A valid dn line is required'),$lines);
} else } else
return false; return FALSE;
} }
} }

View File

@ -16,6 +16,9 @@ use App\Exceptions\InvalidUsage;
class Entry extends Model class Entry extends Model
{ {
private const TAG_CHARS = 'a-zA-Z0-9-';
private const TAG_CHARS_LANG = 'lang-['.self::TAG_CHARS.']';
// Our Attribute objects // Our Attribute objects
private Collection $objects; private Collection $objects;
/* @deprecated */ /* @deprecated */
@ -51,7 +54,11 @@ class Entry extends Model
public function getAttributes(): array public function getAttributes(): array
{ {
return $this->objects return $this->objects
->map(fn($item)=>$item->values) ->flatMap(fn($item)=>
($item->no_attr_tags)
? [strtolower($item->name)=>$item->values]
: $item->values
->flatMap(fn($v,$k)=>[strtolower($item->name.($k ? ';'.$k : ''))=>$v]))
->toArray(); ->toArray();
} }
@ -158,15 +165,26 @@ class Entry extends Model
if (! is_string($value)) if (! is_string($value))
throw new \Exception('value should be a string'); throw new \Exception('value should be a string');
$key = $this->normalizeAttributeKey($key); $key = $this->normalizeAttributeKey(strtolower($key));
if (! config('server')->schema('attributetypes')->has($key)) // If the attribute name has tags
throw new AttributeException(sprintf('Schema doesnt have attribute [%s]',$key)); $matches = [];
if (preg_match(sprintf('/^([%s]+);+([%s;]+)/',self::TAG_CHARS,self::TAG_CHARS),$key,$matches)) {
$attribute = $matches[1];
$tags = $matches[2];
$o = $this->objects->get($key) ?: Attribute\Factory::create($this->dn ?: '',$key,[]); } else {
$o->addValue($value); $attribute = $key;
$tags = '';
}
$this->objects->put($key,$o); if (! config('server')->schema('attributetypes')->has($attribute))
throw new AttributeException(sprintf('Schema doesnt have attribute [%s]',$attribute));
$o = $this->objects->get($attribute) ?: Attribute\Factory::create($this->dn ?: '',$attribute,[]);
$o->addValue($tags,$value);
$this->objects->put($attribute,$o);
} }
/** /**
@ -182,7 +200,7 @@ class Entry extends Model
foreach ($this->attributes as $attrtag => $values) { foreach ($this->attributes as $attrtag => $values) {
// If the attribute name has tags // If the attribute name has tags
$matches = []; $matches = [];
if (preg_match('/^([a-zA-Z]+);+([a-zA-Z-;]+)/',$attrtag,$matches)) { if (preg_match(sprintf('/^([%s]+);+([%s;]+)/',self::TAG_CHARS,self::TAG_CHARS),$attrtag,$matches)) {
$attribute = $matches[1]; $attribute = $matches[1];
$tags = $matches[2]; $tags = $matches[2];
@ -286,7 +304,7 @@ class Entry extends Model
->map(fn($item)=>$item ->map(fn($item)=>$item
->values ->values
->keys() ->keys()
->filter(fn($item)=>preg_match('/lang-[A-Za-z0-9-]+;?/',$item))) ->filter(fn($item)=>preg_match(sprintf('/%s+;?/',self::TAG_CHARS_LANG),$item)))
->filter(fn($item)=>$item->count()); ->filter(fn($item)=>$item->count());
} }
@ -344,7 +362,7 @@ class Entry extends Model
->filter(fn($item)=> ->filter(fn($item)=>
$item && collect(explode(';',$item))->filter( $item && collect(explode(';',$item))->filter(
fn($item)=> fn($item)=>
(! preg_match('/^lang-[A-Za-z0-9-]+$/',$item)) (! preg_match(sprintf('/^%s+$/',self::TAG_CHARS_LANG),$item))
&& (! preg_match('/^binary$/',$item)) && (! preg_match('/^binary$/',$item))
) )
->count()) ->count())