Skip to content

Commit fc10520

Browse files
authored
Merge pull request #5793 from BookStackApp/role_permission_refactor
Permissions: Use of enum references and RolePermission cleanup
2 parents 1ac7409 + a70c733 commit fc10520

File tree

106 files changed

+589
-385
lines changed

Some content is hidden

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

106 files changed

+589
-385
lines changed

app/Access/LoginService.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use BookStack\Exceptions\StoppedAuthenticationException;
1010
use BookStack\Facades\Activity;
1111
use BookStack\Facades\Theme;
12+
use BookStack\Permissions\Permission;
1213
use BookStack\Theming\ThemeEvents;
1314
use BookStack\Users\Models\User;
1415
use Exception;
@@ -50,7 +51,7 @@ public function login(User $user, string $method, bool $remember = false): void
5051
Theme::dispatch(ThemeEvents::AUTH_LOGIN, $method, $user);
5152

5253
// Authenticate on all session guards if a likely admin
53-
if ($user->can('users-manage') && $user->can('user-roles-manage')) {
54+
if ($user->can(Permission::UsersManage) && $user->can(Permission::UserRolesManage)) {
5455
$guards = ['standard', 'ldap', 'saml2', 'oidc'];
5556
foreach ($guards as $guard) {
5657
auth($guard)->login($user);

app/Activity/Controllers/AuditLogApiController.php

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

55
use BookStack\Activity\Models\Activity;
66
use BookStack\Http\ApiController;
7+
use BookStack\Permissions\Permission;
78

89
class AuditLogApiController extends ApiController
910
{
@@ -16,8 +17,8 @@ class AuditLogApiController extends ApiController
1617
*/
1718
public function list()
1819
{
19-
$this->checkPermission('settings-manage');
20-
$this->checkPermission('users-manage');
20+
$this->checkPermission(Permission::SettingsManage);
21+
$this->checkPermission(Permission::UsersManage);
2122

2223
$query = Activity::query()->with(['user']);
2324

app/Activity/Controllers/AuditLogController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Activity\ActivityType;
66
use BookStack\Activity\Models\Activity;
77
use BookStack\Http\Controller;
8+
use BookStack\Permissions\Permission;
89
use BookStack\Sorting\SortUrl;
910
use BookStack\Util\SimpleListOptions;
1011
use Illuminate\Http\Request;
@@ -13,8 +14,8 @@ class AuditLogController extends Controller
1314
{
1415
public function index(Request $request)
1516
{
16-
$this->checkPermission('settings-manage');
17-
$this->checkPermission('users-manage');
17+
$this->checkPermission(Permission::SettingsManage);
18+
$this->checkPermission(Permission::UsersManage);
1819

1920
$sort = $request->get('sort', 'activity_date');
2021
$order = $request->get('order', 'desc');

app/Activity/Controllers/CommentController.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BookStack\Activity\Tools\CommentTreeNode;
88
use BookStack\Entities\Queries\PageQueries;
99
use BookStack\Http\Controller;
10+
use BookStack\Permissions\Permission;
1011
use Illuminate\Http\Request;
1112
use Illuminate\Validation\ValidationException;
1213

@@ -42,7 +43,7 @@ public function savePageComment(Request $request, int $pageId)
4243
}
4344

4445
// Create a new comment.
45-
$this->checkPermission('comment-create-all');
46+
$this->checkPermission(Permission::CommentCreateAll);
4647
$contentRef = $input['content_ref'] ?? '';
4748
$comment = $this->commentRepo->create($page, $input['html'], $input['parent_id'] ?? null, $contentRef);
4849

@@ -64,8 +65,8 @@ public function update(Request $request, int $commentId)
6465
]);
6566

6667
$comment = $this->commentRepo->getById($commentId);
67-
$this->checkOwnablePermission('page-view', $comment->entity);
68-
$this->checkOwnablePermission('comment-update', $comment);
68+
$this->checkOwnablePermission(Permission::PageView, $comment->entity);
69+
$this->checkOwnablePermission(Permission::CommentUpdate, $comment);
6970

7071
$comment = $this->commentRepo->update($comment, $input['html']);
7172

@@ -81,8 +82,8 @@ public function update(Request $request, int $commentId)
8182
public function archive(int $id)
8283
{
8384
$comment = $this->commentRepo->getById($id);
84-
$this->checkOwnablePermission('page-view', $comment->entity);
85-
if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
85+
$this->checkOwnablePermission(Permission::PageView, $comment->entity);
86+
if (!userCan(Permission::CommentUpdate, $comment) && !userCan(Permission::CommentDelete, $comment)) {
8687
$this->showPermissionError();
8788
}
8889

@@ -101,8 +102,8 @@ public function archive(int $id)
101102
public function unarchive(int $id)
102103
{
103104
$comment = $this->commentRepo->getById($id);
104-
$this->checkOwnablePermission('page-view', $comment->entity);
105-
if (!userCan('comment-update', $comment) && !userCan('comment-delete', $comment)) {
105+
$this->checkOwnablePermission(Permission::PageView, $comment->entity);
106+
if (!userCan(Permission::CommentUpdate, $comment) && !userCan(Permission::CommentDelete, $comment)) {
106107
$this->showPermissionError();
107108
}
108109

@@ -121,7 +122,7 @@ public function unarchive(int $id)
121122
public function destroy(int $id)
122123
{
123124
$comment = $this->commentRepo->getById($id);
124-
$this->checkOwnablePermission('comment-delete', $comment);
125+
$this->checkOwnablePermission(Permission::CommentDelete, $comment);
125126

126127
$this->commentRepo->delete($comment);
127128

app/Activity/Controllers/WatchController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use BookStack\Activity\Tools\UserEntityWatchOptions;
66
use BookStack\Entities\Tools\MixedEntityRequestHelper;
77
use BookStack\Http\Controller;
8+
use BookStack\Permissions\Permission;
89
use Illuminate\Http\Request;
910

1011
class WatchController extends Controller
1112
{
1213
public function update(Request $request, MixedEntityRequestHelper $entityHelper)
1314
{
14-
$this->checkPermission('receive-notifications');
15+
$this->checkPermission(Permission::ReceiveNotifications);
1516
$this->preventGuestAccess();
1617

1718
$requestData = $this->validate($request, array_merge([

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/Notifications/Handlers/BaseNotificationHandler.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use BookStack\Activity\Models\Loggable;
66
use BookStack\Activity\Notifications\Messages\BaseActivityNotification;
77
use BookStack\Entities\Models\Entity;
8+
use BookStack\Permissions\Permission;
89
use BookStack\Permissions\PermissionApplicator;
910
use BookStack\Users\Models\User;
1011
use Illuminate\Support\Facades\Log;
@@ -26,7 +27,7 @@ protected function sendNotificationToUserIds(string $notification, array $userId
2627
}
2728

2829
// Prevent sending of the user does not have notification permissions
29-
if (!$user->can('receive-notifications')) {
30+
if (!$user->can(Permission::ReceiveNotifications)) {
3031
continue;
3132
}
3233

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('comment-update', $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('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('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/Activity/Tools/UserEntityWatchOptions.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use BookStack\Entities\Models\BookChild;
88
use BookStack\Entities\Models\Entity;
99
use BookStack\Entities\Models\Page;
10+
use BookStack\Permissions\Permission;
1011
use BookStack\Users\Models\User;
1112
use Illuminate\Database\Eloquent\Builder;
1213

@@ -22,7 +23,7 @@ public function __construct(
2223

2324
public function canWatch(): bool
2425
{
25-
return $this->user->can('receive-notifications') && !$this->user->isGuest();
26+
return $this->user->can(Permission::ReceiveNotifications) && !$this->user->isGuest();
2627
}
2728

2829
public function getWatchLevel(): string

0 commit comments

Comments
 (0)