Passed
Push — master ( df4f71...b35ad3 )
by Jean-Christophe
18:08
created

AuthAccountCreationTrait::createAccountMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 1
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
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);
0 ignored issues
show
Bug introduced by
It seems like fMessage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

185
		echo $this->/** @scrutinizer ignore-call */ fMessage($fMessage);
Loading history...
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()){
0 ignored issues
show
Bug introduced by
It seems like useAjax() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

194
			if($this->/** @scrutinizer ignore-call */ useAjax()){
Loading history...
195
				$frm=$this->_addFrmAjaxBehavior('frm-create');
0 ignored issues
show
Bug introduced by
It seems like _addFrmAjaxBehavior() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

195
				/** @scrutinizer ignore-call */ 
196
    $frm=$this->_addFrmAjaxBehavior('frm-create');
Loading history...
196
				$passwordInputName=$this->_getPasswordInputName();
0 ignored issues
show
Bug introduced by
It seems like _getPasswordInputName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

196
				/** @scrutinizer ignore-call */ 
197
    $passwordInputName=$this->_getPasswordInputName();
Loading history...
197
				$frm->addExtraFieldRules($passwordInputName.'-conf', ['empty',"match[$passwordInputName]"]);
198
				if($this->_newAccountCreationRule('')!==null){
2 ignored issues
show
introduced by
The condition $this->_newAccountCreationRule('') !== null is always false.
Loading history...
Bug introduced by
Are you sure the usage of $this->_newAccountCreationRule('') targeting Ubiquity\controllers\aut...ewAccountCreationRule() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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 () ] );
0 ignored issues
show
Bug introduced by
It seems like _getLoginInputName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
			$this->authLoadView ( $this->_getFiles ()->getViewCreate(), [ 'action' => $this->getBaseUrl () . '/createAccount','loginInputName' => $this->/** @scrutinizer ignore-call */ _getLoginInputName (),'loginLabel' => $this->loginLabel (),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->passwordLabel (),'passwordConfLabel'=>$this->passwordConfLabel(),'rememberCaption' => $this->rememberCaption () ] );
Loading history...
Bug introduced by
It seems like passwordConfLabel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
			$this->authLoadView ( $this->_getFiles ()->getViewCreate(), [ 'action' => $this->getBaseUrl () . '/createAccount','loginInputName' => $this->_getLoginInputName (),'loginLabel' => $this->loginLabel (),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->passwordLabel (),'passwordConfLabel'=>$this->/** @scrutinizer ignore-call */ passwordConfLabel(),'rememberCaption' => $this->rememberCaption () ] );
Loading history...
Bug introduced by
It seems like _getFiles() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
			$this->authLoadView ( $this->/** @scrutinizer ignore-call */ _getFiles ()->getViewCreate(), [ 'action' => $this->getBaseUrl () . '/createAccount','loginInputName' => $this->_getLoginInputName (),'loginLabel' => $this->loginLabel (),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->passwordLabel (),'passwordConfLabel'=>$this->passwordConfLabel(),'rememberCaption' => $this->rememberCaption () ] );
Loading history...
Bug introduced by
It seems like rememberCaption() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

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->/** @scrutinizer ignore-call */ rememberCaption () ] );
Loading history...
Bug introduced by
It seems like authLoadView() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
			$this->/** @scrutinizer ignore-call */ 
206
          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 () ] );
Loading history...
Bug introduced by
It seems like loginLabel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
			$this->authLoadView ( $this->_getFiles ()->getViewCreate(), [ 'action' => $this->getBaseUrl () . '/createAccount','loginInputName' => $this->_getLoginInputName (),'loginLabel' => $this->/** @scrutinizer ignore-call */ loginLabel (),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->passwordLabel (),'passwordConfLabel'=>$this->passwordConfLabel(),'rememberCaption' => $this->rememberCaption () ] );
Loading history...
Bug introduced by
It seems like passwordLabel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

205
			$this->authLoadView ( $this->_getFiles ()->getViewCreate(), [ 'action' => $this->getBaseUrl () . '/createAccount','loginInputName' => $this->_getLoginInputName (),'loginLabel' => $this->loginLabel (),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->/** @scrutinizer ignore-call */ passwordLabel (),'passwordConfLabel'=>$this->passwordConfLabel(),'rememberCaption' => $this->rememberCaption () ] );
Loading history...
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()))){
2 ignored issues
show
Bug introduced by
It seems like $account can also be of type null; however, parameter $login of Ubiquity\controllers\aut...reationTrait::_create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

219
		if($this->_create(/** @scrutinizer ignore-type */ $account,URequest::post($this->_getPasswordInputName()))){
Loading history...
Bug introduced by
It seems like Ubiquity\utils\http\UReq...getPasswordInputName()) can also be of type null; however, parameter $password of Ubiquity\controllers\aut...reationTrait::_create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

219
		if($this->_create($account,/** @scrutinizer ignore-type */ URequest::post($this->_getPasswordInputName()))){
Loading history...
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 ] );
0 ignored issues
show
Bug introduced by
It seems like _getBodySelector() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

230
		$this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ '_message' => $message,'authURL' => $this->getBaseUrl (),'bodySelector' => $this->/** @scrutinizer ignore-call */ _getBodySelector (),'_loginCaption' => $this->_loginCaption ] );
Loading history...
231
	}
232
}
233
234