]> BookStack Code Mirror - bookstack/commitdiff
Merge pull request #2283 from BookStackApp/recycle_bin
authorDan Brown <redacted>
Sat, 7 Nov 2020 15:10:17 +0000 (15:10 +0000)
committerGitHub <redacted>
Sat, 7 Nov 2020 15:10:17 +0000 (15:10 +0000)
Recycle Bin Implementation

1  2 
.env.example.complete
app/Auth/Permissions/PermissionService.php
tests/Uploads/AttachmentTest.php

diff --combined .env.example.complete
index 45b1e13215d475daed4e23734a294602a6119b1d,0e62b3ea6206a382b457321984c45d0a3994b7d2..19643a49f6d83aa6a576fc7d171f52c38a483f2f
@@@ -238,9 -238,9 +238,9 @@@ DISABLE_EXTERNAL_SERVICES=fals
  # Example: AVATAR_URL=https://seccdn.libravatar.org/avatar/${hash}?s=${size}&d=identicon
  AVATAR_URL=
  
 -# Enable draw.io integration
 +# Enable diagrams.net integration
  # Can simply be true/false to enable/disable the integration.
 -# Alternatively, It can be URL to the draw.io instance you want to use.
 +# Alternatively, It can be URL to the diagrams.net instance you want to use.
  # For URLs, The following URL parameters should be included: embed=1&proto=json&spin=1
  DRAWIO=true
  
@@@ -255,6 -255,14 +255,14 @@@ APP_VIEWS_BOOKSHELVES=gri
  # If set to 'false' a limit will not be enforced.
  REVISION_LIMIT=50
  
+ # Recycle Bin Lifetime
+ # The number of days that content will remain in the recycle bin before
+ # being considered for auto-removal. It is not a guarantee that content will
+ # be removed after this time.
+ # Set to 0 for no recycle bin functionality.
+ # Set to -1 for unlimited recycle bin lifetime.
+ RECYCLE_BIN_LIFETIME=30
  # Allow <script> tags in page content
  # Note, if set to 'true' the page editor may still escape scripts.
  ALLOW_CONTENT_SCRIPTS=false
index 043227aab9504910f4c9704081b73099456fccb1,2609779bfa3e79805bd1c2839c3020add1caad29..d9a52c1beb152e900bf89c60df90c0161b97246c
@@@ -3,8 -3,11 +3,8 @@@
  use BookStack\Auth\Permissions;
  use BookStack\Auth\Role;
  use BookStack\Entities\Book;
 -use BookStack\Entities\Bookshelf;
 -use BookStack\Entities\Chapter;
  use BookStack\Entities\Entity;
  use BookStack\Entities\EntityProvider;
 -use BookStack\Entities\Page;
  use BookStack\Ownable;
  use Illuminate\Database\Connection;
  use Illuminate\Database\Eloquent\Builder;
@@@ -48,11 -51,6 +48,6 @@@ class PermissionServic
  
      /**
       * PermissionService constructor.
-      * @param JointPermission $jointPermission
-      * @param EntityPermission $entityPermission
-      * @param Role $role
-      * @param Connection $db
-      * @param EntityProvider $entityProvider
       */
      public function __construct(
          JointPermission $jointPermission,
          });
  
          // Chunk through all bookshelves
-         $this->entityProvider->bookshelf->newQuery()->select(['id', 'restricted', 'created_by'])
+         $this->entityProvider->bookshelf->newQuery()->withTrashed()->select(['id', 'restricted', 'created_by'])
              ->chunk(50, function ($shelves) use ($roles) {
                  $this->buildJointPermissionsForShelves($shelves, $roles);
              });
       */
      protected function bookFetchQuery()
      {
-         return $this->entityProvider->book->newQuery()
+         return $this->entityProvider->book->withTrashed()->newQuery()
              ->select(['id', 'restricted', 'created_by'])->with(['chapters' => function ($query) {
-                 $query->select(['id', 'restricted', 'created_by', 'book_id']);
+                 $query->withTrashed()->select(['id', 'restricted', 'created_by', 'book_id']);
              }, 'pages'  => function ($query) {
-                 $query->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
+                 $query->withTrashed()->select(['id', 'restricted', 'created_by', 'book_id', 'chapter_id']);
              }]);
      }
  
index 5838b019e4affb526e50472bf439285f0c24c865,4614c8e22489b7eaec565aaa2649772a0344d868..5b73aa6ae1a2d925ac9c0991d8e57fecc837b3fc
@@@ -1,53 -1,43 +1,55 @@@
  <?php namespace Tests\Uploads;
  
+ use BookStack\Entities\Managers\TrashCan;
+ use BookStack\Entities\Repos\PageRepo;
  use BookStack\Uploads\Attachment;
  use BookStack\Entities\Page;
  use BookStack\Auth\Permissions\PermissionService;
 +use BookStack\Uploads\AttachmentService;
 +use Illuminate\Http\UploadedFile;
  use Tests\TestCase;
 +use Tests\TestResponse;
  
  class AttachmentTest extends TestCase
  {
      /**
       * Get a test file that can be uploaded
 -     * @param $fileName
 -     * @return \Illuminate\Http\UploadedFile
       */
 -    protected function getTestFile($fileName)
 +    protected function getTestFile(string $fileName): UploadedFile
      {
 -        return new \Illuminate\Http\UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', 55, null, true);
 +        return new UploadedFile(base_path('tests/test-data/test-file.txt'), $fileName, 'text/plain', 55, null, true);
      }
  
      /**
       * Uploads a file with the given name.
 -     * @param $name
 -     * @param int $uploadedTo
 -     * @return \Illuminate\Foundation\Testing\TestResponse
       */
 -    protected function uploadFile($name, $uploadedTo = 0)
 +    protected function uploadFile(string $name, int $uploadedTo = 0): \Illuminate\Foundation\Testing\TestResponse
      {
          $file = $this->getTestFile($name);
          return $this->call('POST', '/attachments/upload', ['uploaded_to' => $uploadedTo], [], ['file' => $file], []);
      }
  
 +    /**
 +     * Create a new attachment
 +     */
 +    protected function createAttachment(Page $page): Attachment
 +    {
 +        $this->post('attachments/link', [
 +            'attachment_link_url' => 'https://example.com',
 +            'attachment_link_name' => 'Example Attachment Link',
 +            'attachment_link_uploaded_to' => $page->id,
 +        ]);
 +
 +        return Attachment::query()->latest()->first();
 +    }
 +
      /**
       * Delete all uploaded files.
       * To assist with cleanup.
       */
      protected function deleteUploads()
      {
 -        $fileService = $this->app->make(\BookStack\Uploads\AttachmentService::class);
 +        $fileService = $this->app->make(AttachmentService::class);
          foreach (Attachment::all() as $file) {
              $fileService->deleteFile($file);
          }
          $page = Page::first();
          $this->asAdmin();
  
 -        $this->call('POST', 'attachments/link', [
 -            'attachment_link_url' => 'https://example.com',
 -            'attachment_link_name' => 'Example Attachment Link',
 -            'attachment_link_uploaded_to' => $page->id,
 -        ]);
 -
 -        $attachmentId = Attachment::first()->id;
 -
 -        $update = $this->call('PUT', 'attachments/' . $attachmentId, [
 +        $attachment = $this->createAttachment($page);
 +        $update = $this->call('PUT', 'attachments/' . $attachment->id, [
              'attachment_edit_name' => 'My new attachment name',
              'attachment_edit_url' => 'https://test.example.com'
          ]);
  
          $expectedData = [
 -            'id' => $attachmentId,
 +            'id' => $attachment->id,
              'path' => 'https://test.example.com',
              'name' => 'My new attachment name',
              'uploaded_to' => $page->id
              'name' => $fileName
          ]);
  
-         $this->call('DELETE', $page->getUrl());
+         app(PageRepo::class)->destroy($page);
+         app(TrashCan::class)->empty();
  
          $this->assertDatabaseMissing('attachments', [
              'name' => $fileName
  
          $this->deleteUploads();
      }
 +
 +    public function test_data_and_js_links_cannot_be_attached_to_a_page()
 +    {
 +        $page = Page::first();
 +        $this->asAdmin();
 +
 +        $badLinks = [
 +            'javascript:alert("bunny")',
 +            ' javascript:alert("bunny")',
 +            'JavaScript:alert("bunny")',
 +            "\t\n\t\nJavaScript:alert(\"bunny\")",
 +            "data:text/html;<a></a>",
 +            "Data:text/html;<a></a>",
 +            "Data:text/html;<a></a>",
 +        ];
 +
 +        foreach ($badLinks as $badLink) {
 +            $linkReq = $this->post('attachments/link', [
 +                'attachment_link_url' => $badLink,
 +                'attachment_link_name' => 'Example Attachment Link',
 +                'attachment_link_uploaded_to' => $page->id,
 +            ]);
 +            $linkReq->assertStatus(422);
 +            $this->assertDatabaseMissing('attachments', [
 +                'path' => $badLink,
 +            ]);
 +        }
 +
 +        $attachment = $this->createAttachment($page);
 +
 +        foreach ($badLinks as $badLink) {
 +            $linkReq = $this->put('attachments/' . $attachment->id, [
 +                'attachment_edit_url' => $badLink,
 +                'attachment_edit_name' => 'Example Attachment Link',
 +            ]);
 +            $linkReq->assertStatus(422);
 +            $this->assertDatabaseMissing('attachments', [
 +                'path' => $badLink,
 +            ]);
 +        }
 +    }
  }