1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\auth; |
4
|
|
|
|
5
|
|
|
use Ubiquity\cache\ClassUtils; |
6
|
|
|
use Ubiquity\utils\http\USession; |
7
|
|
|
use Ubiquity\utils\http\UCookie; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Trait AuthControllerOverrideTrait |
11
|
|
|
* |
12
|
|
|
* @property string $TOKENS_VALIDATE_EMAIL |
13
|
|
|
* @property string $TOKENS_RECOVERY_ACCOUNT |
14
|
|
|
*/ |
15
|
|
|
trait AuthControllerOverrideTrait { |
16
|
|
|
|
17
|
|
|
abstract public function badLogin(); |
18
|
|
|
|
19
|
|
|
abstract public function bad2FACode():void; |
20
|
|
|
|
21
|
|
|
abstract protected function emailValidationDuration():\DateInterval; |
22
|
|
|
|
23
|
|
|
abstract protected function accountRecoveryDuration():\DateInterval; |
24
|
|
|
|
25
|
|
|
abstract public function _getBodySelector():string; |
26
|
|
|
|
27
|
|
|
abstract protected function recoveryAccountCaption():string; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* To override |
31
|
|
|
* Return the base route for this Auth controller |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function _getBaseRoute(){ |
35
|
|
|
return ClassUtils::getClassSimpleName(\get_class($this)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Processes the data posted by the login form |
40
|
|
|
* Have to return the connected user instance |
41
|
|
|
*/ |
42
|
|
|
abstract protected function _connect(); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* To override |
46
|
|
|
* For creating a new user account. |
47
|
|
|
*/ |
48
|
|
|
protected function _create(string $login,string $password):?bool{ |
49
|
|
|
return false; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param object $connected |
54
|
|
|
*/ |
55
|
|
|
abstract protected function onConnect($connected); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* To override for defining a new action when creditentials are invalid. |
59
|
|
|
*/ |
60
|
1 |
|
protected function onBadCreditentials(){ |
61
|
1 |
|
$this->badLogin(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* To override for defining a new action when 2FA code is invalid. |
66
|
|
|
*/ |
67
|
|
|
protected function onBad2FACode():void{ |
68
|
|
|
$this->bad2FACode(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* To override |
73
|
|
|
* Send the 2FA code to the user (email, sms, phone call...) |
74
|
|
|
* @param string $code |
75
|
|
|
* @param mixed $connected |
76
|
|
|
*/ |
77
|
|
|
protected function _send2FACode(string $code,$connected):void{ |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* To override |
83
|
|
|
* Returns true if the creation of $accountName is possible. |
84
|
|
|
* @param string $accountName |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
protected function _newAccountCreationRule(string $accountName):?bool{ |
88
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* To override for defining user session key, default : "activeUser" |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
1 |
|
public function _getUserSessionKey():string { |
96
|
1 |
|
return 'activeUser'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* To override for getting active user, default : USession::get("activeUser") |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
1 |
|
public function _getActiveUser(){ |
104
|
1 |
|
return USession::get($this->_getUserSessionKey()); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Checks if user is valid for the action |
109
|
|
|
* @param string $action |
110
|
|
|
* return boolean true if activeUser is valid |
111
|
|
|
*/ |
112
|
|
|
abstract public function _isValidUser($action=null); |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Returns the value from connected user to save it in the cookie for auto connection |
116
|
|
|
* @param object $connected |
117
|
|
|
*/ |
118
|
|
|
protected function toCookie($connected){ |
119
|
|
|
return; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Sends an email for email checking. |
124
|
|
|
* @param string $email |
125
|
|
|
* @param string $validationURL |
126
|
|
|
* @param string $expire |
127
|
|
|
*/ |
128
|
|
|
protected function _sendEmailValidation(string $email,string $validationURL,string $expire):void{ |
129
|
|
|
|
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Loads the user from database using the cookie value |
134
|
|
|
* @param string $cookie |
135
|
|
|
*/ |
136
|
|
|
protected function fromCookie($cookie){ |
137
|
|
|
return; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Saves the connected user identifier in a cookie |
143
|
|
|
* @param object $connected |
144
|
|
|
*/ |
145
|
|
|
protected function rememberMe($connected){ |
146
|
|
|
$id= $this->toCookie($connected); |
147
|
|
|
if(isset($id)){ |
148
|
|
|
UCookie::set($this->_getUserSessionKey(),$id); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns the cookie for auto connection |
154
|
|
|
* @return NULL|string |
155
|
|
|
*/ |
156
|
1 |
|
protected function getCookieUser(){ |
157
|
1 |
|
return UCookie::get($this->_getUserSessionKey()); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* To override for changing view files |
162
|
|
|
* @return AuthFiles |
163
|
|
|
*/ |
164
|
|
|
protected function getFiles ():AuthFiles{ |
165
|
|
|
return new AuthFiles(); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* To override |
170
|
|
|
* Returns the email from an account object. |
171
|
|
|
* @param mixed $account |
172
|
|
|
* @return string |
173
|
|
|
*/ |
174
|
|
|
protected function getEmailFromNewAccount($account):string{ |
175
|
|
|
return $account; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* To override |
180
|
|
|
* Returns the AuthTokens instance used for tokens generation when sending an email for the account creation. |
181
|
|
|
* @return AuthTokens |
182
|
|
|
*/ |
183
|
|
|
protected function getAuthTokensEmailValidation():AuthTokens{ |
184
|
|
|
return new AuthTokens(self::$TOKENS_VALIDATE_EMAIL,10,$this->emailValidationDuration()->s,false); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* To override |
189
|
|
|
* Returns the AuthTokens instance used for tokens generation for a recovery account. |
190
|
|
|
* @return AuthTokens |
191
|
|
|
*/ |
192
|
|
|
protected function getAuthTokensAccountRecovery():AuthTokens{ |
193
|
|
|
return new AuthTokens(self::$TOKENS_RECOVERY_ACCOUNT,10,$this->accountRecoveryDuration()->s,true); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* To override |
198
|
|
|
* Checks if a valid account matches this email. |
199
|
|
|
* @param string $email |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
protected function isValidEmailForRecovery(string $email):bool { |
203
|
|
|
return true; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Sends an email for account recovery (password reset). |
208
|
|
|
* @param string $email |
209
|
|
|
* @param string $validationURL |
210
|
|
|
* @param string $expire |
211
|
|
|
* @return boolean |
212
|
|
|
*/ |
213
|
|
|
protected function _sendEmailAccountRecovery(string $email,string $validationURL,string $expire):bool{ |
214
|
|
|
return false; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* To override |
219
|
|
|
* Changes the active password associated with the account corresponding to this email. |
220
|
|
|
* @param string $email |
221
|
|
|
* @param string $newPasswordHash |
222
|
|
|
* @return bool |
223
|
|
|
*/ |
224
|
|
|
protected function passwordResetAction(string $email,string $newPasswordHash):bool{ |
225
|
|
|
return false; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
protected function getAccountRecoveryLink():string{ |
229
|
|
|
$href=$this->_getBaseRoute().'/recoveryInit'; |
230
|
|
|
$target=$this->_getBodySelector(); |
231
|
|
|
$caption=$this->recoveryAccountCaption(); |
232
|
|
|
return "<a href='$href' data-target='$target'>$caption</a>"; |
233
|
|
|
} |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
|