]> BookStack Code Mirror - bookstack/commitdiff
Fixed default registration role display options
authorDan Brown <redacted>
Mon, 31 Jan 2022 14:16:56 +0000 (14:16 +0000)
committerDan Brown <redacted>
Mon, 31 Jan 2022 14:16:56 +0000 (14:16 +0000)
- This also allows an admin to choose not to have a default role.
- Also applied latest styleCI fixes.

For #3220

app/Auth/User.php
app/Config/dompdf.php
app/Config/snappy.php
resources/lang/en/common.php
resources/views/settings/index.blade.php
tests/Auth/AuthTest.php

index 540a8d7abbe0d5ebd7f49ed34bd8957ec2b93e79..f969b351f4a169bab7144c36a3c05c3a3de6af9b 100644 (file)
@@ -146,7 +146,7 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
      */
     public function attachDefaultRole(): void
     {
-        $roleId = setting('registration-role');
+        $roleId = intval(setting('registration-role'));
         if ($roleId && $this->roles()->where('id', '=', $roleId)->count() === 0) {
             $this->roles()->attach($roleId);
         }
index 55260a22ad7129c3e60e662d1eca1de76202883c..a8728852c2c9ee05157d1b50e727051b203ca4cb 100644 (file)
@@ -7,9 +7,8 @@
  * Configuration should be altered via the `.env` file or environment variables.
  * Do not edit this file unless you're happy to maintain any changes yourself.
  */
-
 $dompdfPaperSizeMap = [
-    'a4' => 'a4',
+    'a4'     => 'a4',
     'letter' => 'letter',
 ];
 
index abdec88326cb82c1e61f4fcf85ea83817f5ddbb2..8ab10a39c432baa0e6d880136a3584f4e168a021 100644 (file)
@@ -7,9 +7,8 @@
  * Configuration should be altered via the `.env` file or environment variables.
  * Do not edit this file unless you're happy to maintain any changes yourself.
  */
-
 $snappyPaperSizeMap = [
-    'a4' => 'A4',
+    'a4'     => 'A4',
     'letter' => 'Letter',
 ];
 
@@ -19,7 +18,7 @@ return [
         'binary'  => file_exists(base_path('wkhtmltopdf')) ? base_path('wkhtmltopdf') : env('WKHTMLTOPDF', false),
         'timeout' => false,
         'options' => [
-            'outline' => true,
+            'outline'   => true,
             'page-size' => $snappyPaperSizeMap[env('EXPORT_PAGE_SIZE', 'a4')] ?? 'A4',
         ],
         'env'     => [],
index 013042134f3a5c985139d018f0fdad24dd20c39e..2f09e53d1fe88389e923863d875c43e8d2713d4a 100644 (file)
@@ -75,6 +75,7 @@ return [
     'status_active' => 'Active',
     'status_inactive' => 'Inactive',
     'never' => 'Never',
+    'none' => 'None',
 
     // Header
     'header_menu_expand' => 'Expand Header Menu',
index 5fe5f3685ce505d30d30b7fdd2ff81fdef3d492e..8b561565853d18c89204504fd42023d65ea5e5fd 100644 (file)
 
                             <label for="setting-registration-role">{{ trans('settings.reg_default_role') }}</label>
                             <select id="setting-registration-role" name="setting-registration-role" @if($errors->has('setting-registration-role')) class="neg" @endif>
+                                <option value="0" @if(intval(setting('registration-role', '0')) === 0) selected @endif>-- {{ trans('common.none') }} --</option>
                                 @foreach(\BookStack\Auth\Role::all() as $role)
                                     <option value="{{$role->id}}"
                                             data-system-role-name="{{ $role->system_name ?? '' }}"
-                                            @if(setting('registration-role', \BookStack\Auth\Role::first()->id) == $role->id) selected @endif
+                                            @if(intval(setting('registration-role', '0')) === $role->id) selected @endif
                                     >
                                         {{ $role->display_name }}
                                     </option>
index 2c48131a885e559cf114e13c4d0e9cdfa6f8b59f..fd953021d068af884a07145d1fbbb64935a74da5 100644 (file)
@@ -3,6 +3,7 @@
 namespace Tests\Auth;
 
 use BookStack\Auth\Access\Mfa\MfaSession;
+use BookStack\Auth\Role;
 use BookStack\Auth\User;
 use BookStack\Entities\Models\Page;
 use BookStack\Notifications\ConfirmEmail;
@@ -43,7 +44,10 @@ class AuthTest extends TestCase
     public function test_normal_registration()
     {
         // Set settings and get user instance
-        $this->setSettings(['registration-enabled' => 'true']);
+        /** @var Role $registrationRole */
+        $registrationRole = Role::query()->first();
+        $this->setSettings(['registration-enabled' => 'true', 'registration-role' => $registrationRole->id]);
+        /** @var User $user */
         $user = User::factory()->make();
 
         // Test form and ensure user is created
@@ -57,7 +61,12 @@ class AuthTest extends TestCase
         $resp = $this->get('/');
         $resp->assertOk();
         $resp->assertSee($user->name);
+
         $this->assertDatabaseHas('users', ['name' => $user->name, 'email' => $user->email]);
+
+        $user = User::query()->where('email', '=', $user->email)->first();
+        $this->assertEquals(1, $user->roles()->count());
+        $this->assertEquals($registrationRole->id, $user->roles()->first()->id);
     }
 
     public function test_empty_registration_redirects_back_with_errors()
@@ -189,6 +198,14 @@ class AuthTest extends TestCase
         $this->assertNull(auth()->user());
     }
 
+    public function test_registration_role_unset_by_default()
+    {
+        $this->assertFalse(setting('registration-role'));
+
+        $resp = $this->asAdmin()->get('/settings');
+        $resp->assertElementContains('select[name="setting-registration-role"] option[value="0"][selected]', '-- None --');
+    }
+
     public function test_logout()
     {
         $this->asAdmin()->get('/')->assertOk();
Morty Proxy This is a proxified and sanitized view of the page, visit original site.