]> BookStack Code Mirror - bookstack/commitdiff
Authenticated admins on all guards upon login
authorDan Brown <redacted>
Sat, 25 Apr 2020 17:19:22 +0000 (18:19 +0100)
committerDan Brown <redacted>
Sat, 25 Apr 2020 17:19:22 +0000 (18:19 +0100)
For #2031

app/Http/Controllers/Auth/LoginController.php
tests/Auth/AuthTest.php

index 4660c16d5e9b47899595d47cf35b48cc51cabc47..fb2573b5cc2f32abc3f3b70f7b8eeeb0c8d125ab 100644 (file)
@@ -125,6 +125,26 @@ class LoginController extends Controller
         return $this->sendFailedLoginResponse($request);
     }
 
+    /**
+     * The user has been authenticated.
+     *
+     * @param  \Illuminate\Http\Request  $request
+     * @param  mixed  $user
+     * @return mixed
+     */
+    protected function authenticated(Request $request, $user)
+    {
+        // Authenticate on all session guards if a likely admin
+        if ($user->can('users-manage') && $user->can('user-roles-manage')) {
+            $guards = ['standard', 'ldap', 'saml2'];
+            foreach ($guards as $guard) {
+                auth($guard)->login($user);
+            }
+        }
+
+        return redirect()->intended($this->redirectPath());
+    }
+
     /**
      * Validate the user login request.
      *
index 40bcda713d6affc1cd1097346c2b1986eeaf7c15..f1f47696641ac4978f2dc33c87a93b5b8805a3e6 100644 (file)
@@ -381,13 +381,30 @@ class AuthTest extends BrowserKitTest
             ->seePageUrlIs($page->getUrl());
     }
 
+    public function test_login_authenticates_admins_on_all_guards()
+    {
+        $this->post('/login', ['email' => '[email protected]', 'password' => 'password']);
+        $this->assertTrue(auth()->check());
+        $this->assertTrue(auth('ldap')->check());
+        $this->assertTrue(auth('saml2')->check());
+    }
+
+    public function test_login_authenticates_nonadmins_on_default_guard_only()
+    {
+        $editor = $this->getEditor();
+        $editor->password = bcrypt('password');
+        $editor->save();
+
+        $this->post('/login', ['email' => $editor->email, 'password' => 'password']);
+        $this->assertTrue(auth()->check());
+        $this->assertFalse(auth('ldap')->check());
+        $this->assertFalse(auth('saml2')->check());
+    }
+
     /**
      * Perform a login
-     * @param string $email
-     * @param string $password
-     * @return $this
      */
-    protected function login($email, $password)
+    protected function login(string $email, string $password): AuthTest
     {
         return $this->visit('/login')
             ->type($email, '#email')