Removi las validaciones por ahora :/
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Services\Auth0Service;
|
||||
use App\Services\PaginatorService;
|
||||
use App\Services\UuidService;
|
||||
use App\Exceptions\GenericException;
|
||||
use App\Exceptions\ModelNotFoundException;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
@@ -18,20 +19,20 @@ class UsuariosController extends Controller {
|
||||
|
||||
/**
|
||||
* Obtiene de forma paginada los usuarios registrados en el backend
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function all(Request $request) {
|
||||
$usuarios = Usuario::all();
|
||||
|
||||
$paginate = app(PaginatorService::class)->paginate(
|
||||
perPage: $request->input('per_page', 15),
|
||||
page: $request->input('page', 1),
|
||||
total: Usuario::all()->count(),
|
||||
total: $usuarios->count(),
|
||||
route: 'users.all',
|
||||
);
|
||||
|
||||
$data = Usuario::with('restaurantes')
|
||||
$data = $usuarios
|
||||
->skip($paginate['from'] - 1)
|
||||
->take($paginate['per_page'])->get()->all();
|
||||
->take($paginate['per_page'])->all();
|
||||
|
||||
return response()->json([
|
||||
'pagination' => $paginate,
|
||||
@@ -41,22 +42,15 @@ class UsuariosController extends Controller {
|
||||
|
||||
/**
|
||||
* Obtiene un usuario por su id
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function get($id) {
|
||||
if (!str_starts_with($id, 'auth0')) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
}
|
||||
|
||||
public function get(Request $request, $id) {
|
||||
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||
$usuario = Usuario::findOrFail($id);
|
||||
|
||||
return response()->json($usuario);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea un nuevo usuario localmente y en auth0
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(Request $request) {
|
||||
$this->validate($request, [
|
||||
@@ -70,7 +64,6 @@ class UsuariosController extends Controller {
|
||||
]);
|
||||
|
||||
$restaurant = Restaurante::findOrFail($request->input('restaurant'));
|
||||
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
||||
|
||||
$auth0 = app(Auth0Service::class);
|
||||
$auth0User = $auth0->createUser(
|
||||
@@ -101,15 +94,9 @@ class UsuariosController extends Controller {
|
||||
|
||||
/**
|
||||
* Actualiza un usuario
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function update(Request $request, $id) {
|
||||
if (!str_starts_with($id, 'auth0')) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
}
|
||||
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||
|
||||
$this->validate($request, [
|
||||
'nombre' => 'sometimes',
|
||||
@@ -122,8 +109,6 @@ class UsuariosController extends Controller {
|
||||
|
||||
$usuario = Usuario::findOrFail($id);
|
||||
|
||||
$this->canManageUserOrFail($request->user, $usuario);
|
||||
|
||||
$metadata = [];
|
||||
if ($request->input('roles')) $metadata['roles'] = $request->input('roles');
|
||||
|
||||
@@ -151,19 +136,12 @@ class UsuariosController extends Controller {
|
||||
|
||||
/**
|
||||
* Elimina un usuario
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function delete(Request $request, $id) {
|
||||
if (!str_starts_with($id, 'auth0')) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
}
|
||||
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||
|
||||
$usuario = Usuario::findOrFail($id);
|
||||
|
||||
$this->canManageUserOrFail($request->user, $usuario);
|
||||
|
||||
$auth0 = app(Auth0Service::class);
|
||||
$auth0Response = $auth0->deleteUser($usuario->auth0_id);
|
||||
|
||||
@@ -181,24 +159,14 @@ class UsuariosController extends Controller {
|
||||
|
||||
/**
|
||||
* Agrega usuario a un restaurant
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @param $restaurant
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function addToRestaurant(Request $request, $id, $restaurant) {
|
||||
if (!str_starts_with($id, 'auth0')) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
}
|
||||
if (!str_starts_with($restaurant, 'auth0')) {
|
||||
app(UuidService::class)->validOrFail($restaurant);
|
||||
}
|
||||
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||
app(UuidService::class)->validOrFail($restaurant);
|
||||
|
||||
$usuario = Usuario::findOrFail($id);
|
||||
$restaurant = Restaurante::findOrFail($restaurant);
|
||||
|
||||
$this->canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
||||
|
||||
if ($usuario->restaurantes->contains($restaurant)) {
|
||||
return response()->json([
|
||||
'error' => 'already_on_restaurant',
|
||||
@@ -213,25 +181,14 @@ class UsuariosController extends Controller {
|
||||
|
||||
/**
|
||||
* Saca a un usuario de un restaurant
|
||||
*
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @param $restaurant
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function removeFromRestaurant(Request $request, $id, $restaurant) {
|
||||
if (!str_starts_with($id, 'auth0')) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
}
|
||||
if (!str_starts_with($restaurant, 'auth0')) {
|
||||
if (!str_starts_with($id, 'auth0')) app(UuidService::class)->validOrFail($id);
|
||||
app(UuidService::class)->validOrFail($restaurant);
|
||||
}
|
||||
|
||||
$usuario = Usuario::findOrFail($id);
|
||||
$restaurant = Restaurante::findOrFail($restaurant);
|
||||
|
||||
canManageUsersAndRestaurantOrFail($request->user, $restaurant);
|
||||
|
||||
if (!$usuario->restaurantes->contains($restaurant)) {
|
||||
return response()->json([
|
||||
'error' => 'already_not_on_restaurant',
|
||||
@@ -243,32 +200,4 @@ class UsuariosController extends Controller {
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user