Mejoras generales .w. se me olvida que agrego

This commit is contained in:
2021-07-20 00:36:50 -04:00
parent 19207b89f6
commit 7fa39a8b11
9 changed files with 88 additions and 18 deletions

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Exceptions;
use Exception;
class NotAuthorizedException extends Exception {
protected $user;
public function __construct($user) {
$this->user = $user;
}
public function render($request) {
$path = $request->getPathInfo();
return response()->json([
'error' => 'not_authorized',
'message' => 'El usuario ' . $this->user->id . ' no tiene permiso para acceder al endpoint ' . $path
], 401);
}
}

View File

@@ -19,7 +19,11 @@ class RestaurantesController extends Controller {
* Obtiene de forma paginada los restaurantes registrados en el backend
*/
public function all(Request $request) {
$restaurantes = Restaurante::all();
if($request->user->isGlobalAdmin()) {
$restaurantes = Restaurante::all();
} else {
$restaurantes = $request->user->restaurantes;
}
$paginate = app(PaginatorService::class)->paginate(
perPage: $request->input('per_page', 15),
@@ -100,7 +104,6 @@ class RestaurantesController extends Controller {
if($restaurant->zonasProduccion()->count() > 0) throw new CantDeleteHasChildException("restaurant", "zona_produccion");
if($restaurant->categorias()->count() > 0) throw new CantDeleteHasChildException("restaurant", "categoria");
$restaurant->delete();
return response()->json([], 204);
}

View File

@@ -49,6 +49,13 @@ class UsuariosController extends Controller {
return response()->json($usuario);
}
/**
* Se obtiene al usuario logeado
*/
public function getMe(Request $request) {
return response()->json($request->user);
}
/**
* Crea un nuevo usuario localmente y en auth0
*/

View File

@@ -0,0 +1,16 @@
<?php
namespace App\Http\Middleware;
use Closure;
use App\Exceptions\NotAuthorizedException;
class RoleMiddleware {
public function handle($request, Closure $next, $role) {
if(!$request->user->hasRole($role)) {
throw new NotAuthorizedException($request->user);
}
return $next($request);
}
}

View File

@@ -22,14 +22,22 @@ class Restaurante extends Model {
return $restaurante;
}
public function usuarios() {
return $this->belongsToMany(Usuario::class, 'usuarios_restaurantes', 'restaurante_id', 'usuario_id');
}
public function canalesVenta() {
return $this->hasMany(CanalVenta::class, 'restaurante_id');
}
public function categorias() {
return $this->hasMany(Categoria::class, 'restaurante_id');
}
public function compras() {
return $this->hasMany(Compra::class, 'restaurante_id');
}
public function usuarios() {
return $this->belongsToMany(Usuario::class, 'usuarios_restaurantes', 'restaurante_id', 'usuario_id');
}
public function sectores() {
return $this->hasMany(Sector::class, 'restaurante_id');
}
@@ -38,10 +46,6 @@ class Restaurante extends Model {
return $this->hasMany(ZonaProduccion::class, 'restaurante_id');
}
public function categorias() {
return $this->hasMany(Categoria::class, 'restaurante_id');
}
public function proveedores() {
return $this->hasMany(Proveedor::class, 'restaurante_id');
}
@@ -54,7 +58,19 @@ class Restaurante extends Model {
return $this->hasMany(Producto::class, 'restaurante_id');
}
public function compras() {
return $this->hasMany(Compra::class, 'restaurante_id');
public function ventas() {
return $this->hasMany(Venta::class, 'restaurante_id');
}
public function boletasElectronicas() {
return $this->hasMany(BoletaElectronica::class, 'restaurante_id');
}
public function boletasExentas() {
return $this->hasMany(BoletaExenta::class, 'restaurante_id');
}
public function cajas() {
return $this->hasMany(Caja::class, 'restaurante_id');
}
}

View File

@@ -32,11 +32,15 @@ class Usuario extends Model {
}
public function isGlobalAdmin() {
return in_array('global_admin', $this->roles);
return $this->hasRole('global_admin');
}
public function isAdmin() {
return in_array('admin', $this->roles);
return $this->hasRole('admin');
}
public function hasRole($role) {
return in_array($role, $this->roles);
}
public function restaurantes() {