Completed
Push — 2.x ( aeb5ce...f5bd0f )
by Jindřich
03:11
created

SkautisAuthenticator   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 151
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
c 1
b 0
f 0
lcom 2
cbo 9
dl 0
loc 151
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A start() 0 4 1
A getCredentials() 0 23 3
A getUser() 0 16 3
A checkCredentials() 0 5 1
A onAuthenticationFailure() 0 4 1
A onAuthenticationSuccess() 0 4 1
A supportsRememberMe() 0 4 1
A setConfirm() 0 5 1
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
27
    /**
28
     * @var RouterInterface
29
     */
30
    protected $router;
31
32
    /**
33
     * @var Skautis
34
     */
35
    protected $skautis;
36
37
    /**
38
     * @var Session
39
     */
40
    protected $session;
41
42
    /**
43
     * @var UserLoader
44
     */
45
    protected $userLoader;
46
47
    /**
48
     * @var bool
49
     */
50
    protected $confirm;
51
52
    /**
53
     * @var bool
54
     */
55
    protected $anonymousSkautLogin;
56
57
    /**
58
     * SkautisAuthenticator constructor.
59
     * @param Skautis $skautis
60
     * @param RouterInterface $router
61
     * @param Session $session
62
     * @param UserLoader $userLoader
63
     * @param bool $confirm
64
     * @param bool $anonymousSkautLogin
65
     */
66
    public function __construct(Skautis $skautis, RouterInterface $router, Session $session, UserLoader $userLoader, $confirm = true, $anonymousSkautLogin = false)
67
    {
68
        $this->skautis = $skautis;
69
        $this->router = $router;
70
        $this->session = $session;
71
        $this->userLoader = $userLoader;
72
        $this->confirm = $confirm;
73
        $this->anonymousSkautLogin = $anonymousSkautLogin;
74
    }
75
76
    /**
77
     * @inheritdoc
78
     */
79
    public function start(Request $request, AuthenticationException $authException = null)
80
    {
81
        return new RedirectResponse($this->router->generate("skautis_login"));
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87
    public function getCredentials(Request $request)
88
    {
89
        if (!$this->skautis->getUser()->isLoggedIn($this->confirm)) {
90
            return null;
91
        }
92
93
        //Kontrola ze uzivatel prihlaseny do skautisu je stejny jako uzivatel prihlaseny do symfony
94
        $loginId = $this->skautis->getUser()->getLoginId();
95
        if ($loginId != $this->session->get(self::SKAUTIS_LOGIN_ID)) {
96
            $userDetail = $this->skautis->user->UserDetail();
97
            $personId = $userDetail->ID_Person;
98
99
            $this->session->set(self::SKAUTIS_LOGIN_ID, $loginId);
100
            $this->session->set(self::SKAUTIS_PERSON_ID, $personId);
101
        }
102
        else {
103
            $personId = $this->session->get(self::SKAUTIS_PERSON_ID);
104
        }
105
106
        return [
107
            "person_id" => $personId,
108
        ];
109
    }
110
111
    /**
112
     * @inheritdoc
113
     */
114
    public function getUser($credentials, UserProviderInterface $userProvider)
115
    {
116
        $user = $this->userLoader->loadUser($credentials['person_id'], $userProvider);
117
118
        if (!$user && $this->anonymousSkautLogin) {
119
            //@TODO cache?
120
            $userDetail = $this->skautis->user->UserDetail();
121
            $user = new User(
122
                $userDetail->UserName,
123
                "NOPASSS", //@TODO random
124
                [new SkautisRole()]
125
            );
126
        }
127
128
        return $user;
129
    }
130
131
    /**
132
     * @inheritdoc
133
     */
134
    public function checkCredentials($credentials, UserInterface $user)
135
    {
136
        //Nic, getCredentials bere udaje ze $skautis
137
        return true;
138
    }
139
140
    /**
141
     * @inheritdoc
142
     */
143
    public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
144
    {
145
        return null;
146
    }
147
148
    /**
149
     * @inheritdoc
150
     */
151
    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
152
    {
153
        return null;
154
    }
155
156
    /**
157
     * @inheritdoc
158
     */
159
    public function supportsRememberMe()
160
    {
161
        return false;
162
    }
163
164
    /**
165
     * @param boolean $confirm
166
     */
167
    public function setConfirm($confirm)
168
    {
169
        //@TODO constructor?
170
        $this->confirm = $confirm;
171
    }
172
}