]> BookStack Code Mirror - bookstack/blobdiff - app/Http/Controllers/Auth/LoginController.php
Chinese translation update for v0.24.1
[bookstack] / app / Http / Controllers / Auth / LoginController.php
index 3617652c2b6e0cbb4bc5695127128fde3dfd223b..e820154e71be2f85b0d771d54b7ae03627022808 100644 (file)
@@ -2,10 +2,11 @@
 
 namespace BookStack\Http\Controllers\Auth;
 
+use BookStack\Auth\Access\LdapService;
+use BookStack\Auth\Access\SocialAuthService;
+use BookStack\Auth\UserRepo;
 use BookStack\Exceptions\AuthException;
 use BookStack\Http\Controllers\Controller;
-use BookStack\Repos\UserRepo;
-use BookStack\Services\SocialAuthService;
 use Illuminate\Contracts\Auth\Authenticatable;
 use Illuminate\Foundation\Auth\AuthenticatesUsers;
 use Illuminate\Http\Request;
@@ -36,18 +37,21 @@ class LoginController extends Controller
     protected $redirectAfterLogout = '/login';
 
     protected $socialAuthService;
+    protected $ldapService;
     protected $userRepo;
 
     /**
      * Create a new controller instance.
      *
-     * @param SocialAuthService $socialAuthService
-     * @param UserRepo $userRepo
+     * @param \BookStack\Auth\\BookStack\Auth\Access\SocialAuthService $socialAuthService
+     * @param LdapService $ldapService
+     * @param \BookStack\Auth\UserRepo $userRepo
      */
-    public function __construct(SocialAuthService $socialAuthService, UserRepo $userRepo)
+    public function __construct(SocialAuthService $socialAuthService, LdapService $ldapService, UserRepo $userRepo)
     {
         $this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
         $this->socialAuthService = $socialAuthService;
+        $this->ldapService = $ldapService;
         $this->userRepo = $userRepo;
         $this->redirectPath = baseUrl('/');
         $this->redirectAfterLogout = baseUrl('/login');
@@ -66,11 +70,14 @@ class LoginController extends Controller
      * @param Authenticatable $user
      * @return \Illuminate\Http\RedirectResponse
      * @throws AuthException
+     * @throws \BookStack\Exceptions\LdapException
      */
     protected function authenticated(Request $request, Authenticatable $user)
     {
         // Explicitly log them out for now if they do no exist.
-        if (!$user->exists) auth()->logout($user);
+        if (!$user->exists) {
+            auth()->logout($user);
+        }
 
         if (!$user->exists && $user->email === null && !$request->filled('email')) {
             $request->flash();
@@ -83,7 +90,6 @@ class LoginController extends Controller
         }
 
         if (!$user->exists) {
-
             // Check for users with same email already
             $alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
             if ($alreadyUser) {
@@ -95,6 +101,11 @@ class LoginController extends Controller
             auth()->login($user);
         }
 
+        // Sync LDAP groups if required
+        if ($this->ldapService->shouldSyncGroups()) {
+            $this->ldapService->syncGroups($user, $request->get($this->username()));
+        }
+
         $path = session()->pull('url.intended', '/');
         $path = baseUrl($path, true);
         return redirect($path);
@@ -124,10 +135,11 @@ class LoginController extends Controller
      * Redirect to the relevant social site.
      * @param $socialDriver
      * @return \Symfony\Component\HttpFoundation\RedirectResponse
+     * @throws \BookStack\Exceptions\SocialDriverNotConfigured
      */
     public function getSocialLogin($socialDriver)
     {
         session()->put('social-callback', 'login');
         return $this->socialAuthService->startLogIn($socialDriver);
     }
-}
\ No newline at end of file
+}