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