Skip to content

Commit 33a0237

Browse files
committed
Permissions: Updated usage of controller methods to use enum
1 parent 5fc11d4 commit 33a0237

Some content is hidden

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

43 files changed

+235
-226
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/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/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

app/Api/ApiTokenGuard.php

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

55
use BookStack\Access\LoginService;
66
use BookStack\Exceptions\ApiAuthException;
7+
use BookStack\Permissions\Permission;
78
use Illuminate\Auth\GuardHelpers;
89
use Illuminate\Contracts\Auth\Authenticatable;
910
use Illuminate\Contracts\Auth\Guard;
@@ -146,7 +147,7 @@ protected function validateToken(?ApiToken $token, string $secret): void
146147
throw new ApiAuthException(trans('errors.api_user_token_expired'), 403);
147148
}
148149

149-
if (!$token->user->can('access-api')) {
150+
if (!$token->user->can(Permission::AccessApi)) {
150151
throw new ApiAuthException(trans('errors.api_user_no_api_permission'), 403);
151152
}
152153
}

app/Api/UserApiTokenController.php

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

55
use BookStack\Activity\ActivityType;
66
use BookStack\Http\Controller;
7+
use BookStack\Permissions\Permission;
78
use BookStack\Users\Models\User;
89
use Illuminate\Http\Request;
910
use Illuminate\Support\Facades\Hash;
@@ -16,8 +17,8 @@ class UserApiTokenController extends Controller
1617
*/
1718
public function create(Request $request, int $userId)
1819
{
19-
$this->checkPermission('access-api');
20-
$this->checkPermissionOrCurrentUser('users-manage', $userId);
20+
$this->checkPermission(Permission::AccessApi);
21+
$this->checkPermissionOrCurrentUser(Permission::UsersManage, $userId);
2122
$this->updateContext($request);
2223

2324
$user = User::query()->findOrFail($userId);
@@ -35,8 +36,8 @@ public function create(Request $request, int $userId)
3536
*/
3637
public function store(Request $request, int $userId)
3738
{
38-
$this->checkPermission('access-api');
39-
$this->checkPermissionOrCurrentUser('users-manage', $userId);
39+
$this->checkPermission(Permission::AccessApi);
40+
$this->checkPermissionOrCurrentUser(Permission::UsersManage, $userId);
4041

4142
$this->validate($request, [
4243
'name' => ['required', 'max:250'],
@@ -143,8 +144,8 @@ public function destroy(int $userId, int $tokenId)
143144
*/
144145
protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
145146
{
146-
$this->checkPermissionOr('users-manage', function () use ($userId) {
147-
return $userId === user()->id && userCan('access-api');
147+
$this->checkPermissionOr(Permission::UsersManage, function () use ($userId) {
148+
return $userId === user()->id && userCan(Permission::AccessApi);
148149
});
149150

150151
$user = User::query()->findOrFail($userId);

app/App/helpers.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ function userCan(string|Permission $permission, ?Model $ownable = null): bool
5656
* Check if the current user can perform the given action on any items in the system.
5757
* Can be provided the class name of an entity to filter ability to that specific entity type.
5858
*/
59-
function userCanOnAny(string $action, string $entityClass = ''): bool
59+
function userCanOnAny(string|Permission $action, string $entityClass = ''): bool
6060
{
6161
$permissions = app()->make(PermissionApplicator::class);
6262

0 commit comments

Comments
 (0)