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