| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2,14 +2,16 @@ | |||
| 2 | 2 | ||
| 3 | 3 | namespace BookStack\Http\Controllers\Api; | |
| 4 | 4 | ||
| 5 | - use BookStack\Entities\Models\Book; | ||
| 6 | - use BookStack\Entities\Models\Chapter; | ||
| 7 | 5 | use BookStack\Entities\Models\Deletion; | |
| 8 | 6 | use BookStack\Entities\Repos\DeletionRepo; | |
| 9 | 7 | use Closure; | |
| 10 | 8 | ||
| 11 | 9 | class RecycleBinApiController extends ApiController | |
| 12 | 10 | { | |
| 11 | + protected $fieldsToExpose = [ | ||
| 12 | + 'id', 'deleted_by', 'created_at', 'updated_at', 'deletable_type', 'deletable_id', | ||
| 13 | + ]; | ||
| 14 | + | ||
| 13 | 15 | public function __construct() | |
| 14 | 16 | { | |
| 15 | 17 | $this->middleware(function ($request, $next) { | |
@@ -20,9 +22,13 @@ public function __construct() | |||
| 20 | 22 | }); | |
| 21 | 23 | } | |
| 22 | 24 | ||
| 25 | + /** | ||
| 26 | + * Get a top-level listing of the items in the recycle bin. | ||
| 27 | + * Requires the permission to manage settings and restrictions. | ||
| 28 | + */ | ||
| 23 | 29 | public function list() | |
| 24 | 30 | { | |
| 25 | - return $this->apiListingResponse(Deletion::query(), [ | ||
| 31 | + return $this->apiListingResponse(Deletion::query()->with('deletable'), [ | ||
| 26 | 32 | 'id', | |
| 27 | 33 | 'deleted_by', | |
| 28 | 34 | 'created_at', | |
@@ -32,13 +38,22 @@ public function list() | |||
| 32 | 38 | ], [Closure::fromCallable([$this, 'listFormatter'])]); | |
| 33 | 39 | } | |
| 34 | 40 | ||
| 41 | + /** | ||
| 42 | + * Restore a single deletion from the recycle bin. | ||
| 43 | + * You must provide the deletion id, not the id of the corresponding deleted item. | ||
| 44 | + */ | ||
| 35 | 45 | public function restore(DeletionRepo $deletionRepo, string $id) | |
| 36 | 46 | { | |
| 37 | 47 | $restoreCount = $deletionRepo->restore((int) $id); | |
| 38 | 48 | ||
| 39 | 49 | return response()->json(['restore_count' => $restoreCount]); | |
| 40 | 50 | } | |
| 41 | 51 | ||
| 52 | + /** | ||
| 53 | + * Remove a single deletion from the recycle bin. | ||
| 54 | + * Use this endpoint carefully as it will entirely remove the underlying deleted items from the system. | ||
| 55 | + * You must provide the deletion id, not the id of the corresponding deleted item. | ||
| 56 | + */ | ||
| 42 | 57 | public function destroy(DeletionRepo $deletionRepo, string $id) | |
| 43 | 58 | { | |
| 44 | 59 | $deleteCount = $deletionRepo->destroy((int) $id); | |
@@ -48,23 +63,26 @@ public function destroy(DeletionRepo $deletionRepo, string $id) | |||
| 48 | 63 | ||
| 49 | 64 | protected function listFormatter(Deletion $deletion) | |
| 50 | 65 | { | |
| 66 | + $deletion->makeVisible($this->fieldsToExpose); | ||
| 67 | + $deletion->makeHidden('deletable'); | ||
| 68 | + | ||
| 51 | 69 | $deletable = $deletion->deletable; | |
| 52 | - $isBook = $deletable instanceof Book; | ||
| 70 | + $isBook = $deletion->deletable_type === "BookStack\Book"; | ||
| 53 | 71 | $parent = null; | |
| 54 | 72 | $children = null; | |
| 55 | 73 | ||
| 56 | 74 | if ($isBook) { | |
| 57 | - $chapterCount = $deletable->chapters()->withTrashed()->count(); | ||
| 58 | - $children['Bookstack\Chapter'] = $chapterCount; | ||
| 75 | + $chapterCount = $deletable->chapters()->withTrashed()->count(); | ||
| 76 | + $children['BookStack\Chapter'] = $chapterCount; | ||
| 59 | 77 | } | |
| 60 | 78 | ||
| 61 | - if ($isBook || $deletion->deletable instanceof Chapter) { | ||
| 62 | - $pageCount = $deletable->pages()->withTrashed()->count(); | ||
| 63 | - $children['Bookstack\Page'] = $pageCount; | ||
| 79 | + if ($isBook || $deletion->deletable_type === "BookStack\Chapter") { | ||
| 80 | + $pageCount = $deletable->pages()->withTrashed()->count(); | ||
| 81 | + $children['BookStack\Page'] = $pageCount; | ||
| 64 | 82 | } | |
| 65 | 83 | ||
| 66 | 84 | $parentEntity = $deletable->getParent(); | |
| 67 | - $parent = []; | ||
| 85 | + $parent = null; | ||
| 68 | 86 | ||
| 69 | 87 | if ($parentEntity) { | |
| 70 | 88 | $parent['type'] = $parentEntity->getMorphClass(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + { | ||
| 2 | + "delete_count": 2 | ||
| 3 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,34 @@ | |||
| 1 | + { | ||
| 2 | + "data": [ | ||
| 3 | + { | ||
| 4 | + "id": 25, | ||
| 5 | + "deleted_by": 1, | ||
| 6 | + "created_at": "2022-04-24T07:59:34.000000Z", | ||
| 7 | + "updated_at": "2022-04-24T07:59:34.000000Z", | ||
| 8 | + "deletable_type": "BookStack\\Book", | ||
| 9 | + "deletable_id": 4, | ||
| 10 | + "parent": { | ||
| 11 | + "type": "BookStack\\Book", | ||
| 12 | + "id": 25 | ||
| 13 | + }, | ||
| 14 | + "children": { | ||
| 15 | + "BookStack\\Chapter": 0, | ||
| 16 | + "BookStack\\Page": 1 | ||
| 17 | + } | ||
| 18 | + }, | ||
| 19 | + { | ||
| 20 | + "id": 26, | ||
| 21 | + "deleted_by": 1, | ||
| 22 | + "created_at": "2022-04-24T07:59:35.000000Z", | ||
| 23 | + "updated_at": "2022-04-24T07:59:35.000000Z", | ||
| 24 | + "deletable_type": "BookStack\\Book", | ||
| 25 | + "deletable_id": 3, | ||
| 26 | + "parent": [], | ||
| 27 | + "children": { | ||
| 28 | + "BookStack\\Chapter": 1, | ||
| 29 | + "BookStack\\Page": 1 | ||
| 30 | + } | ||
| 31 | + } | ||
| 32 | + ], | ||
| 33 | + "total": 2 | ||
| 34 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,3 @@ | |||
| 1 | + { | ||
| 2 | + "restore_count": 2 | ||
| 3 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,12 +33,12 @@ public function test_settings_manage_permission_needed_for_all_endpoints() | |||
| 33 | 33 | } | |
| 34 | 34 | } | |
| 35 | 35 | ||
| 36 | - public function test_restrictions_manage_all_permission_neeed_for_all_endpoints() | ||
| 36 | + public function test_restrictions_manage_all_permission_needed_for_all_endpoints() | ||
| 37 | 37 | { | |
| 38 | 38 | $editor = $this->getEditor(); | |
| 39 | 39 | $this->giveUserPermissions($editor, ['restrictions-manage-all']); | |
| 40 | 40 | $this->actingAs($editor); | |
| 41 | - | ||
| 41 | + | ||
| 42 | 42 | foreach ($this->endpointMap as [$method, $uri]) { | |
| 43 | 43 | $resp = $this->json($method, $uri); | |
| 44 | 44 | $resp->assertStatus(403); | |
@@ -74,15 +74,15 @@ public function test_index_endpoint_returns_expected_page() | |||
| 74 | 74 | }); | |
| 75 | 75 | ||
| 76 | 76 | $resp->assertJson([ | |
| 77 | - 'data' => $expectedData->values()->all(), | ||
| 77 | + 'data' => $expectedData->values()->all(), | ||
| 78 | 78 | 'total' => 2, | |
| 79 | 79 | ]); | |
| 80 | 80 | } | |
| 81 | 81 | ||
| 82 | 82 | public function test_index_endpoint_returns_children() | |
| 83 | 83 | { | |
| 84 | 84 | $this->actingAsAuthorizedUser(); | |
| 85 | - | ||
| 85 | + | ||
| 86 | 86 | $book = Book::query()->whereHas('pages')->whereHas('chapters')->withCount(['pages', 'chapters'])->first(); | |
| 87 | 87 | $editor = $this->getEditor(); | |
| 88 | 88 | $this->actingAs($editor)->delete($book->getUrl()); | |
@@ -100,15 +100,15 @@ public function test_index_endpoint_returns_children() | |||
| 100 | 100 | 'deletable_type' => $book->getMorphClass(), | |
| 101 | 101 | 'deletable_id' => $book->getKey(), | |
| 102 | 102 | 'children' => [ | |
| 103 | - 'Bookstack\Page' => $book->pages_count, | ||
| 104 | - 'Bookstack\Chapter' => $book->chapters_count, | ||
| 103 | + 'BookStack\Page' => $book->pages_count, | ||
| 104 | + 'BookStack\Chapter' => $book->chapters_count, | ||
| 105 | 105 | ], | |
| 106 | 106 | 'parent' => null, | |
| 107 | - ] | ||
| 107 | + ], | ||
| 108 | 108 | ]; | |
| 109 | 109 | ||
| 110 | 110 | $resp->assertJson([ | |
| 111 | - 'data' => $expectedData, | ||
| 111 | + 'data' => $expectedData, | ||
| 112 | 112 | 'total' => 1, | |
| 113 | 113 | ]); | |
| 114 | 114 | } | |
@@ -136,22 +136,22 @@ public function test_index_endpoint_returns_parent() | |||
| 136 | 136 | 'deletable_id' => $page->getKey(), | |
| 137 | 137 | 'parent' => [ | |
| 138 | 138 | 'type' => 'BookStack\Chapter', | |
| 139 | - 'id' => $page->chapter->getKey() | ||
| 139 | + 'id' => $page->chapter->getKey(), | ||
| 140 | 140 | ], | |
| 141 | 141 | 'children' => null, | |
| 142 | - ] | ||
| 142 | + ], | ||
| 143 | 143 | ]; | |
| 144 | 144 | ||
| 145 | 145 | $resp->assertJson([ | |
| 146 | - 'data' => $expectedData, | ||
| 147 | - 'total' => 1 | ||
| 146 | + 'data' => $expectedData, | ||
| 147 | + 'total' => 1, | ||
| 148 | 148 | ]); | |
| 149 | 149 | } | |
| 150 | 150 | ||
| 151 | 151 | public function test_restore_endpoint() | |
| 152 | 152 | { | |
| 153 | 153 | $this->actingAsAuthorizedUser(); | |
| 154 | - | ||
| 154 | + | ||
| 155 | 155 | $page = Page::query()->first(); | |
| 156 | 156 | $editor = $this->getEditor(); | |
| 157 | 157 | $this->actingAs($editor)->delete($page->getUrl()); | |
@@ -160,22 +160,22 @@ public function test_restore_endpoint() | |||
| 160 | 160 | $deletion = Deletion::query()->orderBy('id')->first(); | |
| 161 | 161 | ||
| 162 | 162 | $this->assertDatabaseHas('pages', [ | |
| 163 | - 'id' => $page->getKey(), | ||
| 164 | - 'deleted_at' => $page->deleted_at, | ||
| 163 | + 'id' => $page->getKey(), | ||
| 164 | + 'deleted_at' => $page->deleted_at, | ||
| 165 | 165 | ]); | |
| 166 | 166 | ||
| 167 | 167 | $this->putJson($this->baseEndpoint . '/' . $deletion->getKey()); | |
| 168 | 168 | ||
| 169 | 169 | $this->assertDatabaseHas('pages', [ | |
| 170 | - 'id' => $page->getKey(), | ||
| 171 | - 'deleted_at' => null, | ||
| 170 | + 'id' => $page->getKey(), | ||
| 171 | + 'deleted_at' => null, | ||
| 172 | 172 | ]); | |
| 173 | 173 | } | |
| 174 | 174 | ||
| 175 | 175 | public function test_destroy_endpoint() | |
| 176 | 176 | { | |
| 177 | 177 | $this->actingAsAuthorizedUser(); | |
| 178 | - | ||
| 178 | + | ||
| 179 | 179 | $page = Page::query()->first(); | |
| 180 | 180 | $editor = $this->getEditor(); | |
| 181 | 181 | $this->actingAs($editor)->delete($page->getUrl()); | |
@@ -184,8 +184,8 @@ public function test_destroy_endpoint() | |||
| 184 | 184 | $deletion = Deletion::query()->orderBy('id')->first(); | |
| 185 | 185 | ||
| 186 | 186 | $this->assertDatabaseHas('pages', [ | |
| 187 | - 'id' => $page->getKey(), | ||
| 188 | - 'deleted_at' => $page->deleted_at, | ||
| 187 | + 'id' => $page->getKey(), | ||
| 188 | + 'deleted_at' => $page->deleted_at, | ||
| 189 | 189 | ]); | |
| 190 | 190 | ||
| 191 | 191 | $this->deleteJson($this->baseEndpoint . '/' . $deletion->getKey()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments