Removi las validaciones por ahora :/
This commit is contained in:
@@ -29,9 +29,15 @@ class Handler extends ExceptionHandler {
|
|||||||
return $rendered;
|
return $rendered;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(config('app.debug') === false) {
|
||||||
|
$message = $exception instanceof HttpException ? $exception->getMessage() : 'Server Error';
|
||||||
|
} else {
|
||||||
|
$message = $exception->getMessage();
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'error' => $rendered->getStatusCode(),
|
'error' => $rendered->getStatusCode(),
|
||||||
'message' => $exception instanceof HttpException ? $exception->getMessage() : 'Server Error',
|
'message' => $message,
|
||||||
], $rendered->getStatusCode());
|
], $rendered->getStatusCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +0,0 @@
|
|||||||
<?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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -13,134 +13,66 @@ use Ramsey\Uuid\Uuid;
|
|||||||
|
|
||||||
class CanalesVentaController extends Controller {
|
class CanalesVentaController extends Controller {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtiene de forma paginada los canales de venta
|
* Obtiene de forma paginada los canales de venta
|
||||||
* de un restaurant registrados en el backend
|
*/
|
||||||
* @param Request $request
|
public function all(Request $request, $restaurante_id) {
|
||||||
* @param $restaurant
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
* @return JsonResponse
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
*/
|
|
||||||
public function all(Request $request, $restaurante_id) {
|
|
||||||
app(UuidService::class)->validOrFail($restaurante_id);
|
|
||||||
$restaurante = Restaurante::findOrFail($restaurante_id);
|
|
||||||
|
|
||||||
$canalesVenta = $restaurante->canalesVenta();
|
$canalesVenta = $restaurante->canalesVenta();
|
||||||
|
|
||||||
$paginate = app(PaginatorService::class)->paginate(
|
$paginate = app(PaginatorService::class)->paginate(
|
||||||
perPage: $request->input('per_page', 15),
|
perPage: $request->input('per_page', 15),
|
||||||
page: $request->input('page', 1),
|
page: $request->input('page', 1),
|
||||||
total: $canalesVenta->count(),
|
total: $canalesVenta->count(),
|
||||||
route: 'canales-venta.all',
|
route: 'canales-venta.all',
|
||||||
data: [$restaurante_id],
|
data: ['restaurante_id' => $restaurante_id],
|
||||||
);
|
);
|
||||||
|
|
||||||
return response()->json([
|
$data = $canalesVenta->get()
|
||||||
'pagination' => $paginate,
|
->skip($paginate['from'] - 1)
|
||||||
'data' => array_values($canalesVenta->get()->skip($paginate['from'] - 1)->take($paginate['per_page'])->all())
|
->take($paginate['per_page'])
|
||||||
]);
|
->all();
|
||||||
|
|
||||||
}
|
return response()->json([
|
||||||
|
'pagination' => $paginate,
|
||||||
|
'data' => $data
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtiene un canal de venta por su id
|
* Obtiene un canal de venta por su id
|
||||||
* @param $restaurante_id
|
*/
|
||||||
* @param $id
|
public function get($restaurante_id, $id) {
|
||||||
* @return JsonResponse
|
app(UuidService::class)->validOrFail($id);
|
||||||
*/
|
app(UuidService::class)->validOrFail($restaurante_id);
|
||||||
public function get($restaurante_id, $id) {
|
|
||||||
app(UuidService::class)->validOrFail($id);
|
|
||||||
app(UuidService::class)->validOrFail($restaurante_id);
|
|
||||||
|
|
||||||
$restaurante = Restaurante::findOrFail($restaurante_id);
|
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||||
$canalVenta = CanalVenta::findOrFail($id);
|
$canalVenta = CanalVenta::findOrFail($id);
|
||||||
|
|
||||||
return response()->json($canalVenta);
|
return response()->json($canalVenta);
|
||||||
}
|
}
|
||||||
|
|
||||||
///**
|
/**
|
||||||
// * Crea un nuevo restaurant
|
* Crea un nuevo canal de venta
|
||||||
// * @param Request $request
|
* @param Request $request
|
||||||
// * @return JsonResponse
|
* @return JsonResponse
|
||||||
// * @throws ValidationException
|
* @throws ValidationException
|
||||||
// */
|
*/
|
||||||
//public function create(Request $request) {
|
public function create(Request $request) {
|
||||||
// $this->validate($request, [
|
$this->validate($request, [
|
||||||
// 'nombre' => 'required'
|
'nombre' => 'required',
|
||||||
// ]);
|
'sector_id' => 'required|exists:sectores,id',
|
||||||
|
'restaurante_id' => 'required|exists:restaurantes,id',
|
||||||
|
'tipo_canal_id' => 'required|exists:tipos_canal,id'
|
||||||
|
]);
|
||||||
|
|
||||||
// if (!$request->user->canManageRestaurants()) {
|
$restaurant = Restaurante::create([
|
||||||
// return response()->json([
|
'id' => Uuid::uuid4(),
|
||||||
// 'error' => 'cant_manage_restaurants',
|
'nombre' => $request->input('nombre')
|
||||||
// 'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
]);
|
||||||
// ], 403);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $restaurant = Restaurante::create([
|
return response()->json($restaurant, 201);
|
||||||
// 'id' => Uuid::uuid4(),
|
}
|
||||||
// 'nombre' => $request->input('nombre')
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
// return response()->json($restaurant, 201);
|
|
||||||
//}
|
|
||||||
|
|
||||||
///**
|
|
||||||
// * Actualiza un restaurante
|
|
||||||
// * @param Request $request
|
|
||||||
// * @param $id
|
|
||||||
// * @return JsonResponse
|
|
||||||
// * @throws ValidationException
|
|
||||||
// */
|
|
||||||
//public function update(Request $request, $id) {
|
|
||||||
// $this->validate($request, [
|
|
||||||
// 'nombre' => 'required'
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
// if (!$request->user->canManageRestaurants()) {
|
|
||||||
// return response()->json([
|
|
||||||
// 'error' => 'cant_manage_restaurants',
|
|
||||||
// 'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
||||||
// ], 403);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $restaurant = Restaurante::findOrNull($id);
|
|
||||||
// if(!$restaurant) {
|
|
||||||
// return response()->json([
|
|
||||||
// 'error' => 'not_found',
|
|
||||||
// 'message' => 'El restaurante con id ' . $id . ' no existe'
|
|
||||||
// ], 404);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $restaurant->nombre = $request->input('nombre');
|
|
||||||
// $restaurant->save();
|
|
||||||
|
|
||||||
// return response()->json($restaurant);
|
|
||||||
//}
|
|
||||||
|
|
||||||
///**
|
|
||||||
// * Elimina un restaurante
|
|
||||||
// * @param Request $request
|
|
||||||
// * @param $id
|
|
||||||
// * @return JsonResponse
|
|
||||||
// * @throws ValidationException
|
|
||||||
// */
|
|
||||||
//public function delete(Request $request, $id) {
|
|
||||||
// if (!$request->user->canManageRestaurants()) {
|
|
||||||
// return response()->json([
|
|
||||||
// 'error' => 'cant_manage_restaurants',
|
|
||||||
// 'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
||||||
// ], 403);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $restaurant = Restaurante::findOrNull($id);
|
|
||||||
// if(!$restaurant) {
|
|
||||||
// return response()->json([
|
|
||||||
// 'error' => 'not_found',
|
|
||||||
// 'message' => 'El restaurante con id ' . $id . ' no existe'
|
|
||||||
// ], 404);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// $restaurant->delete();
|
|
||||||
|
|
||||||
// return response()->json($restaurant);
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,6 @@ use Laravel\Lumen\Routing\Controller as BaseController;
|
|||||||
|
|
||||||
class Controller extends BaseController {
|
class Controller extends BaseController {
|
||||||
protected function buildFailedValidationResponse(Request $request, array $errors) {
|
protected function buildFailedValidationResponse(Request $request, array $errors) {
|
||||||
return ["error" => "validation_error", "message" => $errors];
|
return response()->json(["error" => "validation_error", "message" => $errors], 400);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,6 +4,9 @@ namespace App\Http\Controllers;
|
|||||||
|
|
||||||
use App\Models\Restaurante;
|
use App\Models\Restaurante;
|
||||||
use App\Services\PaginatorService;
|
use App\Services\PaginatorService;
|
||||||
|
use App\Services\UuidService;
|
||||||
|
use App\Exceptions\GenericException;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Validation\ValidationException;
|
use Illuminate\Validation\ValidationException;
|
||||||
@@ -13,43 +16,37 @@ class RestaurantesController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtiene de forma paginada los restaurantes registrados en el backend
|
* Obtiene de forma paginada los restaurantes registrados en el backend
|
||||||
* @param Request $request
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function all(Request $request) {
|
public function all(Request $request) {
|
||||||
|
$restaurantes = Restaurante::all();
|
||||||
|
|
||||||
$paginate = app(PaginatorService::class)->paginate(
|
$paginate = app(PaginatorService::class)->paginate(
|
||||||
perPage: $request->input('per_page', 15),
|
perPage: $request->input('per_page', 15),
|
||||||
page: $request->input('page', 1),
|
page: $request->input('page', 1),
|
||||||
total: Restaurante::all()->count(),
|
total: $restaurantes->count(),
|
||||||
route: 'restaurant.all',
|
route: 'restaurant.all',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$data = $restaurantes
|
||||||
|
->skip($paginate['from'] - 1)
|
||||||
|
->take($paginate['per_page'])
|
||||||
|
->all();
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'pagination' => $paginate,
|
'pagination' => $paginate,
|
||||||
'data' => array_values(Restaurante::all()->skip($paginate['from'] - 1)->take($paginate['per_page'])->all())
|
'data' => $data
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtiene un restaurante por su id
|
* Obtiene un restaurante por su id
|
||||||
* @param $id
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function get($id) {
|
public function get(Request $request, $id) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
app(UuidService::class)->validOrFail($id);
|
||||||
return response()->json([
|
$restaurante = Restaurante::findOrFail($id);
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$restaurante = Restaurante::findOrNull($id);
|
if(!$request->user->isOnRestaurant($restaurante)){
|
||||||
|
return ModelNotFoundException('restaurante', $restaurante->id);
|
||||||
if (!$restaurante) {
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'restaurant_not_found',
|
|
||||||
'message' => 'El restaurant con id ' . $id . ' no existe'
|
|
||||||
], 404);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return response()->json($restaurante);
|
return response()->json($restaurante);
|
||||||
@@ -57,22 +54,12 @@ class RestaurantesController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Crea un nuevo restaurant
|
* Crea un nuevo restaurant
|
||||||
* @param Request $request
|
|
||||||
* @return JsonResponse
|
|
||||||
* @throws ValidationException
|
|
||||||
*/
|
*/
|
||||||
public function create(Request $request) {
|
public function create(Request $request) {
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
'nombre' => 'required'
|
'nombre' => 'required'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!$request->user->canManageRestaurants()) {
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'cant_manage_restaurants',
|
|
||||||
'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
||||||
], 403);
|
|
||||||
}
|
|
||||||
|
|
||||||
$restaurant = Restaurante::create([
|
$restaurant = Restaurante::create([
|
||||||
'id' => Uuid::uuid4(),
|
'id' => Uuid::uuid4(),
|
||||||
'nombre' => $request->input('nombre')
|
'nombre' => $request->input('nombre')
|
||||||
@@ -83,38 +70,15 @@ class RestaurantesController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Actualiza un restaurante
|
* Actualiza un restaurante
|
||||||
* @param Request $request
|
|
||||||
* @param $id
|
|
||||||
* @return JsonResponse
|
|
||||||
* @throws ValidationException
|
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, $id) {
|
public function update(Request $request, $id) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
app(UuidService::class)->validOrFail($id);
|
||||||
return response()->json([
|
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
'nombre' => 'required'
|
'nombre' => 'required'
|
||||||
]);
|
]);
|
||||||
|
|
||||||
if (!$request->user->canManageRestaurants()) {
|
$restaurant = Restaurante::findOrFail($id);
|
||||||
return response()->json([
|
|
||||||
'error' => 'cant_manage_restaurants',
|
|
||||||
'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
||||||
], 403);
|
|
||||||
}
|
|
||||||
|
|
||||||
$restaurant = Restaurante::findOrNull($id);
|
|
||||||
if(!$restaurant) {
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'not_found',
|
|
||||||
'message' => 'El restaurante con id ' . $id . ' no existe'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
$restaurant->nombre = $request->input('nombre');
|
$restaurant->nombre = $request->input('nombre');
|
||||||
$restaurant->save();
|
$restaurant->save();
|
||||||
|
|
||||||
@@ -123,36 +87,12 @@ class RestaurantesController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Elimina un restaurante
|
* Elimina un restaurante
|
||||||
* @param Request $request
|
|
||||||
* @param $id
|
|
||||||
* @return JsonResponse
|
|
||||||
* @throws ValidationException
|
|
||||||
*/
|
*/
|
||||||
public function delete(Request $request, $id) {
|
public function delete(Request $request, $id) {
|
||||||
if (!app(UuidService::class)->is_valid($id)) {
|
app(UuidService::class)->validOrFail($id);
|
||||||
return response()->json([
|
|
||||||
'error' => 'invalid_id',
|
|
||||||
'message' => 'El id ' . $id . ' no es un UUID valido'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$request->user->canManageRestaurants()) {
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'cant_manage_restaurants',
|
|
||||||
'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
|
||||||
], 403);
|
|
||||||
}
|
|
||||||
|
|
||||||
$restaurant = Restaurante::findOrNull($id);
|
|
||||||
if(!$restaurant) {
|
|
||||||
return response()->json([
|
|
||||||
'error' => 'not_found',
|
|
||||||
'message' => 'El restaurante con id ' . $id . ' no existe'
|
|
||||||
], 404);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
$restaurant = Restaurante::findOrFail($id);
|
||||||
$restaurant->delete();
|
$restaurant->delete();
|
||||||
|
|
||||||
return response()->json($restaurant);
|
return response()->json($restaurant);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ use App\Services\Auth0Service;
|
|||||||
use App\Services\PaginatorService;
|
use App\Services\PaginatorService;
|
||||||
use App\Services\UuidService;
|
use App\Services\UuidService;
|
||||||
use App\Exceptions\GenericException;
|
use App\Exceptions\GenericException;
|
||||||
|
use App\Exceptions\ModelNotFoundException;
|
||||||
use Illuminate\Http\JsonResponse;
|
use Illuminate\Http\JsonResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Validation\Rule;
|
use Illuminate\Validation\Rule;
|
||||||
@@ -18,20 +19,20 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtiene de forma paginada los usuarios registrados en el backend
|
* Obtiene de forma paginada los usuarios registrados en el backend
|
||||||
* @param Request $request
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function all(Request $request) {
|
public function all(Request $request) {
|
||||||
|
$usuarios = Usuario::all();
|
||||||
|
|
||||||
$paginate = app(PaginatorService::class)->paginate(
|
$paginate = app(PaginatorService::class)->paginate(
|
||||||
perPage: $request->input('per_page', 15),
|
perPage: $request->input('per_page', 15),
|
||||||
page: $request->input('page', 1),
|
page: $request->input('page', 1),
|
||||||
total: Usuario::all()->count(),
|
total: $usuarios->count(),
|
||||||
route: 'users.all',
|
route: 'users.all',
|
||||||
);
|
);
|
||||||
|
|
||||||
$data = Usuario::with('restaurantes')
|
$data = $usuarios
|
||||||
->skip($paginate['from'] - 1)
|
->skip($paginate['from'] - 1)
|
||||||
->take($paginate['per_page'])->get()->all();
|
->take($paginate['per_page'])->all();
|
||||||
|
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'pagination' => $paginate,
|
'pagination' => $paginate,
|
||||||
@@ -41,22 +42,15 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Obtiene un usuario por su id
|
* Obtiene un usuario por su id
|
||||||
* @param $id
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function get($id) {
|
public function get(Request $request, $id) {
|
||||||
if (!str_starts_with($id, 'auth0')) {
|
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||||
app(UuidService::class)->validOrFail($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
$usuario = Usuario::findOrFail($id);
|
$usuario = Usuario::findOrFail($id);
|
||||||
|
|
||||||
return response()->json($usuario);
|
return response()->json($usuario);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Crea un nuevo usuario localmente y en auth0
|
* Crea un nuevo usuario localmente y en auth0
|
||||||
* @throws ValidationException
|
|
||||||
*/
|
*/
|
||||||
public function create(Request $request) {
|
public function create(Request $request) {
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
@@ -70,7 +64,6 @@ class UsuariosController extends Controller {
|
|||||||
]);
|
]);
|
||||||
|
|
||||||
$restaurant = Restaurante::findOrFail($request->input('restaurant'));
|
$restaurant = Restaurante::findOrFail($request->input('restaurant'));
|
||||||
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
|
||||||
|
|
||||||
$auth0 = app(Auth0Service::class);
|
$auth0 = app(Auth0Service::class);
|
||||||
$auth0User = $auth0->createUser(
|
$auth0User = $auth0->createUser(
|
||||||
@@ -101,15 +94,9 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Actualiza un usuario
|
* Actualiza un usuario
|
||||||
* @param Request $request
|
|
||||||
* @param $id
|
|
||||||
* @return JsonResponse
|
|
||||||
* @throws ValidationException
|
|
||||||
*/
|
*/
|
||||||
public function update(Request $request, $id) {
|
public function update(Request $request, $id) {
|
||||||
if (!str_starts_with($id, 'auth0')) {
|
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||||
app(UuidService::class)->validOrFail($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
$this->validate($request, [
|
$this->validate($request, [
|
||||||
'nombre' => 'sometimes',
|
'nombre' => 'sometimes',
|
||||||
@@ -122,8 +109,6 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
$usuario = Usuario::findOrFail($id);
|
$usuario = Usuario::findOrFail($id);
|
||||||
|
|
||||||
$this->canManageUserOrFail($request->user, $usuario);
|
|
||||||
|
|
||||||
$metadata = [];
|
$metadata = [];
|
||||||
if ($request->input('roles')) $metadata['roles'] = $request->input('roles');
|
if ($request->input('roles')) $metadata['roles'] = $request->input('roles');
|
||||||
|
|
||||||
@@ -151,19 +136,12 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Elimina un usuario
|
* Elimina un usuario
|
||||||
* @param Request $request
|
|
||||||
* @param $id
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function delete(Request $request, $id) {
|
public function delete(Request $request, $id) {
|
||||||
if (!str_starts_with($id, 'auth0')) {
|
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||||
app(UuidService::class)->validOrFail($id);
|
|
||||||
}
|
|
||||||
|
|
||||||
$usuario = Usuario::findOrFail($id);
|
$usuario = Usuario::findOrFail($id);
|
||||||
|
|
||||||
$this->canManageUserOrFail($request->user, $usuario);
|
|
||||||
|
|
||||||
$auth0 = app(Auth0Service::class);
|
$auth0 = app(Auth0Service::class);
|
||||||
$auth0Response = $auth0->deleteUser($usuario->auth0_id);
|
$auth0Response = $auth0->deleteUser($usuario->auth0_id);
|
||||||
|
|
||||||
@@ -181,24 +159,14 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Agrega usuario a un restaurant
|
* Agrega usuario a un restaurant
|
||||||
* @param Request $request
|
|
||||||
* @param $id
|
|
||||||
* @param $restaurant
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function addToRestaurant(Request $request, $id, $restaurant) {
|
public function addToRestaurant(Request $request, $id, $restaurant) {
|
||||||
if (!str_starts_with($id, 'auth0')) {
|
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||||
app(UuidService::class)->validOrFail($id);
|
app(UuidService::class)->validOrFail($restaurant);
|
||||||
}
|
|
||||||
if (!str_starts_with($restaurant, 'auth0')) {
|
|
||||||
app(UuidService::class)->validOrFail($restaurant);
|
|
||||||
}
|
|
||||||
|
|
||||||
$usuario = Usuario::findOrFail($id);
|
$usuario = Usuario::findOrFail($id);
|
||||||
$restaurant = Restaurante::findOrFail($restaurant);
|
$restaurant = Restaurante::findOrFail($restaurant);
|
||||||
|
|
||||||
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
|
||||||
|
|
||||||
if ($usuario->restaurantes->contains($restaurant)) {
|
if ($usuario->restaurantes->contains($restaurant)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'error' => 'already_on_restaurant',
|
'error' => 'already_on_restaurant',
|
||||||
@@ -213,25 +181,14 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Saca a un usuario de un restaurant
|
* Saca a un usuario de un restaurant
|
||||||
*
|
|
||||||
* @param Request $request
|
|
||||||
* @param $id
|
|
||||||
* @param $restaurant
|
|
||||||
* @return JsonResponse
|
|
||||||
*/
|
*/
|
||||||
public function removeFromRestaurant(Request $request, $id, $restaurant) {
|
public function removeFromRestaurant(Request $request, $id, $restaurant) {
|
||||||
if (!str_starts_with($id, 'auth0')) {
|
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||||
app(UuidService::class)->validOrFail($id);
|
|
||||||
}
|
|
||||||
if (!str_starts_with($restaurant, 'auth0')) {
|
|
||||||
app(UuidService::class)->validOrFail($restaurant);
|
app(UuidService::class)->validOrFail($restaurant);
|
||||||
}
|
|
||||||
|
|
||||||
$usuario = Usuario::findOrFail($id);
|
$usuario = Usuario::findOrFail($id);
|
||||||
$restaurant = Restaurante::findOrFail($restaurant);
|
$restaurant = Restaurante::findOrFail($restaurant);
|
||||||
|
|
||||||
canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
|
||||||
|
|
||||||
if (!$usuario->restaurantes->contains($restaurant)) {
|
if (!$usuario->restaurantes->contains($restaurant)) {
|
||||||
return response()->json([
|
return response()->json([
|
||||||
'error' => 'already_not_on_restaurant',
|
'error' => 'already_not_on_restaurant',
|
||||||
@@ -243,32 +200,4 @@ class UsuariosController extends Controller {
|
|||||||
|
|
||||||
return response()->json($usuario->fresh(['restaurantes']));
|
return response()->json($usuario->fresh(['restaurantes']));
|
||||||
}
|
}
|
||||||
|
|
||||||
private function canManageUsersAndRestaurantOrFail(Usuario $user, Restaurante $restaurante) {
|
|
||||||
if (!$user->canManageUsers()) {
|
|
||||||
throw new GenericException('cant_manage_users',
|
|
||||||
'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios',
|
|
||||||
403);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$user->hasPermissionsOnRestaurant($restaurante)) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private function canManageUserOrFail(Usuario $manager, Usuario $user) {
|
|
||||||
if (!$manager->canManageUsers()) {
|
|
||||||
throw new GenericException('cant_manage_users',
|
|
||||||
'El usuario ' . $user->id . ' no tiene permisos para manipular usuarios',
|
|
||||||
403);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$manager->hasPermissionsOverUser($user)) {
|
|
||||||
throw new GenericException('cant_manage_that_user',
|
|
||||||
'El usuario ' . $manager->id . ' no tiene permisos para manipular al usuario ' . $user->id,
|
|
||||||
403);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Models;
|
namespace App\Models;
|
||||||
|
|
||||||
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;
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,6 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
|||||||
use Illuminate\Database\QueryException;
|
use Illuminate\Database\QueryException;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
|
||||||
* @method static create(array $array)
|
|
||||||
* @method static find($id)
|
|
||||||
* @property mixed id
|
|
||||||
*/
|
|
||||||
class Restaurante extends Model {
|
class Restaurante extends Model {
|
||||||
use UuidPrimaryKey, SoftDeletes;
|
use UuidPrimaryKey, SoftDeletes;
|
||||||
|
|
||||||
@@ -24,11 +19,7 @@ class Restaurante extends Model {
|
|||||||
|
|
||||||
public static function findOrFail($id) {
|
public static function findOrFail($id) {
|
||||||
$restaurante = Restaurante::find($id);
|
$restaurante = Restaurante::find($id);
|
||||||
|
if(!$restaurante) throw new ModelNotFoundException("restaurant", $id);
|
||||||
if(!$restaurante){
|
|
||||||
throw new ModelNotFoundException("restaurant", $id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $restaurante;
|
return $restaurante;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,28 +5,13 @@ namespace App\Models;
|
|||||||
use App\Services\Auth0Service;
|
use App\Services\Auth0Service;
|
||||||
use App\Traits\UuidPrimaryKey;
|
use App\Traits\UuidPrimaryKey;
|
||||||
use App\Exceptions\ModelNotFoundException;
|
use App\Exceptions\ModelNotFoundException;
|
||||||
|
use App\Exceptions\HasNoPermissionsOnRestaurantException;
|
||||||
|
use App\Exceptions\CantManageRestaurantsException;
|
||||||
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;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Usuario
|
|
||||||
*
|
|
||||||
* @property $id
|
|
||||||
* @property $auth0_id
|
|
||||||
* @property $nombre
|
|
||||||
* @property $roles
|
|
||||||
* @property $restaurantes
|
|
||||||
*
|
|
||||||
* @method static find($id)
|
|
||||||
* @method static where(string $string, $sub)
|
|
||||||
* @method static create(array $array)
|
|
||||||
* @method static paginate($perPage = null, $columns = ['*'], $pageName = 'page', $page = null)
|
|
||||||
*
|
|
||||||
* @package App\Models
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class Usuario extends Model {
|
class Usuario extends Model {
|
||||||
use UuidPrimaryKey, SoftDeletes;
|
use UuidPrimaryKey, SoftDeletes;
|
||||||
|
|
||||||
@@ -35,66 +20,23 @@ class Usuario extends Model {
|
|||||||
protected $appends = ['roles'];
|
protected $appends = ['roles'];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Busca un usuario según su id o auth_0 id, dependiendo del formato entregado en $id
|
* Busca un usuario o envia una excepcion si no se encuentra
|
||||||
* @param $id
|
|
||||||
* @return Usuario
|
|
||||||
*/
|
*/
|
||||||
public static function findOrFail($id) {
|
public static function findOrFail($id) {
|
||||||
if (str_starts_with($id, 'auth0')) {
|
if (str_starts_with($id, 'auth0')) $usuario = Usuario::where('auth0_id', urldecode($id));
|
||||||
$usuario = Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first();
|
else $usuario = Usuario::where('id', $id);
|
||||||
} else {
|
|
||||||
$usuario = Usuario::where('id', $id)->with('restaurantes')->first();
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!$usuario) {
|
if(!$usuario) throw new ModelNotFoundException("usuario", $id);
|
||||||
throw new ModelNotFoundException("usuario", $id);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $usuario;
|
return $usuario->first();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canManageRestaurants() {
|
public function isGlobalAdmin() {
|
||||||
if (in_array('global_admin', $this->roles)) return true;
|
return in_array('global_admin', $this->roles);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
public function isAdmin() {
|
||||||
* Valida que el usuario tiene permisos sobre otro usuario
|
return in_array('admin', $this->roles);
|
||||||
*
|
|
||||||
* Esto se cumple cuando el usuario es global_admin o es administrador y comparte restaurant con el otro usuario
|
|
||||||
* @param $user
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasPermissionsOverUser($user) {
|
|
||||||
if (in_array('global_admin', $this->roles)) return true;
|
|
||||||
if (!in_array('admin', $this->roles)) return false;
|
|
||||||
if ($this->restaurantes->intersect($user->restaurantes)->count() > 0) return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Valida que el usuario tiene permisos en un restaurant
|
|
||||||
*
|
|
||||||
* Esto se cumple cuando el usuario es global_admin o el usuario esta dentro del restaurant.
|
|
||||||
* @param $restaurant
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function hasPermissionsOnRestaurant($restaurant) {
|
|
||||||
if (in_array('global_admin', $this->roles)) return true;
|
|
||||||
if ($this->restaurantes->contains($restaurant)) return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Valida que el usuario puede manejar otros usuarios
|
|
||||||
*
|
|
||||||
* Esto es cumplido cuando el usuario tiene rol de global_admin y/o admin
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function canManageUsers() {
|
|
||||||
if (in_array('global_admin', $this->roles)) return true;
|
|
||||||
if (in_array('admin', $this->roles)) return true;
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function restaurantes() {
|
public function restaurantes() {
|
||||||
|
|||||||
Reference in New Issue
Block a user