Refactor de login y home para que dependan del UserRole

This commit is contained in:
Alejandro Tasistro 2025-05-15 20:19:07 -03:00
parent 2e78d39f12
commit a9e9966c93
7 changed files with 50 additions and 22 deletions

View file

@ -31,14 +31,7 @@ class LoginController extends Controller
protected function authenticated(Request $request, $user)
{
if ($user->is_compras) {
return redirect('compras/pedidos');
} else if ($user->is_admin) {
session(['admin_gdc' => $user->grupo_de_compra_id]);
return redirect('admin/pedidos');
} else {
return redirect('/');
}
return redirect('/');
}
/**

View file

@ -0,0 +1,30 @@
<?php
namespace App\Http\Controllers;
use App\UserRole;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class RouteController extends Controller
{
function home(Request $request) {
if (!Auth::check())
return redirect('/login');
$barrio = UserRole::where('nombre', 'barrio')->first();
$admin = UserRole::where('nombre', 'admin_barrio')->first();
$comision = UserRole::where('nombre', 'comision')->first();
switch ($request->user()->role_id) {
case $barrio->id:
return redirect('/productos');
case $admin->id:
return redirect('/admin');
case $comision->id:
return redirect('/compras');
default:
abort(400, 'Rol de usuario invalido');
}
}
}

View file

@ -3,6 +3,7 @@
namespace App\Http\Middleware;
use Illuminate\Auth\Middleware\Authenticate as Middleware;
use Illuminate\Support\Facades\Auth;
class Authenticate extends Middleware
{
@ -14,7 +15,12 @@ class Authenticate extends Middleware
*/
protected function redirectTo($request)
{
if (! $request->expectsJson()) {
if (!$request->expectsJson()) {
$path = $request->path();
if (preg_match('~^admin.*~i', $path))
return route('admin.login');
if (preg_match('~^compras.*~i', $path))
return route('compras.login');
return route('login');
}
}

View file

@ -22,7 +22,7 @@
</div>
@enderror
<comunes-region-select v-bind:admin="true"></comunes-region-select>
<form method="post" action="login">
<form method="post" action="/login">
@csrf
<comunes-barrio-select v-bind:admin="true"></comunes-barrio-select>
<admin-login></admin-login>

View file

@ -22,7 +22,7 @@
Contraseña incorrecta, intentalo nuevamente.
</div>
@enderror
<form method="post" action="login">
<form method="post" action="/login">
@csrf
<compras-login></compras-login>
</form>

View file

@ -24,7 +24,7 @@
</div>
@enderror
<comunes-region-select></comunes-region-select>
<form method="post" action="login">
<form method="post" action="/login">
@csrf
<comunes-barrio-select></comunes-barrio-select>
<pedidos-login></pedidos-login>

View file

@ -20,13 +20,11 @@ if (App::environment('production')) {
URL::forceScheme('https');
}
Route::get('/', 'ProductoController@index')->name('productos');
Route::get('/', 'RouteController@home')->name('home');
Auth::routes(['register' => false]);
Route::get('/productos', 'ProductoController@index')->name('productos.index');
Route::get('/admin', 'AdminController@show')->name('admin_login.show');
Route::get('/admin/login', 'AdminController@show')->name('admin.login');
Route::get('/admin/obtener_sesion', function() {
return [
@ -34,8 +32,8 @@ Route::get('/admin/obtener_sesion', function() {
];
})->name('admin_obtener_sesion');
Route::middleware(['auth', 'admin'])->group( function () {
Route::get('/admin/pedidos', 'AdminController@index')->name('admin_login.index');
Route::middleware(['auth', 'role:admin_barrio'])->group( function () {
Route::get('/admin', 'AdminController@index')->name('admin.pedidos');
Route::get('/admin/exportar-planillas-a-pdf/{gdc}', 'AdminController@exportarPedidosAPdf');
@ -44,7 +42,8 @@ Route::middleware(['auth', 'admin'])->group( function () {
Route::get('/admin/exportar-pedido-con-nucleos-a-csv/{gdc}', 'AdminController@exportarPedidoConNucleosACSV');
});
Route::middleware('auth')->group( function() {
Route::middleware(['auth', 'role:barrio'])->group( function() {
Route::get('/productos', 'ProductoController@index')->name('productos.index');
Route::name('subpedidos.')->prefix("subpedidos")->group( function() {
Route::get('/', function() {
@ -77,10 +76,10 @@ Route::middleware('auth')->group( function() {
});
});
Route::get('/compras', 'ComprasController@show')->name('compras_login.show');
Route::get('/compras/login', 'ComprasController@show')->name('compras.login');
Route::middleware(['compras'])->group( function() {
Route::get('/compras/pedidos', 'ComprasController@indexPedidos')->name('compras.pedidos');
Route::middleware(['auth', 'role:comision'])->group( function() {
Route::get('/compras', 'ComprasController@indexPedidos')->name('compras.pedidos');
Route::get('/compras/pedidos/descargar', 'ComprasController@descargarPedidos')->name('compras.pedidos.descargar');
Route::get('/compras/pedidos/notas', 'ComprasController@descargarNotas')->name('compras.pedidos.descargar');
Route::get('/compras/pedidos/pdf', 'ComprasController@pdf')->name('compras.pedidos.pdf');