Skip to content

Commit 1721543

Browse files
committed
Fixed default registration role display options
- This also allows an admin to choose not to have a default role. - Also applied latest styleCI fixes. For #3220
1 parent 90c5430 commit 1721543

File tree

6 files changed

+25
-8
lines changed

6 files changed

+25
-8
lines changed

app/Auth/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ public function hasSystemRole(string $roleSystemName): bool
146146
*/
147147
public function attachDefaultRole(): void
148148
{
149-
$roleId = setting('registration-role');
149+
$roleId = intval(setting('registration-role'));
150150
if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
151151
$this->roles()->attach($roleId);
152152
}

app/Config/dompdf.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
* Configuration should be altered via the `.env` file or environment variables.
88
* Do not edit this file unless you're happy to maintain any changes yourself.
99
*/
10-
1110
$dompdfPaperSizeMap = [
12-
'a4' => 'a4',
11+
'a4' => 'a4',
1312
'letter' => 'letter',
1413
];
1514

app/Config/snappy.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
* Configuration should be altered via the `.env` file or environment variables.
88
* Do not edit this file unless you're happy to maintain any changes yourself.
99
*/
10-
1110
$snappyPaperSizeMap = [
12-
'a4' => 'A4',
11+
'a4' => 'A4',
1312
'letter' => 'Letter',
1413
];
1514

@@ -19,7 +18,7 @@
1918
'binary' => file_exists(base_path('wkhtmltopdf')) ? base_path('wkhtmltopdf') : env('WKHTMLTOPDF', false),
2019
'timeout' => false,
2120
'options' => [
22-
'outline' => true,
21+
'outline' => true,
2322
'page-size' => $snappyPaperSizeMap[env('EXPORT_PAGE_SIZE', 'a4')] ?? 'A4',
2423
],
2524
'env' => [],

resources/lang/en/common.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
'status_active' => 'Active',
7676
'status_inactive' => 'Inactive',
7777
'never' => 'Never',
78+
'none' => 'None',
7879

7980
// Header
8081
'header_menu_expand' => 'Expand Header Menu',

resources/views/settings/index.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,10 +227,11 @@
227227

228228
<label for="setting-registration-role">{{ trans('settings.reg_default_role') }}</label>
229229
<select id="setting-registration-role" name="setting-registration-role" @if($errors->has('setting-registration-role')) class="neg" @endif>
230+
<option value="0" @if(intval(setting('registration-role', '0')) === 0) selected @endif>-- {{ trans('common.none') }} --</option>
230231
@foreach(\BookStack\Auth\Role::all() as $role)
231232
<option value="{{$role->id}}"
232233
data-system-role-name="{{ $role->system_name ?? '' }}"
233-
@if(setting('registration-role', \BookStack\Auth\Role::first()->id) == $role->id) selected @endif
234+
@if(intval(setting('registration-role', '0')) === $role->id) selected @endif
234235
>
235236
{{ $role->display_name }}
236237
</option>

tests/Auth/AuthTest.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Auth;
44

55
use BookStack\Auth\Access\Mfa\MfaSession;
6+
use BookStack\Auth\Role;
67
use BookStack\Auth\User;
78
use BookStack\Entities\Models\Page;
89
use BookStack\Notifications\ConfirmEmail;
@@ -43,7 +44,10 @@ public function test_registration_showing()
4344
public function test_normal_registration()
4445
{
4546
// Set settings and get user instance
46-
$this->setSettings(['registration-enabled' => 'true']);
47+
/** @var Role $registrationRole */
48+
$registrationRole = Role::query()->first();
49+
$this->setSettings(['registration-enabled' => 'true', 'registration-role' => $registrationRole->id]);
50+
/** @var User $user */
4751
$user = User::factory()->make();
4852

4953
// Test form and ensure user is created
@@ -57,7 +61,12 @@ public function test_normal_registration()
5761
$resp = $this->get('/');
5862
$resp->assertOk();
5963
$resp->assertSee($user->name);
64+
6065
$this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
66+
67+
$user = User::query()->where('email', '=', $user->email)->first();
68+
$this->assertEquals(1, $user->roles()->count());
69+
$this->assertEquals($registrationRole->id, $user->roles()->first()->id);
6170
}
6271

6372
public function test_empty_registration_redirects_back_with_errors()
@@ -189,6 +198,14 @@ public function test_restricted_registration_with_confirmation_disabled()
189198
$this->assertNull(auth()->user());
190199
}
191200

201+
public function test_registration_role_unset_by_default()
202+
{
203+
$this->assertFalse(setting('registration-role'));
204+
205+
$resp = $this->asAdmin()->get('/settings');
206+
$resp->assertElementContains('select[name="setting-registration-role"] option[value="0"][selected]', '-- None --');
207+
}
208+
192209
public function test_logout()
193210
{
194211
$this->asAdmin()->get('/')->assertOk();

0 commit comments

Comments
 (0)