Skip to content

Commit a70c733

Browse files
committed
Permissions: Cleanup after review of enum implementation PR
1 parent 573d692 commit a70c733

File tree

19 files changed

+56
-45
lines changed

19 files changed

+56
-45
lines changed

app/Activity/Tools/CommentTree.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use BookStack\Activity\Models\Comment;
66
use BookStack\Entities\Models\Page;
7+
use BookStack\Permissions\Permission;
78

89
class CommentTree
910
{
@@ -70,7 +71,7 @@ public function getCommentNodeForId(int $commentId): ?CommentTreeNode
7071
public function canUpdateAny(): bool
7172
{
7273
foreach ($this->comments as $comment) {
73-
if (userCan(\BookStack\Permissions\Permission::CommentUpdate, $comment)) {
74+
if (userCan(Permission::CommentUpdate, $comment)) {
7475
return true;
7576
}
7677
}

app/Activity/Tools/TagClassGenerator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Entities\Models\BookChild;
77
use BookStack\Entities\Models\Entity;
88
use BookStack\Entities\Models\Page;
9+
use BookStack\Permissions\Permission;
910

1011
class TagClassGenerator
1112
{
@@ -26,14 +27,14 @@ public function generate(): array
2627
array_push($classes, ...$this->generateClassesForTag($tag));
2728
}
2829

29-
if ($this->entity instanceof BookChild && userCan(\BookStack\Permissions\Permission::View, $this->entity->book)) {
30+
if ($this->entity instanceof BookChild && userCan(Permission::BookView, $this->entity->book)) {
3031
$bookTags = $this->entity->book->tags;
3132
foreach ($bookTags as $bookTag) {
3233
array_push($classes, ...$this->generateClassesForTag($bookTag, 'book-'));
3334
}
3435
}
3536

36-
if ($this->entity instanceof Page && $this->entity->chapter && userCan(\BookStack\Permissions\Permission::View, $this->entity->chapter)) {
37+
if ($this->entity instanceof Page && $this->entity->chapter && userCan(Permission::ChapterView, $this->entity->chapter)) {
3738
$chapterTags = $this->entity->chapter->tags;
3839
foreach ($chapterTags as $chapterTag) {
3940
array_push($classes, ...$this->generateClassesForTag($chapterTag, 'chapter-'));

app/Entities/Controllers/PageController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public function destroyDraft(string $bookSlug, int $pageId)
342342

343343
$this->showSuccessNotification(trans('entities.pages_delete_draft_success'));
344344

345-
if ($chapter && userCan(\BookStack\Permissions\Permission::View, $chapter)) {
345+
if ($chapter && userCan(Permission::ChapterView, $chapter)) {
346346
return redirect($chapter->getUrl());
347347
}
348348

app/Entities/Repos/ChapterRepo.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use BookStack\Exceptions\MoveOperationException;
1212
use BookStack\Exceptions\PermissionsException;
1313
use BookStack\Facades\Activity;
14+
use BookStack\Permissions\Permission;
1415
use BookStack\Util\DatabaseTransaction;
1516
use Exception;
1617

@@ -87,7 +88,7 @@ public function move(Chapter $chapter, string $parentIdentifier): Book
8788
throw new MoveOperationException('Book to move chapter into not found');
8889
}
8990

90-
if (!userCan(\BookStack\Permissions\Permission::ChapterCreate, $parent)) {
91+
if (!userCan(Permission::ChapterCreate, $parent)) {
9192
throw new PermissionsException('User does not have permission to create a chapter within the chosen book');
9293
}
9394

app/Entities/Repos/PageRepo.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use BookStack\Exceptions\MoveOperationException;
1717
use BookStack\Exceptions\PermissionsException;
1818
use BookStack\Facades\Activity;
19+
use BookStack\Permissions\Permission;
1920
use BookStack\References\ReferenceStore;
2021
use BookStack\References\ReferenceUpdater;
2122
use BookStack\Util\DatabaseTransaction;
@@ -55,7 +56,7 @@ public function getNewDraftPage(Entity $parent)
5556
}
5657

5758
$defaultTemplate = $page->chapter->defaultTemplate ?? $page->book->defaultTemplate;
58-
if ($defaultTemplate && userCan(\BookStack\Permissions\Permission::View, $defaultTemplate)) {
59+
if ($defaultTemplate && userCan(Permission::PageView, $defaultTemplate)) {
5960
$page->forceFill([
6061
'html' => $defaultTemplate->html,
6162
'markdown' => $defaultTemplate->markdown,
@@ -142,7 +143,7 @@ public function update(Page $page, array $input): Page
142143

143144
protected function updateTemplateStatusAndContentFromInput(Page $page, array $input): void
144145
{
145-
if (isset($input['template']) && userCan(\BookStack\Permissions\Permission::TemplatesManage)) {
146+
if (isset($input['template']) && userCan(Permission::TemplatesManage)) {
146147
$page->template = ($input['template'] === 'true');
147148
}
148149

@@ -165,7 +166,7 @@ protected function updateTemplateStatusAndContentFromInput(Page $page, array $in
165166
$pageContent->setNewHTML($input['html'], user());
166167
}
167168

168-
if (($newEditor !== $currentEditor || empty($page->editor)) && userCan(\BookStack\Permissions\Permission::EditorChange)) {
169+
if (($newEditor !== $currentEditor || empty($page->editor)) && userCan(Permission::EditorChange)) {
169170
$page->editor = $newEditor->value;
170171
} elseif (empty($page->editor)) {
171172
$page->editor = $defaultEditor->value;
@@ -271,7 +272,7 @@ public function move(Page $page, string $parentIdentifier): Entity
271272
throw new MoveOperationException('Book or chapter to move page into not found');
272273
}
273274

274-
if (!userCan(\BookStack\Permissions\Permission::PageCreate, $parent)) {
275+
if (!userCan(Permission::PageCreate, $parent)) {
275276
throw new PermissionsException('User does not have permission to create a page within the new parent');
276277
}
277278

app/Entities/Tools/Cloner.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use BookStack\Entities\Repos\BookRepo;
1313
use BookStack\Entities\Repos\ChapterRepo;
1414
use BookStack\Entities\Repos\PageRepo;
15+
use BookStack\Permissions\Permission;
1516
use BookStack\Uploads\Image;
1617
use BookStack\Uploads\ImageService;
1718
use Illuminate\Http\UploadedFile;
@@ -49,7 +50,7 @@ public function cloneChapter(Chapter $original, Book $parent, string $newName):
4950

5051
$copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
5152

52-
if (userCan(\BookStack\Permissions\Permission::PageCreate, $copyChapter)) {
53+
if (userCan(Permission::PageCreate, $copyChapter)) {
5354
/** @var Page $page */
5455
foreach ($original->getVisiblePages() as $page) {
5556
$this->clonePage($page, $copyChapter, $page->name);
@@ -61,7 +62,7 @@ public function cloneChapter(Chapter $original, Book $parent, string $newName):
6162

6263
/**
6364
* Clone the given book.
64-
* Clones all child chapters & pages.
65+
* Clones all child chapters and pages.
6566
*/
6667
public function cloneBook(Book $original, string $newName): Book
6768
{
@@ -74,19 +75,19 @@ public function cloneBook(Book $original, string $newName): Book
7475
// Clone contents
7576
$directChildren = $original->getDirectVisibleChildren();
7677
foreach ($directChildren as $child) {
77-
if ($child instanceof Chapter && userCan(\BookStack\Permissions\Permission::ChapterCreate, $copyBook)) {
78+
if ($child instanceof Chapter && userCan(Permission::ChapterCreate, $copyBook)) {
7879
$this->cloneChapter($child, $copyBook, $child->name);
7980
}
8081

81-
if ($child instanceof Page && !$child->draft && userCan(\BookStack\Permissions\Permission::PageCreate, $copyBook)) {
82+
if ($child instanceof Page && !$child->draft && userCan(Permission::PageCreate, $copyBook)) {
8283
$this->clonePage($child, $copyBook, $child->name);
8384
}
8485
}
8586

8687
// Clone bookshelf relationships
8788
/** @var Bookshelf $shelf */
8889
foreach ($original->shelves as $shelf) {
89-
if (userCan(\BookStack\Permissions\Permission::BookshelfUpdate, $shelf)) {
90+
if (userCan(Permission::BookshelfUpdate, $shelf)) {
9091
$shelf->appendBook($copyBook);
9192
}
9293
}

app/Entities/Tools/PageEditorData.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BookStack\Entities\Queries\EntityQueries;
88
use BookStack\Entities\Tools\Markdown\HtmlToMarkdown;
99
use BookStack\Entities\Tools\Markdown\MarkdownToHtml;
10+
use BookStack\Permissions\Permission;
1011

1112
class PageEditorData
1213
{
@@ -98,9 +99,9 @@ protected function getEditorType(Page $page): PageEditorType
9899
{
99100
$editorType = PageEditorType::forPage($page) ?: PageEditorType::getSystemDefault();
100101

101-
// Use requested editor if valid and if we have permission
102+
// Use the requested editor if valid and if we have permission
102103
$requestedType = PageEditorType::fromRequestValue($this->requestedEditor);
103-
if ($requestedType && userCan(\BookStack\Permissions\Permission::EditorChange)) {
104+
if ($requestedType && userCan(Permission::EditorChange)) {
104105
$editorType = $requestedType;
105106
}
106107

app/Entities/Tools/PermissionsUpdater.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public function updateBookPermissionsFromShelf(Bookshelf $shelf, $checkUserPermi
150150

151151
/** @var Book $book */
152152
foreach ($shelfBooks as $book) {
153-
if ($checkUserPermissions && !userCan(\BookStack\Permissions\Permission::RestrictionsManage, $book)) {
153+
if ($checkUserPermissions && !userCan(Permission::RestrictionsManage, $book)) {
154154
continue;
155155
}
156156
$book->permissions()->delete();

app/Exports/ImportRepo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use BookStack\Exports\ZipExports\ZipExportValidator;
1717
use BookStack\Exports\ZipExports\ZipImportRunner;
1818
use BookStack\Facades\Activity;
19+
use BookStack\Permissions\Permission;
1920
use BookStack\Uploads\FileStorage;
2021
use Illuminate\Database\Eloquent\Builder;
2122
use Illuminate\Database\Eloquent\Collection;
@@ -46,7 +47,7 @@ public function queryVisible(): Builder
4647
{
4748
$query = Import::query();
4849

49-
if (!userCan(\BookStack\Permissions\Permission::SettingsManage)) {
50+
if (!userCan(Permission::SettingsManage)) {
5051
$query->where('created_by', user()->id);
5152
}
5253

@@ -57,7 +58,7 @@ public function findVisible(int $id): Import
5758
{
5859
$query = Import::query();
5960

60-
if (!userCan(\BookStack\Permissions\Permission::SettingsManage)) {
61+
if (!userCan(Permission::SettingsManage)) {
6162
$query->where('created_by', user()->id);
6263
}
6364

app/Exports/ZipExports/ZipExportReferences.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use BookStack\Exports\ZipExports\Models\ZipExportImage;
1313
use BookStack\Exports\ZipExports\Models\ZipExportModel;
1414
use BookStack\Exports\ZipExports\Models\ZipExportPage;
15+
use BookStack\Permissions\Permission;
1516
use BookStack\Uploads\Attachment;
1617
use BookStack\Uploads\Image;
1718

@@ -135,7 +136,7 @@ protected function handleModelReference(Model $model, ZipExportModel $exportMode
135136
// Find and include images if in visibility
136137
$page = $model->getPage();
137138
$pageExportModel = $this->pages[$page->id] ?? ($exportModel instanceof ZipExportPage ? $exportModel : null);
138-
if (isset($this->images[$model->id]) || ($page && $pageExportModel && userCan(\BookStack\Permissions\Permission::View, $page))) {
139+
if (isset($this->images[$model->id]) || ($page && $pageExportModel && userCan(Permission::PageView, $page))) {
139140
if (!isset($this->images[$model->id])) {
140141
$exportImage = ZipExportImage::fromModel($model, $files);
141142
$this->images[$model->id] = $exportImage;

0 commit comments

Comments
 (0)