1 <?php namespace BookStack;
4 class Role extends Model
7 protected $fillable = ['display_name', 'description'];
10 * The roles that belong to the role.
12 public function users()
14 return $this->belongsToMany(User::class);
18 * Get all related JointPermissions.
19 * @return \Illuminate\Database\Eloquent\Relations\HasMany
21 public function jointPermissions()
23 return $this->hasMany(JointPermission::class);
27 * The RolePermissions that belong to the role.
29 public function permissions()
31 return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
35 * Check if this role has a permission.
36 * @param $permissionName
39 public function hasPermission($permissionName)
41 $permissions = $this->getRelationValue('permissions');
42 foreach ($permissions as $permission) {
43 if ($permission->getRawAttribute('name') === $permissionName) return true;
49 * Add a permission to this role.
50 * @param RolePermission $permission
52 public function attachPermission(RolePermission $permission)
54 $this->permissions()->attach($permission->id);
58 * Detach a single permission from this role.
59 * @param RolePermission $permission
61 public function detachPermission(RolePermission $permission)
63 $this->permissions()->detach($permission->id);
67 * Get the role object for the specified role.
71 public static function getRole($roleName)
73 return static::where('name', '=', $roleName)->first();
77 * Get the role object for the specified system role.
81 public static function getSystemRole($roleName)
83 return static::where('system_name', '=', $roleName)->first();
87 * Get all visible roles
90 public static function visible()
92 return static::where('hidden', '=', false)->orderBy('name')->get();