Skip to content

Commit 5fc11d4

Browse files
committed
Permissions: Added enum usage to controller helpers
Also fixed various missing types or spelling/formatting points. Added down action for role_permission table changes in migration.
1 parent c8716df commit 5fc11d4

3 files changed

Lines changed: 23 additions & 22 deletions

File tree

app/Http/Controller.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use BookStack\App\Model;
77
use BookStack\Exceptions\NotifyException;
88
use BookStack\Facades\Activity;
9+
use BookStack\Permissions\Permission;
910
use Illuminate\Foundation\Bus\DispatchesJobs;
1011
use Illuminate\Foundation\Validation\ValidatesRequests;
1112
use Illuminate\Http\JsonResponse;
@@ -27,10 +28,9 @@ protected function isSignedIn(): bool
2728
}
2829

2930
/**
30-
* Stops the application and shows a permission error if
31-
* the application is in demo mode.
31+
* Stops the application and shows a permission error if the application is in demo mode.
3232
*/
33-
protected function preventAccessInDemoMode()
33+
protected function preventAccessInDemoMode(): void
3434
{
3535
if (config('app.env') === 'demo') {
3636
$this->showPermissionError();
@@ -40,14 +40,13 @@ protected function preventAccessInDemoMode()
4040
/**
4141
* Adds the page title into the view.
4242
*/
43-
public function setPageTitle(string $title)
43+
public function setPageTitle(string $title): void
4444
{
4545
view()->share('pageTitle', $title);
4646
}
4747

4848
/**
49-
* On a permission error redirect to home and display.
50-
* the error as a notification.
49+
* On a permission error redirect to home and display the error as a notification.
5150
*
5251
* @throws NotifyException
5352
*/
@@ -61,7 +60,7 @@ protected function showPermissionError(string $redirectLocation = '/'): never
6160
/**
6261
* Checks that the current user has the given permission otherwise throw an exception.
6362
*/
64-
protected function checkPermission(string $permission): void
63+
protected function checkPermission(string|Permission $permission): void
6564
{
6665
if (!user() || !user()->can($permission)) {
6766
$this->showPermissionError();
@@ -81,7 +80,7 @@ protected function preventGuestAccess(): void
8180
/**
8281
* Check the current user's permissions against an ownable item otherwise throw an exception.
8382
*/
84-
protected function checkOwnablePermission(string $permission, Model $ownable, string $redirectLocation = '/'): void
83+
protected function checkOwnablePermission(string|Permission $permission, Model $ownable, string $redirectLocation = '/'): void
8584
{
8685
if (!userCan($permission, $ownable)) {
8786
$this->showPermissionError($redirectLocation);
@@ -92,7 +91,7 @@ protected function checkOwnablePermission(string $permission, Model $ownable, st
9291
* Check if a user has a permission or bypass the permission
9392
* check if the given callback resolves true.
9493
*/
95-
protected function checkPermissionOr(string $permission, callable $callback): void
94+
protected function checkPermissionOr(string|Permission $permission, callable $callback): void
9695
{
9796
if ($callback() !== true) {
9897
$this->checkPermission($permission);
@@ -103,15 +102,15 @@ protected function checkPermissionOr(string $permission, callable $callback): vo
103102
* Check if the current user has a permission or bypass if the provided user
104103
* id matches the current user.
105104
*/
106-
protected function checkPermissionOrCurrentUser(string $permission, int $userId): void
105+
protected function checkPermissionOrCurrentUser(string|Permission $permission, int $userId): void
107106
{
108107
$this->checkPermissionOr($permission, function () use ($userId) {
109108
return $userId === user()->id;
110109
});
111110
}
112111

113112
/**
114-
* Send back a json error message.
113+
* Send back a JSON error message.
115114
*/
116115
protected function jsonError(string $messageText = '', int $statusCode = 500): JsonResponse
117116
{
@@ -127,23 +126,23 @@ protected function download(): DownloadResponseFactory
127126
}
128127

129128
/**
130-
* Show a positive, successful notification to the user on next view load.
129+
* Show a positive, successful notification to the user on the next view load.
131130
*/
132131
protected function showSuccessNotification(string $message): void
133132
{
134133
session()->flash('success', $message);
135134
}
136135

137136
/**
138-
* Show a warning notification to the user on next view load.
137+
* Show a warning notification to the user on the next view load.
139138
*/
140139
protected function showWarningNotification(string $message): void
141140
{
142141
session()->flash('warning', $message);
143142
}
144143

145144
/**
146-
* Show an error notification to the user on next view load.
145+
* Show an error notification to the user on the next view load.
147146
*/
148147
protected function showErrorNotification(string $message): void
149148
{

app/Users/Controllers/RoleApiController.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@
1010

1111
class RoleApiController extends ApiController
1212
{
13-
protected PermissionsRepo $permissionsRepo;
14-
1513
protected array $fieldsToExpose = [
1614
'display_name', 'description', 'mfa_enforced', 'external_auth_id', 'created_at', 'updated_at',
1715
];
@@ -35,10 +33,9 @@ class RoleApiController extends ApiController
3533
]
3634
];
3735

38-
public function __construct(PermissionsRepo $permissionsRepo)
39-
{
40-
$this->permissionsRepo = $permissionsRepo;
41-
36+
public function __construct(
37+
protected PermissionsRepo $permissionsRepo
38+
) {
4239
// Checks for all endpoints in this controller
4340
$this->middleware(function ($request, $next) {
4441
$this->checkPermission('user-roles-manage');
@@ -125,9 +122,9 @@ public function delete(string $id)
125122
}
126123

127124
/**
128-
* Format the given role model for single-result display.
125+
* Format the given role model for a single-result display.
129126
*/
130-
protected function singleFormatter(Role $role)
127+
protected function singleFormatter(Role $role): void
131128
{
132129
$role->load('users:id,name,slug');
133130
$role->unsetRelation('permissions');

database/migrations/2025_09_02_111542_remove_unused_columns.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,10 @@ public function down(): void
2929
Schema::table('comments', function (Blueprint $table) {
3030
$table->longText('text')->nullable();
3131
});
32+
33+
Schema::table('role_permissions', function (Blueprint $table) {
34+
$table->string('display_name')->nullable();
35+
$table->string('description')->nullable();
36+
});
3237
}
3338
};

0 commit comments

Comments
 (0)