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('BookStack\User');
18 * Get all related entity permissions.
19 * @return \Illuminate\Database\Eloquent\Relations\HasMany
21 public function entityPermissions()
23 return $this->hasMany(EntityPermission::class);
27 * The permissions that belong to the role.
29 public function permissions()
31 return $this->belongsToMany('BookStack\Permission');
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 Permission $permission
52 public function attachPermission(Permission $permission)
54 $this->permissions()->attach($permission->id);
58 * Detach a single permission from this role.
59 * @param Permission $permission
61 public function detachPermission(Permission $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();