diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index d29678c..d3ddf3a 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers; use App\Models\Account; +use App\Models\User; +use App\Models\Role; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use App\Http\UserBalanceDTO; @@ -37,6 +39,32 @@ private function generarCbuAleatorio() return substr(str_shuffle(str_repeat('0123456789', 3)), 0, 22); } + + + + public function getUserAccounts(Request $request, $user_id) +{ + // Obtiene el usuario autenticado + $user = $request->user(); + + // Obtiene el rol de administrador dinámicamente + $adminRole = Role::where('name', 'ADMIN')->first(); + + // Verifica si el usuario autenticado tiene el rol de administrador + if ($user && $user->role_id !== $adminRole->id) { + return response()->json(['message' => "No tiene permiso para acceder a esta función"], 403); + } + + // Busca las cuentas asociadas al usuario con el ID proporcionado + $accounts = Account::where('user_id', $user_id)->get(); + + if ($accounts->isEmpty()) { + return response()->json(['message' => "No se encontraron cuentas asociadas a este usuario"], 404); + } + + return response()->json(['accounts' => $accounts], 200); +} + // Proporciona el balance de la cuenta del usuario mediante DTO UserBalance y transforma en Array el resultado public function balance() { diff --git a/app/Http/Controllers/AuthController.php b/app/Http/Controllers/AuthController.php index 2ebcd47..397752b 100644 --- a/app/Http/Controllers/AuthController.php +++ b/app/Http/Controllers/AuthController.php @@ -104,7 +104,7 @@ public function login(Request $request) return response()->ok(['token' => $token, 'user' => $user]); //respuesta mostrando el token y el usuario } - return response()->json(['error' => 'Usuario no encontrado'], 404); //manejo de errores si el usuario no se encuentra + return response()->json(['error' => 'Usuario no encontrado'], 404); } } diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php index 494c050..fc223c1 100644 --- a/app/Http/Kernel.php +++ b/app/Http/Kernel.php @@ -13,6 +13,12 @@ class Kernel extends HttpKernel * * @var array */ + + protected $routeMiddleware = [ + 'auth' => \App\Http\Middleware\Authenticate::class, + 'admin' => \App\Http\Middleware\AdminMiddleware::class, + ]; + protected $middleware = [ // \App\Http\Middleware\TrustHosts::class, \App\Http\Middleware\TrustProxies::class, @@ -21,6 +27,7 @@ class Kernel extends HttpKernel \Illuminate\Foundation\Http\Middleware\ValidatePostSize::class, \App\Http\Middleware\TrimStrings::class, \Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class, + ]; /** diff --git a/app/Http/Middleware/AdminMiddleware.php b/app/Http/Middleware/AdminMiddleware.php new file mode 100644 index 0000000..099015d --- /dev/null +++ b/app/Http/Middleware/AdminMiddleware.php @@ -0,0 +1,22 @@ +role_id === 2) { + return $next($request); + } + + // Si el usuario no es administrador, puedes redirigir o devolver una respuesta de error + return response()->json(['error' => 'Acceso no autorizado'], 403); + } +} diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php index d4ef644..da7d339 100644 --- a/app/Http/Middleware/Authenticate.php +++ b/app/Http/Middleware/Authenticate.php @@ -7,11 +7,10 @@ class Authenticate extends Middleware { - /** - * Get the path the user should be redirected to when they are not authenticated. - */ - protected function redirectTo(Request $request): ?string + public function redirectTo($request) { - return $request->expectsJson() ? null : route('login'); + if (!$request->expectsJson()) { + return route('auth.login'); // Asegúrate de que 'login' coincida con el nombre de tu ruta de inicio de sesión. + } } } diff --git a/routes/api.php b/routes/api.php index 7309fda..2549f51 100644 --- a/routes/api.php +++ b/routes/api.php @@ -6,6 +6,7 @@ use App\Http\Controllers\AccountController; use App\Http\Controllers\TransactionController; use App\Http\Controllers\PaymentController; +use App\Http\Middleware\AdminMiddleware; use Illuminate\Http\Request; @@ -35,7 +36,12 @@ // SOLICITUD GET a /users: Ruta para traer todos los usuarios (Solo ADMIN) Route::get('/users', [UserController::class, 'index']); + + }); + + //RUTA listar cuentas de usuarios segun su id + Route::get('/accounts/{user_id}', [AccountController::class, 'getUserAccounts'])->middleware([AdminMiddleware::class]); // SOLICITUD POST a /accounts: Ruta para crear una cuenta en ARS o USD Route::post('/accounts', [AccountController::class, 'createAccount']);