121 lines
2.7 KiB
PHP
121 lines
2.7 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;
|
|
|
|
class User extends Authenticatable
|
|
{
|
|
/** @use HasFactory<\Database\Factories\UserFactory> */
|
|
use HasFactory, Notifiable;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'name',
|
|
'slug',
|
|
'email',
|
|
'password',
|
|
'is_dummy_account',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be hidden for serialization.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $hidden = [
|
|
'password',
|
|
'remember_token',
|
|
];
|
|
|
|
/**
|
|
* Get the attributes that should be cast.
|
|
*
|
|
* @return array<string, string>
|
|
*/
|
|
protected function casts(): array
|
|
{
|
|
return [
|
|
'email_verified_at' => 'datetime',
|
|
'password' => 'hashed',
|
|
];
|
|
}
|
|
|
|
public function mediaLists()
|
|
{
|
|
return $this->belongsToMany(MediaList::class, 'user_media_lists', 'user_id', 'media_list_id');
|
|
}
|
|
|
|
public function roles()
|
|
{
|
|
return $this->belongsToMany(Role::class, 'role_user');
|
|
}
|
|
|
|
public function permissions()
|
|
{
|
|
return $this->belongsToMany(Permission::class, 'permission_user');
|
|
}
|
|
|
|
public function hasRole($role)
|
|
{
|
|
if (is_string($role)) {
|
|
return $this->roles->contains('slug', $role);
|
|
}
|
|
|
|
return $this->roles->contains('id', $role->id);
|
|
}
|
|
|
|
public function hasPermission($permission)
|
|
{
|
|
if (is_string($permission)) {
|
|
// Check direct permissions
|
|
if ($this->permissions->contains('slug', $permission)) {
|
|
return true;
|
|
}
|
|
|
|
// Check role permissions
|
|
foreach ($this->roles as $role) {
|
|
if ($role->hasPermission($permission)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
return $this->permissions->contains('id', $permission->id);
|
|
}
|
|
|
|
public function hasAnyRole($roles)
|
|
{
|
|
if (is_string($roles)) {
|
|
return $this->roles->contains('slug', $roles);
|
|
}
|
|
|
|
foreach ($roles as $role) {
|
|
if ($this->hasRole($role)) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public function assignRole($role)
|
|
{
|
|
return $this->roles()->save($role);
|
|
}
|
|
|
|
public function givePermissionTo($permission)
|
|
{
|
|
return $this->permissions()->save($permission);
|
|
}
|
|
}
|