1 <?php namespace BookStack;
3 class Role extends Model
6 protected $fillable = ['display_name', 'description'];
9 * The roles that belong to the role.
11 public function users()
13 return $this->belongsToMany(User::class);
17 * Get all related JointPermissions.
18 * @return \Illuminate\Database\Eloquent\Relations\HasMany
20 public function jointPermissions()
22 return $this->hasMany(JointPermission::class);
26 * The RolePermissions that belong to the role.
28 public function permissions()
30 return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
34 * Check if this role has a permission.
35 * @param $permissionName
38 public function hasPermission($permissionName)
40 $permissions = $this->getRelationValue('permissions');
41 foreach ($permissions as $permission) {
42 if ($permission->getRawAttribute('name') === $permissionName) {
50 * Add a permission to this role.
51 * @param RolePermission $permission
53 public function attachPermission(RolePermission $permission)
55 $this->permissions()->attach($permission->id);
59 * Detach a single permission from this role.
60 * @param RolePermission $permission
62 public function detachPermission(RolePermission $permission)
64 $this->permissions()->detach($permission->id);
68 * Get the role object for the specified role.
72 public static function getRole($roleName)
74 return static::where('name', '=', $roleName)->first();
78 * Get the role object for the specified system role.
82 public static function getSystemRole($roleName)
84 return static::where('system_name', '=', $roleName)->first();
88 * Get all visible roles
91 public static function visible()
93 return static::where('hidden', '=', false)->orderBy('name')->get();