1 <?php namespace BookStack\Auth;
3 use BookStack\Auth\Permissions\JointPermission;
6 class Role extends Model
9 protected $fillable = ['display_name', 'description', 'external_auth_id'];
12 * The roles that belong to the role.
14 public function users()
16 return $this->belongsToMany(User::class);
20 * Get all related JointPermissions.
21 * @return \Illuminate\Database\Eloquent\Relations\HasMany
23 public function jointPermissions()
25 return $this->hasMany(JointPermission::class);
29 * The RolePermissions that belong to the role.
31 public function permissions()
33 return $this->belongsToMany(Permissions\RolePermission::class, 'permission_role', 'role_id', 'permission_id');
37 * Check if this role has a permission.
38 * @param $permissionName
41 public function hasPermission($permissionName)
43 $permissions = $this->getRelationValue('permissions');
44 foreach ($permissions as $permission) {
45 if ($permission->getRawAttribute('name') === $permissionName) {
53 * Add a permission to this role.
54 * @param \BookStack\Auth\Permissions\RolePermission $permission
56 public function attachPermission(Permissions\RolePermission $permission)
58 $this->permissions()->attach($permission->id);
62 * Detach a single permission from this role.
63 * @param \BookStack\Auth\Permissions\RolePermission $permission
65 public function detachPermission(Permissions\RolePermission $permission)
67 $this->permissions()->detach($permission->id);
71 * Get the role object for the specified role.
75 public static function getRole($roleName)
77 return static::where('name', '=', $roleName)->first();
81 * Get the role object for the specified system role.
85 public static function getSystemRole($roleName)
87 return static::where('system_name', '=', $roleName)->first();
91 * Get all visible roles
94 public static function visible()
96 return static::where('hidden', '=', false)->orderBy('name')->get();