xref: /webtrees/app/Http/Middleware/UseSession.php (revision b5979037de52280b1023c9eb58518a3089e7d267)
1*b5979037SGreg Roach<?php
2*b5979037SGreg Roach/**
3*b5979037SGreg Roach * webtrees: online genealogy
4*b5979037SGreg Roach * Copyright (C) 2019 webtrees development team
5*b5979037SGreg Roach * This program is free software: you can redistribute it and/or modify
6*b5979037SGreg Roach * it under the terms of the GNU General Public License as published by
7*b5979037SGreg Roach * the Free Software Foundation, either version 3 of the License, or
8*b5979037SGreg Roach * (at your option) any later version.
9*b5979037SGreg Roach * This program is distributed in the hope that it will be useful,
10*b5979037SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*b5979037SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*b5979037SGreg Roach * GNU General Public License for more details.
13*b5979037SGreg Roach * You should have received a copy of the GNU General Public License
14*b5979037SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
15*b5979037SGreg Roach */
16*b5979037SGreg Roachdeclare(strict_types=1);
17*b5979037SGreg Roach
18*b5979037SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
19*b5979037SGreg Roach
20*b5979037SGreg Roachuse Carbon\Carbon;
21*b5979037SGreg Roachuse Closure;
22*b5979037SGreg Roachuse Fisharebest\Webtrees\Auth;
23*b5979037SGreg Roachuse Fisharebest\Webtrees\Session;
24*b5979037SGreg Roachuse Symfony\Component\HttpFoundation\Request;
25*b5979037SGreg Roachuse Symfony\Component\HttpFoundation\Response;
26*b5979037SGreg Roachuse Throwable;
27*b5979037SGreg Roach
28*b5979037SGreg Roach/**
29*b5979037SGreg Roach * Middleware to activate sessions.
30*b5979037SGreg Roach */
31*b5979037SGreg Roachclass UseSession implements MiddlewareInterface
32*b5979037SGreg Roach{
33*b5979037SGreg Roach    /**
34*b5979037SGreg Roach     * @param Request $request
35*b5979037SGreg Roach     * @param Closure $next
36*b5979037SGreg Roach     *
37*b5979037SGreg Roach     * @return Response
38*b5979037SGreg Roach     * @throws Throwable
39*b5979037SGreg Roach     */
40*b5979037SGreg Roach    public function handle(Request $request, Closure $next): Response
41*b5979037SGreg Roach    {
42*b5979037SGreg Roach        // Sessions
43*b5979037SGreg Roach        Session::start();
44*b5979037SGreg Roach
45*b5979037SGreg Roach        // Update the last-login time no more than once a minute.
46*b5979037SGreg Roach        $next_session_update = Carbon::createFromTimestamp((int) Session::get('session_time_updates'))->addMinute();
47*b5979037SGreg Roach        if ($next_session_update < Carbon::now()) {
48*b5979037SGreg Roach            $timestamp_now = Carbon::now()->timestamp;
49*b5979037SGreg Roach
50*b5979037SGreg Roach            if (Session::get('masquerade') === null) {
51*b5979037SGreg Roach                Auth::user()->setPreference('sessiontime', (string) $timestamp_now);
52*b5979037SGreg Roach            }
53*b5979037SGreg Roach            Session::put('session_time_updates', $timestamp_now);
54*b5979037SGreg Roach        }
55*b5979037SGreg Roach
56*b5979037SGreg Roach        return $next($request);
57*b5979037SGreg Roach    }
58*b5979037SGreg Roach}
59