1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\auth\traits; |
4
|
|
|
|
5
|
|
|
use Ajax\semantic\components\validation\Rule; |
6
|
|
|
use Ubiquity\controllers\auth\AuthTokens; |
7
|
|
|
use Ubiquity\utils\base\UDateTime; |
8
|
|
|
use Ubiquity\utils\flash\FlashMessage; |
9
|
|
|
use Ubiquity\utils\http\URequest; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait AuthAccountCreationTrait |
13
|
|
|
* |
14
|
|
|
*/ |
15
|
|
|
trait AuthAccountCreationTrait { |
16
|
|
|
|
17
|
|
|
protected static string $TOKENS_VALIDATE_EMAIL='email.validation'; |
18
|
|
|
abstract protected function getBaseUrl():string; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Returns true for account creation. |
22
|
|
|
* @return boolean |
23
|
|
|
*/ |
24
|
1 |
|
protected function hasAccountCreation():bool{ |
25
|
1 |
|
return false; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
protected function hasEmailValidation():bool{ |
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Returns the default validity duration of a mail validation link. |
38
|
|
|
* @return \DateInterval |
39
|
|
|
*/ |
40
|
|
|
protected function emailValidationDuration():\DateInterval{ |
41
|
|
|
return new \DateInterval('PT24H'); |
42
|
|
|
} |
43
|
|
|
/** |
44
|
|
|
* To override for modifying the account creation message. |
45
|
|
|
* |
46
|
|
|
* @param FlashMessage $fMessage |
47
|
|
|
*/ |
48
|
|
|
protected function createAccountMessage(FlashMessage $fMessage) { |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* To override for modifying the account creation message information. |
53
|
|
|
* |
54
|
|
|
* @param FlashMessage $fMessage |
55
|
|
|
*/ |
56
|
|
|
protected function canCreateAccountMessage(FlashMessage $fMessage) { |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* To override for modifying the error for account creation. |
61
|
|
|
* |
62
|
|
|
* @param FlashMessage $fMessage |
63
|
|
|
*/ |
64
|
|
|
protected function createAccountErrorMessage(FlashMessage $fMessage) { |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* To override |
69
|
|
|
* Displayed when email is valid. |
70
|
|
|
* @param FlashMessage $fMessage |
71
|
|
|
*/ |
72
|
|
|
protected function emailValidationSuccess(FlashMessage $fMessage){ |
73
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* To override |
78
|
|
|
* Displayed when email is invalid or if an error occurs. |
79
|
|
|
* @param FlashMessage $fMessage |
80
|
|
|
*/ |
81
|
|
|
protected function emailValidationError(FlashMessage $fMessage){ |
82
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* To override |
87
|
|
|
* For creating a new user account. |
88
|
|
|
*/ |
89
|
|
|
protected function _create(string $login,string $password):?bool{ |
90
|
|
|
return false; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* To override |
95
|
|
|
* Returns true if the creation of $accountName is possible. |
96
|
|
|
* @param string $accountName |
97
|
|
|
* @return bool |
98
|
|
|
*/ |
99
|
|
|
protected function _newAccountCreationRule(string $accountName):?bool{ |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Sends an email for email checking. |
105
|
|
|
* @param string $email |
106
|
|
|
* @param string $validationURL |
107
|
|
|
* @param string $expire |
108
|
|
|
*/ |
109
|
|
|
protected function _sendEmailValidation(string $email,string $validationURL,string $expire):void{ |
110
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* To override |
115
|
|
|
* Returns the email from an account object. |
116
|
|
|
* @param mixed $account |
117
|
|
|
* @return string |
118
|
|
|
*/ |
119
|
|
|
protected function getEmailFromNewAccount($account):string{ |
120
|
|
|
return $account; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* To override |
125
|
|
|
* Returns the AuthTokens instance used for tokens generation when sending an email for the account creation. |
126
|
|
|
* @return AuthTokens |
127
|
|
|
*/ |
128
|
|
|
protected function getAuthTokensEmailValidation():AuthTokens{ |
129
|
|
|
return new AuthTokens(self::$TOKENS_VALIDATE_EMAIL,10,$this->emailValidationDuration()->s,false); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
protected function generateEmailValidationUrl($email):array { |
133
|
|
|
$duration=$this->emailValidationDuration(); |
134
|
|
|
$tokens=$this->getAuthTokensEmailValidation(); |
135
|
|
|
$d=new \DateTime(); |
136
|
|
|
$dExpire=$d->add($duration); |
137
|
|
|
$key=$tokens->store(['email'=>$email]); |
138
|
|
|
return ['url'=>$key.'/'.\md5($email),'expire'=>$dExpire]; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function prepareEmailValidation(string $email){ |
142
|
|
|
$data=$this->generateEmailValidationUrl($email); |
143
|
|
|
$validationURL=$this->getBaseUrl().'/checkEmail/'.$data['url']; |
144
|
|
|
$this->_sendEmailValidation($email, $validationURL,UDateTime::elapsed($data['expire'])); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* To override |
149
|
|
|
* Checks an email. |
150
|
|
|
* |
151
|
|
|
* @param string $mail |
152
|
|
|
* @return bool |
153
|
|
|
*/ |
154
|
|
|
protected function validateEmail(string $mail):bool{ |
155
|
|
|
return true; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Route for email validation checking when creating a new account. |
161
|
|
|
* @param string $key |
162
|
|
|
* @param string $hashMail |
163
|
|
|
*/ |
164
|
|
|
public function checkEmail(string $key,string $hashMail){ |
165
|
|
|
$isValid=false; |
166
|
|
|
$tokens=$this->getAuthTokensEmailValidation(); |
167
|
|
|
if($tokens->exists($key)){ |
168
|
|
|
if(!$tokens->expired($key)){ |
169
|
|
|
$data=$tokens->fetch($key); |
170
|
|
|
$email=$data['email']; |
171
|
|
|
if(\md5($email)===$hashMail && $this->validateEmail($email)){ |
172
|
|
|
$fMessage = new FlashMessage ( "Your email <b>$email</b> has been validated.", 'Account creation', 'success', 'user' ); |
173
|
|
|
$this->emailValidationSuccess($fMessage); |
174
|
|
|
$isValid=true; |
175
|
|
|
} |
176
|
|
|
$msg='This validation link is not valid!'; |
177
|
|
|
}else{ |
178
|
|
|
$msg='This validation link is no longer active!'; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
if(!$isValid){ |
182
|
|
|
$fMessage = new FlashMessage ( $msg??'This validation link is not valid!', 'Account creation', 'error', 'user' ); |
183
|
|
|
$this->emailValidationError($fMessage); |
184
|
|
|
} |
185
|
|
|
echo $this->fMessage($fMessage); |
|
|
|
|
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Displays the account creation form. |
190
|
|
|
* Form is submited to /createAccount action |
191
|
|
|
*/ |
192
|
|
|
public function addAccount(){ |
193
|
|
|
if($this->hasAccountCreation()){ |
194
|
|
|
if($this->useAjax()){ |
|
|
|
|
195
|
|
|
$frm=$this->_addFrmAjaxBehavior('frm-create'); |
|
|
|
|
196
|
|
|
$passwordInputName=$this->_getPasswordInputName(); |
|
|
|
|
197
|
|
|
$frm->addExtraFieldRules($passwordInputName.'-conf', ['empty',"match[$passwordInputName]"]); |
198
|
|
|
if($this->_newAccountCreationRule('')!==null){ |
|
|
|
|
199
|
|
|
$this->jquery->exec(Rule::ajax($this->jquery, 'checkAccount', $this->getBaseUrl () . '/newAccountCreationRule', '{}', 'result=data.result;', 'postForm', [ |
200
|
|
|
'form' => 'frm-create' |
201
|
|
|
]), true); |
202
|
|
|
$frm->addExtraFieldRule($this->_getLoginInputName(), 'checkAccount','Account {value} is not available!'); |
203
|
|
|
} |
204
|
|
|
} |
205
|
|
|
$this->authLoadView ( $this->_getFiles ()->getViewCreate(), [ 'action' => $this->getBaseUrl () . '/createAccount','loginInputName' => $this->_getLoginInputName (),'loginLabel' => $this->loginLabel (),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->passwordLabel (),'passwordConfLabel'=>$this->passwordConfLabel(),'rememberCaption' => $this->rememberCaption () ] ); |
|
|
|
|
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Submit for a new account creation. |
212
|
|
|
* |
213
|
|
|
* @post |
214
|
|
|
*/ |
215
|
|
|
#[\Ubiquity\attributes\items\router\Post] |
216
|
|
|
public function createAccount(){ |
217
|
|
|
$account=URequest::post($this->_getLoginInputName()); |
218
|
|
|
$msgSup=''; |
219
|
|
|
if($this->_create($account,URequest::post($this->_getPasswordInputName()))){ |
|
|
|
|
220
|
|
|
if($this->hasEmailValidation()){ |
221
|
|
|
$email=$this->getEmailFromNewAccount($account); |
222
|
|
|
$this->prepareEmailValidation($email); |
223
|
|
|
$msgSup="<br>Confirm your email address <b>$email</b> by checking your mailbox."; |
224
|
|
|
} |
225
|
|
|
$msg=new FlashMessage ( '<b>{account}</b> account created with success!'.$msgSup, 'Account creation', 'success', 'check square' ); |
226
|
|
|
}else{ |
227
|
|
|
$msg=new FlashMessage ( 'The account <b>{account}</b> was not created!', 'Account creation', 'error', 'warning circle' ); |
228
|
|
|
} |
229
|
|
|
$message=$this->fMessage($msg->parseContent(['account'=>$account])); |
230
|
|
|
$this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ '_message' => $message,'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ] ); |
|
|
|
|
231
|
|
|
} |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|