Passed
Push — master ( 0ed261...e52b37 )
by Jean-Christophe
17:09
created

AuthControllerOverrideTrait::_getActiveUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 2
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 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
trait AuthControllerOverrideTrait {
10
	
11
	abstract public function badLogin();
12
	abstract public function bad2FACode();
13
	
14
	/**
15
	 * To override
16
	 * Return the base route for this Auth controller
17
	 * @return string
18
	 */
19
	public function _getBaseRoute(){
20
		return ClassUtils::getClassSimpleName(get_class($this));
21
	}
22
	
23
	/**
24
	 * Processes the data posted by the login form
25
	 * Have to return the connected user instance
26
	 */
27
	abstract protected function _connect();
28
	
29
	/**
30
	 * To override
31
	 * For creating a new user account.
32
	 */
33
	protected function _create(string $login,string $password):?bool{
34
		return false;
35
	}
36
	
37
	/**
38
	 * @param object $connected
39
	 */
40
	abstract protected function onConnect($connected);
41
	
42
	/**
43
	 * To override for defining a new action when creditentials are invalid.
44
	 */
45 1
	protected function onBadCreditentials(){
46 1
		$this->badLogin();
47 1
	}
48
	
49
	/**
50
	 * To override for defining a new action when 2FA code is invalid.
51
	 */
52
	protected function onBad2FACode(){
53
		$this->bad2FACode();
54
	}
55
	
56
	/**
57
	 * To override
58
	 * Send the 2FA code to the user (email, sms, phone call...)
59
	 * @param string $code
60
	 * @param mixed $connected
61
	 */
62
	protected function _send2FACode(string $code,$connected){
63
		
64
	}
65
	
66
	/**
67
	 * To override
68
	 * Returns true if the creation of $accountName is possible.
69
	 * @param string $accountName
70
	 * @return bool
71
	 */
72
	protected function newAccountCreationRule(string $accountName):?bool{
73
		
74
	}
75
	
76
	/**
77
	 * To override for defining user session key, default : "activeUser"
78
	 * @return string
79
	 */
80 1
	public function _getUserSessionKey(){
81 1
		return 'activeUser';
82
	}
83
	
84
	/**
85
	 * To override for getting active user, default : USession::get("activeUser")
86
	 * @return string
87
	 */
88 1
	public function _getActiveUser(){
89 1
		return USession::get($this->_getUserSessionKey());
90
	}
91
	
92
	/**
93
	 * Checks if user is valid for the action
94
	 * @param string $action
95
	 * return boolean true if activeUser is valid
96
	 */
97
	abstract public function _isValidUser($action=null);
98
	
99
	/**
100
	 * Returns the value from connected user to save it in the cookie for auto connection
101
	 * @param object $connected
102
	 */
103
	protected function toCookie($connected){
104
		return;
105
	}
106
	
107
	/**
108
	 * Sends an email for email checking.
109
	 * @param string $email
110
	 * @param string $validationURL
111
	 */
112
	protected function _sendEmailValidation(string $email,string $validationURL){
113
		
114
	}
115
	
116
	/**
117
	 * Loads the user from database using the cookie value
118
	 * @param string $cookie
119
	 */
120
	protected function fromCookie($cookie){
121
		return;
122
	}
123
	
124
	
125
	/**
126
	 * Saves the connected user identifier in a cookie
127
	 * @param object $connected
128
	 */
129
	protected function rememberMe($connected){
130
		$id= $this->toCookie($connected);
131
		if(isset($id)){
132
			UCookie::set($this->_getUserSessionKey(),$id);
133
		}
134
	}
135
	
136
	/**
137
	 * Returns the cookie for auto connection
138
	 * @return NULL|string
139
	 */
140 1
	protected function getCookieUser(){
141 1
		return UCookie::get($this->_getUserSessionKey());
142
	}
143
	
144
	/**
145
	 * To override for changing view files
146
	 * @return AuthFiles
147
	 */
148
	protected function getFiles ():AuthFiles{
149
		return new AuthFiles();
150
	}
151
	
152
	/**
153
	 * To override
154
	 * @param mixed $account
155
	 * @return string
156
	 */
157
	protected function getEmailFromNewAccount($account):string{
158
		return $account;
159
	}
160
}
161
162