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

@@ -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;
}
}