3 namespace Tests\Helpers;
5 use BookStack\Entities\Models\Entity;
6 use BookStack\Permissions\Models\EntityPermission;
7 use BookStack\Permissions\Models\RolePermission;
8 use BookStack\Permissions\Permission;
9 use BookStack\Settings\SettingService;
10 use BookStack\Users\Models\Role;
11 use BookStack\Users\Models\User;
13 class PermissionsProvider
15 public function __construct(
16 protected UserRoleProvider $userRoleProvider
20 public function makeAppPublic(): void
22 $settings = app(SettingService::class);
23 $settings->put('app-public', 'true');
27 * Grant role permissions to the provided user.
29 public function grantUserRolePermissions(User $user, array $permissions): void
31 $newRole = $this->userRoleProvider->createRole($permissions);
32 $user->attachRole($newRole);
34 $user->clearPermissionCache();
38 * Completely remove specific role permissions from the provided user.
40 public function removeUserRolePermissions(User $user, array $permissions): void
42 foreach ($permissions as $permissionName) {
43 /** @var RolePermission $permission */
44 $permission = RolePermission::query()
45 ->where('name', '=', $permissionName)
48 $roles = $user->roles()->whereHas('permissions', function ($query) use ($permission) {
49 $query->where('id', '=', $permission->id);
52 /** @var Role $role */
53 foreach ($roles as $role) {
54 $role->detachPermission($permission);
57 $user->clearPermissionCache();
62 * Change the owner of the given entity to the given user.
64 public function changeEntityOwner(Entity $entity, User $newOwner): void
66 $entity->owned_by = $newOwner->id;
68 $entity->rebuildPermissions();
72 * Regenerate the permission for an entity.
73 * Centralised to manage clearing of cached elements between requests.
75 public function regenerateForEntity(Entity $entity): void
77 $entity->rebuildPermissions();
81 * Set the given entity as having restricted permissions, and apply the given
82 * permissions for the given roles.
83 * @param string[] $actions
84 * @param Role[] $roles
86 public function setEntityPermissions(Entity $entity, array $actions = [], array $roles = [], $inherit = false): void
88 $entity->permissions()->delete();
93 // Set default permissions to not allow actions so that only the provided role permissions are at play.
94 $permissions[] = ['role_id' => 0, 'view' => false, 'create' => false, 'update' => false, 'delete' => false];
97 foreach ($roles as $role) {
98 $permissions[] = $this->actionListToEntityPermissionData($actions, $role->id);
101 $this->addEntityPermissionEntries($entity, $permissions);
104 public function addEntityPermission(Entity $entity, array $actionList, Role $role)
106 $permissionData = $this->actionListToEntityPermissionData($actionList, $role->id);
107 $this->addEntityPermissionEntries($entity, [$permissionData]);
110 public function setFallbackPermissions(Entity $entity, array $actionList)
112 $entity->permissions()->where('role_id', '=', 0)->delete();
113 $permissionData = $this->actionListToEntityPermissionData($actionList, 0);
114 $this->addEntityPermissionEntries($entity, [$permissionData]);
118 * Disable inherited permissions on the given entity.
119 * Effectively sets the "Other Users" UI permission option to not inherit, with no permissions.
121 public function disableEntityInheritedPermissions(Entity $entity): void
123 $entity->permissions()->where('role_id', '=', 0)->delete();
124 $fallback = $this->actionListToEntityPermissionData([]);
125 $this->addEntityPermissionEntries($entity, [$fallback]);
128 protected function addEntityPermissionEntries(Entity $entity, array $entityPermissionData): void
130 $entity->permissions()->createMany($entityPermissionData);
131 $entity->load('permissions');
132 $this->regenerateForEntity($entity);
136 * For the given simple array of string actions (view, create, update, delete), convert
137 * the format to entity permission data, where permission is granted if the action is in the
138 * given actionList array.
140 protected function actionListToEntityPermissionData(array $actionList, int $roleId = 0): array
142 $permissionData = ['role_id' => $roleId];
143 foreach (Permission::genericForEntity() as $permission) {
144 $permissionData[$permission->value] = in_array($permission->value, $actionList);
147 return $permissionData;