2022-10-09 18:14:11 +02:00
|
|
|
<?php
|
|
|
|
|
2023-05-17 18:56:55 +02:00
|
|
|
namespace BookStack\Permissions;
|
2022-10-09 18:14:11 +02:00
|
|
|
|
|
|
|
use BookStack\Entities\Models\Entity;
|
2023-05-17 18:56:55 +02:00
|
|
|
use BookStack\Permissions\Models\EntityPermission;
|
|
|
|
use BookStack\Users\Models\Role;
|
2022-10-09 18:14:11 +02:00
|
|
|
|
|
|
|
class PermissionFormData
|
|
|
|
{
|
|
|
|
protected Entity $entity;
|
|
|
|
|
|
|
|
public function __construct(Entity $entity)
|
|
|
|
{
|
|
|
|
$this->entity = $entity;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-10-10 13:24:23 +02:00
|
|
|
* Get the permissions with assigned roles.
|
2022-10-09 18:14:11 +02:00
|
|
|
*/
|
2022-10-10 13:24:23 +02:00
|
|
|
public function permissionsWithRoles(): array
|
2022-10-09 18:14:11 +02:00
|
|
|
{
|
|
|
|
return $this->entity->permissions()
|
|
|
|
->with('role')
|
|
|
|
->where('role_id', '!=', 0)
|
2022-10-10 13:24:23 +02:00
|
|
|
->get()
|
|
|
|
->sortBy('role.display_name')
|
2022-10-09 18:14:11 +02:00
|
|
|
->all();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the roles that don't yet have specific permissions for the
|
|
|
|
* entity we're managing permissions for.
|
|
|
|
*/
|
|
|
|
public function rolesNotAssigned(): array
|
|
|
|
{
|
|
|
|
$assigned = $this->entity->permissions()->pluck('role_id');
|
|
|
|
return Role::query()
|
|
|
|
->where('system_name', '!=', 'admin')
|
|
|
|
->whereNotIn('id', $assigned)
|
|
|
|
->orderBy('display_name', 'asc')
|
|
|
|
->get()
|
|
|
|
->all();
|
|
|
|
}
|
|
|
|
|
2022-10-10 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* Get the entity permission for the "Everyone Else" option.
|
|
|
|
*/
|
|
|
|
public function everyoneElseEntityPermission(): EntityPermission
|
|
|
|
{
|
2022-10-12 13:12:36 +02:00
|
|
|
/** @var ?EntityPermission $permission */
|
2022-10-10 18:22:38 +02:00
|
|
|
$permission = $this->entity->permissions()
|
|
|
|
->where('role_id', '=', 0)
|
|
|
|
->first();
|
|
|
|
return $permission ?? (new EntityPermission());
|
|
|
|
}
|
|
|
|
|
2022-10-09 18:14:11 +02:00
|
|
|
/**
|
|
|
|
* Get the "Everyone Else" role entry.
|
|
|
|
*/
|
|
|
|
public function everyoneElseRole(): Role
|
|
|
|
{
|
|
|
|
return (new Role())->forceFill([
|
|
|
|
'id' => 0,
|
2022-10-11 16:52:56 +02:00
|
|
|
'display_name' => trans('entities.permissions_role_everyone_else'),
|
|
|
|
'description' => trans('entities.permissions_role_everyone_else_desc'),
|
2022-10-09 18:14:11 +02:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|