aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/JsonApi/Middlewares/Auth/SessionStrategy.php
blob: a0f14f8645babe578ba6309a2aec2a94707fb958 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php

namespace JsonApi\Middlewares\Auth;

use Psr\Http\Message\ResponseInterface as Response;

class SessionStrategy implements Strategy
{
    /** @var ?\User */
    protected $user;

    public function check()
    {
        return !is_null($this->user());
    }

    /**
     * @SuppressWarnings(PHPMD.Superglobals)
     */
    public function user()
    {
        if (!is_null($this->user)) {
            return $this->user;
        }

        $isAuthenticated =
            isset($GLOBALS['user']) && 'nobody' !== $GLOBALS['user']->id;

        if ($isAuthenticated) {
            $this->user = $GLOBALS['user']->getAuthenticatedUser();
        }

        return $this->user;
    }

    public function addChallenge(Response $response)
    {
        return $response;
    }
}