]> BookStack Code Mirror - bookstack/blobdiff - tests/Helpers/PermissionsProvider.php
respective book and chapter structure added.
[bookstack] / tests / Helpers / PermissionsProvider.php
index ac9a2a68a61f65a44d71a39c49a8eb76ea82f4f5..cb036fe97c71c964bfdb6c8b018364014f9f02b9 100644 (file)
@@ -2,19 +2,24 @@
 
 namespace Tests\Helpers;
 
-use BookStack\Auth\Permissions\EntityPermission;
-use BookStack\Auth\Permissions\RolePermission;
-use BookStack\Auth\Role;
-use BookStack\Auth\User;
 use BookStack\Entities\Models\Entity;
+use BookStack\Permissions\Models\EntityPermission;
+use BookStack\Permissions\Models\RolePermission;
+use BookStack\Settings\SettingService;
+use BookStack\Users\Models\Role;
+use BookStack\Users\Models\User;
 
 class PermissionsProvider
 {
-    protected UserRoleProvider $userRoleProvider;
+    public function __construct(
+        protected UserRoleProvider $userRoleProvider
+    ) {
+    }
 
-    public function __construct(UserRoleProvider $userRoleProvider)
+    public function makeAppPublic(): void
     {
-        $this->userRoleProvider = $userRoleProvider;
+        $settings = app(SettingService::class);
+        $settings->put('app-public', 'true');
     }
 
     /**
@@ -85,7 +90,7 @@ class PermissionsProvider
 
         if (!$inherit) {
             // Set default permissions to not allow actions so that only the provided role permissions are at play.
-            $permissions[] = ['role_id' => null, 'user_id' => null, 'view' => false, 'create' => false, 'update' => false, 'delete' => false];
+            $permissions[] = ['role_id' => 0, 'view' => false, 'create' => false, 'update' => false, 'delete' => false];
         }
 
         foreach ($roles as $role) {
@@ -95,9 +100,16 @@ class PermissionsProvider
         $this->addEntityPermissionEntries($entity, $permissions);
     }
 
-    public function addEntityPermission(Entity $entity, array $actionList, ?Role $role = null, ?User $user = null)
+    public function addEntityPermission(Entity $entity, array $actionList, Role $role)
+    {
+        $permissionData = $this->actionListToEntityPermissionData($actionList, $role->id);
+        $this->addEntityPermissionEntries($entity, [$permissionData]);
+    }
+
+    public function setFallbackPermissions(Entity $entity, array $actionList)
     {
-        $permissionData = $this->actionListToEntityPermissionData($actionList, $role->id ?? null, $user->id ?? null);
+        $entity->permissions()->where('role_id', '=', 0)->delete();
+        $permissionData = $this->actionListToEntityPermissionData($actionList, 0);
         $this->addEntityPermissionEntries($entity, [$permissionData]);
     }
 
@@ -107,7 +119,7 @@ class PermissionsProvider
      */
     public function disableEntityInheritedPermissions(Entity $entity): void
     {
-        $entity->permissions()->whereNull(['user_id', 'role_id'])->delete();
+        $entity->permissions()->where('role_id', '=', 0)->delete();
         $fallback = $this->actionListToEntityPermissionData([]);
         $this->addEntityPermissionEntries($entity, [$fallback]);
     }
@@ -124,9 +136,9 @@ class PermissionsProvider
      * the format to entity permission data, where permission is granted if the action is in the
      * given actionList array.
      */
-    protected function actionListToEntityPermissionData(array $actionList, int $roleId = null, int $userId = null): array
+    protected function actionListToEntityPermissionData(array $actionList, int $roleId = 0): array
     {
-        $permissionData = ['role_id' => $roleId, 'user_id' => $userId];
+        $permissionData = ['role_id' => $roleId];
         foreach (EntityPermission::PERMISSIONS as $possibleAction) {
             $permissionData[$possibleAction] = in_array($possibleAction, $actionList);
         }