File: D:/HostingSpaces/SBogers10/netwerkbrabant.komma.pro/app/KommaApp/Auth/SiteLoginController.php
<?php
namespace App\KommaApp\Auth;
use App\Http\Controllers\Controller;
use App\KommaApp\Kms\ActionLog\ActionLogService;
use App\KommaApp\Users\Models\User;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
class SiteLoginController 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.
|
*/
/**
* @var int $maxAttempts How many times you may try to login with wrong credentials before being locked out
*/
protected $maxAttempts = 5;
/**
* @var int $decayMinutes How long it takes before you can login again after being locked out in minutes
*/
protected $decayMinutes = 1;
use AuthenticatesUsers;
/**
* Where to redirect users after login.
*
* @var string
*/
protected $redirectTo = '/mijn-bedrijf';
protected function guard()
{
return \Auth::guard('siteUser');
}
public function showLoginForm()
{
// Redirect if already logged in
if( $this->guard()->check() ) {
return redirect($this->redirectPath());
}
// Check if it should show an inactive company error message
if(session('inactiveCompany', null)){
$flashOld = session('_flash.old', []);
$flashOld[] = 'inactiveCompany';
session(['_flash.old' => $flashOld]);
}
return view('site.auth.login');
}
/**
* Attempt to log the user into the application.
*
* @param \Illuminate\Http\Request $request
* @return bool
*/
protected function attemptLogin(Request $request)
{
$valid = $this->guard()->attempt(
$this->credentials($request), $request->filled('remember')
);
if($valid) {
// Check if user has a company
$company = $this->guard()->user()->getCompany();
if(isset($company) && $company){
// Check if the company is still active
if($company->active){
ActionLogService::Log('Logged in successfully at the front end');
}
else{
$valid = false;
ActionLogService::Log('Failed to login at the frontend (Company is inactive)', [
'email' => $request->input('email'),
'ip' => $request->ip(),
]);
session(['inactiveCompany' => true]);
$this->guard()->logout();
}
}
else{
$valid = false;
ActionLogService::Log('Failed to login at the frontend (No company bind to this user)', [
'email' => $request->input('email'),
'ip' => $request->ip(),
]);
$this->guard()->logout();
}
} else {
ActionLogService::Log('Failed to login at the frontend (wrong credentials or permissions)', [
'email' => $request->input('email'),
'ip' => $request->ip(),
]);
}
return $valid;
}
/**
* Get the needed authorization credentials from the request.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
protected function credentials(Request $request)
{
return $request->only($this->username(), User::PASSWORD_COLUMN_NAME);
}
/**
* Log the user out of the application.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function logout(Request $request)
{
ActionLogService::Log('Logged out at the frontend');
$this->guard()->logout();
$request->session()->invalidate();
return redirect('/');
}
}