Completed
Push — 2.x ( e4dad7...054f88 )
by Jindřich
02:18
created

SkautisAuthenticator::getUser()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 13
nc 3
nop 2
1
<?php
2
3
namespace SkautisBundle\Security\Authentication;
4
5
use Skautis\Skautis;
6
use SkautisBundle\Security\Core\Role\SkautisRole;
7
use Symfony\Component\HttpFoundation\RedirectResponse;
8
use Symfony\Component\HttpFoundation\Request;
9
use Symfony\Component\Routing\RouterInterface;
10
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
11
use Symfony\Component\Security\Core\Exception\AuthenticationException;
12
use Symfony\Component\Security\Core\User\User;
13
use Symfony\Component\Security\Core\User\UserInterface;
14
use Symfony\Component\Security\Core\User\UserProviderInterface;
15
use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
16
use Symfony\Component\HttpFoundation\Session\Session;
17
18
/**
19
 * Class SkautisAuthenticator
20
 * GuardAuthenticator explained https://symfony.com/doc/master/cookbook/security/guard-authentication.html
21
 */
22
class SkautisAuthenticator extends  AbstractGuardAuthenticator //implements GuardAuthenticatorInterface
0 ignored issues
show
Coding Style introduced by
Expected 1 space before "AbstractGuardAuthenticator"; 2 found
Loading history...
23
{
24
    const SKAUTIS_LOGIN_ID = "skautis_login_id";
25
    const SKAUTIS_PERSON_ID = "skautis_person_id";
26
    const SKAUTIS_USERNAME = "skautis_username";
27
28
    /**
29
     * @var RouterInterface
30
     */
31
    protected $router;
32
33
    /**
34
     * @var Skautis
35
     */
36
    protected $skautis;
37
38
    /**
39
     * @var Session
40
     */
41
    protected $session;
42
43
    /**
44
     * @var UserLoader
45
     */
46
    protected $userLoader;
47
48
    /**
49
     * @var bool
50
     */
51
    protected $confirm;
52
53
    /**
54
     * @var bool
55
     */
56
    protected $anonymousSkautLogin;
57
58
    /**
59
     * SkautisAuthenticator constructor.
60
     * @param Skautis $skautis
61
     * @param RouterInterface $router
62
     * @param Session $session
63
     * @param UserLoader $userLoader
64
     * @param bool $confirm
65
     * @param bool $anonymousSkautLogin
66
     */
67
    public function __construct(Skautis $skautis, RouterInterface $router, Session $session, UserLoader $userLoader, $confirm = true, $anonymousSkautLogin = false)
68
    {
69
        $this->skautis = $skautis;
70
        $this->router = $router;
71
        $this->session = $session;
72
        $this->userLoader = $userLoader;
73
        $this->confirm = $confirm;
74
        $this->anonymousSkautLogin = $anonymousSkautLogin;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function start(Request $request, AuthenticationException $authException = null)
81
    {
82
        return new RedirectResponse($this->router->generate("skautis_login"));
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88
    public function getCredentials(Request $request)
89
    {
90
        if (!$this->skautis->getUser()->isLoggedIn($this->confirm)) {
91
            return null;
92
        }
93
94
        //Kontrola ze uzivatel prihlaseny do skautisu je stejny jako uzivatel prihlaseny do symfony
95
        $loginId = $this->skautis->getUser()->getLoginId();
96
        if ($loginId != $this->session->get(self::SKAUTIS_LOGIN_ID)) {
97
            $userDetail = $this->skautis->user->UserDetail();
98
            $personId = $userDetail->ID_Person;
99
100
            $this->session->set(self::SKAUTIS_LOGIN_ID, $loginId);
101
            $this->session->set(self::SKAUTIS_PERSON_ID, $personId);
102
        }
103
        else {
104
            $personId = $this->session->get(self::SKAUTIS_PERSON_ID);
105
        }
106
107
        return [
108
            "person_id" => $personId,
109
        ];
110
    }
111
112
    /**
113
     * @inheritdoc
114
     */
115
    public function getUser($credentials, UserProviderInterface $userProvider)
116
    {
117
        $user = $this->userLoader->loadUser($credentials['person_id'], $userProvider);
118
119
120
        if (!$user && $this->anonymousSkautLogin) {
0 ignored issues
show
Coding Style introduced by
Blank line found at start of control structure
Loading history...
121
122
            $userName = $this->session->get(self::SKAUTIS_USERNAME);
123
            if ($userName == null) {
124
                $userName = $this->skautis->user->UserDetail()->UserName;
125
                $this->session->set(self::SKAUTIS_USERNAME, $userName);
126
            }
127
128
            $data = sha1(bin2hex(random_bytes(32)));
129
            $user = new User(
130
                $userName,
131
                "NOPASSS-$data",
132
                [new SkautisRole()]
133
            );
134
        }
135
        
136
        return $user;
137
    }
138
139
    /**
140
     * @inheritdoc
141
     */
142
    public function checkCredentials($credentials, UserInterface $user)
143
    {
144
        //Nic, getCredentials bere udaje ze $skautis
145
        return true;
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
152
    {
153
        return null;
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
160
    {
161
        return null;
162
    }
163
164
    /**
165
     * @inheritdoc
166
     */
167
    public function supportsRememberMe()
168
    {
169
        return false;
170
    }
171
}