++ fix: Show causer name from activity properties if not available in relation

This commit is contained in:
2026-04-07 15:09:49 +00:00
parent be1ac25047
commit aac13522e5
3 changed files with 221 additions and 41 deletions

View File

@@ -2,10 +2,13 @@
namespace App\Livewire\Settings;
use App\Models\Assegnazione;
use App\Models\User;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Livewire\Component;
use Spatie\Permission\Models\Permission;
use Spatie\Activitylog\Models\Activity;
use Spatie\Permission\Models\Role;
class UsersIndex extends Component
{
@@ -13,15 +16,26 @@ class UsersIndex extends Component
public string $email = '';
public string $password = '';
public string $password_confirmation = '';
public array $selectedPermissions = [];
public array $availablePermissions = [];
public string $selectedRole = '';
public array $availableRoles = [];
public ?int $editingUserId = null;
public string $editName = '';
public string $editEmail = '';
public string $editPassword = '';
public string $editPassword_confirmation = '';
public string $editSelectedRole = '';
public function mount(): void
{
$this->availablePermissions = Permission::query()
$this->availableRoles = Role::query()
->orderBy('name')
->pluck('name')
->all();
if (! empty($this->availableRoles)) {
$this->selectedRole = $this->availableRoles[0];
}
}
protected function rules(): array
@@ -30,8 +44,22 @@ class UsersIndex extends Component
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'email', 'max:255', Rule::unique('users', 'email')],
'password' => ['required', 'string', 'min:8', 'confirmed'],
'selectedPermissions' => ['array'],
'selectedPermissions.*' => ['string', Rule::in($this->availablePermissions)],
'selectedRole' => ['required', 'string', Rule::in($this->availableRoles)],
];
}
protected function editRules(): array
{
return [
'editName' => ['required', 'string', 'max:255'],
'editEmail' => [
'required',
'email',
'max:255',
Rule::unique('users', 'email')->ignore($this->editingUserId),
],
'editPassword' => ['nullable', 'string', 'min:8', 'confirmed'],
'editSelectedRole' => ['required', 'string', Rule::in($this->availableRoles)],
];
}
@@ -45,16 +73,120 @@ class UsersIndex extends Component
'password' => $validated['password'],
]);
$user->syncPermissions($validated['selectedPermissions'] ?? []);
$user->syncRoles([$validated['selectedRole']]);
$this->reset(['name', 'email', 'password', 'password_confirmation', 'selectedPermissions']);
$this->reset(['name', 'email', 'password', 'password_confirmation']);
if (! empty($this->availableRoles)) {
$this->selectedRole = $this->availableRoles[0];
}
session()->flash('success', 'Utente creato con successo.');
}
public function startEdit(int $userId): void
{
$user = User::query()->with('roles')->findOrFail($userId);
$this->editingUserId = $user->id;
$this->editName = $user->name;
$this->editEmail = $user->email;
$this->editPassword = '';
$this->editPassword_confirmation = '';
$this->editSelectedRole = $user->roles->first()?->name ?? ($this->availableRoles[0] ?? '');
}
public function cancelEdit(): void
{
$this->reset([
'editingUserId',
'editName',
'editEmail',
'editPassword',
'editPassword_confirmation',
'editSelectedRole',
]);
}
public function updateUser(): void
{
if (! $this->editingUserId) {
return;
}
$validated = $this->validate($this->editRules());
$user = User::query()->findOrFail($this->editingUserId);
$user->name = $validated['editName'];
$user->email = $validated['editEmail'];
if (! empty($validated['editPassword'])) {
$user->password = $validated['editPassword'];
}
$user->save();
$user->syncRoles([$validated['editSelectedRole']]);
$this->cancelEdit();
session()->flash('success', 'Utente aggiornato con successo.');
}
public function deleteUser(int $userId): void
{
$currentUser = auth()->user();
$user = User::query()->with('roles')->findOrFail($userId);
if (! $currentUser || $currentUser->id === $user->id) {
session()->flash('error', 'Non puoi cancellare il tuo utente.');
return;
}
if ($user->hasRole('amministratore') && User::role('amministratore')->count() <= 1) {
session()->flash('error', 'Non puoi cancellare l\'ultimo amministratore.');
return;
}
DB::transaction(function () use ($user, $currentUser) {
$causerName = $user->name;
$causerEmail = $user->email;
$deletedAt = now()->toDateTimeString();
Activity::query()
->where('causer_type', User::class)
->where('causer_id', $user->id)
->chunkById(200, function ($activities) use ($causerName, $causerEmail, $deletedAt) {
foreach ($activities as $activity) {
$properties = $activity->properties?->toArray() ?? [];
$properties['causer_name'] = $causerName;
$properties['causer_email'] = $causerEmail;
$properties['causer_deleted_at'] = $deletedAt;
$activity->properties = $properties;
$activity->save();
}
});
Assegnazione::query()
->where('created_by', $user->id)
->update(['created_by' => $currentUser->id]);
Assegnazione::query()
->where('returned_by', $user->id)
->update(['returned_by' => $currentUser->id]);
$user->syncRoles([]);
$user->delete();
});
if ($this->editingUserId === $userId) {
$this->cancelEdit();
}
session()->flash('success', 'Utente cancellato. I log sono stati preservati.');
}
public function render()
{
return view('livewire.settings.users-index', [
'users' => User::query()->with('roles', 'permissions')->orderBy('name')->get(),
'users' => User::query()->with('roles')->orderBy('name')->get(),
]);
}
}

View File

@@ -40,7 +40,7 @@
@forelse($activities as $activity)
<tr class="hover:bg-gray-50">
<td class="px-3 py-2 text-xs text-gray-500 whitespace-nowrap">{{ $activity->created_at->format('d/m/Y H:i:s') }}</td>
<td class="px-3 py-2 text-xs">{{ $activity->causer?->name ?? 'Sistema' }}</td>
<td class="px-3 py-2 text-xs">{{ $activity->causer?->name ?? data_get($activity->properties, 'causer_name') ?? 'Sistema' }}</td>
<td class="px-3 py-2">
<span class="inline-flex px-2 py-0.5 text-xs font-medium rounded-full
{{ match($activity->description) {

View File

@@ -1,9 +1,16 @@
<div class="space-y-6">
<div>
<h1 class="text-2xl font-bold text-gray-900">Utenti</h1>
<p class="text-sm text-gray-500 mt-1">Crea utenti e assegna i permessi applicativi.</p>
<p class="text-sm text-gray-500 mt-1">Crea utenti e assegna un ruolo applicativo.</p>
</div>
@if (session()->has('success'))
<div class="rounded-lg bg-green-50 p-3 text-sm text-green-700 border border-green-200">{{ session('success') }}</div>
@endif
@if (session()->has('error'))
<div class="rounded-lg bg-red-50 p-3 text-sm text-red-700 border border-red-200">{{ session('error') }}</div>
@endif
<div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
<h2 class="text-lg font-semibold text-gray-900 mb-4">Nuovo utente</h2>
@@ -34,16 +41,13 @@
</div>
<div>
<p class="block text-sm font-medium text-gray-700 mb-2">Permessi utente</p>
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-2">
@foreach($availablePermissions as $permission)
<label class="flex items-center gap-2 rounded border border-gray-200 px-3 py-2 text-sm text-gray-700 hover:bg-gray-50">
<input wire:model="selectedPermissions" type="checkbox" value="{{ $permission }}" class="rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
<span>{{ $permission }}</span>
</label>
<label for="selectedRole" class="block text-sm font-medium text-gray-700">Ruolo *</label>
<select wire:model="selectedRole" id="selectedRole" class="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 text-sm">
@foreach($availableRoles as $role)
<option value="{{ $role }}">{{ ucfirst($role) }}</option>
@endforeach
</div>
@error('selectedPermissions.*') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
</select>
@error('selectedRole') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
</div>
<div>
@@ -62,32 +66,76 @@
<th class="px-4 py-2 text-left font-medium text-gray-600">Nome</th>
<th class="px-4 py-2 text-left font-medium text-gray-600">Email</th>
<th class="px-4 py-2 text-left font-medium text-gray-600">Ruoli</th>
<th class="px-4 py-2 text-left font-medium text-gray-600">Permessi diretti</th>
<th class="px-4 py-2 text-left font-medium text-gray-600">Azioni</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-100">
@forelse($users as $user)
<tr>
<td class="px-4 py-2 text-gray-900">{{ $user->name }}</td>
<td class="px-4 py-2 text-gray-700">{{ $user->email }}</td>
<td class="px-4 py-2">
<div class="flex flex-wrap gap-1">
@forelse($user->roles as $role)
<span class="inline-flex items-center rounded-full bg-indigo-50 px-2 py-0.5 text-xs font-medium text-indigo-700">{{ $role->name }}</span>
@empty
<span class="text-xs text-gray-400">-</span>
@endforelse
</div>
</td>
<td class="px-4 py-2">
<div class="flex flex-wrap gap-1">
@forelse($user->permissions as $permission)
<span class="inline-flex items-center rounded-full bg-gray-100 px-2 py-0.5 text-xs font-medium text-gray-700">{{ $permission->name }}</span>
@empty
<span class="text-xs text-gray-400">-</span>
@endforelse
</div>
</td>
@if($editingUserId === $user->id)
<td class="px-4 py-2 align-top" colspan="4">
<div class="grid grid-cols-1 md:grid-cols-2 gap-3">
<div>
<label class="block text-xs font-medium text-gray-600">Nome</label>
<input wire:model="editName" type="text" class="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 text-sm">
@error('editName') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
</div>
<div>
<label class="block text-xs font-medium text-gray-600">Email</label>
<input wire:model="editEmail" type="email" class="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 text-sm">
@error('editEmail') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
</div>
<div>
<label class="block text-xs font-medium text-gray-600">Nuova password (opzionale)</label>
<input wire:model="editPassword" type="password" class="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 text-sm">
@error('editPassword') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
</div>
<div>
<label class="block text-xs font-medium text-gray-600">Conferma password</label>
<input wire:model="editPassword_confirmation" type="password" class="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 text-sm">
</div>
<div>
<label class="block text-xs font-medium text-gray-600">Ruolo</label>
<select wire:model="editSelectedRole" class="mt-1 block w-full rounded-lg border-gray-300 shadow-sm focus:ring-indigo-500 focus:border-indigo-500 text-sm">
@foreach($availableRoles as $role)
<option value="{{ $role }}">{{ ucfirst($role) }}</option>
@endforeach
</select>
@error('editSelectedRole') <p class="text-red-500 text-xs mt-1">{{ $message }}</p> @enderror
</div>
</div>
<div class="mt-3 flex gap-2">
<button wire:click="updateUser" type="button" class="px-3 py-1.5 text-xs font-medium text-white bg-indigo-600 rounded-lg hover:bg-indigo-700">Salva</button>
<button wire:click="cancelEdit" type="button" class="px-3 py-1.5 text-xs font-medium text-gray-700 bg-gray-100 rounded-lg hover:bg-gray-200">Annulla</button>
</div>
</td>
@else
<td class="px-4 py-2 text-gray-900">{{ $user->name }}</td>
<td class="px-4 py-2 text-gray-700">{{ $user->email }}</td>
<td class="px-4 py-2">
<div class="flex flex-wrap gap-1">
@forelse($user->roles as $role)
<span class="inline-flex items-center rounded-full bg-indigo-50 px-2 py-0.5 text-xs font-medium text-indigo-700">{{ $role->name }}</span>
@empty
<span class="text-xs text-gray-400">-</span>
@endforelse
</div>
</td>
<td class="px-4 py-2">
<div class="flex gap-2">
<button wire:click="startEdit({{ $user->id }})" type="button" class="px-2 py-1 text-xs font-medium text-indigo-700 bg-indigo-50 rounded hover:bg-indigo-100">Modifica</button>
<button
wire:click="deleteUser({{ $user->id }})"
onclick="if(!confirm('Confermi la cancellazione dell\'utente? I log verranno preservati.')) event.stopImmediatePropagation();"
type="button"
class="px-2 py-1 text-xs font-medium text-red-700 bg-red-50 rounded hover:bg-red-100"
>
Cancella
</button>
</div>
</td>
@endif
</tr>
@empty
<tr>