166 lines
5.4 KiB
PHP
166 lines
5.4 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Livewire;
|
||
|
|
|
||
|
|
use Livewire\Component;
|
||
|
|
use Livewire\Attributes\Layout;
|
||
|
|
use App\Models\User;
|
||
|
|
use App\Models\Company;
|
||
|
|
use Spatie\Permission\Models\Role;
|
||
|
|
use Illuminate\Support\Facades\Auth;
|
||
|
|
use Illuminate\Support\Facades\Hash;
|
||
|
|
use Illuminate\Validation\Rules\Password;
|
||
|
|
|
||
|
|
#[Layout('layouts.app')]
|
||
|
|
class UserForm extends Component
|
||
|
|
{
|
||
|
|
public ?User $user = null;
|
||
|
|
|
||
|
|
// Información personal
|
||
|
|
public string $title = '';
|
||
|
|
public string $lastName = '';
|
||
|
|
public string $firstName = '';
|
||
|
|
|
||
|
|
// Validación
|
||
|
|
public string $userStatus = 'active';
|
||
|
|
public string $validFrom = '';
|
||
|
|
public string $validUntil = '';
|
||
|
|
public string $formPassword = '';
|
||
|
|
|
||
|
|
// Contacto
|
||
|
|
public ?int $companyId = null;
|
||
|
|
public string $address = '';
|
||
|
|
public string $phone = '';
|
||
|
|
public string $email = '';
|
||
|
|
|
||
|
|
// Permisos
|
||
|
|
public string $formRole = '';
|
||
|
|
|
||
|
|
// Notas
|
||
|
|
public string $notes = '';
|
||
|
|
|
||
|
|
// Catálogos
|
||
|
|
public $roles;
|
||
|
|
public $companies;
|
||
|
|
|
||
|
|
public function mount(?User $user = null): void
|
||
|
|
{
|
||
|
|
if (!Auth::user()->hasRole('Admin')) abort(403);
|
||
|
|
|
||
|
|
$this->roles = Role::orderBy('name')->get();
|
||
|
|
$this->companies = Company::where('estado', 'activo')->orderBy('name')->get();
|
||
|
|
$this->formRole = $this->roles->first()?->name ?? '';
|
||
|
|
|
||
|
|
if ($user && $user->exists) {
|
||
|
|
$this->user = $user;
|
||
|
|
$this->title = $user->title ?? '';
|
||
|
|
$this->lastName = $user->last_name ?? '';
|
||
|
|
$this->firstName = $user->first_name ?? '';
|
||
|
|
$this->userStatus = $user->status ?? 'active';
|
||
|
|
$this->validFrom = $user->valid_from?->format('Y-m-d') ?? '';
|
||
|
|
$this->validUntil = $user->valid_until?->format('Y-m-d') ?? '';
|
||
|
|
$this->companyId = $user->company_id;
|
||
|
|
$this->address = $user->address ?? '';
|
||
|
|
$this->phone = $user->phone ?? '';
|
||
|
|
$this->email = $user->email;
|
||
|
|
$this->notes = $user->notes ?? '';
|
||
|
|
$this->formRole = $user->roles->first()?->name ?? $this->formRole;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
protected function rules(): array
|
||
|
|
{
|
||
|
|
$id = $this->user?->id ?? 'NULL';
|
||
|
|
$rules = [
|
||
|
|
'lastName' => 'required|string|max:100',
|
||
|
|
'firstName' => 'required|string|max:100',
|
||
|
|
'title' => 'nullable|string|max:20',
|
||
|
|
'userStatus' => 'required|in:active,inactive,suspended',
|
||
|
|
'validFrom' => 'nullable|date',
|
||
|
|
'validUntil' => 'nullable|date|after_or_equal:validFrom',
|
||
|
|
'companyId' => 'required|exists:companies,id',
|
||
|
|
'address' => 'nullable|string',
|
||
|
|
'phone' => 'nullable|string|max:30',
|
||
|
|
'email' => "required|email|max:255|unique:users,email,{$id}",
|
||
|
|
'formRole' => 'required|exists:roles,name',
|
||
|
|
];
|
||
|
|
|
||
|
|
if (!$this->user) {
|
||
|
|
$rules['formPassword'] = ['required', Password::min(8)->letters()->mixedCase()->numbers()];
|
||
|
|
} elseif ($this->formPassword !== '') {
|
||
|
|
$rules['formPassword'] = [Password::min(8)->letters()->mixedCase()->numbers()];
|
||
|
|
}
|
||
|
|
|
||
|
|
return $rules;
|
||
|
|
}
|
||
|
|
|
||
|
|
protected $validationAttributes = [
|
||
|
|
'lastName' => 'apellidos',
|
||
|
|
'firstName' => 'nombre',
|
||
|
|
'userStatus' => 'estado',
|
||
|
|
'validFrom' => 'fecha de inicio',
|
||
|
|
'validUntil' => 'fecha de fin',
|
||
|
|
'companyId' => 'empresa',
|
||
|
|
'formPassword'=> 'contraseña',
|
||
|
|
'formRole' => 'rol',
|
||
|
|
];
|
||
|
|
|
||
|
|
public function copyCompanyAddress(): void
|
||
|
|
{
|
||
|
|
if (!$this->companyId) return;
|
||
|
|
$company = Company::find($this->companyId);
|
||
|
|
if ($company?->address) {
|
||
|
|
$this->address = $company->address;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public function save(): void
|
||
|
|
{
|
||
|
|
$this->validate();
|
||
|
|
|
||
|
|
if ($this->user && $this->user->id === Auth::id()
|
||
|
|
&& $this->user->hasRole('Admin') && $this->formRole !== 'Admin') {
|
||
|
|
$this->addError('formRole', 'No puedes quitarte el rol Admin a ti mismo.');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$fullName = trim($this->firstName . ' ' . $this->lastName);
|
||
|
|
|
||
|
|
$data = [
|
||
|
|
'name' => $fullName,
|
||
|
|
'title' => $this->title ?: null,
|
||
|
|
'first_name' => $this->firstName,
|
||
|
|
'last_name' => $this->lastName,
|
||
|
|
'status' => $this->userStatus,
|
||
|
|
'valid_from' => $this->validFrom ?: null,
|
||
|
|
'valid_until'=> $this->validUntil ?: null,
|
||
|
|
'company_id' => $this->companyId,
|
||
|
|
'address' => $this->address ?: null,
|
||
|
|
'phone' => $this->phone ?: null,
|
||
|
|
'email' => $this->email,
|
||
|
|
'notes' => $this->notes ?: null,
|
||
|
|
];
|
||
|
|
|
||
|
|
if ($this->formPassword !== '') {
|
||
|
|
$data['password'] = Hash::make($this->formPassword);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($this->user && $this->user->exists) {
|
||
|
|
$this->user->update($data);
|
||
|
|
$this->user->syncRoles([$this->formRole]);
|
||
|
|
session()->flash('notify', 'Usuario actualizado correctamente.');
|
||
|
|
} else {
|
||
|
|
$user = User::create($data);
|
||
|
|
$user->assignRole($this->formRole);
|
||
|
|
session()->flash('notify', 'Usuario creado correctamente.');
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->redirect(route('admin.users'), navigate: true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function render()
|
||
|
|
{
|
||
|
|
return view('livewire.user-form');
|
||
|
|
}
|
||
|
|
}
|