From 3fc6b55757ceab61b701d884fcf4fad90f798b3e Mon Sep 17 00:00:00 2001 From: Deon George Date: Sun, 16 Mar 2025 10:34:02 +1100 Subject: [PATCH] Start of work to handle attribute tags - should help with #75 and #16 --- app/Classes/LDAP/Export/LDIF.php | 14 +-- app/Ldap/Entry.php | 91 +++++++++++++++---- .../attribute_tags_cant_manage.blade.php | 9 ++ .../attribute/krbticketflags.blade.php | 36 +++++--- resources/views/fragment/dn/header.blade.php | 6 ++ 5 files changed, 115 insertions(+), 41 deletions(-) create mode 100644 resources/views/components/alert/attribute_tags_cant_manage.blade.php diff --git a/app/Classes/LDAP/Export/LDIF.php b/app/Classes/LDAP/Export/LDIF.php index 873f37fd..3e37517e 100644 --- a/app/Classes/LDAP/Export/LDIF.php +++ b/app/Classes/LDAP/Export/LDIF.php @@ -41,12 +41,14 @@ class LDIF extends Export // Display Attributes foreach ($o->getObjects() as $ao) { - foreach ($ao->values as $value) { - $result .= $this->multiLineDisplay( - Str::isAscii($value) - ? sprintf('%s: %s',$ao->name,$value) - : sprintf('%s:: %s',$ao->name,base64_encode($value)) - ,$this->br); + foreach ($ao->values as $tag => $tagvalues) { + foreach ($tagvalues as $value) { + $result .= $this->multiLineDisplay( + Str::isAscii($value) + ? sprintf('%s: %s',$ao->name.($tag ? ';'.$tag : ''),$value) + : sprintf('%s:: %s',$ao->name.($tag ? ';'.$tag : ''),base64_encode($value)) + ,$this->br); + } } } } diff --git a/app/Ldap/Entry.php b/app/Ldap/Entry.php index e9653785..2cdd05ba 100644 --- a/app/Ldap/Entry.php +++ b/app/Ldap/Entry.php @@ -179,34 +179,34 @@ class Entry extends Model $result = collect(); $entry_oc = Arr::get($this->attributes,'objectclass',[]); - foreach ($this->attributes as $attribute => $values) { + foreach ($this->attributes as $attrtag => $values) { // If the attribute name has tags $matches = []; - if (preg_match('/^([a-zA-Z]+)(;([a-zA-Z-;]+))+/',$attribute,$matches)) { + if (preg_match('/^([a-zA-Z]+);+([a-zA-Z-;]+)/',$attrtag,$matches)) { $attribute = $matches[1]; - - // If the attribute doesnt exist we'll create it - $o = Arr::get( - $result, - $attribute, - Factory::create( - $this->dn, - $attribute, - Arr::get($this->original,$attribute,[]), - $entry_oc, - )); - $o->setLangTag($matches[3],$values); + $tags = $matches[2]; } else { - $o = Factory::create($this->dn,$attribute,Arr::get($this->original,$attribute,[]),$entry_oc); + $attribute = $attrtag; + $tags = NULL; } - if (! $result->has($attribute)) { - // Store our new values to know if this attribute has changed - $o->values = collect($values); + $orig = Arr::get($this->original,$attrtag,[]); - $result->put($attribute,$o); - } + // If the attribute doesnt exist we'll create it + $o = Arr::get( + $result, + $attribute, + Factory::create( + $this->dn, + $attribute, + [$tags=>$orig], + $entry_oc, + )); + + $o->values = $o->values->merge([$tags=>$values]); + + $result->put($attribute,$o); } $sort = collect(config('pla.attr_display_order',[]))->map(fn($item)=>strtolower($item)); @@ -274,6 +274,35 @@ class Entry extends Model ->filter(fn($item)=>$item->is_internal); } + /** + * Identify the language tags (RFC 3866) used by this entry + * + * @return Collection + */ + public function getLangTags(): Collection + { + return $this->getObjects() + ->map(fn($item)=>$item + ->values + ->keys() + ->filter(fn($item)=>preg_match('/lang-[A-Za-z0-9-]+;?/',$item))) + ->filter(fn($item)=>$item->count()); + } + + /** + * Of all the items with lang tags, which ones have more than 1 lang tag + * + * @return Collection + */ + public function getLangMultiTags(): Collection + { + return $this->getLangTags() + ->map(fn($item)=>$item->values() + ->map(fn($item)=>explode(';',$item)) + ->filter(fn($item)=>count($item) > 1)) + ->filter(fn($item)=>$item->count()); + } + /** * Get an attribute as an object * @@ -299,6 +328,28 @@ class Entry extends Model return $this->objects; } + /** + * Find other attribute tags used by this entry + * + * @return Collection + */ + public function getOtherTags(): Collection + { + return $this->getObjects() + ->map(fn($item)=>$item + ->values + ->keys() + ->filter(fn($item)=> + $item && collect(explode(';',$item))->filter( + fn($item)=> + (! preg_match('/^lang-[A-Za-z0-9-]+$/',$item)) + && (! preg_match('/^binary$/',$item)) + ) + ->count()) + ) + ->filter(fn($item)=>$item->count()); + } + /** * Return a list of attributes without any values * diff --git a/resources/views/components/alert/attribute_tags_cant_manage.blade.php b/resources/views/components/alert/attribute_tags_cant_manage.blade.php new file mode 100644 index 00000000..6c61b07b --- /dev/null +++ b/resources/views/components/alert/attribute_tags_cant_manage.blade.php @@ -0,0 +1,9 @@ +
+ + + + + +
Unable to display this attribute as it has attribute tags [{!! $tags->join(', ') !!}].
+ You can manage it with an LDIF import.
+
\ No newline at end of file diff --git a/resources/views/components/attribute/krbticketflags.blade.php b/resources/views/components/attribute/krbticketflags.blade.php index bec368e7..0b246f08 100644 --- a/resources/views/components/attribute/krbticketflags.blade.php +++ b/resources/views/components/attribute/krbticketflags.blade.php @@ -1,23 +1,29 @@ - @foreach(($o->values->count() ? $o->values : ($new ? [0] : NULL)) as $value) - @if($edit) -
-
+ + @if($o->values->keys()->count() <= 1) + @foreach((count($o->values->first()) ? $o->values->first() : ($new ? [0] : NULL)) as $value) + @if($edit) +
+
-
- ($e=$errors->get($o->name_lc.'.'.$loop->index)),'mb-1','border-focus'=>$o->values->contains($value)]) name="{{ $o->name_lc }}[]" value="{{ $value }}" @readonly(true)> +
+ ($e=$errors->get($o->name_lc.'.'.$loop->index)),'mb-1','border-focus'=>$o->values->contains($value)]) name="{{ $o->name_lc }}[]" value="{{ $value }}" @readonly(true)> -
- @if($e) - {{ join('|',$e) }} - @endif +
+ @if($e) + {{ join('|',$e) }} + @endif +
-
- @else - {{ $value }} - @endif - @endforeach + @else + {{ $value }} + @endif + @endforeach + + @else + + @endif @section($o->name_lc.'-scripts') diff --git a/resources/views/fragment/dn/header.blade.php b/resources/views/fragment/dn/header.blade.php index 4a7166d8..fddc54d1 100644 --- a/resources/views/fragment/dn/header.blade.php +++ b/resources/views/fragment/dn/header.blade.php @@ -26,6 +26,12 @@ + @if(($x=$o->getLangTags())->count()) + + Tags + {{ $x->flatMap(fn($item)=>$item->values())->unique()->join(', ') }} + + @endif