Removi las validaciones por ahora :/
This commit is contained in:
@@ -29,9 +29,15 @@ class Handler extends ExceptionHandler {
|
||||
return $rendered;
|
||||
}
|
||||
|
||||
if(config('app.debug') === false) {
|
||||
$message = $exception instanceof HttpException ? $exception->getMessage() : 'Server Error';
|
||||
} else {
|
||||
$message = $exception->getMessage();
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => $rendered->getStatusCode(),
|
||||
'message' => $exception instanceof HttpException ? $exception->getMessage() : 'Server Error',
|
||||
'message' => $message,
|
||||
], $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 {
|
||||
|
||||
/**
|
||||
* Obtiene de forma paginada los canales de venta
|
||||
* de un restaurant registrados en el backend
|
||||
* @param Request $request
|
||||
* @param $restaurant
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function all(Request $request, $restaurante_id) {
|
||||
app(UuidService::class)->validOrFail($restaurante_id);
|
||||
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||
/**
|
||||
* Obtiene de forma paginada los canales de venta
|
||||
*/
|
||||
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(
|
||||
perPage: $request->input('per_page', 15),
|
||||
page: $request->input('page', 1),
|
||||
total: $canalesVenta->count(),
|
||||
route: 'canales-venta.all',
|
||||
data: [$restaurante_id],
|
||||
);
|
||||
$paginate = app(PaginatorService::class)->paginate(
|
||||
perPage: $request->input('per_page', 15),
|
||||
page: $request->input('page', 1),
|
||||
total: $canalesVenta->count(),
|
||||
route: 'canales-venta.all',
|
||||
data: ['restaurante_id' => $restaurante_id],
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'pagination' => $paginate,
|
||||
'data' => array_values($canalesVenta->get()->skip($paginate['from'] - 1)->take($paginate['per_page'])->all())
|
||||
]);
|
||||
$data = $canalesVenta->get()
|
||||
->skip($paginate['from'] - 1)
|
||||
->take($paginate['per_page'])
|
||||
->all();
|
||||
|
||||
}
|
||||
return response()->json([
|
||||
'pagination' => $paginate,
|
||||
'data' => $data
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene un canal de venta por su id
|
||||
* @param $restaurante_id
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function get($restaurante_id, $id) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
app(UuidService::class)->validOrFail($restaurante_id);
|
||||
/**
|
||||
* Obtiene un canal de venta por su id
|
||||
*/
|
||||
public function get($restaurante_id, $id) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
app(UuidService::class)->validOrFail($restaurante_id);
|
||||
|
||||
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||
$canalVenta = CanalVenta::findOrFail($id);
|
||||
$restaurante = Restaurante::findOrFail($restaurante_id);
|
||||
$canalVenta = CanalVenta::findOrFail($id);
|
||||
|
||||
return response()->json($canalVenta);
|
||||
}
|
||||
return response()->json($canalVenta);
|
||||
}
|
||||
|
||||
///**
|
||||
// * Crea un nuevo restaurant
|
||||
// * @param Request $request
|
||||
// * @return JsonResponse
|
||||
// * @throws ValidationException
|
||||
// */
|
||||
//public function create(Request $request) {
|
||||
// $this->validate($request, [
|
||||
// 'nombre' => 'required'
|
||||
// ]);
|
||||
/**
|
||||
* Crea un nuevo canal de venta
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(Request $request) {
|
||||
$this->validate($request, [
|
||||
'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()) {
|
||||
// return response()->json([
|
||||
// 'error' => 'cant_manage_restaurants',
|
||||
// 'message' => 'El usuario ' . $request->user->id . ' no tiene permisos para manipular restaurantes'
|
||||
// ], 403);
|
||||
// }
|
||||
$restaurant = Restaurante::create([
|
||||
'id' => Uuid::uuid4(),
|
||||
'nombre' => $request->input('nombre')
|
||||
]);
|
||||
|
||||
// $restaurant = Restaurante::create([
|
||||
// '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);
|
||||
//}
|
||||
return response()->json($restaurant, 201);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,6 +7,6 @@ use Laravel\Lumen\Routing\Controller as BaseController;
|
||||
|
||||
class Controller extends BaseController {
|
||||
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\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\ValidationException;
|
||||
@@ -13,43 +16,37 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* Obtiene de forma paginada los restaurantes registrados en el backend
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function all(Request $request) {
|
||||
$restaurantes = Restaurante::all();
|
||||
|
||||
$paginate = app(PaginatorService::class)->paginate(
|
||||
perPage: $request->input('per_page', 15),
|
||||
page: $request->input('page', 1),
|
||||
total: Restaurante::all()->count(),
|
||||
total: $restaurantes->count(),
|
||||
route: 'restaurant.all',
|
||||
);
|
||||
|
||||
$data = $restaurantes
|
||||
->skip($paginate['from'] - 1)
|
||||
->take($paginate['per_page'])
|
||||
->all();
|
||||
|
||||
return response()->json([
|
||||
'pagination' => $paginate,
|
||||
'data' => array_values(Restaurante::all()->skip($paginate['from'] - 1)->take($paginate['per_page'])->all())
|
||||
'data' => $data
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene un restaurante 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);
|
||||
}
|
||||
public function get(Request $request, $id) {
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
$restaurante = Restaurante::findOrFail($id);
|
||||
|
||||
$restaurante = Restaurante::findOrNull($id);
|
||||
|
||||
if (!$restaurante) {
|
||||
return response()->json([
|
||||
'error' => 'restaurant_not_found',
|
||||
'message' => 'El restaurant con id ' . $id . ' no existe'
|
||||
], 404);
|
||||
if(!$request->user->isOnRestaurant($restaurante)){
|
||||
return ModelNotFoundException('restaurante', $restaurante->id);
|
||||
}
|
||||
|
||||
return response()->json($restaurante);
|
||||
@@ -57,22 +54,12 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* Crea un nuevo restaurant
|
||||
* @param Request $request
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
public function create(Request $request) {
|
||||
$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::create([
|
||||
'id' => Uuid::uuid4(),
|
||||
'nombre' => $request->input('nombre')
|
||||
@@ -83,38 +70,15 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* Actualiza un restaurante
|
||||
* @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);
|
||||
}
|
||||
app(UuidService::class)->validOrFail($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 = Restaurante::findOrFail($id);
|
||||
$restaurant->nombre = $request->input('nombre');
|
||||
$restaurant->save();
|
||||
|
||||
@@ -123,36 +87,12 @@ class RestaurantesController extends Controller {
|
||||
|
||||
/**
|
||||
* Elimina un restaurante
|
||||
* @param Request $request
|
||||
* @param $id
|
||||
* @return JsonResponse
|
||||
* @throws ValidationException
|
||||
*/
|
||||
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 (!$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);
|
||||
}
|
||||
app(UuidService::class)->validOrFail($id);
|
||||
|
||||
$restaurant = Restaurante::findOrFail($id);
|
||||
$restaurant->delete();
|
||||
|
||||
return response()->json($restaurant);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\Traits\UuidPrimaryKey;
|
||||
use App\Exceptions\ModelNotFoundException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
|
||||
@@ -10,11 +10,6 @@ use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
/**
|
||||
* @method static create(array $array)
|
||||
* @method static find($id)
|
||||
* @property mixed id
|
||||
*/
|
||||
class Restaurante extends Model {
|
||||
use UuidPrimaryKey, SoftDeletes;
|
||||
|
||||
@@ -24,11 +19,7 @@ class Restaurante extends Model {
|
||||
|
||||
public static function findOrFail($id) {
|
||||
$restaurante = Restaurante::find($id);
|
||||
|
||||
if(!$restaurante){
|
||||
throw new ModelNotFoundException("restaurant", $id);
|
||||
}
|
||||
|
||||
if(!$restaurante) throw new ModelNotFoundException("restaurant", $id);
|
||||
return $restaurante;
|
||||
}
|
||||
|
||||
|
||||
@@ -5,28 +5,13 @@ namespace App\Models;
|
||||
use App\Services\Auth0Service;
|
||||
use App\Traits\UuidPrimaryKey;
|
||||
use App\Exceptions\ModelNotFoundException;
|
||||
use App\Exceptions\HasNoPermissionsOnRestaurantException;
|
||||
use App\Exceptions\CantManageRestaurantsException;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Database\QueryException;
|
||||
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 {
|
||||
use UuidPrimaryKey, SoftDeletes;
|
||||
|
||||
@@ -35,66 +20,23 @@ class Usuario extends Model {
|
||||
protected $appends = ['roles'];
|
||||
|
||||
/**
|
||||
* Busca un usuario según su id o auth_0 id, dependiendo del formato entregado en $id
|
||||
* @param $id
|
||||
* @return Usuario
|
||||
* Busca un usuario o envia una excepcion si no se encuentra
|
||||
*/
|
||||
public static function findOrFail($id) {
|
||||
if (str_starts_with($id, 'auth0')) {
|
||||
$usuario = Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first();
|
||||
} else {
|
||||
$usuario = Usuario::where('id', $id)->with('restaurantes')->first();
|
||||
}
|
||||
if (str_starts_with($id, 'auth0')) $usuario = Usuario::where('auth0_id', urldecode($id));
|
||||
else $usuario = Usuario::where('id', $id);
|
||||
|
||||
if(!$usuario) {
|
||||
throw new ModelNotFoundException("usuario", $id);
|
||||
}
|
||||
if(!$usuario) throw new ModelNotFoundException("usuario", $id);
|
||||
|
||||
return $usuario;
|
||||
return $usuario->first();
|
||||
}
|
||||
|
||||
public function canManageRestaurants() {
|
||||
if (in_array('global_admin', $this->roles)) return true;
|
||||
return false;
|
||||
public function isGlobalAdmin() {
|
||||
return in_array('global_admin', $this->roles);
|
||||
}
|
||||
|
||||
/**
|
||||
* Valida que el usuario tiene permisos sobre otro usuario
|
||||
*
|
||||
* 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 isAdmin() {
|
||||
return in_array('admin', $this->roles);
|
||||
}
|
||||
|
||||
public function restaurantes() {
|
||||
|
||||
Reference in New Issue
Block a user