Test Failed
Push — master ( cf3dbd...f03535 )
by Jean-Christophe
27:09
created

AuthControllerOverrideTrait   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Test Coverage

Coverage 25.71%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 22
eloc 22
c 2
b 0
f 1
dl 0
loc 212
ccs 9
cts 35
cp 0.2571
rs 10

21 Methods

Rating   Name   Duplication   Size   Complexity  
A _create() 0 2 1
A _newAccountCreationRule() 0 1 1
A _sendEmailValidation() 0 1 1
A toCookie() 0 2 1
A _getBaseRoute() 0 2 1
A _sendEmailAccountRecovery() 0 1 1
A _getUserSessionKey() 0 2 1
A getFiles() 0 2 1
A getAuthTokensEmailValidation() 0 2 1
A rememberMe() 0 4 2
A getAuthTokensAccountRecovery() 0 2 1
A passwordResetAction() 0 2 1
A onBadCreditentials() 0 2 1
A _getActiveUser() 0 2 1
A getEmailFromNewAccount() 0 2 1
A fromCookie() 0 2 1
A getCookieUser() 0 2 1
A isValidEmailForRecovery() 0 2 1
A _send2FACode() 0 1 1
A getAccountRecoveryLink() 0 4 1
A onBad2FACode() 0 2 1
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{
3 ignored issues
show
Unused Code introduced by
The parameter $validationURL is not used and could be removed. ( Ignorable by Annotation )

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

124
	protected function _sendEmailValidation(string $email,/** @scrutinizer ignore-unused */ string $validationURL,string $expire):void{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $email is not used and could be removed. ( Ignorable by Annotation )

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

124
	protected function _sendEmailValidation(/** @scrutinizer ignore-unused */ string $email,string $validationURL,string $expire):void{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $expire is not used and could be removed. ( Ignorable by Annotation )

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

124
	protected function _sendEmailValidation(string $email,string $validationURL,/** @scrutinizer ignore-unused */ string $expire):void{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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 {
1 ignored issue
show
Unused Code introduced by
The parameter $email is not used and could be removed. ( Ignorable by Annotation )

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

198
	protected function isValidEmailForRecovery(/** @scrutinizer ignore-unused */ string $email):bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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{
3 ignored issues
show
Unused Code introduced by
The parameter $expire is not used and could be removed. ( Ignorable by Annotation )

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

208
	protected function _sendEmailAccountRecovery(string $email,string $validationURL,/** @scrutinizer ignore-unused */ string $expire):void{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $email is not used and could be removed. ( Ignorable by Annotation )

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

208
	protected function _sendEmailAccountRecovery(/** @scrutinizer ignore-unused */ string $email,string $validationURL,string $expire):void{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $validationURL is not used and could be removed. ( Ignorable by Annotation )

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

208
	protected function _sendEmailAccountRecovery(string $email,/** @scrutinizer ignore-unused */ string $validationURL,string $expire):void{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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{
2 ignored issues
show
Unused Code introduced by
The parameter $newPasswordHash is not used and could be removed. ( Ignorable by Annotation )

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

219
	protected function passwordResetAction(string $email,/** @scrutinizer ignore-unused */ string $newPasswordHash):bool{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $email is not used and could be removed. ( Ignorable by Annotation )

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

219
	protected function passwordResetAction(/** @scrutinizer ignore-unused */ string $email,string $newPasswordHash):bool{

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
220
		return false;
221
	}
222
223
	protected function getAccountRecoveryLink():string{
224
		$href=$this->_getBaseRoute().'/recoveryInit';
225
		$target=$this->_getBodySelector();
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

225
		/** @scrutinizer ignore-call */ 
226
  $target=$this->_getBodySelector();
Loading history...
226
		return "<a href='$href' data-target='$target'>Forgot your password?</a>";
227
	}
228
}
229
230