|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* WebHemi. |
|
4
|
|
|
* |
|
5
|
|
|
* PHP version 7.1 |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com) |
|
8
|
|
|
* @license https://opensource.org/licenses/MIT The MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* @link http://www.gixx-web.com |
|
11
|
|
|
*/ |
|
12
|
|
|
declare(strict_types = 1); |
|
13
|
|
|
|
|
14
|
|
|
namespace WebHemi\Auth\ServiceAdapter; |
|
15
|
|
|
|
|
16
|
|
|
use WebHemi\Auth; |
|
17
|
|
|
use WebHemi\Configuration\ServiceInterface as ConfigurationInterface; |
|
18
|
|
|
use WebHemi\Data\Entity\User\UserEntity; |
|
19
|
|
|
use WebHemi\Data\Storage\User\UserStorage; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class AbstractServiceAdapter |
|
23
|
|
|
*/ |
|
24
|
|
|
abstract class AbstractServiceAdapter implements Auth\ServiceInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var Auth\ResultInterface */ |
|
27
|
|
|
private $authResult; |
|
28
|
|
|
/** @var Auth\StorageInterface */ |
|
29
|
|
|
private $authStorage; |
|
30
|
|
|
/** @var UserStorage */ |
|
31
|
|
|
private $dataStorage; |
|
32
|
|
|
/** @var ConfigurationInterface */ |
|
33
|
|
|
protected $configuration; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* AbstractServiceAdapter constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ConfigurationInterface $configuration |
|
39
|
|
|
* @param Auth\ResultInterface $authResultPrototype |
|
40
|
|
|
* @param Auth\StorageInterface $authStorage |
|
41
|
|
|
* @param UserStorage $dataStorage |
|
42
|
|
|
*/ |
|
43
|
3 |
|
public function __construct( |
|
44
|
|
|
ConfigurationInterface $configuration, |
|
45
|
|
|
Auth\ResultInterface $authResultPrototype, |
|
46
|
|
|
Auth\StorageInterface $authStorage, |
|
47
|
|
|
UserStorage $dataStorage |
|
48
|
|
|
) { |
|
49
|
3 |
|
$this->configuration = $configuration->getConfig('auth'); |
|
50
|
3 |
|
$this->authResult = $authResultPrototype; |
|
51
|
3 |
|
$this->authStorage = $authStorage; |
|
52
|
3 |
|
$this->dataStorage = $dataStorage; |
|
53
|
3 |
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Gets the auth storage instance. (e.g.: AuthSessionStorage) |
|
57
|
|
|
* |
|
58
|
|
|
* @return Auth\StorageInterface |
|
59
|
|
|
*/ |
|
60
|
1 |
|
protected function getAuthStorage() : Auth\StorageInterface |
|
61
|
|
|
{ |
|
62
|
1 |
|
return $this->authStorage; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Gets the data storage instance. (e.g.: UserStorage) |
|
67
|
|
|
* |
|
68
|
|
|
* @return UserStorage |
|
69
|
|
|
*/ |
|
70
|
2 |
|
protected function getDataStorage() : UserStorage |
|
71
|
|
|
{ |
|
72
|
2 |
|
return $this->dataStorage; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Gets a new instance of the auth result container. |
|
77
|
|
|
* |
|
78
|
|
|
* @return Auth\ResultInterface |
|
79
|
|
|
*/ |
|
80
|
2 |
|
protected function getNewAuthResultInstance() : Auth\ResultInterface |
|
81
|
|
|
{ |
|
82
|
2 |
|
return clone $this->authResult; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Authenticates the user. |
|
87
|
|
|
* |
|
88
|
|
|
* @param Auth\CredentialInterface $credential |
|
89
|
|
|
* @return Auth\ResultInterface |
|
90
|
|
|
*/ |
|
91
|
|
|
abstract public function authenticate(Auth\CredentialInterface $credential) : Auth\ResultInterface; |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Sets the authenticated user. |
|
95
|
|
|
* |
|
96
|
|
|
* @param UserEntity $dataEntity |
|
97
|
|
|
* @return Auth\ServiceInterface |
|
98
|
|
|
*/ |
|
99
|
2 |
|
public function setIdentity(UserEntity $dataEntity) : Auth\ServiceInterface |
|
100
|
|
|
{ |
|
101
|
2 |
|
$this->authStorage->setIdentity($dataEntity); |
|
102
|
|
|
|
|
103
|
2 |
|
return $this; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Checks whether the user is authenticated or not. |
|
108
|
|
|
* |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
2 |
|
public function hasIdentity() : bool |
|
112
|
|
|
{ |
|
113
|
2 |
|
return $this->authStorage->hasIdentity(); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Gets the authenticated user's entity. |
|
118
|
|
|
* |
|
119
|
|
|
* @return UserEntity|null |
|
120
|
|
|
*/ |
|
121
|
2 |
|
public function getIdentity() : ? UserEntity |
|
122
|
|
|
{ |
|
123
|
2 |
|
return $this->authStorage->getIdentity(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Clears the session. |
|
128
|
|
|
* |
|
129
|
|
|
* @return Auth\ServiceInterface |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function clearIdentity() : Auth\ServiceInterface |
|
132
|
|
|
{ |
|
133
|
1 |
|
$this->authStorage->clearIdentity(); |
|
134
|
1 |
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|