Files
wticreatorstudio/Modules/Chat/Entities/Role.php
T
2024-02-12 22:54:20 -05:00

216 lines
6.4 KiB
PHP

<?php
namespace Modules\Chat\Entities;
use Eloquent as Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Carbon;
use Spatie\Permission\Contracts\Role as RoleContract;
use Spatie\Permission\Exceptions\GuardDoesNotMatch;
use Spatie\Permission\Exceptions\RoleAlreadyExists;
use Spatie\Permission\Exceptions\RoleDoesNotExist;
use Spatie\Permission\Guard;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Traits\HasPermissions;
use Spatie\Permission\Traits\RefreshesPermissionCache;
/**
* Class Role
*
* @version November 12, 2019, 11:13 am UTC
* @property int $id
* @property string $name
* @property string $guard_name
* @property Carbon|null $created_at
* @property Carbon|null $updated_at
* @method static Builder|Role newModelQuery()
* @method static Builder|Role newQuery()
* @method static Builder|Role query()
* @method static Builder|Role whereCreatedAt($value)
* @method static Builder|Role whereGuardName($value)
* @method static Builder|Role whereId($value)
* @method static Builder|Role whereName($value)
* @method static Builder|Role whereUpdatedAt($value)
* @mixin Model
* @property int $is_default
* @method static Builder|Role whereIsDefault($value)
* @property-write mixed $raw
* @property-read \Illuminate\Database\Eloquent\Collection|\Modules\Chat\Entities[] $users
* @property-read int|null $users_count
*/
class Role extends Model //implements RoleContract
{
use HasFactory;
//use HasPermissions;
//use RefreshesPermissionCache;
public $table = 'roles';
public $fillable = [
'name',
'guard_name',
'is_default',
];
const ADMIN_ROLE = 1;
const MEMBER_ROLE = 2;
const MEMBER_ROLE_NAME = 'Member';
const ADMIN_ROLE_NAME = 'Admin';
protected $with = ['permissions'];
/**
* The attributes that should be casted to native types.
*
* @var array
*/
protected $casts = [
'id' => 'integer',
];
/**
* Validation rules
*
* @var array
*/
public static $rules = [
'name' => 'required',
'permissions' => 'required',
];
/**
* @return MorphToMany
*/
public function users()
{
return $this->morphedByMany(User::class, 'model', 'model_has_roles', 'role_id', 'model_id');
}
public function __construct(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? config('auth.defaults.guard');
parent::__construct($attributes);
}
/**
* Find a role by its name and guard name.
*
* @param string $name
* @param string|null $guardName
*
* @throws \Spatie\Permission\Exceptions\RoleDoesNotExist
* @return \Spatie\Permission\Contracts\Role|\Spatie\Permission\Models\Role
*/
public static function findByName(string $name, $guardName = null): RoleContract
{
$guardName = $guardName ?? Guard::getDefaultName(static::class);
$role = static::where('name', $name)->where('guard_name', $guardName)->first();
if (! $role) {
throw RoleDoesNotExist::named($name);
}
return $role;
}
public static function findById(int $id, $guardName = null): RoleContract
{
$guardName = $guardName ?? Guard::getDefaultName(static::class);
$role = static::where('id', $id)->where('guard_name', $guardName)->first();
if (! $role) {
throw RoleDoesNotExist::withId($id);
}
return $role;
}
/**
* Find or create role by its name (and optionally guardName).
*
* @param string $name
* @param string|null $guardName
*
* @return \Spatie\Permission\Contracts\Role
*/
public static function findOrCreate(string $name, $guardName = null): RoleContract
{
$guardName = $guardName ?? Guard::getDefaultName(static::class);
$role = static::where('name', $name)->where('guard_name', $guardName)->first();
if (! $role) {
return static::query()->create(['name' => $name, 'guard_name' => $guardName]);
}
return $role;
}
public static function create(array $attributes = [])
{
$attributes['guard_name'] = $attributes['guard_name'] ?? Guard::getDefaultName(static::class);
if (static::where('name', $attributes['name'])->where('guard_name', $attributes['guard_name'])->first()) {
throw RoleAlreadyExists::create($attributes['name'], $attributes['guard_name']);
}
return static::query()->create($attributes);
}
public function getTable()
{
return config('permission.table_names.roles', parent::getTable());
}
/**
* A role may be given various permissions.
*/
public function permissions(): BelongsToMany
{
return $this->belongsToMany(
config('permission.models.permission'),
config('permission.table_names.role_has_permissions'),
'role_id',
'permission_id'
);
}
/**
* Determine if the user may perform the given permission.
*
* @param string|Permission $permission
*
* @throws \Spatie\Permission\Exceptions\GuardDoesNotMatch
* @return bool
*/
public function hasPermissionTo($permission): bool
{
if (config('permission.enable_wildcard_permission', false)) {
return $this->hasWildcardPermission($permission, $this->getDefaultGuardName());
}
$permissionClass = $this->getPermissionClass();
if (is_string($permission)) {
$permission = $permissionClass->findByName($permission, $this->getDefaultGuardName());
}
if (is_int($permission)) {
$permission = $permissionClass->findById($permission, $this->getDefaultGuardName());
}
if (! $this->getGuardNames()->contains($permission->guard_name)) {
throw GuardDoesNotMatch::create($permission->guard_name, $this->getGuardNames());
}
return $this->permissions->contains('id', $permission->id);
}
}