From 98a0b87afe7ff5641827effb88213507ec01bc25 Mon Sep 17 00:00:00 2001 From: Deon George Date: Mon, 16 Jun 2025 23:04:07 +1000 Subject: [PATCH] Add objects directly to Entry::class when rendering a template. Fix objectclasses and attributes processing for templates --- app/Classes/Template.php | 7 +++--- app/Http/Controllers/HomeController.php | 9 ++++++-- app/Ldap/Entry.php | 23 +++++++++++++++---- .../views/fragment/template/dn.blade.php | 2 +- templates/example.json | 16 +++++++++++++ templates/user_account.json | 3 ++- 6 files changed, 49 insertions(+), 11 deletions(-) diff --git a/app/Classes/Template.php b/app/Classes/Template.php index 1a4be7ee..97468e81 100644 --- a/app/Classes/Template.php +++ b/app/Classes/Template.php @@ -31,11 +31,12 @@ class Template public function __get(string $key): mixed { return match ($key) { - 'name' => Str::replaceEnd('.json','',$this->file), - 'attributes' => collect(array_map('strtolower',array_keys(Arr::get($this->template,$key)))), - 'objectclasses' => collect(array_map('strtolower',Arr::get($this->template,$key))), + 'attributes' => collect(Arr::get($this->template,$key))->keys(), 'enabled' => Arr::get($this->template,$key,FALSE) && (! $this->invalid), 'icon','regexp','title' => Arr::get($this->template,$key), + 'name' => Str::replaceEnd('.json','',$this->file), + 'objectclasses' => collect(Arr::get($this->template,$key)), + 'order' => collect(Arr::get($this->template,'attributes'))->map(fn($item)=>$item['order']), default => throw new \Exception('Unknown key: '.$key), }; diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 3507851e..751e3a5a 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -58,8 +58,13 @@ class HomeController extends Controller $template = $o->template($request->validated('template')); $o->objectclass = [Entry::TAG_NOTAG=>$template->objectclasses->toArray()]; - foreach ($o->getAvailableAttributes()->filter(fn($item)=>$item->names_lc->intersect($template->attributes)->count()) as $ao) - $o->{$ao->name} = [Entry::TAG_NOTAG=>'']; + foreach ($o->getAvailableAttributes() + ->filter(fn($item)=>$item->names_lc->intersect($template->attributes->map('strtolower'))->count()) + ->sortBy(fn($item)=>Arr::get($template->order,$item->name)) as $ao) + { + $o->addObjectItem($ao->name, + Factory::create(dn: '',attribute: $ao->name,values: [Entry::TAG_NOTAG=>''],oc: $o->objectclass)); + } } $step = $request->step ? $request->step+1 : old('step'); diff --git a/app/Ldap/Entry.php b/app/Ldap/Entry.php index 9dca8c15..d0a86ccf 100644 --- a/app/Ldap/Entry.php +++ b/app/Ldap/Entry.php @@ -130,7 +130,7 @@ class Entry extends Model $o = $this->objects->get($attribute) ?: Factory::create($this->dn ?: '',$attribute,[],Arr::get($this->attributes,'objectclass',[])); $o->values = collect($value); - $this->objects->put($key,$o); + $this->addObjectItem($key,$o); return $this; } @@ -159,8 +159,11 @@ class Entry extends Model if ($this->dn && (! in_array(strtolower($this->dn),['cn=subschema']))) { $this->templates = $this->templates ->filter(fn($item)=>$item->enabled - && (! count($item->objectclasses->diff(array_map('strtolower',Arr::get($this->attributes,'objectclass')))))) - ->sortBy(fn($item)=>$item); + && (! $item->objectclasses + ->map('strtolower') + ->diff(array_map('strtolower',Arr::get($this->attributes,'objectclass'))) + ->count())) + ->sortBy(fn($item)=>$item->title); } return $this; @@ -208,7 +211,19 @@ class Entry extends Model $o = $this->objects->get($attribute) ?: Attribute\Factory::create($this->dn ?: '',$attribute,[]); $o->addValue($tag,[$value]); - $this->objects->put($attribute,$o); + $this->addObjectItem($attribute,$o); + } + + /** + * Add a new object item directly + * + * @param string $name + * @param Attribute $o + * @return void + */ + public function addObjectItem(string $name,Attribute $o): void + { + $this->objects->put($name,$o); } /** diff --git a/resources/views/fragment/template/dn.blade.php b/resources/views/fragment/template/dn.blade.php index cf778406..db69f9e6 100644 --- a/resources/views/fragment/template/dn.blade.php +++ b/resources/views/fragment/template/dn.blade.php @@ -8,7 +8,7 @@
@php($up=(session()->pull('updated') ?: collect())) - @foreach($o->getVisibleAttributes()->filter(fn($item)=>$template->attributes->contains($item->name_lc)) as $ao) + @foreach($o->getVisibleAttributes()->filter(fn($item)=>$template->attributes->map('strtolower')->contains($item->name_lc)) as $ao) @endforeach
diff --git a/templates/example.json b/templates/example.json index b775b879..13d57331 100644 --- a/templates/example.json +++ b/templates/example.json @@ -19,7 +19,23 @@ "attribute2": { "display": "Attribute 2", "hint": "This is an example", + "type": "input", // Default is input "order": 2 + }, + "attribute3": { + "display": "Attribute 3", + "type": "select", + "options": { + "/bin/bash": "Bash", + "/bin/csh": "C Shell", + "/bin/dash": "Dash", + "/bin/sh": "Shell", + "/bin/tsh": "Turbo C Shell", + "/bin/zsh": "ZSH", + "/bin/false": "False", + "/usr/sbin/nologin": "No Login" + }, + "order": 8 } } } diff --git a/templates/user_account.json b/templates/user_account.json index 689eb685..61359c80 100644 --- a/templates/user_account.json +++ b/templates/user_account.json @@ -66,7 +66,8 @@ }, "loginShell": { "display": "Login Shell", - "select": { + "type": "select", + "options": { "/bin/bash": "Bash", "/bin/csh": "C Shell", "/bin/dash": "Dash",