1 <?php namespace BookStack\Auth;
3 use BookStack\Auth\Permissions\JointPermission;
4 use BookStack\Auth\Permissions\RolePermission;
7 class Role extends Model
10 protected $fillable = ['display_name', 'description', 'external_auth_id'];
13 * The roles that belong to the role.
15 public function users()
17 return $this->belongsToMany(User::class)->orderBy('name', 'asc');
21 * Get all related JointPermissions.
22 * @return \Illuminate\Database\Eloquent\Relations\HasMany
24 public function jointPermissions()
26 return $this->hasMany(JointPermission::class);
30 * The RolePermissions that belong to the role.
32 public function permissions()
34 return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
38 * Check if this role has a permission.
39 * @param $permissionName
42 public function hasPermission($permissionName)
44 $permissions = $this->getRelationValue('permissions');
45 foreach ($permissions as $permission) {
46 if ($permission->getRawAttribute('name') === $permissionName) {
54 * Add a permission to this role.
55 * @param RolePermission $permission
57 public function attachPermission(RolePermission $permission)
59 $this->permissions()->attach($permission->id);
63 * Detach a single permission from this role.
64 * @param RolePermission $permission
66 public function detachPermission(RolePermission $permission)
68 $this->permissions()->detach($permission->id);
72 * Get the role object for the specified role.
76 public static function getRole($roleName)
78 return static::where('name', '=', $roleName)->first();
82 * Get the role object for the specified system role.
86 public static function getSystemRole($roleName)
88 return static::where('system_name', '=', $roleName)->first();
92 * Get all visible roles
95 public static function visible()
97 return static::where('hidden', '=', false)->orderBy('name')->get();