Laravel 13 + Livewire/Volt app for managing client organizations, documents, passwords, flexible assets, virtual machines, contacts, domains, todos, shortcuts, and role-based user administration.
160 lines
6.6 KiB
PHP
160 lines
6.6 KiB
PHP
<?php
|
|
|
|
use App\Models\Organization;
|
|
use App\Models\User;
|
|
use Illuminate\Support\Facades\Hash;
|
|
use Illuminate\Validation\Rule;
|
|
use Illuminate\Validation\Rules\Password;
|
|
use Livewire\Component;
|
|
use Spatie\Permission\Models\Role;
|
|
|
|
new class extends Component
|
|
{
|
|
public ?User $user = null;
|
|
|
|
public string $name = '';
|
|
public string $email = '';
|
|
public string $password = '';
|
|
public string $password_confirmation = '';
|
|
public string $role = '';
|
|
public bool $is_msp_staff = false;
|
|
|
|
/** @var array<int, int> */
|
|
public array $organization_ids = [];
|
|
|
|
public function mount(?User $user = null): void
|
|
{
|
|
if ($user?->exists) {
|
|
$this->user = $user;
|
|
$this->name = $user->name;
|
|
$this->email = $user->email;
|
|
$this->role = $user->getRoleNames()->first() ?? '';
|
|
$this->is_msp_staff = (bool) $user->is_msp_staff;
|
|
$this->organization_ids = $user->organizations->pluck('id')->all();
|
|
}
|
|
}
|
|
|
|
public function save(): void
|
|
{
|
|
$validated = $this->validate([
|
|
'name' => 'required|string|max:255',
|
|
'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')->ignore($this->user?->id)],
|
|
'password' => [$this->user?->exists ? 'nullable' : 'required', 'string', Password::default(), 'confirmed'],
|
|
'role' => 'required|exists:roles,name',
|
|
'is_msp_staff' => 'boolean',
|
|
'organization_ids' => 'array',
|
|
'organization_ids.*' => 'exists:organizations,id',
|
|
]);
|
|
|
|
$attributes = [
|
|
'name' => $validated['name'],
|
|
'email' => $validated['email'],
|
|
'is_msp_staff' => $validated['is_msp_staff'],
|
|
];
|
|
|
|
if (! empty($validated['password'])) {
|
|
$attributes['password'] = Hash::make($validated['password']);
|
|
}
|
|
|
|
if ($this->user?->exists) {
|
|
$this->user->update($attributes);
|
|
} else {
|
|
$this->user = User::create($attributes);
|
|
}
|
|
|
|
$this->user->syncRoles([$this->role]);
|
|
$this->user->organizations()->sync($this->organization_ids);
|
|
|
|
session()->flash('status', "Utilisateur « {$this->user->name} » enregistré.");
|
|
$this->redirectRoute('users.index', navigate: true);
|
|
}
|
|
|
|
public function with(): array
|
|
{
|
|
return [
|
|
'roles' => Role::orderBy('name')->get(),
|
|
'organizations' => Organization::orderBy('name')->get(),
|
|
];
|
|
}
|
|
};
|
|
?>
|
|
|
|
<div class="max-w-2xl">
|
|
<p class="text-sm text-slate-500">Gestion des utilisateurs</p>
|
|
<h1 class="mb-6 text-2xl font-semibold text-slate-900">
|
|
{{ $user?->exists ? 'Modifier l\'utilisateur' : 'Nouvel utilisateur' }}
|
|
</h1>
|
|
|
|
<form wire:submit="save" class="space-y-4 rounded border border-slate-200 bg-white p-6">
|
|
<div class="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label class="block text-sm font-medium text-slate-700">Nom</label>
|
|
<input type="text" wire:model="name"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
@error('name') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium text-slate-700">Email</label>
|
|
<input type="email" wire:model="email"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
@error('email') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
|
</div>
|
|
</div>
|
|
|
|
<div class="grid grid-cols-2 gap-4">
|
|
<div>
|
|
<label class="block text-sm font-medium text-slate-700">Mot de passe {{ $user?->exists ? '(laisser vide pour ne pas changer)' : '' }}</label>
|
|
<input type="password" wire:model="password" autocomplete="new-password"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
@error('password') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
|
</div>
|
|
<div>
|
|
<label class="block text-sm font-medium text-slate-700">Confirmation</label>
|
|
<input type="password" wire:model="password_confirmation" autocomplete="new-password"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label class="block text-sm font-medium text-slate-700">Rôle</label>
|
|
<select wire:model="role"
|
|
class="mt-1 w-full rounded border-slate-300 text-sm shadow-sm focus:border-slate-500 focus:ring-slate-500">
|
|
<option value="">—</option>
|
|
@foreach ($roles as $roleOption)
|
|
<option value="{{ $roleOption->name }}">{{ $roleOption->name }}</option>
|
|
@endforeach
|
|
</select>
|
|
@error('role') <p class="mt-1 text-sm text-red-600">{{ $message }}</p> @enderror
|
|
</div>
|
|
|
|
<label class="flex items-center gap-2 text-sm text-slate-700">
|
|
<input type="checkbox" wire:model.live="is_msp_staff" class="rounded border-slate-300">
|
|
Membre de l'équipe MSP (accès à toutes les organisations)
|
|
</label>
|
|
|
|
@unless ($is_msp_staff)
|
|
<div>
|
|
<label class="block text-sm font-medium text-slate-700">Organisations accessibles</label>
|
|
<div class="mt-2 max-h-48 space-y-1 overflow-y-auto rounded border border-slate-200 p-3">
|
|
@forelse ($organizations as $organization)
|
|
<label class="flex items-center gap-2 text-sm text-slate-700">
|
|
<input type="checkbox" wire:model="organization_ids" value="{{ $organization->id }}" class="rounded border-slate-300">
|
|
{{ $organization->name }}
|
|
</label>
|
|
@empty
|
|
<p class="text-sm text-slate-400">Aucune organisation.</p>
|
|
@endforelse
|
|
</div>
|
|
</div>
|
|
@endunless
|
|
|
|
<div class="flex items-center gap-3 pt-2">
|
|
<button type="submit"
|
|
class="rounded bg-slate-900 px-4 py-2 text-sm font-medium text-white hover:bg-slate-700">
|
|
Enregistrer
|
|
</button>
|
|
<a href="{{ route('users.index') }}" class="text-sm text-slate-500 hover:text-slate-900">Annuler</a>
|
|
</div>
|
|
</form>
|
|
</div>
|