1 <?php namespace BookStack\Auth;
3 use BookStack\Auth\Permissions\JointPermission;
4 use BookStack\Auth\Permissions\RolePermission;
9 * @property string $display_name
10 * @property string $description
11 * @property string $external_auth_id
12 * @package BookStack\Auth
14 class Role extends Model
17 protected $fillable = ['display_name', 'description', 'external_auth_id'];
20 * The roles that belong to the role.
22 public function users()
24 return $this->belongsToMany(User::class)->orderBy('name', 'asc');
28 * Get all related JointPermissions.
29 * @return \Illuminate\Database\Eloquent\Relations\HasMany
31 public function jointPermissions()
33 return $this->hasMany(JointPermission::class);
37 * The RolePermissions that belong to the role.
39 public function permissions()
41 return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
45 * Check if this role has a permission.
46 * @param $permissionName
49 public function hasPermission($permissionName)
51 $permissions = $this->getRelationValue('permissions');
52 foreach ($permissions as $permission) {
53 if ($permission->getRawAttribute('name') === $permissionName) {
61 * Add a permission to this role.
62 * @param RolePermission $permission
64 public function attachPermission(RolePermission $permission)
66 $this->permissions()->attach($permission->id);
70 * Detach a single permission from this role.
71 * @param RolePermission $permission
73 public function detachPermission(RolePermission $permission)
75 $this->permissions()->detach([$permission->id]);
79 * Get the role object for the specified role.
83 public static function getRole($roleName)
85 return static::query()->where('name', '=', $roleName)->first();
89 * Get the role object for the specified system role.
93 public static function getSystemRole($roleName)
95 return static::query()->where('system_name', '=', $roleName)->first();
99 * Get all visible roles
102 public static function visible()
104 return static::query()->where('hidden', '=', false)->orderBy('name')->get();
108 * Get the roles that can be restricted.
109 * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection
111 public static function restrictable()
113 return static::query()->where('system_name', '!=', 'admin')->get();