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 |
|
|
|
|
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
|
|
|
\dump($this); |
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
|
|
|
if (!$user && $this->anonymousSkautLogin) { |
120
|
|
|
//@TODO cache? |
121
|
|
|
$userDetail = $this->skautis->user->UserDetail(); |
122
|
|
|
$user = new User( |
123
|
|
|
$userDetail->UserName, |
124
|
|
|
"NOPASSS", //@TODO random |
125
|
|
|
[new SkautisRole()] |
126
|
|
|
); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $user; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @inheritdoc |
134
|
|
|
*/ |
135
|
|
|
public function checkCredentials($credentials, UserInterface $user) |
136
|
|
|
{ |
137
|
|
|
//Nic, getCredentials bere udaje ze $skautis |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritdoc |
143
|
|
|
*/ |
144
|
|
|
public function onAuthenticationFailure(Request $request, AuthenticationException $exception) |
145
|
|
|
{ |
146
|
|
|
return null; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritdoc |
151
|
|
|
*/ |
152
|
|
|
public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey) |
153
|
|
|
{ |
154
|
|
|
return null; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @inheritdoc |
159
|
|
|
*/ |
160
|
|
|
public function supportsRememberMe() |
161
|
|
|
{ |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
} |