Skip to content

Commit 419dbad

Browse files
committed
Permissions: Updated use of helpers to use enums
Also added middlware method to Permission enum to allow easier usage with controller middleware.
1 parent 33a0237 commit 419dbad

62 files changed

Lines changed: 165 additions & 145 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

app/Activity/Controllers/WebhookController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Activity\Models\Webhook;
77
use BookStack\Activity\Queries\WebhooksAllPaginatedAndSorted;
88
use BookStack\Http\Controller;
9+
use BookStack\Permissions\Permission;
910
use BookStack\Util\SimpleListOptions;
1011
use Illuminate\Http\Request;
1112

@@ -14,7 +15,7 @@ class WebhookController extends Controller
1415
public function __construct()
1516
{
1617
$this->middleware([
17-
'can:settings-manage',
18+
Permission::SettingsManage->middleware()
1819
]);
1920
}
2021

app/Activity/Tools/CommentTree.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function getCommentNodeForId(int $commentId): ?CommentTreeNode
7070
public function canUpdateAny(): bool
7171
{
7272
foreach ($this->comments as $comment) {
73-
if (userCan('comment-update', $comment)) {
73+
if (userCan(\BookStack\Permissions\Permission::CommentUpdate, $comment)) {
7474
return true;
7575
}
7676
}

app/Activity/Tools/TagClassGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ public function generate(): array
2626
array_push($classes, ...$this->generateClassesForTag($tag));
2727
}
2828

29-
if ($this->entity instanceof BookChild && userCan('view', $this->entity->book)) {
29+
if ($this->entity instanceof BookChild && userCan(\BookStack\Permissions\Permission::View, $this->entity->book)) {
3030
$bookTags = $this->entity->book->tags;
3131
foreach ($bookTags as $bookTag) {
3232
array_push($classes, ...$this->generateClassesForTag($bookTag, 'book-'));
3333
}
3434
}
3535

36-
if ($this->entity instanceof Page && $this->entity->chapter && userCan('view', $this->entity->chapter)) {
36+
if ($this->entity instanceof Page && $this->entity->chapter && userCan(\BookStack\Permissions\Permission::View, $this->entity->chapter)) {
3737
$chapterTags = $this->entity->chapter->tags;
3838
foreach ($chapterTags as $chapterTag) {
3939
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('view', $chapter)) {
345+
if ($chapter && userCan(\BookStack\Permissions\Permission::View, $chapter)) {
346346
return redirect($chapter->getUrl());
347347
}
348348

app/Entities/Repos/ChapterRepo.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function move(Chapter $chapter, string $parentIdentifier): Book
8787
throw new MoveOperationException('Book to move chapter into not found');
8888
}
8989

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

app/Entities/Repos/PageRepo.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public function getNewDraftPage(Entity $parent)
5555
}
5656

5757
$defaultTemplate = $page->chapter->defaultTemplate ?? $page->book->defaultTemplate;
58-
if ($defaultTemplate && userCan('view', $defaultTemplate)) {
58+
if ($defaultTemplate && userCan(\BookStack\Permissions\Permission::View, $defaultTemplate)) {
5959
$page->forceFill([
6060
'html' => $defaultTemplate->html,
6161
'markdown' => $defaultTemplate->markdown,
@@ -142,7 +142,7 @@ public function update(Page $page, array $input): Page
142142

143143
protected function updateTemplateStatusAndContentFromInput(Page $page, array $input): void
144144
{
145-
if (isset($input['template']) && userCan('templates-manage')) {
145+
if (isset($input['template']) && userCan(\BookStack\Permissions\Permission::TemplatesManage)) {
146146
$page->template = ($input['template'] === 'true');
147147
}
148148

@@ -165,7 +165,7 @@ protected function updateTemplateStatusAndContentFromInput(Page $page, array $in
165165
$pageContent->setNewHTML($input['html'], user());
166166
}
167167

168-
if (($newEditor !== $currentEditor || empty($page->editor)) && userCan('editor-change')) {
168+
if (($newEditor !== $currentEditor || empty($page->editor)) && userCan(\BookStack\Permissions\Permission::EditorChange)) {
169169
$page->editor = $newEditor->value;
170170
} elseif (empty($page->editor)) {
171171
$page->editor = $defaultEditor->value;
@@ -271,7 +271,7 @@ public function move(Page $page, string $parentIdentifier): Entity
271271
throw new MoveOperationException('Book or chapter to move page into not found');
272272
}
273273

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

app/Entities/Tools/Cloner.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function cloneChapter(Chapter $original, Book $parent, string $newName):
4949

5050
$copyChapter = $this->chapterRepo->create($chapterDetails, $parent);
5151

52-
if (userCan('page-create', $copyChapter)) {
52+
if (userCan(\BookStack\Permissions\Permission::PageCreate, $copyChapter)) {
5353
/** @var Page $page */
5454
foreach ($original->getVisiblePages() as $page) {
5555
$this->clonePage($page, $copyChapter, $page->name);
@@ -74,19 +74,19 @@ public function cloneBook(Book $original, string $newName): Book
7474
// Clone contents
7575
$directChildren = $original->getDirectVisibleChildren();
7676
foreach ($directChildren as $child) {
77-
if ($child instanceof Chapter && userCan('chapter-create', $copyBook)) {
77+
if ($child instanceof Chapter && userCan(\BookStack\Permissions\Permission::ChapterCreate, $copyBook)) {
7878
$this->cloneChapter($child, $copyBook, $child->name);
7979
}
8080

81-
if ($child instanceof Page && !$child->draft && userCan('page-create', $copyBook)) {
81+
if ($child instanceof Page && !$child->draft && userCan(\BookStack\Permissions\Permission::PageCreate, $copyBook)) {
8282
$this->clonePage($child, $copyBook, $child->name);
8383
}
8484
}
8585

8686
// Clone bookshelf relationships
8787
/** @var Bookshelf $shelf */
8888
foreach ($original->shelves as $shelf) {
89-
if (userCan('bookshelf-update', $shelf)) {
89+
if (userCan(\BookStack\Permissions\Permission::BookshelfUpdate, $shelf)) {
9090
$shelf->appendBook($copyBook);
9191
}
9292
}

app/Entities/Tools/PageEditorData.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function getEditorType(Page $page): PageEditorType
100100

101101
// Use requested editor if valid and if we have permission
102102
$requestedType = PageEditorType::fromRequestValue($this->requestedEditor);
103-
if ($requestedType && userCan('editor-change')) {
103+
if ($requestedType && userCan(\BookStack\Permissions\Permission::EditorChange)) {
104104
$editorType = $requestedType;
105105
}
106106

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('restrictions-manage', $book)) {
153+
if ($checkUserPermissions && !userCan(\BookStack\Permissions\Permission::RestrictionsManage, $book)) {
154154
continue;
155155
}
156156
$book->permissions()->delete();

app/Exports/Controllers/BookExportApiController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\Exports\ExportFormatter;
77
use BookStack\Exports\ZipExports\ZipExportBuilder;
88
use BookStack\Http\ApiController;
9+
use BookStack\Permissions\Permission;
910
use Throwable;
1011

1112
class BookExportApiController extends ApiController
@@ -14,7 +15,7 @@ public function __construct(
1415
protected ExportFormatter $exportFormatter,
1516
protected BookQueries $queries,
1617
) {
17-
$this->middleware('can:content-export');
18+
$this->middleware(Permission::ContentExport->middleware());
1819
}
1920

2021
/**

0 commit comments

Comments
 (0)