Ubiquity 2.5.2
php rapid development framework
Loading...
Searching...
No Matches
AuthAccountCreationTrait.php
Go to the documentation of this file.
1<?php
2
4
5use Ajax\semantic\components\validation\Rule;
6use Ajax\semantic\html\collections\form\HtmlForm;
12
18
19 protected static string $TOKENS_VALIDATE_EMAIL='email.validation';
20
21 abstract protected function getBaseUrl():string;
22
23 abstract protected function fMessage(FlashMessage $fMessage, $id = null):string;
24
25 abstract protected function useAjax():bool;
26
27 abstract public function _addFrmAjaxBehavior($id):HtmlForm;
28
29 abstract public function _getPasswordInputName():string;
30
31 abstract public function _getLoginInputName():string;
32
33 abstract protected function authLoadView($viewName, $vars = [ ]):void;
34
35 abstract protected function rememberCaption():string;
36
37 abstract protected function loginLabel():string;
38
39 abstract protected function passwordConfLabel(): string;
40
41 abstract protected function passwordLabel(): string;
42
43 abstract protected function _getFiles(): AuthFiles;
44
45 abstract public function _getBodySelector():string;
46
51 protected function hasAccountCreation():bool{
52 return false;
53 }
54
59 protected function hasEmailValidation():bool{
60 return false;
61 }
62
67 protected function emailValidationDuration():\DateInterval{
68 return new \DateInterval('PT24H');
69 }
75 protected function createAccountMessage(FlashMessage $fMessage) {
76 }
77
83 protected function canCreateAccountMessage(FlashMessage $fMessage) {
84 }
85
91 protected function createAccountErrorMessage(FlashMessage $fMessage) {
92 }
93
99 protected function emailValidationSuccess(FlashMessage $fMessage){
100
101 }
102
108 protected function emailValidationError(FlashMessage $fMessage){
109
110 }
111
116 protected function _create(string $login,string $password):?bool{
117 return false;
118 }
119
126 protected function _newAccountCreationRule(string $accountName):?bool{
127
128 }
129
136 protected function _sendEmailValidation(string $email,string $validationURL,string $expire):void{
137
138 }
139
146 protected function getEmailFromNewAccount($account):string{
147 return $account;
148 }
149
156 return new AuthTokens(self::$TOKENS_VALIDATE_EMAIL,10,$this->emailValidationDuration()->s,false);
157 }
158
159 protected function generateEmailValidationUrl($email):array {
160 $duration=$this->emailValidationDuration();
161 $tokens=$this->getAuthTokensEmailValidation();
162 $d=new \DateTime();
163 $dExpire=$d->add($duration);
164 $key=$tokens->store(['email'=>$email]);
165 return ['url'=>$key.'/'.\md5($email),'expire'=>$dExpire];
166 }
167
168 protected function prepareEmailValidation(string $email){
169 $data=$this->generateEmailValidationUrl($email);
170 $validationURL=$this->getBaseUrl().'/checkEmail/'.$data['url'];
171 $this->_sendEmailValidation($email, $validationURL,UDateTime::elapsed($data['expire']));
172 }
173
181 protected function validateEmail(string $mail):bool{
182 return true;
183 }
184
185
191 public function checkEmail(string $key,string $hashMail){
192 $isValid=false;
193 $tokens=$this->getAuthTokensEmailValidation();
194 if($tokens->exists($key)){
195 if(!$tokens->expired($key)){
196 $data=$tokens->fetch($key);
197 $email=$data['email'];
198 if(\md5($email)===$hashMail && $this->validateEmail($email)){
199 $fMessage = new FlashMessage ( "Your email <b>$email</b> has been validated.", 'Account creation', 'success', 'user' );
200 $this->emailValidationSuccess($fMessage);
201 $isValid=true;
202 }
203 $msg='This validation link is not valid!';
204 }else{
205 $msg='This validation link is no longer active!';
206 }
207 }
208 if(!$isValid){
209 $fMessage = new FlashMessage ( $msg??'This validation link is not valid!', 'Account creation', 'error', 'user' );
210 $this->emailValidationError($fMessage);
211 }
212 echo $this->fMessage($fMessage);
213 }
214
219 public function addAccount(){
220 if($this->hasAccountCreation()){
221 $loginInputName=$this->_getLoginInputName();
222 $passwordInputName=$this->_getPasswordInputName();
223 if($this->useAjax()){
224 $frm=$this->_addFrmAjaxBehavior('frm-create');
225 $frm->addExtraFieldRules($passwordInputName.'-conf', ['empty',"match[$passwordInputName]"]);
226 if($this->_newAccountCreationRule('')!==null){
227 $this->jquery->exec(Rule::ajax($this->jquery, 'checkAccount', $this->getBaseUrl () . '/newAccountCreationRule', '{}', 'result=data.result;', 'postForm', [
228 'form' => 'frm-create'
229 ]), true);
230 $frm->addExtraFieldRule($loginInputName, 'checkAccount',"This $loginInputName value is not available!");
231 }
232 }
233 $this->authLoadView ( $this->_getFiles ()->getViewCreate(), [ 'action' => $this->getBaseUrl () . '/createAccount','loginInputName' => $loginInputName,'loginLabel' => $this->loginLabel (),'passwordInputName' => $passwordInputName,'passwordLabel' => $this->passwordLabel (),'passwordConfLabel'=>$this->passwordConfLabel(),'rememberCaption' => $this->rememberCaption () ] );
234 }
235 }
236
237
243 #[\Ubiquity\attributes\items\router\Post]
244 public function createAccount(){
245 $account=URequest::post($this->_getLoginInputName());
246 $msgSup='';
247 if($this->_create($account,URequest::post($this->_getPasswordInputName()))){
248 if($this->hasEmailValidation()){
249 $email=$this->getEmailFromNewAccount($account);
250 $this->prepareEmailValidation($email);
251 $msgSup="<br>Confirm your email address <b>$email</b> by checking your mailbox.";
252 }
253 $msg=new FlashMessage ( '<b>{account}</b> account created with success!'.$msgSup, 'Account creation', 'success', 'check square' );
254 }else{
255 $msg=new FlashMessage ( 'The account <b>{account}</b> was not created!', 'Account creation', 'error', 'warning circle' );
256 }
257 $message=$this->fMessage($msg->parseContent(['account'=>$account]));
258 $this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ '_message' => $message,'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ] );
259 }
260}
261
Ubiquity\controllers\auth$AuthFiles This class is part of Ubiquity.
Definition AuthFiles.php:13
canCreateAccountMessage(FlashMessage $fMessage)
To override for modifying the account creation message information.
_create(string $login, string $password)
To override For creating a new user account.
createAccountMessage(FlashMessage $fMessage)
To override for modifying the account creation message.
_newAccountCreationRule(string $accountName)
To override Returns true if the creation of $accountName is possible.
createAccountErrorMessage(FlashMessage $fMessage)
To override for modifying the error for account creation.
emailValidationError(FlashMessage $fMessage)
To override Displayed when email is invalid or if an error occurs.
checkEmail(string $key, string $hashMail)
Route for email validation checking when creating a new account.
emailValidationSuccess(FlashMessage $fMessage)
To override Displayed when email is valid.
getAuthTokensEmailValidation()
To override Returns the AuthTokens instance used for tokens generation when sending an email for the ...
emailValidationDuration()
Returns the default validity duration of a mail validation link.
DateTime utilities Ubiquity\utils\base$UDateTime This class is part of Ubiquity.
Definition UDateTime.php:14
Http Request utilities, wrapper for accessing to $_GET, $_POST and php://input.
Definition URequest.php:18