From f08fdb1bcdf1853e0a92ee16b69f347976cbb378 Mon Sep 17 00:00:00 2001 From: Deon George Date: Sat, 1 Mar 2025 19:30:21 +1100 Subject: [PATCH] Update Request validation, so that it also knows about required schema attributes --- app/Classes/LDAP/Schema/AttributeType.php | 31 ++++++++++++++++++++++- app/Http/Requests/EntryRequest.php | 4 +-- config/ldap.php | 8 ++++-- 3 files changed, 38 insertions(+), 5 deletions(-) diff --git a/app/Classes/LDAP/Schema/AttributeType.php b/app/Classes/LDAP/Schema/AttributeType.php index c4000562..25ddc8e7 100644 --- a/app/Classes/LDAP/Schema/AttributeType.php +++ b/app/Classes/LDAP/Schema/AttributeType.php @@ -289,7 +289,6 @@ final class AttributeType extends Base { case 'type': return $this->type; case 'usage': return $this->usage; case 'used_in_object_classes': return $this->used_in_object_classes; - case 'validation': return Arr::get(config('ldap.validation'),$this->name_lc); default: return parent::__get($key); } @@ -601,4 +600,34 @@ final class AttributeType extends Base { $this->type = $type; } + + /** + * Return Request validation array + * + * This will merge configured validation with schema required attributes + * + * @param array $array + * @return array|null + */ + public function validation(array $array): ?array + { + // For each item in array, we need to get the OC heirachy + $heirachy = collect($array) + ->filter() + ->map(fn($item)=>config('server') + ->schema('objectclasses',$item) + ->getSupClasses() + ->push($item)) + ->flatten() + ->unique(); + + $validation = collect(Arr::get(config('ldap.validation'),$this->name_lc,[])); + if (($heirachy->intersect($this->required_by_object_classes)->count() > 0) + && (! collect($validation->get($this->name_lc))->contains('required'))) { + $validation->put($this->name_lc,array_merge(['required','min:1'],$validation->get($this->name_lc,[]))) + ->put($this->name_lc.'.*',array_merge(['required','min:1'],$validation->get($this->name_lc.'.*',[]))); + } + + return $validation->toArray(); + } } \ No newline at end of file diff --git a/app/Http/Requests/EntryRequest.php b/app/Http/Requests/EntryRequest.php index 5aba0c77..c454aa9b 100644 --- a/app/Http/Requests/EntryRequest.php +++ b/app/Http/Requests/EntryRequest.php @@ -26,9 +26,9 @@ class EntryRequest extends FormRequest return config('server') ->schema('attributetypes') ->intersectByKeys($this->request) - ->transform(function($item) { return $item->validation; }) + ->map(fn($item)=>$item->validation(request()->get('objectclass'))) ->filter() - ->flatMap(function($item) { return $item; }) + ->flatMap(fn($item)=>$item) ->toArray(); } } \ No newline at end of file diff --git a/config/ldap.php b/config/ldap.php index 2fa7440e..07dff22b 100644 --- a/config/ldap.php +++ b/config/ldap.php @@ -1,5 +1,7 @@ [ 'objectclass' => [ 'objectclass'=>[ + 'required', 'array', - 'min:1' + 'min:1', + new HasStructuralObjectClass, ] ], 'gidnumber' => [ @@ -170,4 +174,4 @@ return [ ] ], ], -]; +]; \ No newline at end of file