mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-28 17:02:04 +03:00
Copied over work from user_permissions branch
Only that relevant to the additional testing work.
This commit is contained in:
@ -2,8 +2,6 @@
|
||||
|
||||
namespace Tests\Helpers;
|
||||
|
||||
use BookStack\Auth\Permissions\EntityPermission;
|
||||
use BookStack\Auth\Role;
|
||||
use BookStack\Auth\User;
|
||||
use BookStack\Entities\Models\Book;
|
||||
use BookStack\Entities\Models\Bookshelf;
|
||||
@ -186,44 +184,6 @@ class EntityProvider
|
||||
return $pageRepo->publishDraft($draftPage, $input);
|
||||
}
|
||||
|
||||
/**
|
||||
* Regenerate the permission for an entity.
|
||||
* Centralised to manage clearing of cached elements between requests.
|
||||
*/
|
||||
public function regenPermissions(Entity $entity): void
|
||||
{
|
||||
$entity->rebuildPermissions();
|
||||
$entity->load('jointPermissions');
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the given entity as having restricted permissions, and apply the given
|
||||
* permissions for the given roles.
|
||||
* @param string[] $actions
|
||||
* @param Role[] $roles
|
||||
*/
|
||||
public function setPermissions(Entity $entity, array $actions = [], array $roles = []): void
|
||||
{
|
||||
$entity->permissions()->delete();
|
||||
|
||||
$permissions = [
|
||||
// Set default permissions to not allow actions so that only the provided role permissions are at play.
|
||||
['role_id' => 0, 'view' => false, 'create' => false, 'update' => false, 'delete' => false],
|
||||
];
|
||||
|
||||
foreach ($roles as $role) {
|
||||
$permission = ['role_id' => $role->id];
|
||||
foreach (EntityPermission::PERMISSIONS as $possibleAction) {
|
||||
$permission[$possibleAction] = in_array($possibleAction, $actions);
|
||||
}
|
||||
$permissions[] = $permission;
|
||||
}
|
||||
|
||||
$entity->permissions()->createMany($permissions);
|
||||
$entity->load('permissions');
|
||||
$this->regenPermissions($entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Entity|Entity[] $entities
|
||||
*/
|
||||
|
Reference in New Issue
Block a user