99 lines
3.6 KiB
PHP
99 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models\Permission;
|
|
use App\Models\Account\Account;
|
|
use Dipper\Foundation\Core\Model;
|
|
use App\Exceptions\NotExistException;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Dipper\Foundation\Nestedset\NodeTrait;
|
|
use App\Models\Permission\Traits\HasPermissions;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
use App\Models\Permission\Traits\RefreshesPermissionCache;
|
|
|
|
/**
|
|
* 角色
|
|
*
|
|
* @property int $id
|
|
* @property string $name 标识
|
|
* @property string $remark 备注
|
|
* @property int $_lft
|
|
* @property int $_rgt
|
|
* @property int $parent_id
|
|
* @property \Illuminate\Support\Carbon|null $created_at
|
|
* @property \Illuminate\Support\Carbon|null $updated_at
|
|
* @property-read \Dipper\Foundation\Nestedset\Collection|\App\Models\Account\Account[] $accounts
|
|
* @property-read \Dipper\Foundation\Nestedset\Collection|\App\Models\Permission\Role[] $children
|
|
* @property-read \App\Models\Permission\Role $parent
|
|
* @property-read \Dipper\Foundation\Nestedset\Collection|\App\Models\Permission\Permission[] $permissions
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role d()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role newModelQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role newQuery()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role permission($permissions)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role query()
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereCreatedAt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereLft($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereName($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereParentId($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereRemark($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereRgt($value)
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Permission\Role whereUpdatedAt($value)
|
|
* @mixin \Eloquent
|
|
*/
|
|
class Role extends Model
|
|
{
|
|
use NodeTrait, HasPermissions, RefreshesPermissionCache;
|
|
|
|
protected $table = 'roles';
|
|
|
|
/**
|
|
* Get the hidden attributes for the model.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getHidden()
|
|
{
|
|
return [$this->getLftName(), $this->getRgtName()];
|
|
}
|
|
|
|
/**
|
|
* A role belongs to some users of the model associated.
|
|
*/
|
|
public function accounts(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Account::class, 'account_has_roles', 'role_id', 'account_id');
|
|
}
|
|
|
|
|
|
public static function findById(int $id): Role
|
|
{
|
|
$role = static::where('id', $id)->first();
|
|
|
|
if (!$role) {
|
|
throw new NotExistException('角色不存在');
|
|
}
|
|
|
|
return $role;
|
|
}
|
|
|
|
/**
|
|
* Determine if the user may perform the given permission.
|
|
*
|
|
* @param string|Permission $permission
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function hasPermissionTo($permission): bool
|
|
{
|
|
if (is_string($permission)) {
|
|
$permission = app(Permission::class)->findByName($permission);
|
|
}
|
|
|
|
if (is_int($permission)) {
|
|
$permission = app(Permission::class)->findById($permission);
|
|
}
|
|
|
|
return $this->permissions->contains('id', $permission->id);
|
|
}
|
|
}
|