1 <?php namespace BookStack\Auth;
3 use BookStack\Auth\Permissions\JointPermission;
4 use BookStack\Auth\Permissions\RolePermission;
6 use Illuminate\Database\Eloquent\Collection;
7 use Illuminate\Database\Eloquent\Relations\HasMany;
12 * @property string $display_name
13 * @property string $description
14 * @property string $external_auth_id
15 * @property string $system_name
17 class Role extends Model
20 protected $fillable = ['display_name', 'description', 'external_auth_id'];
23 * The roles that belong to the role.
25 public function users()
27 return $this->belongsToMany(User::class)->orderBy('name', 'asc');
31 * Get all related JointPermissions.
33 public function jointPermissions(): HasMany
35 return $this->hasMany(JointPermission::class);
39 * The RolePermissions that belong to the role.
41 public function permissions()
43 return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
47 * Check if this role has a permission.
49 public function hasPermission(string $permissionName): bool
51 $permissions = $this->getRelationValue('permissions');
52 foreach ($permissions as $permission) {
53 if ($permission->getRawAttribute('name') === $permissionName) {
61 * Add a permission to this role.
63 public function attachPermission(RolePermission $permission)
65 $this->permissions()->attach($permission->id);
69 * Detach a single permission from this role.
71 public function detachPermission(RolePermission $permission)
73 $this->permissions()->detach([$permission->id]);
77 * Get the role of the specified display name.
79 public static function getRole(string $displayName): ?Role
81 return static::query()->where('display_name', '=', $displayName)->first();
85 * Get the role object for the specified system role.
87 public static function getSystemRole(string $systemName): ?Role
89 return static::query()->where('system_name', '=', $systemName)->first();
93 * Get all visible roles
95 public static function visible(): Collection
97 return static::query()->where('hidden', '=', false)->orderBy('name')->get();
101 * Get the roles that can be restricted.
103 public static function restrictable(): Collection
105 return static::query()->where('system_name', '!=', 'admin')->get();