Removi las validaciones por ahora :/

This commit is contained in:
2021-07-09 01:10:43 -04:00
parent 61b9e25c3b
commit 450a171827
9 changed files with 107 additions and 387 deletions

View File

@@ -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() {