Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Smp1 16 #15

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
4f101e3
...
javiercavalero Nov 18, 2023
5b67b87
SMP1-16
javiercavalero Nov 21, 2023
b34f23c
SMP1-16 modifica la funcion getUserAccounts y elimina middleware de a…
javiercavalero Nov 22, 2023
c812d18
SMP1-16
javiercavalero Nov 24, 2023
ec0d866
SMP1-16
javiercavalero Nov 24, 2023
a4d3d90
SMP1-16
javiercavalero Nov 24, 2023
566ed70
SMP1-16 arreglo de enrutamiento
javiercavalero Nov 24, 2023
6ca9409
SMP1-11 agrega endpoint de autenticacion de usuario
javiercavalero Nov 11, 2023
4f5aeee
SMP1-12 Endpoint para eliminar un usuario
fabianloza Nov 14, 2023
1db1960
...
javiercavalero Nov 18, 2023
de78329
...
javiercavalero Nov 18, 2023
938348a
SMP1-17 Endpoint para crear una cuenta
fabianloza Nov 16, 2023
f32f75f
SMP1-20 Endpoint para realizar un depósito
fabianloza Nov 16, 2023
abe5690
SMP1-13
jorgeMartinLorente Nov 16, 2023
dd5fd51
cambios solicitud index
jorgeMartinLorente Nov 17, 2023
249fa8b
SMP1-16 modifica la funcion getUserAccounts y elimina middleware de a…
javiercavalero Nov 22, 2023
950f10b
SMP1-16 modifica la funcion getUserAccounts y elimina middleware de a…
javiercavalero Nov 22, 2023
cc0cec3
SMP1-16
javiercavalero Nov 24, 2023
ebc1b6f
SMP1-21 Endpoint para realizar un pago
fabianloza Nov 18, 2023
59f0823
Actualización del README.md
fabianloza Nov 18, 2023
1008854
SMP1-19 Creacion DTO balance + rutas y metodo en AccountController
Nov 18, 2023
6c2aa36
SMP1-18 Endpoint para enviar dinero
fabianloza Nov 22, 2023
317054c
SMP1-24
jorgeMartinLorente Nov 21, 2023
542ebd1
SMP1-23
jorgeMartinLorente Nov 22, 2023
2577d61
SMP1-29 Endpoint para listar transacciones
fabianloza Nov 22, 2023
a6b9b57
SMP1-16
javiercavalero Nov 24, 2023
d8d43e6
SMP1-16 arreglo de enrutamiento
javiercavalero Nov 24, 2023
e4c01f1
SMP1-25 Creacion de seeder datos para el modelo Transaction
Nov 23, 2023
96239c3
SMP1-31 Endpoint para editar una transacción
fabianloza Nov 23, 2023
faa9286
SMP1-32 Endpoint para actualizar una cuenta
fabianloza Nov 23, 2023
105d031
SMP1-16
javiercavalero Nov 24, 2023
75a7fc5
SMP1-16 resuelve conflictos
javiercavalero Nov 24, 2023
8db33c0
SMP1-16 resuelve conflictos
javiercavalero Nov 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions app/Http/Controllers/AccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

7 changes: 7 additions & 0 deletions app/Http/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class Kernel extends HttpKernel
*
* @var array<int, class-string|string>
*/

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,
Expand All @@ -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,

];

/**
Expand Down
22 changes: 22 additions & 0 deletions app/Http/Middleware/AdminMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
use Illuminate\Support\Facades\Auth;

class AdminMiddleware
{
public function handle($request, Closure $next)
{
// Verificar si el usuario autenticado tiene el rol de administrador
if (Auth::check() && Auth::user()->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);
}
}
9 changes: 4 additions & 5 deletions app/Http/Middleware/Authenticate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}
}
}
6 changes: 6 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand Down Expand Up @@ -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']);
Expand Down