2015-08-29 10:03:42 -04:00
|
|
|
<?php
|
|
|
|
|
2015-09-10 14:31:09 -04:00
|
|
|
namespace BookStack;
|
2015-08-29 10:03:42 -04:00
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
|
|
|
class Role extends Model
|
|
|
|
{
|
2016-02-27 14:24:42 -05:00
|
|
|
|
|
|
|
protected $fillable = ['display_name', 'description'];
|
2015-09-05 12:42:05 -04:00
|
|
|
|
2015-08-29 10:03:42 -04:00
|
|
|
/**
|
|
|
|
* The roles that belong to the role.
|
|
|
|
*/
|
|
|
|
public function users()
|
|
|
|
{
|
2015-09-10 14:31:09 -04:00
|
|
|
return $this->belongsToMany('BookStack\User');
|
2015-08-29 10:03:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The permissions that belong to the role.
|
|
|
|
*/
|
|
|
|
public function permissions()
|
|
|
|
{
|
2015-09-10 14:31:09 -04:00
|
|
|
return $this->belongsToMany('BookStack\Permission');
|
2015-08-29 10:03:42 -04:00
|
|
|
}
|
|
|
|
|
2016-02-27 14:24:42 -05:00
|
|
|
/**
|
|
|
|
* Check if this role has a permission.
|
|
|
|
* @param $permission
|
|
|
|
*/
|
|
|
|
public function hasPermission($permission)
|
|
|
|
{
|
|
|
|
return $this->permissions->pluck('name')->contains($permission);
|
|
|
|
}
|
|
|
|
|
2015-08-29 10:03:42 -04:00
|
|
|
/**
|
|
|
|
* Add a permission to this role.
|
|
|
|
* @param Permission $permission
|
|
|
|
*/
|
|
|
|
public function attachPermission(Permission $permission)
|
|
|
|
{
|
|
|
|
$this->permissions()->attach($permission->id);
|
|
|
|
}
|
|
|
|
|
2016-01-02 09:48:35 -05: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 12:42:05 -04:00
|
|
|
}
|
2015-08-29 10:03:42 -04:00
|
|
|
}
|