Mejoras en la api
This commit is contained in:
@@ -18,14 +18,4 @@ COPY composer.lock .
|
|||||||
RUN composer install --no-ansi --no-dev --no-interaction --no-plugins \
|
RUN composer install --no-ansi --no-dev --no-interaction --no-plugins \
|
||||||
--no-progress --no-scripts --optimize-autoloader
|
--no-progress --no-scripts --optimize-autoloader
|
||||||
|
|
||||||
COPY app ./app
|
|
||||||
COPY artisan ./artisan
|
|
||||||
COPY bootstrap ./bootstrap
|
|
||||||
COPY config ./config
|
|
||||||
COPY public ./public
|
|
||||||
COPY resources ./resources
|
|
||||||
COPY routes ./routes
|
|
||||||
COPY storage ./storage
|
|
||||||
COPY .env.production .env
|
|
||||||
|
|
||||||
ENTRYPOINT ["php", "-S", "0.0.0.0:8080", "-t", "public"]
|
ENTRYPOINT ["php", "-S", "0.0.0.0:8080", "-t", "public"]
|
||||||
|
|||||||
25
backend/app/Exceptions/GenericException.php
Normal file
25
backend/app/Exceptions/GenericException.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
21
backend/app/Exceptions/InvalidUuidException.php
Normal file
21
backend/app/Exceptions/InvalidUuidException.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
23
backend/app/Exceptions/ModelNotFoundException.php
Normal file
23
backend/app/Exceptions/ModelNotFoundException.php
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -21,21 +21,8 @@ class CanalesVentaController extends Controller {
|
|||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function all(Request $request, $restaurante_id) {
|
public function all(Request $request, $restaurante_id) {
|
||||||
if (!app(UuidService::class)->is_valid($restaurante_id)) {
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
return response()->json([
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
'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);
|
|
||||||
}
|
|
||||||
|
|
||||||
$canalesVenta = $restaurante->canalesVenta();
|
$canalesVenta = $restaurante->canalesVenta();
|
||||||
|
|
||||||
@@ -61,36 +48,11 @@ class CanalesVentaController extends Controller {
|
|||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function get($restaurante_id, $id) {
|
public function get($restaurante_id, $id) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
app(UuidService::class)->validOrFail($id);
|
||||||
return response()->json([
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!app(UuidService::class)->is_valid($restaurante_id)) {
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
return response()->json([
|
$canalVenta = CanalVenta::findOrFail($id);
|
||||||
'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);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json($canalVenta);
|
return response()->json($canalVenta);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ use App\Models\Restaurante;
|
|||||||
use App\Models\Usuario;
|
use App\Models\Usuario;
|
||||||
use App\Services\Auth0Service;
|
use App\Services\Auth0Service;
|
||||||
use App\Services\PaginatorService;
|
use App\Services\PaginatorService;
|
||||||
|
use App\Services\UuidService;
|
||||||
|
use App\Exceptions\GenericException;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
@@ -27,33 +29,27 @@ class UsuariosController extends Controller {
|
|||||||
route: 'users.all',
|
route: 'users.all',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$data = Usuario::with('restaurantes')
|
||||||
|
->skip($paginate['from'] - 1)
|
||||||
|
->take($paginate['per_page'])->get()->all();
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'pagination' => $paginate,
|
'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
|
* @param $id
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function get($id) {
|
public function get($id) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
if (!str_starts_with($id, 'auth0')) {
|
||||||
return response()->json([
|
app(UuidService::class)->validOrFail($id);
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$usuario = Usuario::findByIdOrAuth0Id($id);
|
$usuario = Usuario::findOrFail($id);
|
||||||
|
|
||||||
if (!$usuario) {
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'user_not_found',
|
|
||||||
'message' => 'El usuario con id o auth0_id ' . $id . ' no existe'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
return response()->json($usuario);
|
return response()->json($usuario);
|
||||||
}
|
}
|
||||||
@@ -73,12 +69,8 @@ class UsuariosController extends Controller {
|
|||||||
'restaurant' => 'required|exists:restaurantes,id',
|
'restaurant' => 'required|exists:restaurantes,id',
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$restaurant = Restaurante::findOrNull($request->input('restaurant'));
|
$restaurant = Restaurante::findOrFail($request->input('restaurant'));
|
||||||
|
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
||||||
$cantManageUsersOrRestaurant = $this->cantManageUsersOrRestaurant($request->user, $restaurant);
|
|
||||||
if ($cantManageUsersOrRestaurant) {
|
|
||||||
return $cantManageUsersOrRestaurant;
|
|
||||||
}
|
|
||||||
|
|
||||||
$auth0 = app(Auth0Service::class);
|
$auth0 = app(Auth0Service::class);
|
||||||
$auth0User = $auth0->createUser(
|
$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 Request $request
|
||||||
* @param $id
|
* @param $id
|
||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
* @throws ValidationException
|
* @throws ValidationException
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, $id) {
|
public function update(Request $request, $id) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
if (!str_starts_with($id, 'auth0')) {
|
||||||
return response()->json([
|
app(UuidService::class)->validOrFail($id);
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
@@ -131,19 +120,9 @@ class UsuariosController extends Controller {
|
|||||||
'roles.*' => ['sometimes', Rule::in(['admin', 'mesero', 'recaudador', 'productor'])],
|
'roles.*' => ['sometimes', Rule::in(['admin', 'mesero', 'recaudador', 'productor'])],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$usuario = Usuario::findByIdOrAuth0Id($id);
|
$usuario = Usuario::findOrFail($id);
|
||||||
|
|
||||||
if (!$usuario) {
|
$this->canManageUserOrFail($request->user, $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;
|
|
||||||
}
|
|
||||||
|
|
||||||
$metadata = [];
|
$metadata = [];
|
||||||
if ($request->input('roles')) $metadata['roles'] = $request->input('roles');
|
if ($request->input('roles')) $metadata['roles'] = $request->input('roles');
|
||||||
@@ -177,28 +156,13 @@ class UsuariosController extends Controller {
|
|||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function delete(Request $request, $id) {
|
public function delete(Request $request, $id) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
if (!str_starts_with($id, 'auth0')) {
|
||||||
return response()->json([
|
app(UuidService::class)->validOrFail($id);
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @var Usuario $logged_user */
|
$usuario = Usuario::findOrFail($id);
|
||||||
$logged_user = $request->user;
|
|
||||||
$usuario = Usuario::findByIdOrAuth0Id($id);
|
|
||||||
|
|
||||||
if (!$usuario) {
|
$this->canManageUserOrFail($request->user, $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;
|
|
||||||
}
|
|
||||||
|
|
||||||
$auth0 = app(Auth0Service::class);
|
$auth0 = app(Auth0Service::class);
|
||||||
$auth0Response = $auth0->deleteUser($usuario->auth0_id);
|
$auth0Response = $auth0->deleteUser($usuario->auth0_id);
|
||||||
@@ -223,44 +187,17 @@ class UsuariosController extends Controller {
|
|||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function addToRestaurant(Request $request, $id, $restaurant) {
|
public function addToRestaurant(Request $request, $id, $restaurant) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
if (!str_starts_with($id, 'auth0')) {
|
||||||
return response()->json([
|
app(UuidService::class)->validOrFail($id);
|
||||||
'error' => 'invalid_id',
|
}
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
if (!str_starts_with($restaurant, 'auth0')) {
|
||||||
], 404);
|
app(UuidService::class)->validOrFail($restaurant);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!app(UuidService::class)->is_valid($restaurant)) {
|
$usuario = Usuario::findOrFail($id);
|
||||||
return response()->json([
|
$restaurant = Restaurante::findOrFail($restaurant);
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $restaurant. ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
/** @var Usuario $logged_user */
|
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
||||||
$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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ($usuario->restaurantes->contains($restaurant)) {
|
if ($usuario->restaurantes->contains($restaurant)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@@ -283,41 +220,17 @@ class UsuariosController extends Controller {
|
|||||||
* @return JsonResponse
|
* @return JsonResponse
|
||||||
*/
|
*/
|
||||||
public function removeFromRestaurant(Request $request, $id, $restaurant) {
|
public function removeFromRestaurant(Request $request, $id, $restaurant) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
if (!str_starts_with($id, 'auth0')) {
|
||||||
return response()->json([
|
app(UuidService::class)->validOrFail($id);
|
||||||
'error' => 'invalid_id',
|
}
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
if (!str_starts_with($restaurant, 'auth0')) {
|
||||||
], 404);
|
app(UuidService::class)->validOrFail($restaurant);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!app(UuidService::class)->is_valid($restaurant)) {
|
$usuario = Usuario::findOrFail($id);
|
||||||
return response()->json([
|
$restaurant = Restaurante::findOrFail($restaurant);
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $restaurant. ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$usuario = Usuario::findByIdOrAuth0Id($id);
|
canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$usuario->restaurantes->contains($restaurant)) {
|
if (!$usuario->restaurantes->contains($restaurant)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
@@ -331,38 +244,31 @@ class UsuariosController extends Controller {
|
|||||||
return response()->json($usuario->fresh(['restaurantes']));
|
return response()->json($usuario->fresh(['restaurantes']));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function cantManageUsersOrRestaurant(Usuario $user, Restaurante $restaurante) {
|
private function canManageUsersAndRestaurantOrFail(Usuario $user, Restaurante $restaurante) {
|
||||||
if (!$user->canManageUsers()) {
|
if (!$user->canManageUsers()) {
|
||||||
return response()->json([
|
throw new GenericException('cant_manage_users',
|
||||||
'error' => 'cant_manage_users',
|
'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios',
|
||||||
'message' => 'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios'
|
403);
|
||||||
], 403);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$user->hasPermissionsOnRestaurant($restaurante)) {
|
if (!$user->hasPermissionsOnRestaurant($restaurante)) {
|
||||||
return response()->json([
|
throw new GenericException('cant_manage_user_of_another_restaurant',
|
||||||
'error' => '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',
|
||||||
'message' => 'El usuario ' . $user->id . ' no puede manipular un usuario en el restaurant ' . $restaurante->id . ' porque que no pertenece a el'
|
403);
|
||||||
], 403);
|
|
||||||
}
|
}
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function cantManageUser(Usuario $manager, Usuario $user) {
|
private function canManageUserOrFail(Usuario $manager, Usuario $user) {
|
||||||
if (!$manager->canManageUsers()) {
|
if (!$manager->canManageUsers()) {
|
||||||
return response()->json([
|
throw new GenericException('cant_manage_users',
|
||||||
'error' => 'cant_manage_users',
|
'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios',
|
||||||
'message' => 'El usuario ' . $manager->id . ' no tiene permisos para manipular usuarios'
|
403);
|
||||||
], 403);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$manager->hasPermissionsOverUser($user)) {
|
if (!$manager->hasPermissionsOverUser($user)) {
|
||||||
return response()->json([
|
throw new GenericException('cant_manage_that_user',
|
||||||
'error' => 'cant_manage_that_user',
|
'El usuario ' . $manager->id . ' no tiene permisos para manipular al usuario ' . $user->id,
|
||||||
'message' => 'El usuario ' . $manager->id . ' no tiene permisos para manipular al usuario ' . $user->id
|
403);
|
||||||
], 403);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,13 +11,14 @@ class CanalVenta extends Model {
|
|||||||
|
|
||||||
protected $table = 'canales_venta';
|
protected $table = 'canales_venta';
|
||||||
|
|
||||||
public static function findOrNull($id) {
|
public static function findOrFail($id) {
|
||||||
try {
|
$canal_venta = CanalVenta::find($id);
|
||||||
return CanalVenta::find($id);
|
|
||||||
} catch (QueryException $ex) {
|
if(!$canal_venta){
|
||||||
Log::warning('Se intento obtener un canal de venta con un id invalido', ['id' => $id]);
|
throw new ModelNotFoundException("canal_venta", $id);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $canal_venta;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tipoCanal() {
|
public function tipoCanal() {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Models\CanalVenta;
|
use App\Models\CanalVenta;
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Database\QueryException;
|
use Illuminate\Database\QueryException;
|
||||||
@@ -21,13 +22,14 @@ class Restaurante extends Model {
|
|||||||
|
|
||||||
protected $fillable = ['id', 'nombre'];
|
protected $fillable = ['id', 'nombre'];
|
||||||
|
|
||||||
public static function findOrNull($id) {
|
public static function findOrFail($id) {
|
||||||
try {
|
$restaurante = Restaurante::find($id);
|
||||||
return Restaurante::find($id);
|
|
||||||
} catch (QueryException $ex) {
|
if(!$restaurante){
|
||||||
Log::warning('Se intento obtener un restaurante con un id invalido', ['id' => $id]);
|
throw new ModelNotFoundException("restaurant", $id);
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $restaurante;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function usuarios() {
|
public function usuarios() {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ namespace App\Models;
|
|||||||
|
|
||||||
use App\Services\Auth0Service;
|
use App\Services\Auth0Service;
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Illuminate\Database\QueryException;
|
use Illuminate\Database\QueryException;
|
||||||
@@ -38,17 +39,18 @@ class Usuario extends Model {
|
|||||||
* @param $id
|
* @param $id
|
||||||
* @return Usuario
|
* @return Usuario
|
||||||
*/
|
*/
|
||||||
public static function findByIdOrAuth0Id($id) {
|
public static function findOrFail($id) {
|
||||||
if (str_starts_with($id, 'auth0')) {
|
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 {
|
} else {
|
||||||
try {
|
$usuario = Usuario::where('id', $id)->with('restaurantes')->first();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(!$usuario) {
|
||||||
|
throw new ModelNotFoundException("usuario", $id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $usuario;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canManageRestaurants() {
|
public function canManageRestaurants() {
|
||||||
|
|||||||
@@ -3,10 +3,17 @@
|
|||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
|
use App\Exceptions\InvalidUuidException;
|
||||||
|
|
||||||
class UuidService extends ServiceProvider {
|
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';
|
$regex = '/^[a-f\d]{8}(-[a-f\d]{4}){4}[a-f\d]{8}$/i';
|
||||||
return preg_match($regex, $uuid) === 1;
|
return preg_match($regex, $uuid) === 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function validOrFail(string $uuid) {
|
||||||
|
if(!$this->isValid($uuid)) {
|
||||||
|
throw new InvalidUuidException($uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user