2018-09-25 13:30:50 +02:00
|
|
|
<?php namespace BookStack\Auth;
|
|
|
|
|
|
|
|
use BookStack\Auth\Permissions\JointPermission;
|
2019-02-03 18:34:15 +01:00
|
|
|
use BookStack\Auth\Permissions\RolePermission;
|
2018-09-25 13:30:50 +02:00
|
|
|
use BookStack\Model;
|
2020-08-04 15:55:01 +02:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2015-08-29 16:03:42 +02:00
|
|
|
|
2019-11-16 16:24:09 +01:00
|
|
|
/**
|
|
|
|
* Class Role
|
2020-08-04 15:55:01 +02:00
|
|
|
* @property int $id
|
2019-11-16 16:24:09 +01:00
|
|
|
* @property string $display_name
|
|
|
|
* @property string $description
|
|
|
|
* @property string $external_auth_id
|
2020-08-04 15:55:01 +02:00
|
|
|
* @property string $system_name
|
2019-11-16 16:24:09 +01:00
|
|
|
*/
|
2015-08-29 16:03:42 +02:00
|
|
|
class Role extends Model
|
|
|
|
{
|
2016-02-27 20:24:42 +01:00
|
|
|
|
2018-07-15 20:34:42 +02:00
|
|
|
protected $fillable = ['display_name', 'description', 'external_auth_id'];
|
2015-09-05 18:42:05 +02:00
|
|
|
|
2015-08-29 16:03:42 +02:00
|
|
|
/**
|
|
|
|
* The roles that belong to the role.
|
|
|
|
*/
|
|
|
|
public function users()
|
|
|
|
{
|
2019-02-03 18:34:15 +01:00
|
|
|
return $this->belongsToMany(User::class)->orderBy('name', 'asc');
|
2015-08-29 16:03:42 +02:00
|
|
|
}
|
|
|
|
|
2016-04-24 17:54:20 +02:00
|
|
|
/**
|
2016-05-01 22:20:50 +02:00
|
|
|
* Get all related JointPermissions.
|
2016-04-24 17:54:20 +02:00
|
|
|
*/
|
2020-08-04 15:55:01 +02:00
|
|
|
public function jointPermissions(): HasMany
|
2016-04-24 17:54:20 +02:00
|
|
|
{
|
2016-05-01 22:20:50 +02:00
|
|
|
return $this->hasMany(JointPermission::class);
|
2016-04-24 17:54:20 +02:00
|
|
|
}
|
|
|
|
|
2015-08-29 16:03:42 +02:00
|
|
|
/**
|
2016-05-01 22:20:50 +02:00
|
|
|
* The RolePermissions that belong to the role.
|
2015-08-29 16:03:42 +02:00
|
|
|
*/
|
|
|
|
public function permissions()
|
|
|
|
{
|
2019-02-03 18:34:15 +01:00
|
|
|
return $this->belongsToMany(RolePermission::class, 'permission_role', 'role_id', 'permission_id');
|
2015-08-29 16:03:42 +02:00
|
|
|
}
|
|
|
|
|
2016-02-27 20:24:42 +01:00
|
|
|
/**
|
|
|
|
* Check if this role has a permission.
|
|
|
|
*/
|
2020-08-04 15:55:01 +02:00
|
|
|
public function hasPermission(string $permissionName): bool
|
2016-02-27 20:24:42 +01:00
|
|
|
{
|
2016-04-30 18:16:06 +02:00
|
|
|
$permissions = $this->getRelationValue('permissions');
|
|
|
|
foreach ($permissions as $permission) {
|
2018-01-28 17:58:52 +01:00
|
|
|
if ($permission->getRawAttribute('name') === $permissionName) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-30 18:16:06 +02:00
|
|
|
}
|
|
|
|
return false;
|
2016-02-27 20:24:42 +01:00
|
|
|
}
|
|
|
|
|
2015-08-29 16:03:42 +02:00
|
|
|
/**
|
|
|
|
* Add a permission to this role.
|
|
|
|
*/
|
2019-02-03 18:34:15 +01:00
|
|
|
public function attachPermission(RolePermission $permission)
|
2015-08-29 16:03:42 +02:00
|
|
|
{
|
|
|
|
$this->permissions()->attach($permission->id);
|
|
|
|
}
|
|
|
|
|
2016-04-09 13:37:58 +02:00
|
|
|
/**
|
|
|
|
* Detach a single permission from this role.
|
|
|
|
*/
|
2019-02-03 18:34:15 +01:00
|
|
|
public function detachPermission(RolePermission $permission)
|
2016-04-09 13:37:58 +02:00
|
|
|
{
|
2019-12-30 20:42:46 +01:00
|
|
|
$this->permissions()->detach([$permission->id]);
|
2016-04-09 13:37:58 +02:00
|
|
|
}
|
|
|
|
|
2016-01-02 15:48:35 +01:00
|
|
|
/**
|
2020-08-04 15:55:01 +02:00
|
|
|
* Get the role of the specified display name.
|
2016-01-02 15:48:35 +01:00
|
|
|
*/
|
2020-08-04 15:55:01 +02:00
|
|
|
public static function getRole(string $displayName): ?Role
|
2016-01-02 15:48:35 +01:00
|
|
|
{
|
2020-08-04 15:55:01 +02:00
|
|
|
return static::query()->where('display_name', '=', $displayName)->first();
|
2015-09-05 18:42:05 +02:00
|
|
|
}
|
2016-05-01 20:36:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the role object for the specified system role.
|
|
|
|
*/
|
2020-08-04 15:55:01 +02:00
|
|
|
public static function getSystemRole(string $systemName): ?Role
|
2016-05-01 20:36:53 +02:00
|
|
|
{
|
2020-08-04 15:55:01 +02:00
|
|
|
return static::query()->where('system_name', '=', $systemName)->first();
|
2016-05-01 20:36:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-01 22:20:50 +02:00
|
|
|
* Get all visible roles
|
2016-05-01 20:36:53 +02:00
|
|
|
*/
|
2020-08-04 15:55:01 +02:00
|
|
|
public static function visible(): Collection
|
2016-05-01 20:36:53 +02:00
|
|
|
{
|
2019-10-05 13:55:01 +02:00
|
|
|
return static::query()->where('hidden', '=', false)->orderBy('name')->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the roles that can be restricted.
|
|
|
|
*/
|
2020-08-04 15:55:01 +02:00
|
|
|
public static function restrictable(): Collection
|
2019-10-05 13:55:01 +02:00
|
|
|
{
|
|
|
|
return static::query()->where('system_name', '!=', 'admin')->get();
|
2016-05-01 20:36:53 +02:00
|
|
|
}
|
2015-08-29 16:03:42 +02:00
|
|
|
}
|