Files
unified-restaurant-original/backend/app/Models/Usuario.php
2021-04-30 02:23:57 -04:00

64 lines
1.6 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 Ramsey\Uuid\Uuid;
/**
* Class Usuario
*
* @property $id
* @property $auth0_id
* @property $nombre
*
* @method static find($id)
* @method static where(string $string, $sub)
*
* @package App\Models
*
*/
class Usuario extends Model {
use UuidPrimaryKey, SoftDeletes;
protected $table = 'usuarios';
protected $appends = ['roles'];
public static function findByIdOrAuth0Id($id) {
if (str_starts_with($id, 'auth0')) {
return Usuario::where('auth0_id', urldecode($id))->first();
} else {
return Usuario::where('id', $id)->first();
}
}
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'] : [];
}
}