124 lines
3.7 KiB
PHP
124 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Services\Auth0Service;
|
|
use App\Traits\UuidPrimaryKey;
|
|
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;
|
|
|
|
protected $table = 'usuarios';
|
|
protected $fillable = ['id', 'auth0_id', 'nombre'];
|
|
protected $appends = ['roles'];
|
|
|
|
/**
|
|
* Busca un usuario según su id o auth_0 id, dependiendo del formato entregado en $id
|
|
* @param $id
|
|
* @return Usuario
|
|
*/
|
|
public static function findByIdOrAuth0Id($id) {
|
|
if (str_starts_with($id, 'auth0')) {
|
|
return Usuario::where('auth0_id', urldecode($id))->with('restaurantes')->first();
|
|
} else {
|
|
try {
|
|
return Usuario::where('id', $id)->with('restaurantes')->first();
|
|
} catch (QueryException $ex) {
|
|
Log::warning('Se intento obtener un usuario con un id invalido', ['id' => $id]);
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
public function canManageRestaurants() {
|
|
if (in_array('global_admin', $this->roles)) return true;
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* 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 restaurantes() {
|
|
return $this->belongsToMany(Restaurante::class, 'usuarios_restaurantes', 'usuario_id', 'restaurante_id');
|
|
}
|
|
|
|
public function administrador() {
|
|
return $this->hasOne(Administrador::class);
|
|
}
|
|
|
|
public function recaudador() {
|
|
return $this->hasOne(Recaudador::class);
|
|
}
|
|
|
|
public function mesero() {
|
|
return $this->hasOne(Mesero::class);
|
|
}
|
|
|
|
public function productor() {
|
|
return $this->hasOne(Productor::class);
|
|
}
|
|
|
|
public function getRolesAttribute() {
|
|
$auth0Service = app(Auth0Service::class);
|
|
$auth0User = $auth0Service->getUser($this->auth0_id);
|
|
return $this->attributes['roles'] = array_key_exists('app_metadata', $auth0User) ? $auth0User['app_metadata']['roles'] : [];
|
|
}
|
|
}
|