Actualización de modelos, controladores y middleware
This commit is contained in:
@@ -2,12 +2,15 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\AccionLog;
|
||||
use App\Models\CredencialCliente;
|
||||
use App\Models\CredencialProfesional;
|
||||
use App\Models\LogSeguridad;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\RateLimiter;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class AuthController extends Controller
|
||||
@@ -17,24 +20,65 @@ class AuthController extends Controller
|
||||
$request->validate([
|
||||
'correo' => ['required', 'string'],
|
||||
'contra' => ['required', 'string'],
|
||||
'website' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
if (!$this->honeypotValido($request)) {
|
||||
return back()
|
||||
->withInput($request->except(['contra', 'website']))
|
||||
->withErrors(['login_error' => 'No se pudo procesar el inicio de sesión.']);
|
||||
}
|
||||
|
||||
$correo = trim((string) $request->input('correo'));
|
||||
$contra = (string) $request->input('contra');
|
||||
|
||||
$credencial = CredencialCliente::where('correo', $correo)->first();
|
||||
$credencial = CredencialCliente::with('cliente.persona.telefonos')->where('correo', $correo)->first();
|
||||
if (!$credencial || !$this->credencialValida($contra, (string) $credencial->contra)) {
|
||||
return back()
|
||||
->withInput($request->except('contra'))
|
||||
->with('login_error', 'Usuario o contraseña incorrectos.');
|
||||
}
|
||||
|
||||
if ((int) ($credencial->cliente?->baja_id ?? 0) !== 1) {
|
||||
return back()
|
||||
->withInput($request->except('contra'))
|
||||
->with('login_error', 'No es posible iniciar sesion. Comuníquese con un profesional de Abogadas del Litoral');
|
||||
}
|
||||
|
||||
$token = Str::random(64);
|
||||
$credencial->token = $token;
|
||||
$credencial->fecha_hora = now();
|
||||
$credencial->save();
|
||||
|
||||
return back()->with('login_success', 'Login exitoso.');
|
||||
$personaCliente = $credencial->cliente?->persona;
|
||||
$nombreCliente = trim((string) ($personaCliente?->nombre ?? ''));
|
||||
$apellidoCliente = trim((string) ($personaCliente?->apellido ?? ''));
|
||||
$celularCliente = trim((string) ($personaCliente?->telefonos?->first()?->telefono ?? ''));
|
||||
|
||||
$request->session()->regenerate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
$request->session()->put([
|
||||
'cliente_auth' => true,
|
||||
'cliente_id' => (int) ($credencial->cliente?->id ?? 0),
|
||||
'cliente_token' => $token,
|
||||
'cliente_correo' => (string) $credencial->correo,
|
||||
'cliente_nombre' => $nombreCliente !== '' ? $nombreCliente : (string) $credencial->correo,
|
||||
'cliente_apellido' => $apellidoCliente,
|
||||
'cliente_celular' => $celularCliente,
|
||||
]);
|
||||
|
||||
$this->registrarAccionSeguridad(
|
||||
(int) ($credencial->cliente?->persona_id ?? 0),
|
||||
'CLIENTE',
|
||||
(string) $request->ip(),
|
||||
'Inició sesión',
|
||||
'El cliente ID ' . (int) ($credencial->cliente?->id ?? 0) . ' inició sesión.'
|
||||
);
|
||||
|
||||
$this->limpiarThrottle($request, 'login-cliente-web');
|
||||
|
||||
return redirect('/cliente/dashboard')->with('login_success', 'Login exitoso.');
|
||||
}
|
||||
|
||||
public function loginPersonalWeb(Request $request): RedirectResponse
|
||||
@@ -42,24 +86,75 @@ class AuthController extends Controller
|
||||
$request->validate([
|
||||
'usuario' => ['required', 'string'],
|
||||
'contra' => ['required', 'string'],
|
||||
'website' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
if (!$this->honeypotValido($request)) {
|
||||
return back()
|
||||
->withInput($request->except(['contra', 'website']))
|
||||
->withErrors(['login_error' => 'No se pudo procesar el inicio de sesión.']);
|
||||
}
|
||||
|
||||
$usuario = trim((string) $request->input('usuario'));
|
||||
$contra = (string) $request->input('contra');
|
||||
|
||||
$credencial = CredencialProfesional::where('usuario', $usuario)->first();
|
||||
$credencial = CredencialProfesional::with(['administrador.persona', 'profesional.persona'])
|
||||
->where('usuario', $usuario)
|
||||
->first();
|
||||
if (!$credencial || !$this->credencialValida($contra, (string) $credencial->contra)) {
|
||||
return back()
|
||||
->withInput($request->except('contra'))
|
||||
->with('login_error', 'Usuario o contraseña incorrectos.');
|
||||
}
|
||||
|
||||
if (strtoupper((string) $credencial->rol) !== 'ADMIN' && (int) ($credencial->profesional?->baja_id ?? 0) !== 1) {
|
||||
return back()
|
||||
->withInput($request->except('contra'))
|
||||
->with('login_error', 'Usted está dado de baja. Comuníquese con el administrador');
|
||||
}
|
||||
|
||||
$token = Str::random(64);
|
||||
$credencial->token = $token;
|
||||
$credencial->fecha_hora = now();
|
||||
$credencial->save();
|
||||
|
||||
return back()->with('login_success', 'Login exitoso.');
|
||||
$rol = strtoupper((string) $credencial->rol);
|
||||
$personaProfesional = $credencial->profesional?->persona;
|
||||
$nombreProfesional = trim((string) ($personaProfesional?->nombre ?? '') . ' ' . (string) ($personaProfesional?->apellido ?? ''));
|
||||
$personaAdmin = $credencial->administrador?->persona;
|
||||
$nombreAdmin = trim((string) ($personaAdmin?->nombre ?? '') . ' ' . (string) ($personaAdmin?->apellido ?? ''));
|
||||
$nombreSesion = $rol === 'ADMIN' ? $nombreAdmin : $nombreProfesional;
|
||||
|
||||
$request->session()->regenerate();
|
||||
$request->session()->regenerateToken();
|
||||
|
||||
$request->session()->put([
|
||||
'personal_auth' => true,
|
||||
'personal_token' => $token,
|
||||
'personal_usuario' => (string) $credencial->usuario,
|
||||
'personal_nombre' => $nombreSesion !== '' ? $nombreSesion : (string) $credencial->usuario,
|
||||
'personal_apellido' => $rol === 'ADMIN'
|
||||
? ($personaAdmin?->apellido ?? '')
|
||||
: ($personaProfesional?->apellido ?? ''),
|
||||
'personal_rol' => $rol,
|
||||
'personal_credencial_id' => (int) $credencial->id,
|
||||
]);
|
||||
|
||||
$this->registrarAccionSeguridad(
|
||||
$rol === 'ADMIN'
|
||||
? (int) ($credencial->administrador?->persona_id ?? 0)
|
||||
: (int) ($credencial->profesional?->persona_id ?? 0),
|
||||
$rol === 'ADMIN' ? 'ADMIN' : 'PROFESIONAL',
|
||||
(string) $request->ip(),
|
||||
'Inició sesión',
|
||||
($rol === 'ADMIN' ? 'La administradora ID ' . (int) ($credencial->administrador?->id ?? 0) : 'El profesional ID ' . (int) ($credencial->profesional?->id ?? 0)) . ' inició sesión.'
|
||||
);
|
||||
|
||||
$this->limpiarThrottle($request, 'login-personal-web');
|
||||
|
||||
$redirectPath = $rol === 'ADMIN' ? '/administrador/dashboard' : '/profesional/dashboard';
|
||||
|
||||
return redirect($redirectPath)->with('login_success', 'Login exitoso.');
|
||||
}
|
||||
|
||||
public function loginCliente(Request $request): JsonResponse
|
||||
@@ -72,7 +167,7 @@ class AuthController extends Controller
|
||||
$correo = trim((string) $request->input('correo'));
|
||||
$contra = (string) $request->input('contra');
|
||||
|
||||
$credencial = CredencialCliente::where('correo', $correo)->first();
|
||||
$credencial = CredencialCliente::with('cliente')->where('correo', $correo)->first();
|
||||
if (!$credencial || !$this->credencialValida($contra, (string) $credencial->contra)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
@@ -80,11 +175,28 @@ class AuthController extends Controller
|
||||
], 401);
|
||||
}
|
||||
|
||||
if ((int) ($credencial->cliente?->baja_id ?? 0) !== 1) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No es posible iniciar sesion. Comuníquese con un profesional de Abogadas del Litoral',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$token = Str::random(64);
|
||||
$credencial->token = $token;
|
||||
$credencial->fecha_hora = now();
|
||||
$credencial->save();
|
||||
|
||||
$this->registrarAccionSeguridad(
|
||||
(int) ($credencial->cliente?->persona_id ?? 0),
|
||||
'CLIENTE',
|
||||
(string) $request->ip(),
|
||||
'Inició sesión',
|
||||
'El cliente ID ' . (int) ($credencial->cliente?->id ?? 0) . ' inició sesión.'
|
||||
);
|
||||
|
||||
$this->limpiarThrottle($request, 'login-cliente-api');
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
@@ -106,7 +218,7 @@ class AuthController extends Controller
|
||||
$usuario = trim((string) $request->input('usuario'));
|
||||
$contra = (string) $request->input('contra');
|
||||
|
||||
$credencial = CredencialProfesional::where('usuario', $usuario)->first();
|
||||
$credencial = CredencialProfesional::with(['administrador', 'profesional'])->where('usuario', $usuario)->first();
|
||||
if (!$credencial || !$this->credencialValida($contra, (string) $credencial->contra)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
@@ -114,11 +226,32 @@ class AuthController extends Controller
|
||||
], 401);
|
||||
}
|
||||
|
||||
if (strtoupper((string) $credencial->rol) !== 'ADMIN' && (int) ($credencial->profesional?->baja_id ?? 0) !== 1) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Usted está dado de baja. Comuníquese con el administrador',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$token = Str::random(64);
|
||||
$credencial->token = $token;
|
||||
$credencial->fecha_hora = now();
|
||||
$credencial->save();
|
||||
|
||||
$rol = strtoupper((string) $credencial->rol);
|
||||
|
||||
$this->registrarAccionSeguridad(
|
||||
$rol === 'ADMIN'
|
||||
? (int) ($credencial->administrador?->persona_id ?? 0)
|
||||
: (int) ($credencial->profesional?->persona_id ?? 0),
|
||||
$rol === 'ADMIN' ? 'ADMIN' : 'PROFESIONAL',
|
||||
(string) $request->ip(),
|
||||
'Inició sesión',
|
||||
($rol === 'ADMIN' ? 'La administradora ID ' . (int) ($credencial->administrador?->id ?? 0) : 'El profesional ID ' . (int) ($credencial->profesional?->id ?? 0)) . ' inició sesión.'
|
||||
);
|
||||
|
||||
$this->limpiarThrottle($request, 'login-personal-api');
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
@@ -147,13 +280,13 @@ class AuthController extends Controller
|
||||
$tipoDetectado = null;
|
||||
|
||||
if ($tipo === 'cliente') {
|
||||
$credencial = CredencialCliente::where('correo', $identificador)->first();
|
||||
$credencial = CredencialCliente::with('cliente')->where('correo', $identificador)->first();
|
||||
$tipoDetectado = 'cliente';
|
||||
} elseif ($tipo === 'profesional') {
|
||||
$credencial = CredencialProfesional::where('usuario', $identificador)->first();
|
||||
$tipoDetectado = 'personal';
|
||||
} else {
|
||||
$credencial = CredencialCliente::where('correo', $identificador)->first();
|
||||
$credencial = CredencialCliente::with('cliente')->where('correo', $identificador)->first();
|
||||
$tipoDetectado = $credencial ? 'cliente' : null;
|
||||
|
||||
if (!$credencial) {
|
||||
@@ -169,11 +302,52 @@ class AuthController extends Controller
|
||||
], 401);
|
||||
}
|
||||
|
||||
if ($credencial instanceof CredencialCliente
|
||||
&& (int) ($credencial->cliente?->baja_id ?? 0) !== 1) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No es posible iniciar sesion. Comuníquese con un profesional de Abogadas del Litoral',
|
||||
], 403);
|
||||
}
|
||||
|
||||
if ($credencial instanceof CredencialProfesional
|
||||
&& strtoupper((string) $credencial->rol) !== 'ADMIN'
|
||||
&& (int) ($credencial->profesional?->baja_id ?? 0) !== 1) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Usted está dado de baja. Comuníquese con el administrador',
|
||||
], 403);
|
||||
}
|
||||
|
||||
$token = Str::random(64);
|
||||
$credencial->token = $token;
|
||||
$credencial->fecha_hora = now();
|
||||
$credencial->save();
|
||||
|
||||
if ($credencial instanceof CredencialCliente) {
|
||||
$this->registrarAccionSeguridad(
|
||||
(int) ($credencial->cliente?->persona_id ?? 0),
|
||||
'CLIENTE',
|
||||
(string) $request->ip(),
|
||||
'Inició sesión',
|
||||
'El cliente ID ' . (int) ($credencial->cliente?->id ?? 0) . ' inició sesión.'
|
||||
);
|
||||
} elseif ($credencial instanceof CredencialProfesional) {
|
||||
$rolDescripcion = strtoupper((string) $credencial->rol) === 'ADMIN' ? 'ADMIN' : 'PROFESIONAL';
|
||||
|
||||
$this->registrarAccionSeguridad(
|
||||
$rolDescripcion === 'ADMIN'
|
||||
? (int) ($credencial->administrador?->persona_id ?? 0)
|
||||
: (int) ($credencial->profesional?->persona_id ?? 0),
|
||||
$rolDescripcion,
|
||||
(string) $request->ip(),
|
||||
'Inició sesión',
|
||||
($rolDescripcion === 'ADMIN' ? 'La administradora ID ' . (int) ($credencial->administrador?->id ?? 0) : 'El profesional ID ' . (int) ($credencial->profesional?->id ?? 0)) . ' inició sesión.'
|
||||
);
|
||||
}
|
||||
|
||||
$this->limpiarThrottle($request, 'login-api-general');
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => [
|
||||
@@ -186,6 +360,11 @@ class AuthController extends Controller
|
||||
], 200);
|
||||
}
|
||||
|
||||
private function honeypotValido(Request $request): bool
|
||||
{
|
||||
return trim((string) $request->input('website', '')) === '';
|
||||
}
|
||||
|
||||
public function logout(Request $request): JsonResponse
|
||||
{
|
||||
$token = (string) $request->input('token', '');
|
||||
@@ -196,8 +375,16 @@ class AuthController extends Controller
|
||||
], 422);
|
||||
}
|
||||
|
||||
$credencialCliente = CredencialCliente::where('token', $token)->first();
|
||||
$credencialCliente = CredencialCliente::with('cliente')->where('token', $token)->first();
|
||||
if ($credencialCliente) {
|
||||
$this->registrarAccionSeguridad(
|
||||
(int) ($credencialCliente->cliente?->persona_id ?? 0),
|
||||
'CLIENTE',
|
||||
(string) $request->ip(),
|
||||
'Cerró sesión',
|
||||
'El cliente ID ' . (int) ($credencialCliente->cliente?->id ?? 0) . ' cerró sesión.'
|
||||
);
|
||||
|
||||
$credencialCliente->token = null;
|
||||
$credencialCliente->fecha_hora = now();
|
||||
$credencialCliente->save();
|
||||
@@ -208,8 +395,20 @@ class AuthController extends Controller
|
||||
], 200);
|
||||
}
|
||||
|
||||
$credencialProfesional = CredencialProfesional::where('token', $token)->first();
|
||||
$credencialProfesional = CredencialProfesional::with(['administrador', 'profesional'])->where('token', $token)->first();
|
||||
if ($credencialProfesional) {
|
||||
$rol = strtoupper((string) $credencialProfesional->rol);
|
||||
|
||||
$this->registrarAccionSeguridad(
|
||||
$rol === 'ADMIN'
|
||||
? (int) ($credencialProfesional->administrador?->persona_id ?? 0)
|
||||
: (int) ($credencialProfesional->profesional?->persona_id ?? 0),
|
||||
$rol === 'ADMIN' ? 'ADMIN' : 'PROFESIONAL',
|
||||
(string) $request->ip(),
|
||||
'Cerró sesión',
|
||||
($rol === 'ADMIN' ? 'La administradora ID ' . (int) ($credencialProfesional->administrador?->id ?? 0) : 'El profesional ID ' . (int) ($credencialProfesional->profesional?->id ?? 0)) . ' cerró sesión.'
|
||||
);
|
||||
|
||||
$credencialProfesional->token = null;
|
||||
$credencialProfesional->fecha_hora = now();
|
||||
$credencialProfesional->save();
|
||||
@@ -234,4 +433,31 @@ class AuthController extends Controller
|
||||
|
||||
return Hash::check($contraIngresada, $contraGuardada);
|
||||
}
|
||||
|
||||
private function limpiarThrottle(Request $request, string $prefijo): void
|
||||
{
|
||||
RateLimiter::clear(md5($prefijo . $request->ip()));
|
||||
}
|
||||
|
||||
private function registrarAccionSeguridad(?int $personaId, string $rol, string $ipOrigen, string $accionDescripcion, string $descripcion): void
|
||||
{
|
||||
if (!$personaId) {
|
||||
return;
|
||||
}
|
||||
|
||||
$accion = AccionLog::query()->firstWhere('descripcion', $accionDescripcion);
|
||||
|
||||
if (!$accion) {
|
||||
return;
|
||||
}
|
||||
|
||||
LogSeguridad::create([
|
||||
'descripcion' => $descripcion,
|
||||
'fechahora' => now(),
|
||||
'IPorigen' => $ipOrigen,
|
||||
'rol' => $rol,
|
||||
'persona_id' => (int) $personaId,
|
||||
'accion_id' => (int) $accion->id,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\EstadoProfesional;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class EstadoProfesionalController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$items = EstadoProfesional::all();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $items,
|
||||
'message' => 'Registros obtenidos correctamente',
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$payload = $request->only((new EstadoProfesional())->getFillable());
|
||||
$estadoProfesional = EstadoProfesional::create($payload);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $estadoProfesional,
|
||||
'message' => 'Registro creado correctamente',
|
||||
], 201);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(EstadoProfesional $estadoProfesional): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $estadoProfesional,
|
||||
'message' => 'Registro obtenido correctamente',
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, EstadoProfesional $estadoProfesional): JsonResponse
|
||||
{
|
||||
$payload = $request->only((new EstadoProfesional())->getFillable());
|
||||
$estadoProfesional->update($payload);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'data' => $estadoProfesional,
|
||||
'message' => 'Registro actualizado correctamente',
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(EstadoProfesional $estadoProfesional): JsonResponse
|
||||
{
|
||||
$estadoProfesional->delete();
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Registro eliminado correctamente',
|
||||
], 200);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user