2020-10-08 15:38:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
|
|
use Closure;
|
|
|
|
use App\User;
|
|
|
|
use Carbon\Carbon;
|
|
|
|
use Illuminate\Http\Response;
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
2021-10-15 23:46:21 +02:00
|
|
|
use Illuminate\Support\Facades\Log;
|
2020-10-08 15:38:36 +02:00
|
|
|
|
2021-11-17 23:37:16 +01:00
|
|
|
class KickOutInactiveUser
|
2020-10-08 15:38:36 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Handle an incoming request.
|
|
|
|
*
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param \Closure $next
|
|
|
|
* @return mixed
|
|
|
|
*/
|
2021-10-29 21:51:58 +02:00
|
|
|
public function handle($request, Closure $next, $guard = null)
|
2020-10-08 15:38:36 +02:00
|
|
|
{
|
2021-11-17 23:37:16 +01:00
|
|
|
// We do not track activity of guest or user authenticated against a bearer token
|
|
|
|
if (Auth::guest() || $request->bearerToken()) {
|
2020-10-08 15:38:36 +02:00
|
|
|
return $next($request);
|
|
|
|
}
|
|
|
|
|
2021-11-17 23:37:16 +01:00
|
|
|
$user = Auth::user();
|
2020-10-08 15:38:36 +02:00
|
|
|
$now = Carbon::now();
|
2020-10-09 13:35:03 +02:00
|
|
|
$inactiveFor = $now->diffInSeconds(Carbon::parse($user->last_seen_at));
|
2020-10-08 15:38:36 +02:00
|
|
|
|
|
|
|
// Fetch all setting values
|
2021-09-26 22:06:49 +02:00
|
|
|
$settingService = resolve('App\Services\SettingServiceInterface');
|
2021-10-29 21:51:58 +02:00
|
|
|
$kickUserAfterXSecond = intval($settingService->get('kickUserAfter')) * 60;
|
2020-10-09 13:35:03 +02:00
|
|
|
|
|
|
|
// If user has been inactive longer than the allowed inactivity period
|
|
|
|
if ($kickUserAfterXSecond > 0 && $inactiveFor > $kickUserAfterXSecond) {
|
|
|
|
|
2020-10-08 15:38:36 +02:00
|
|
|
$user->last_seen_at = $now->format('Y-m-d H:i:s');
|
|
|
|
$user->save();
|
2021-11-17 23:37:16 +01:00
|
|
|
|
2021-10-29 21:51:58 +02:00
|
|
|
Log::notice('Inactive user detected, authentication rejected');
|
2020-10-08 15:38:36 +02:00
|
|
|
|
|
|
|
return response()->json(['message' => 'unauthorised'], Response::HTTP_UNAUTHORIZED);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $next($request);
|
|
|
|
}
|
2021-10-29 21:51:58 +02:00
|
|
|
}
|