2016-04-30 16:16:06 +00:00
|
|
|
<?php namespace BookStack;
|
2015-08-29 14:03:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
class Role extends Model
|
|
|
|
{
|
2016-02-27 19:24:42 +00:00
|
|
|
|
|
|
|
protected $fillable = ['display_name', 'description'];
|
2015-09-05 16:42:05 +00:00
|
|
|
|
2015-08-29 14:03:42 +00:00
|
|
|
/**
|
|
|
|
* The roles that belong to the role.
|
|
|
|
*/
|
|
|
|
public function users()
|
|
|
|
{
|
2015-09-10 18:31:09 +00:00
|
|
|
return $this->belongsToMany('BookStack\User');
|
2015-08-29 14:03:42 +00:00
|
|
|
}
|
|
|
|
|
2016-04-24 15:54:20 +00:00
|
|
|
/**
|
|
|
|
* Get all related entity permissions.
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
|
|
*/
|
|
|
|
public function entityPermissions()
|
|
|
|
{
|
|
|
|
return $this->hasMany(EntityPermission::class);
|
|
|
|
}
|
|
|
|
|
2015-08-29 14:03:42 +00:00
|
|
|
/**
|
|
|
|
* The permissions that belong to the role.
|
|
|
|
*/
|
|
|
|
public function permissions()
|
|
|
|
{
|
2015-09-10 18:31:09 +00:00
|
|
|
return $this->belongsToMany('BookStack\Permission');
|
2015-08-29 14:03:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-27 19:24:42 +00:00
|
|
|
/**
|
|
|
|
* Check if this role has a permission.
|
2016-04-30 16:16:06 +00:00
|
|
|
* @param $permissionName
|
|
|
|
* @return bool
|
2016-02-27 19:24:42 +00:00
|
|
|
*/
|
2016-04-30 16:16:06 +00:00
|
|
|
public function hasPermission($permissionName)
|
2016-02-27 19:24:42 +00:00
|
|
|
{
|
2016-04-30 16:16:06 +00:00
|
|
|
$permissions = $this->getRelationValue('permissions');
|
|
|
|
foreach ($permissions as $permission) {
|
|
|
|
if ($permission->getRawAttribute('name') === $permissionName) return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-02-27 19:24:42 +00:00
|
|
|
}
|
|
|
|
|
2015-08-29 14:03:42 +00:00
|
|
|
/**
|
|
|
|
* Add a permission to this role.
|
|
|
|
* @param Permission $permission
|
|
|
|
*/
|
|
|
|
public function attachPermission(Permission $permission)
|
|
|
|
{
|
|
|
|
$this->permissions()->attach($permission->id);
|
|
|
|
}
|
|
|
|
|
2016-04-09 11:37:58 +00:00
|
|
|
/**
|
|
|
|
* Detach a single permission from this role.
|
|
|
|
* @param Permission $permission
|
|
|
|
*/
|
|
|
|
public function detachPermission(Permission $permission)
|
|
|
|
{
|
|
|
|
$this->permissions()->detach($permission->id);
|
|
|
|
}
|
|
|
|
|
2016-01-02 14:48:35 +00:00
|
|
|
/**
|
|
|
|
* Get the role object for the specified role.
|
|
|
|
* @param $roleName
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function getRole($roleName)
|
|
|
|
{
|
|
|
|
return static::where('name', '=', $roleName)->first();
|
2015-09-05 16:42:05 +00:00
|
|
|
}
|
2016-05-01 18:36:53 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the role object for the specified system role.
|
|
|
|
* @param $roleName
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function getSystemRole($roleName)
|
|
|
|
{
|
|
|
|
return static::where('system_name', '=', $roleName)->first();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* GEt all visible roles
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function visible()
|
|
|
|
{
|
|
|
|
return static::where('hidden', '=', false)->orderBy('name')->get();
|
|
|
|
}
|
|
|
|
|
2015-08-29 14:03:42 +00:00
|
|
|
}
|