100 lines
2.2 KiB
PHP
100 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Foundation\Auth\User as Authenticatable;
|
|
use Illuminate\Notifications\Notifiable;
|
|
use Laravel\Sanctum\HasApiTokens;
|
|
use Illuminate\Support\Str;
|
|
use Spatie\Permission\Traits\HasRoles;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
use HasApiTokens, HasFactory, Notifiable, HasRoles;
|
|
public $incrementing = false;
|
|
protected $keyType = "string";
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var string[]
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'nrk',
|
|
'password',
|
|
'role_name'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [
|
|
//'email_verified_at' => 'datetime',
|
|
];
|
|
|
|
public function roleUser()
|
|
{
|
|
return $this->hasOne(RoleUser::class, 'user_id', 'id');
|
|
}
|
|
|
|
protected static function boot() {
|
|
static::creating(function ($model) {
|
|
if (!$model->id){
|
|
$model->id = (string) Str::uuid();
|
|
}
|
|
});
|
|
parent::boot();
|
|
}
|
|
|
|
public function authorizeRoles($roles)
|
|
{
|
|
if ($this->hasAnyRole($roles)) {
|
|
return true;
|
|
}
|
|
abort(403, 'This action is unauthorized.');
|
|
}
|
|
|
|
// public function hasAnyRole($roles)
|
|
// {
|
|
// if (is_array($roles)) {
|
|
// foreach ($roles as $role) {
|
|
// if ($this->hasRole($role)) {
|
|
// return true;
|
|
// }
|
|
// }
|
|
// } else {
|
|
// if ($this->hasRole($roles)) {
|
|
// return true;
|
|
// }
|
|
// }
|
|
// return false;
|
|
// }
|
|
|
|
// public function hasRole($role)
|
|
// {
|
|
// $hasRole = $this->roleUser()->with(["role"=>function($query) use ($role){
|
|
// $query->where("name",$role);
|
|
// }])->first();
|
|
|
|
// if ($hasRole->role){
|
|
// return true;
|
|
// }else{
|
|
// return false;
|
|
// }
|
|
// }
|
|
}
|