Compare commits

...

2 Commits

Author SHA1 Message Date
b87b1dcfd0 Enhancements to logic that makes form.select component
All checks were successful
Create Docker Image / Test Application (x86_64) (push) Successful in 30s
Create Docker Image / Build Docker Image (x86_64) (push) Successful in 1m34s
Create Docker Image / Build Docker Image (arm64) (push) Successful in 2m49s
Create Docker Image / Final Docker Image Manifest (push) Successful in 10s
2025-06-11 19:33:36 +09:30
d61ecc96db Enable creation of new entries via templates 2025-06-11 19:28:38 +09:30
11 changed files with 109 additions and 31 deletions

View File

@ -30,15 +30,20 @@ class Template
public function __get(string $key): mixed
{
return match ($key) {
'attributes' => array_map('strtolower',array_keys(Arr::get($this->template,$key))),
'objectclasses' => array_map('strtolower',Arr::get($this->template,$key)),
'enabled' => Arr::get($this->template,$key,FALSE),
'icon','regexp' => Arr::get($this->template,$key),
'attributes' => collect(array_map('strtolower',array_keys(Arr::get($this->template,$key)))),
'objectclasses' => collect(array_map('strtolower',Arr::get($this->template,$key))),
'enabled' => Arr::get($this->template,$key,FALSE) && (! $this->invalid),
'icon','regexp','title' => Arr::get($this->template,$key),
default => throw new \Exception('Unknown key: '.$key),
};
}
public function __isset(string $key): bool
{
return array_key_exists($key,$this->template);
}
public function __toString(): string
{
return $this->invalid ? '' : Arr::get($this->template,'title','No Template Name');

View File

@ -55,16 +55,24 @@ class HomeController extends Controller
$key = $this->request_key($request,collect(old()));
$template = NULL;
$o = new Entry;
$o->setRDNBase($key['dn']);
if (count($x=array_filter(old('objectclass',$request->objectclass)))) {
$o->objectclass = $x;
if (count($x=collect(old('objectclass',$request->validated('objectclass')))->dot()->filter())) {
$o->objectclass = Arr::undot($x);
// Also add in our required attributes
foreach($o->getAvailableAttributes()->filter(fn($item)=>$item->required) as $ao)
$o->{$ao->name} = [Entry::TAG_NOTAG=>''];
$o->setRDNBase($key['dn']);
} elseif ($request->validated('template')) {
$template = $o->template($request->validated('template'));
$o->objectclass = [Entry::TAG_NOTAG=>$template->objectclasses->toArray()];
// @todo We need to add aliases
foreach($o->getAvailableAttributes()->filter(fn($item)=>$template->attributes->contains($item)) as $ao)
$o->{$ao->name} = [Entry::TAG_NOTAG=>''];
}
$step = $request->step ? $request->step+1 : old('step');
@ -74,6 +82,7 @@ class HomeController extends Controller
->with('bases',$this->bases())
->with('o',$o)
->with('step',$step)
->with('template',$template)
->with('container',old('container',$key['dn']));
}
@ -383,7 +392,12 @@ class HomeController extends Controller
->with('bases',$this->bases());
// If we are rendering a DN, rebuild our object
if ($key['dn']) {
if ($key['cmd'] === 'create') {
$o = new Entry;
$o->setRDNBase($key['dn']);
} elseif ($key['dn']) {
// @todo Need to handle if DN is null, for example if the user's session expired and the ACLs dont let them retrieve $key['dn']
$o = config('server')->fetch($key['dn']);
foreach (collect(old())->except(['key','dn','step','_token','userpassword_hash','rdn','rdn_value']) as $attr => $value)
@ -393,6 +407,7 @@ class HomeController extends Controller
return match ($key['cmd']) {
'create' => $view
->with('container',old('container',$key['dn']))
->with('o',$o)
->with('step',1),
'dn' => $view

View File

@ -66,12 +66,43 @@ class EntryAddRequest extends FormRequest
'min:1',
'max:1',
],
'objectclass._null_'=>[
'required',
'objectclass._null_' => [
function (string $attribute,mixed $value,\Closure $fail) {
$oc = collect($value)->dot()->filter();
// If this is step 1 and there is no objectclass, and no template, then fail
if ((! $oc->count())
&& (request()->post('step') == 1)
&& (! request()->post('template')))
{
$fail(__('Select an objectclass or a template'));
}
// Cant have both an objectclass and a template
if (request()->post('template') && $oc->count())
$fail(__('You cannot select a template and an objectclass'));
},
'array',
'min:1',
new HasStructuralObjectClass,
]
],
'template' => [
function (string $attribute,mixed $value,\Closure $fail) {
$oc = collect(request()->post('objectclass'))->dot()->filter();
// If this is step 1 and there is no objectclass, and no template, then fail
if ((! collect($value)->filter()->count())
&& (request()->post('step') == 1)
&& (! $oc->count()))
{
$fail(__('Select an objectclass or a template'));
}
// Cant have both an objectclass and a template
if ($oc->count() && strlen($value))
$fail(__('You cannot select a template and an objectclass'));
},
],
])
->toArray();
}

View File

@ -147,7 +147,7 @@ class Entry extends Model
// Filter out our templates specific for this entry
if ($this->dn && (! in_array(strtolower($this->dn),['cn=subschema']))) {
$this->templates = $this->templates
->filter(fn($item)=>! count(array_diff($item->objectclasses,array_map('strtolower',Arr::get($this->attributes,'objectclass')))));
->filter(fn($item)=>! count($item->objectclasses->diff(array_map('strtolower',Arr::get($this->attributes,'objectclass')))));
}
return $this;

View File

@ -24,6 +24,7 @@ class HasStructuralObjectClass implements ValidationRule
if ($item && config('server')->schema('objectclasses',$item)->isStructural())
return;
$fail('There isnt a Structural Objectclass.');
if (collect($value)->dot()->filter()->count())
$fail(__('There isnt a Structural Objectclass.'));
}
}

View File

@ -38,7 +38,7 @@
</div>
<div class="modal-body">
<x-form.select id="newoc" label="Select from..."/>
<x-form.select id="newoc" name="newoc" :label="__('Select from').'...'"/>
</div>
<div class="modal-footer">

View File

@ -1,4 +1,4 @@
<button id="form-reset" class="btn btn-outline-danger">@lang('Reset')</button>
<button id="form-reset" class="btn btn-sm btn-outline-danger">@lang('Reset')</button>
@section('page-scripts')
<script>

View File

@ -1,28 +1,32 @@
<x-form.base {{ $attributes }}>
@isset($name)
<input type="hidden" id="{{ $id ?? $name }}_disabled" name="{{ $name }}" value="" disabled>
@else
@php(throw new \Exception('here'))
@dd('no name',$id)
@endisset
<select class="form-select @isset($name)@error((! empty($old)) ? $old : ($id ?? $name)) is-invalid @enderror @endisset" id="{{ $id ?? $name }}" @isset($name)name="{{ $name }}"@endisset @required(isset($required) && $required) @disabled(isset($disabled) && $disabled)>
@if((empty($value) && ! empty($options)) || isset($addnew) || isset($choose))
<select class="form-select @error($old ?? ($id ?? $name)) is-invalid @enderror" id="{{ $id ?? $name }}" name="{{ $name }}" @required($required ?? FALSE) @disabled($disabled ?? FALSE)>
@if((empty($value) && ! empty($options)) || isset($addnew))
<option value=""></option>
@isset($addnew)
<option value="new">{{ $addnew ?: 'Add New' }}</option>
@endisset
@endif
@isset($options)
@empty($groupby)
@foreach($options as $option)
@continue(! Arr::get($option,'value'))
<option value="{{ Arr::get($option,'id') }}" @selected(isset($name) && (Arr::get($option,'id') == old($old ?? $name,$value ?? '')))>{{ Arr::get($option,'value') }}</option>
<option value="{{ Arr::get($option,'id') }}" @selected(Arr::get($option,'id') == collect(old())->dot()->get(isset($old) ? $old.'.0' : ($id ?? $name)))>{{ Arr::get($option,'value') }}</option>
@endforeach
@else
@foreach($options->groupBy($groupby) as $group)
<optgroup label="{{ $groupby == 'active' ? (Arr::get($group->first(),$groupby) ? 'Active' : 'Not Active') : Arr::get($group->first(),$groupby) }}">
<optgroup label="{{ Arr::get($group->first(),$groupby) }}">
@foreach($group as $option)
@continue(! Arr::get($option,'value'))
<option value="{{ Arr::get($option,'id') }}" @selected(isset($name) && (Arr::get($option,'id') == old($old ?? $name,$value ?? '')))>{{ Arr::get($option,'value') }}</option>
<option value="{{ Arr::get($option,'id') }}" @selected(Arr::get($option,'id') == collect(old())->dot()->get(isset($old) ? $old.'.0' : ($id ?? $name)))>{{ Arr::get($option,'value') }}</option>
@endforeach
</optgroup>
@endforeach

View File

@ -14,7 +14,7 @@
<div class="row">
<div class="col-12 pt-2">
<x-form.select id="newattr" :label="__('Select from').'...'" :options="$o->getMissingAttributes()->sortBy('name')->unique('name')->map(fn($item)=>['id'=>$item->name,'value'=>$item->name])"/>
<x-form.select name="newattr" :label="__('Select from').'...'" :options="$o->getMissingAttributes()->sortBy('name')->unique('name')->map(fn($item)=>['id'=>$item->name,'value'=>$item->name])"/>
</div>
</div>
</div>

View File

@ -9,7 +9,7 @@
@php($up=(session()->pull('updated') ?: collect()))
@php($attributes=$o->template($template)?->attributes)
@foreach($o->getVisibleAttributes()->filter(fn($item)=>in_array($item,$attributes)) as $ao)
@foreach($o->getVisibleAttributes()->filter(fn($item)=>$attributes->contains($item)) as $ao)
<x-attribute-type :o="$ao" :edit="TRUE" :new="FALSE" :updated="$up->contains($ao->name_lc)"/>
@endforeach
</div>

View File

@ -17,7 +17,7 @@
<div class="main-card mb-3 card">
<div class="card-header">
@lang('Create New Entry') - @lang('Step') {{ $step }}
@lang('Create New Entry') - @lang('Step') {{ $step }} @isset($template) <span class="ms-auto"><i class="fa fa-fw {{ $template->icon }}"></i> {{ $template->title }}</span>@endisset
</div>
<div class="card-body">
@ -30,19 +30,35 @@
@switch($step)
@case(1)
<div class="row">
<div class="col-12 col-md-6">
<div class="col-12 col-md-5">
<x-form.select
id="objectclass"
name="objectclass[{{ Entry::TAG_NOTAG }}][]"
old="objectclass.{{ Entry::TAG_NOTAG }}"
:label="__('Select a Structural ObjectClass...')"
:label="__('Select a Structural ObjectClass').'...'"
:options="($oc=$server->schema('objectclasses'))
->filter(fn($item)=>$item->isStructural())
->sortBy(fn($item)=>$item->name_lc)
->map(fn($item)=>['id'=>$item->name,'value'=>$item->name])"
multiple="false"
:allowclear="TRUE"
/>
</div>
@if($o->templates->count())
<div class="col-md-1">
<strong>@lang('OR')</strong>
</div>
<div class="col-12 col-md-5">
<x-form.select
name="template"
:label="__('Select a Template').'...'"
:options="$o->templates
->map(fn($item,$key)=>['id'=>$key,'value'=>$item->title])"
:allowclear="TRUE"
/>
</div>
@endif
</div>
@break
@ -53,14 +69,12 @@
<x-attribute-type :o="$ao" :edit="TRUE" :new="FALSE" :updated="FALSE"/>
@endforeach
@include('fragment.dn.add_attr')
@break;
@endswitch
</form>
<div class="row d-none pt-3">
<div class="col-12 {{ $step > 1 ? 'offset-sm-2' : '' }} col-lg-10">
<div class="col-11 {{ $step > 1 ? 'text-end' : '' }} pe-0">
<x-form.reset form="dn-create"/>
<x-form.submit :action="__('Next')" form="dn-create"/>
</div>
@ -102,7 +116,15 @@
}
$(document).ready(function() {
@if($step === 2)
@if($step === 1)
$('#objectclass').on('select2:open',function(){
$('#template').val(null).trigger('change');
});
$('#template').on('select2:open',function(){
$('#objectclass').val(null).trigger('change');
})
@elseif($step === 2)
$('#newattr').on('change',function(item) {
var oc = $('attribute#objectclass input[type=text]')
.map((key,item)=>{return $(item).val()}).toArray();