3 namespace Tests\Helpers;
5 use BookStack\Auth\Permissions\EntityPermission;
6 use BookStack\Auth\Permissions\JointPermissionBuilder;
7 use BookStack\Auth\Permissions\RolePermission;
8 use BookStack\Auth\Role;
9 use BookStack\Auth\User;
10 use BookStack\Entities\Models\Entity;
12 class PermissionsProvider
14 protected UserRoleProvider $userRoleProvider;
16 public function __construct(UserRoleProvider $userRoleProvider)
18 $this->userRoleProvider = $userRoleProvider;
22 * Grant role permissions to the provided user.
24 public function grantUserRolePermissions(User $user, array $permissions): void
26 $newRole = $this->userRoleProvider->createRole($permissions);
27 $user->attachRole($newRole);
29 $user->clearPermissionCache();
33 * Completely remove specific role permissions from the provided user.
35 public function removeUserRolePermissions(User $user, array $permissions): void
37 $permissionBuilder = app()->make(JointPermissionBuilder::class);
39 foreach ($permissions as $permissionName) {
40 /** @var RolePermission $permission */
41 $permission = RolePermission::query()
42 ->where('name', '=', $permissionName)
45 $roles = $user->roles()->whereHas('permissions', function ($query) use ($permission) {
46 $query->where('id', '=', $permission->id);
49 /** @var Role $role */
50 foreach ($roles as $role) {
51 $role->detachPermission($permission);
52 $permissionBuilder->rebuildForRole($role);
55 $user->clearPermissionCache();
60 * Regenerate the permission for an entity.
61 * Centralised to manage clearing of cached elements between requests.
63 public function regenerateForEntity(Entity $entity): void
65 $entity->rebuildPermissions();
66 $entity->load('jointPermissions');
70 * Set the given entity as having restricted permissions, and apply the given
71 * permissions for the given roles.
72 * @param string[] $actions
73 * @param Role[] $roles
75 public function setEntityPermissions(Entity $entity, array $actions = [], array $roles = [], $inherit = false): void
77 $entity->permissions()->delete();
82 // Set default permissions to not allow actions so that only the provided role permissions are at play.
83 $permissions[] = ['role_id' => null, 'user_id' => null, 'view' => false, 'create' => false, 'update' => false, 'delete' => false];
86 foreach ($roles as $role) {
87 $permissions[] = $this->actionListToEntityPermissionData($actions, $role->id);
90 $this->addEntityPermissionEntries($entity, $permissions);
93 public function addEntityPermission(Entity $entity, array $actionList, ?Role $role = null, ?User $user = null)
95 $permissionData = $this->actionListToEntityPermissionData($actionList, $role->id ?? null, $user->id ?? null);
96 $this->addEntityPermissionEntries($entity, [$permissionData]);
100 * Disable inherited permissions on the given entity.
101 * Effectively sets the "Other Users" UI permission option to not inherit, with no permissions.
103 public function disableEntityInheritedPermissions(Entity $entity): void
105 $entity->permissions()->whereNull(['user_id', 'role_id'])->delete();
106 $fallback = $this->actionListToEntityPermissionData([]);
107 $this->addEntityPermissionEntries($entity, [$fallback]);
110 protected function addEntityPermissionEntries(Entity $entity, array $entityPermissionData): void
112 $entity->permissions()->createMany($entityPermissionData);
113 $entity->load('permissions');
114 $this->regenerateForEntity($entity);
118 * For the given simple array of string actions (view, create, update, delete), convert
119 * the format to entity permission data, where permission is granted if the action is in the
120 * given actionList array.
122 protected function actionListToEntityPermissionData(array $actionList, int $roleId = null, int $userId = null): array
124 $permissionData = ['role_id' => $roleId, 'user_id' => $userId];
125 foreach (EntityPermission::PERMISSIONS as $possibleAction) {
126 $permissionData[$possibleAction] = in_array($possibleAction, $actionList);
129 return $permissionData;