1 <?php namespace BookStack\Auth;
3 use BookStack\Auth\Permissions\JointPermission;
4 use BookStack\Auth\Permissions\RolePermission;
5 use BookStack\Interfaces\Loggable;
7 use Illuminate\Database\Eloquent\Collection;
8 use Illuminate\Database\Eloquent\Relations\HasMany;
13 * @property string $display_name
14 * @property string $description
15 * @property string $external_auth_id
16 * @property string $system_name
18 class Role extends Model implements Loggable
21 protected $fillable = ['display_name', 'description', 'external_auth_id'];
24 * The roles that belong to the role.
26 public function users()
28 return $this->belongsToMany(User::class)->orderBy('name', 'asc');
32 * Get all related JointPermissions.
34 public function jointPermissions(): HasMany
36 return $this->hasMany(JointPermission::class);
40 * The RolePermissions that belong to the role.
42 public function permissions()
44 return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
48 * Check if this role has a permission.
50 public function hasPermission(string $permissionName): bool
52 $permissions = $this->getRelationValue('permissions');
53 foreach ($permissions as $permission) {
54 if ($permission->getRawAttribute('name') === $permissionName) {
62 * Add a permission to this role.
64 public function attachPermission(RolePermission $permission)
66 $this->permissions()->attach($permission->id);
70 * Detach a single permission from this role.
72 public function detachPermission(RolePermission $permission)
74 $this->permissions()->detach([$permission->id]);
78 * Get the role of the specified display name.
80 public static function getRole(string $displayName): ?Role
82 return static::query()->where('display_name', '=', $displayName)->first();
86 * Get the role object for the specified system role.
88 public static function getSystemRole(string $systemName): ?Role
90 return static::query()->where('system_name', '=', $systemName)->first();
94 * Get all visible roles
96 public static function visible(): Collection
98 return static::query()->where('hidden', '=', false)->orderBy('name')->get();
102 * Get the roles that can be restricted.
104 public static function restrictable(): Collection
106 return static::query()->where('system_name', '!=', 'admin')->get();
112 public function logDescriptor(): string
114 return "({$this->id}) {$this->display_name}";