Mejoras en la api

This commit is contained in:
2021-06-16 03:40:12 -04:00
parent 10f464eced
commit 64c05456af
10 changed files with 160 additions and 221 deletions

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Exceptions;
use Exception;
class GenericException extends Exception {
protected $error;
protected $message;
protected $status;
public function __construct($error, $message, $status) {
$this->error = $error;
$this->message = $message;
$this->status = $status;
}
public function render($request) {
return response()->json([
'error' => $this->error
'message' => $this->message
], $status);
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace App\Exceptions;
use Exception;
class InvalidUuidException extends Exception {
protected $uuid;
public function __construct($uuid) {
$this->uuid = $uuid;
}
public function render($request) {
return response()->json([
'error' => 'invalid_uuid',
'message' => 'El id ' . $this->uuid . ' no es un UUID valido'
], 400);
}
}

View File

@@ -0,0 +1,23 @@
<?php
namespace App\Exceptions;
use Exception;
class ModelNotFoundException extends Exception {
protected $modelName;
protected $id;
public function __construct($modelName, $id) {
$this->modelName= $modelName;
$this->id = $id;
}
public function render($request) {
return response()->json([
'error' => $this->modelName . '_not_found',
'message' => 'El ' . $this->modelName . ' con id ' . $this->id . ' no existe'
], 404);
}
}

View File

@@ -21,21 +21,8 @@ class CanalesVentaController extends Controller {
* @return JsonResponse
*/
public function all(Request $request, $restaurante_id) {
if (!app(UuidService::class)->is_valid($restaurante_id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $restaurante_id . ' no es un UUID valido'
], 404);
}
$restaurante = Restaurante::findOrNull($restaurante_id);
if (!$restaurante) {
return response()->json([
'error' => 'restaurant_not_found',
'message' => 'El restaurant con id ' . $restaurante_id . ' no existe'
], 404);
}
app(UuidService::class)->validOrFail($restaurante_id);
$restaurante = Restaurante::findOrFail($restaurante_id);
$canalesVenta = $restaurante->canalesVenta();
@@ -61,36 +48,11 @@ class CanalesVentaController extends Controller {
* @return JsonResponse
*/
public function get($restaurante_id, $id) {
if (!app(UuidService::class)->is_valid($id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $id . ' no es un UUID valido'
], 404);
}
app(UuidService::class)->validOrFail($id);
app(UuidService::class)->validOrFail($restaurante_id);
if (!app(UuidService::class)->is_valid($restaurante_id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $restaurante_id . ' no es un UUID valido'
], 404);
}
$restaurante = Restaurante::findOrNull($restaurante_id);
if (!$restaurante) {
return response()->json([
'error' => 'restaurant_not_found',
'message' => 'El restaurant con id ' . $restaurante_id . ' no existe'
], 404);
}
$canalVenta = CanalVenta::findOrNull($id);
if (!$canalVenta) {
return response()->json([
'error' => 'canal_venta_not_found',
'message' => 'El canal de venta con id ' . $id . ' no existe'
], 404);
}
$restaurante = Restaurante::findOrFail($restaurante_id);
$canalVenta = CanalVenta::findOrFail($id);
return response()->json($canalVenta);
}

View File

@@ -6,6 +6,8 @@ use App\Models\Restaurante;
use App\Models\Usuario;
use App\Services\Auth0Service;
use App\Services\PaginatorService;
use App\Services\UuidService;
use App\Exceptions\GenericException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
@@ -27,33 +29,27 @@ class UsuariosController extends Controller {
route: 'users.all',
);
$data = Usuario::with('restaurantes')
->skip($paginate['from'] - 1)
->take($paginate['per_page'])->get()->all();
return response()->json([
'pagination' => $paginate,
'data' => array_values(Usuario::with('restaurantes')->skip($paginate['from'] - 1)->take($paginate['per_page'])->get()->all())
'data' => $data
]);
}
/**
* Obtiene un usuario por su id, siendo el id de auth0 o el id de la base de datos
* Obtiene un usuario por su id
* @param $id
* @return JsonResponse
*/
public function get($id) {
if (!app(UuidService::class)->is_valid($id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $id . ' no es un UUID valido'
], 404);
if (!str_starts_with($id, 'auth0')) {
app(UuidService::class)->validOrFail($id);
}
$usuario = Usuario::findByIdOrAuth0Id($id);
if (!$usuario) {
return response()->json([
'error' => 'user_not_found',
'message' => 'El usuario con id o auth0_id ' . $id . ' no existe'
], 404);
}
$usuario = Usuario::findOrFail($id);
return response()->json($usuario);
}
@@ -73,12 +69,8 @@ class UsuariosController extends Controller {
'restaurant' => 'required|exists:restaurantes,id',
]);
$restaurant = Restaurante::findOrNull($request->input('restaurant'));
$cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant);
if ($cantManageUsersOrRestaurant) {
return $cantManageUsersOrRestaurant;
}
$restaurant = Restaurante::findOrFail($request->input('restaurant'));
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
$auth0 = app(Auth0Service::class);
$auth0User = $auth0->createUser(
@@ -108,18 +100,15 @@ class UsuariosController extends Controller {
}
/**
* Actualiza un usuario, dado su id o auth0_id
* Actualiza un usuario
* @param Request $request
* @param $id
* @return JsonResponse
* @throws ValidationException
*/
public function update(Request $request, $id) {
if (!app(UuidService::class)->is_valid($id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $id . ' no es un UUID valido'
], 404);
if (!str_starts_with($id, 'auth0')) {
app(UuidService::class)->validOrFail($id);
}
$this->validate($request, [
@@ -131,19 +120,9 @@ class UsuariosController extends Controller {
'roles.*' => ['sometimes', Rule::in(['admin', 'mesero', 'recaudador', 'productor'])],
]);
$usuario = Usuario::findByIdOrAuth0Id($id);
$usuario = Usuario::findOrFail($id);
if (!$usuario) {
return response()->json([
'error' => 'not_found',
'message' => 'El usuario con id ' . $id . ' no existe'
], 404);
}
$cantManageUser = $this->cantManageUser($request->user, $usuario);
if ($cantManageUser) {
return $cantManageUser;
}
$this->canManageUserOrFail($request->user, $usuario);
$metadata = [];
if ($request->input('roles')) $metadata['roles'] = $request->input('roles');
@@ -177,28 +156,13 @@ class UsuariosController extends Controller {
* @return JsonResponse
*/
public function delete(Request $request, $id) {
if (!app(UuidService::class)->is_valid($id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $id . ' no es un UUID valido'
], 404);
if (!str_starts_with($id, 'auth0')) {
app(UuidService::class)->validOrFail($id);
}
/** @var Usuario $logged_user */
$logged_user = $request->user;
$usuario = Usuario::findByIdOrAuth0Id($id);
$usuario = Usuario::findOrFail($id);
if (!$usuario) {
return response()->json([
'error' => 'not_found',
'message' => 'El usuario con id ' . $id . ' no existe'
], 404);
}
$cantManageUser = $this->cantManageUser($request->user, $usuario);
if ($cantManageUser) {
return $cantManageUser;
}
$this->canManageUserOrFail($request->user, $usuario);
$auth0 = app(Auth0Service::class);
$auth0Response = $auth0->deleteUser($usuario->auth0_id);
@@ -223,44 +187,17 @@ class UsuariosController extends Controller {
* @return JsonResponse
*/
public function addToRestaurant(Request $request, $id, $restaurant) {
if (!app(UuidService::class)->is_valid($id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $id . ' no es un UUID valido'
], 404);
if (!str_starts_with($id, 'auth0')) {
app(UuidService::class)->validOrFail($id);
}
if (!str_starts_with($restaurant, 'auth0')) {
app(UuidService::class)->validOrFail($restaurant);
}
if (!app(UuidService::class)->is_valid($restaurant)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $restaurant. ' no es un UUID valido'
], 404);
}
$usuario = Usuario::findOrFail($id);
$restaurant = Restaurante::findOrFail($restaurant);
/** @var Usuario $logged_user */
$logged_user = $request->user;
$usuario = Usuario::findByIdOrAuth0Id($id);
if (!$usuario) {
return response()->json([
'error' => 'not_found',
'message' => 'El usuario con id ' . $id . ' no existe'
], 404);
}
$restaurant = Restaurante::findOrNull($restaurant);
if (!$restaurant) {
return response()->json([
'error' => 'not_found',
'message' => 'El restaurante con id ' . $id . ' no existe'
], 404);
}
$cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant);
if ($cantManageUsersOrRestaurant) {
return $cantManageUsersOrRestaurant;
}
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
if ($usuario->restaurantes->contains($restaurant)) {
return response()->json([
@@ -283,41 +220,17 @@ class UsuariosController extends Controller {
* @return JsonResponse
*/
public function removeFromRestaurant(Request $request, $id, $restaurant) {
if (!app(UuidService::class)->is_valid($id)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $id . ' no es un UUID valido'
], 404);
if (!str_starts_with($id, 'auth0')) {
app(UuidService::class)->validOrFail($id);
}
if (!str_starts_with($restaurant, 'auth0')) {
app(UuidService::class)->validOrFail($restaurant);
}
if (!app(UuidService::class)->is_valid($restaurant)) {
return response()->json([
'error' => 'invalid_id',
'message' => 'El id ' . $restaurant. ' no es un UUID valido'
], 404);
}
$usuario = Usuario::findOrFail($id);
$restaurant = Restaurante::findOrFail($restaurant);
$usuario = Usuario::findByIdOrAuth0Id($id);
if (!$usuario) {
return response()->json([
'error' => 'not_found',
'message' => 'El usuario con id ' . $id . ' no existe'
], 404);
}
$restaurant = Restaurante::findOrNull($restaurant);
if (!$restaurant) {
return response()->json([
'error' => 'not_found',
'message' => 'El restaurante con id ' . $id . ' no existe'
], 404);
}
$cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant);
if ($cantManageUsersOrRestaurant) {
return $cantManageUsersOrRestaurant;
}
canManageUsersAndRestaurantOrFail($request->user, $restaurant);
if (!$usuario->restaurantes->contains($restaurant)) {
return response()->json([
@@ -331,38 +244,31 @@ class UsuariosController extends Controller {
return response()->json($usuario->fresh(['restaurantes']));
}
private function cantManageUsersOrRestaurant(Usuario $user, Restaurante $restaurante) {
private function canManageUsersAndRestaurantOrFail(Usuario $user, Restaurante $restaurante) {
if (!$user->canManageUsers()) {
return response()->json([
'error' => 'cant_manage_users',
'message' => 'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios'
], 403);
throw new GenericException('cant_manage_users',
'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios',
403);
}
if (!$user->hasPermissionsOnRestaurant($restaurante)) {
return response()->json([
'error' => 'cant_manage_user_of_another_restaurant',
'message' => 'El usuario ' . $user->id . ' no puede manipular un usuario en el restaurant ' . $restaurante->id . ' porque que no pertenece a el'
], 403);
throw new GenericException('cant_manage_user_of_another_restaurant',
'El usuario ' . $user->id . ' no puede manipular un usuario en el restaurant ' . $restaurante->id . ' porque que no pertenece a el',
403);
}
return false;
}
private function cantManageUser(Usuario $manager, Usuario $user) {
private function canManageUserOrFail(Usuario $manager, Usuario $user) {
if (!$manager->canManageUsers()) {
return response()->json([
'error' => 'cant_manage_users',
'message' => 'El usuario ' . $manager->id . ' no tiene permisos para manipular usuarios'
], 403);
throw new GenericException('cant_manage_users',
'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios',
403);
}
if (!$manager->hasPermissionsOverUser($user)) {
return response()->json([
'error' => 'cant_manage_that_user',
'message' => 'El usuario ' . $manager->id . ' no tiene permisos para manipular al usuario ' . $user->id
], 403);
throw new GenericException('cant_manage_that_user',
'El usuario ' . $manager->id . ' no tiene permisos para manipular al usuario ' . $user->id,
403);
}
return false;
}
}

View File

@@ -11,13 +11,14 @@ class CanalVenta extends Model {
protected $table = 'canales_venta';
public static function findOrNull($id) {
try {
return CanalVenta::find($id);
} catch (QueryException $ex) {
Log::warning('Se intento obtener un canal de venta con un id invalido', ['id' => $id]);
return null;
public static function findOrFail($id) {
$canal_venta = CanalVenta::find($id);
if(!$canal_venta){
throw new ModelNotFoundException("canal_venta", $id);
}
return $canal_venta;
}
public function tipoCanal() {

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use App\Models\CanalVenta;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\QueryException;
@@ -21,13 +22,14 @@ class Restaurante extends Model {
protected $fillable = ['id', 'nombre'];
public static function findOrNull($id) {
try {
return Restaurante::find($id);
} catch (QueryException $ex) {
Log::warning('Se intento obtener un restaurante con un id invalido', ['id' => $id]);
return null;
public static function findOrFail($id) {
$restaurante = Restaurante::find($id);
if(!$restaurante){
throw new ModelNotFoundException("restaurant", $id);
}
return $restaurante;
}
public function usuarios() {

View File

@@ -4,6 +4,7 @@ namespace App\Models;
use App\Services\Auth0Service;
use App\Traits\UuidPrimaryKey;
use App\Exceptions\ModelNotFoundException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\QueryException;
@@ -38,17 +39,18 @@ class Usuario extends Model {
* @param $id
* @return Usuario
*/
public static function findByIdOrAuth0Id($id) {
public static function findOrFail($id) {
if (str_starts_with($id, 'auth0')) {
return Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first();
$usuario = Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first();
} else {
try {
return Usuario::where('id', $id)->with('restaurantes')->first();
} catch (QueryException $ex) {
Log::warning('Se intento obtener un usuario con un id invalido', ['id' => $id]);
return null;
}
$usuario = Usuario::where('id', $id)->with('restaurantes')->first();
}
if(!$usuario) {
throw new ModelNotFoundException("usuario", $id);
}
return $usuario;
}
public function canManageRestaurants() {

View File

@@ -3,10 +3,17 @@
namespace App\Services;
use Illuminate\Support\ServiceProvider;
use App\Exceptions\InvalidUuidException;
class UuidService extends ServiceProvider {
public function is_valid(string $uuid) {
public function isValid(string $uuid) {
$regex = '/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i';
return preg_match($regex, $uuid) === 1;
}
public function validOrFail(string $uuid) {
if(!$this->isValid($uuid)) {
throw new InvalidUuidException($uuid);
}
}
}