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