Skip to content

Commit f08fdb1

Browse files
committed
Update Request validation, so that it also knows about required schema attributes
1 parent 0684424 commit f08fdb1

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

app/Classes/LDAP/Schema/AttributeType.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ public function __get(string $key): mixed
289289
case 'type': return $this->type;
290290
case 'usage': return $this->usage;
291291
case 'used_in_object_classes': return $this->used_in_object_classes;
292-
case 'validation': return Arr::get(config('ldap.validation'),$this->name_lc);
293292

294293
default: return parent::__get($key);
295294
}
@@ -601,4 +600,34 @@ public function setType($type) {
601600

602601
$this->type = $type;
603602
}
603+
604+
/**
605+
* Return Request validation array
606+
*
607+
* This will merge configured validation with schema required attributes
608+
*
609+
* @param array $array
610+
* @return array|null
611+
*/
612+
public function validation(array $array): ?array
613+
{
614+
// For each item in array, we need to get the OC heirachy
615+
$heirachy = collect($array)
616+
->filter()
617+
->map(fn($item)=>config('server')
618+
->schema('objectclasses',$item)
619+
->getSupClasses()
620+
->push($item))
621+
->flatten()
622+
->unique();
623+
624+
$validation = collect(Arr::get(config('ldap.validation'),$this->name_lc,[]));
625+
if (($heirachy->intersect($this->required_by_object_classes)->count() > 0)
626+
&& (! collect($validation->get($this->name_lc))->contains('required'))) {
627+
$validation->put($this->name_lc,array_merge(['required','min:1'],$validation->get($this->name_lc,[])))
628+
->put($this->name_lc.'.*',array_merge(['required','min:1'],$validation->get($this->name_lc.'.*',[])));
629+
}
630+
631+
return $validation->toArray();
632+
}
604633
}

app/Http/Requests/EntryRequest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ public function rules()
2626
return config('server')
2727
->schema('attributetypes')
2828
->intersectByKeys($this->request)
29-
->transform(function($item) { return $item->validation; })
29+
->map(fn($item)=>$item->validation(request()->get('objectclass')))
3030
->filter()
31-
->flatMap(function($item) { return $item; })
31+
->flatMap(fn($item)=>$item)
3232
->toArray();
3333
}
3434
}

config/ldap.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
use App\Rules\HasStructuralObjectClass;
4+
35
return [
46

57
/*
@@ -119,8 +121,10 @@
119121
'validation' => [
120122
'objectclass' => [
121123
'objectclass'=>[
124+
'required',
122125
'array',
123-
'min:1'
126+
'min:1',
127+
new HasStructuralObjectClass,
124128
]
125129
],
126130
'gidnumber' => [
@@ -170,4 +174,4 @@
170174
]
171175
],
172176
],
173-
];
177+
];

0 commit comments

Comments
 (0)