Avances con varias cosas~

This commit is contained in:
2025-01-17 08:31:59 -03:00
parent ee19e479f4
commit 5fc8e2bda1
21 changed files with 463 additions and 121 deletions

View File

@@ -0,0 +1,118 @@
<?php
namespace App\Livewire\Usuarios;
use App\Enums\RoleName;
use App\Models\Role;
use App\Models\User;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Session;
use Illuminate\Validation\Rule;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Validate;
use Livewire\Component;
use TallStackUi\Traits\Interactions;
class Edit extends Component
{
use Interactions;
#[Locked]
public ?User $user = null;
public $name = null;
public $email = null;
public $password = null;
public $password_confirmation = null;
public $roles = [];
public $change_password = false;
public function mount(): void
{
if ($this->user) {
$this->name = $this->user->name;
$this->email = $this->user->email;
$this->roles = $this->user->roles()->pluck('id')->toArray();
}
}
public function render(): View
{
return view('livewire.usuarios.edit');
}
public function rules(): array
{
return [
'name' => 'required|string',
'email' => ['required', 'email', Rule::unique('users', 'email')->ignore($this->user->id)],
'password' => $this->change_password ? 'required|string|min:8|confirmed' : 'nullable|string|min:8|confirmed',
'password_confirmation' => $this->change_password ? 'required' : 'nullable',
'roles' => 'nullable',
];
}
public function save(): void
{
$this->validate();
if ($this->user) {
$this->update();
} else {
$this->store();
}
$this->redirectRoute('usuarios.index', navigate: true);
}
public function update(): void
{
$this->authorize('update', $this->user);
$this->user->name = $this->name;
$this->user->email = $this->email;
if ($this->change_password) {
$this->user->password = Hash::make($this->password);
}
$this->user->save();
$isSuperAdmin = $this->user->roles()->where('name', RoleName::SuperAdmin)->exists();
$this->user->roles()->sync($this->roles);
if ($isSuperAdmin) {
$this->user->roles()->attach(Role::where('name', RoleName::SuperAdmin)->first()->id);
}
Session::flash('toast', ['type' => 'success', 'message' => 'Usuario modificado correctamente']);
}
public function store(): void
{
$this->authorize('create', User::class);
$user = User::create([
'name' => $this->name,
'email' => $this->email,
'password' => Hash::make($this->password),
]);
$user->roles()->sync($this->roles);
Session::flash('toast', ['type' => 'success', 'message' => 'Usuario registrado correctamente']);
}
#[Computed]
public function availableRoles(): Collection
{
return Role::whereNot('name', RoleName::SuperAdmin)->get();
}
}

View File

@@ -0,0 +1,66 @@
<?php
namespace App\Livewire\Usuarios;
use App\Enums\RoleName;
use App\Models\Turno;
use App\Models\User;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Component;
use TallStackUi\Traits\Interactions;
class Index extends Component
{
use Interactions;
public function render(): View
{
return view('livewire.usuarios.index');
}
public function confirmDelete($id): void
{
$user = User::find($id);
$this->authorize('delete', $user);
if ($user->roles()->where('name', RoleName::SuperAdmin)->exists()) {
$this->toast()->error('Error', 'No se puede eliminar un super administrador')->send();
return;
}
$this->dialog()->question('¿Esta seguro de eliminar este usuario?',
'No podra ser recuperado')
->confirm('Eliminar Usuario', method: 'doDelete', params: $id)
->cancel()
->send();
}
public function doDelete($id): void
{
$user = User::find($id);
$this->authorize('delete', $user);
$user->delete();
$this->toast()->success('Usuario eliminado correctamente')->send();
}
#[Computed]
public function headers(): array
{
return [
['index' => 'name', 'label' => 'Nombre'],
['index' => 'email', 'label' => 'Email'],
['index' => 'action', 'label' => 'Acciones'],
];
}
#[Computed]
public function rows(): LengthAwarePaginator
{
return User::paginate();
}
}