|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services\Skautis; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\SmartObject; |
|
6
|
|
|
use Nette\Security\IAuthenticator; |
|
7
|
|
|
use Nette\Security\Identity; |
|
8
|
|
|
use App\Repositories\SocialLoginRepository; |
|
9
|
|
|
use App\Repositories\PersonRepository; |
|
10
|
|
|
use App\Repositories\UserRepository; |
|
11
|
|
|
use App\Services\UserService; |
|
|
|
|
|
|
12
|
|
|
use App\Services\SkautIS\UserService as SkautisUserService; |
|
13
|
|
|
use App\Services\SkautIS\AuthService as SkautisAuthService; |
|
14
|
|
|
|
|
15
|
|
|
class Authenticator implements IAuthenticator |
|
16
|
|
|
{ |
|
17
|
|
|
use SmartObject; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var SocialLoginRepository |
|
21
|
|
|
*/ |
|
22
|
|
|
private $socialLoginRepository; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var PersonRepository |
|
26
|
|
|
*/ |
|
27
|
|
|
private $personRepository; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var UserRepository |
|
31
|
|
|
*/ |
|
32
|
|
|
private $userRepository; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @var AuthService |
|
36
|
|
|
*/ |
|
37
|
|
|
private $skautisAuthService; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @var \App\Services\SkautIS\UserService |
|
41
|
|
|
*/ |
|
42
|
|
|
private $skautisUserService; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var UserService |
|
46
|
|
|
*/ |
|
47
|
|
|
private $userService; |
|
48
|
|
|
|
|
49
|
|
|
public function __construct( |
|
50
|
|
|
SocialLoginRepository $socialLoginRepository, |
|
51
|
|
|
PersonRepository $personRepository, |
|
52
|
|
|
UserRepository $userRepository, |
|
53
|
|
|
SkautisAuthService $skautisAuthService, |
|
54
|
|
|
SkautisUserService $skautisUserService, |
|
55
|
|
|
UserService $userService |
|
56
|
|
|
) { |
|
57
|
|
|
$this->socialLoginRepository = $socialLoginRepository; |
|
58
|
|
|
$this->personRepository = $personRepository; |
|
59
|
|
|
$this->userRepository = $userRepository; |
|
60
|
|
|
$this->skautisAuthService = $skautisAuthService; |
|
61
|
|
|
$this->skautisUserService = $skautisUserService; |
|
62
|
|
|
$this->userService = $userService; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
public function authenticate(array $credentials): Identity |
|
66
|
|
|
{ |
|
67
|
|
|
$credentials = $credentials[0]; |
|
68
|
|
|
|
|
69
|
|
|
$this->skautisAuthService->setInit($credentials); |
|
70
|
|
|
$this->guardSkautisLoggedIn(); |
|
71
|
|
|
$userDetail = $this->skautisUserService->getPersonalDetail(); |
|
72
|
|
|
$token = $userDetail->ID; |
|
73
|
|
|
|
|
74
|
|
|
$user = $this->userService->findByProviderAndToken('skautis', $token); |
|
75
|
|
|
if(!$user) { |
|
76
|
|
|
$userDetail = $this->skautisUserService->getPersonalDetail(); |
|
77
|
|
|
$user = $this->userService->createAccount($token, $userDetail); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return new Identity($user->id, $user->role, ['username' => $user->nick]); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @throws AuthenticationException |
|
85
|
|
|
*/ |
|
86
|
|
|
protected function guardSkautisLoggedIn() |
|
87
|
|
|
{ |
|
88
|
|
|
if (!$this->skautisUserService->isLoggedIn()) { |
|
89
|
|
|
throw new AuthenticationException('Nemáte platné přihlášení do skautISu!'); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: