2016-09-17 13:22:04 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Http\Controllers\Auth;
|
|
|
|
|
2016-12-04 11:51:39 -05:00
|
|
|
use BookStack\Exceptions\AuthException;
|
2016-09-17 13:22:04 -04:00
|
|
|
use BookStack\Http\Controllers\Controller;
|
|
|
|
use BookStack\Repos\UserRepo;
|
2018-07-15 12:57:25 -04:00
|
|
|
use BookStack\Services\LdapService;
|
2016-09-17 13:22:04 -04:00
|
|
|
use BookStack\Services\SocialAuthService;
|
|
|
|
use Illuminate\Contracts\Auth\Authenticatable;
|
|
|
|
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
|
|
|
class LoginController extends Controller
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Login Controller
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| This controller handles authenticating users for the application and
|
|
|
|
| redirecting them to your home screen. The controller uses a trait
|
|
|
|
| to conveniently provide its functionality to your applications.
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
use AuthenticatesUsers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Where to redirect users after login.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $redirectTo = '/';
|
|
|
|
|
|
|
|
protected $redirectPath = '/';
|
|
|
|
protected $redirectAfterLogout = '/login';
|
|
|
|
|
|
|
|
protected $socialAuthService;
|
2018-07-15 13:21:45 -04:00
|
|
|
protected $ldapService;
|
2016-09-17 13:22:04 -04:00
|
|
|
protected $userRepo;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new controller instance.
|
|
|
|
*
|
|
|
|
* @param SocialAuthService $socialAuthService
|
2018-07-15 13:21:45 -04:00
|
|
|
* @param LdapService $ldapService
|
2016-09-17 13:22:04 -04:00
|
|
|
* @param UserRepo $userRepo
|
|
|
|
*/
|
2018-07-15 13:21:45 -04:00
|
|
|
public function __construct(SocialAuthService $socialAuthService, LdapService $ldapService, UserRepo $userRepo)
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
|
|
|
$this->middleware('guest', ['only' => ['getLogin', 'postLogin']]);
|
|
|
|
$this->socialAuthService = $socialAuthService;
|
2018-07-15 13:21:45 -04:00
|
|
|
$this->ldapService = $ldapService;
|
2016-09-17 13:22:04 -04:00
|
|
|
$this->userRepo = $userRepo;
|
|
|
|
$this->redirectPath = baseUrl('/');
|
|
|
|
$this->redirectAfterLogout = baseUrl('/login');
|
|
|
|
parent::__construct();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function username()
|
|
|
|
{
|
|
|
|
return config('auth.method') === 'standard' ? 'email' : 'username';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides the action when a user is authenticated.
|
|
|
|
* If the user authenticated but does not exist in the user table we create them.
|
|
|
|
* @param Request $request
|
|
|
|
* @param Authenticatable $user
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws AuthException
|
2018-08-12 08:28:40 -04:00
|
|
|
* @throws \BookStack\Exceptions\LdapException
|
2016-09-17 13:22:04 -04:00
|
|
|
*/
|
|
|
|
protected function authenticated(Request $request, Authenticatable $user)
|
|
|
|
{
|
|
|
|
// Explicitly log them out for now if they do no exist.
|
2018-01-28 11:58:52 -05:00
|
|
|
if (!$user->exists) {
|
|
|
|
auth()->logout($user);
|
|
|
|
}
|
2016-09-17 13:22:04 -04:00
|
|
|
|
2017-11-19 10:56:06 -05:00
|
|
|
if (!$user->exists && $user->email === null && !$request->filled('email')) {
|
2016-09-17 13:22:04 -04:00
|
|
|
$request->flash();
|
|
|
|
session()->flash('request-email', true);
|
|
|
|
return redirect('/login');
|
|
|
|
}
|
|
|
|
|
2017-11-19 10:56:06 -05:00
|
|
|
if (!$user->exists && $user->email === null && $request->filled('email')) {
|
2016-09-17 13:22:04 -04:00
|
|
|
$user->email = $request->get('email');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$user->exists) {
|
|
|
|
// Check for users with same email already
|
|
|
|
$alreadyUser = $user->newQuery()->where('email', '=', $user->email)->count() > 0;
|
|
|
|
if ($alreadyUser) {
|
2016-12-04 11:51:39 -05:00
|
|
|
throw new AuthException(trans('errors.error_user_exists_different_creds', ['email' => $user->email]));
|
2016-09-17 13:22:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$user->save();
|
|
|
|
$this->userRepo->attachDefaultRole($user);
|
|
|
|
auth()->login($user);
|
|
|
|
}
|
|
|
|
|
2018-07-15 13:21:45 -04:00
|
|
|
// Sync LDAP groups if required
|
|
|
|
if ($this->ldapService->shouldSyncGroups()) {
|
2018-08-19 10:24:42 -04:00
|
|
|
$this->ldapService->syncGroups($user, $request->get($this->username()));
|
2018-07-02 13:27:43 -04:00
|
|
|
}
|
2018-07-02 13:09:39 -04:00
|
|
|
|
2018-07-02 13:27:43 -04:00
|
|
|
$path = session()->pull('url.intended', '/');
|
2016-09-17 13:22:04 -04:00
|
|
|
$path = baseUrl($path, true);
|
|
|
|
return redirect($path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the application login form.
|
2017-12-10 08:56:25 -05:00
|
|
|
* @param Request $request
|
2016-09-17 13:22:04 -04:00
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
2017-12-10 08:56:25 -05:00
|
|
|
public function getLogin(Request $request)
|
2016-09-17 13:22:04 -04:00
|
|
|
{
|
|
|
|
$socialDrivers = $this->socialAuthService->getActiveDrivers();
|
|
|
|
$authMethod = config('auth.method');
|
2017-12-10 08:56:25 -05:00
|
|
|
|
|
|
|
if ($request->has('email')) {
|
|
|
|
session()->flashInput([
|
|
|
|
'email' => $request->get('email'),
|
|
|
|
'password' => (config('app.env') === 'demo') ? $request->get('password', '') : ''
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2016-09-17 13:22:04 -04:00
|
|
|
return view('auth/login', ['socialDrivers' => $socialDrivers, 'authMethod' => $authMethod]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Redirect to the relevant social site.
|
|
|
|
* @param $socialDriver
|
|
|
|
* @return \Symfony\Component\HttpFoundation\RedirectResponse
|
2018-07-15 13:21:45 -04:00
|
|
|
* @throws \BookStack\Exceptions\SocialDriverNotConfigured
|
2016-09-17 13:22:04 -04:00
|
|
|
*/
|
|
|
|
public function getSocialLogin($socialDriver)
|
|
|
|
{
|
|
|
|
session()->put('social-callback', 'login');
|
|
|
|
return $this->socialAuthService->startLogIn($socialDriver);
|
|
|
|
}
|
2018-01-28 11:58:52 -05:00
|
|
|
}
|