Completed
Push — master ( ab7f39...f31c26 )
by Jean-Christophe
02:08
created

AuthController::_getBodySelector()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Ubiquity\controllers\auth;
3
4
use Ubiquity\utils\http\USession;
5
use Ubiquity\utils\http\URequest;
6
use Ubiquity\utils\flash\FlashMessage;
7
use Ubiquity\controllers\ControllerBase;
8
use Ubiquity\controllers\Auth\AuthFiles;
9
use Ubiquity\cache\ClassUtils;
10
use Ubiquity\utils\http\UResponse;
11
use Ubiquity\utils\base\UString;
12
13
 /**
14
 * Controller Auth
15
 * @property \Ajax\php\ubiquity\JsUtils $jquery
16
 **/
17
abstract class AuthController extends ControllerBase{
18
	/**
19
	 * @var AuthFiles
20
	 */
21
	protected $authFiles;
22
	
23
	public function index(){
24
		$this->authLoadView($this->_getFiles()->getViewIndex(),["action"=>$this->_getBaseRoute()."/connect",
25
				"loginInputName"=>$this->_getLoginInputName(),"loginLabel"=>$this->loginLabel(),
26
				"passwordInputName"=>$this->_getPasswordInputName(),"passwordLabel"=>$this->passwordLabel()
27
		]);
28
	}
29
	
30
	/**
31
	 * To override
32
	 * Return the base route for this Auth controller
33
	 * @return string
34
	 */
35
	public function _getBaseRoute(){
36
		return ClassUtils::getClassSimpleName(get_class($this));
37
	}
38
	/**
39
	 * {@inheritDoc}
40
	 * @see \controllers\ControllerBase::isValid()
41
	 */
42
	public final function isValid() {
0 ignored issues
show
Coding Style introduced by
As per PSR2, final should precede the visibility keyword.
Loading history...
43
		return true;
44
	}
45
	
46
	/**
47
	 * Action called when the user does not have access rights to a requested resource
48
	 * @param array|string $urlParts
49
	 */
50
	public function noAccess($urlParts){
51
		if(!is_array($urlParts)){
52
			$urlParts=explode(".", $urlParts);
53
		}
54
		USession::set("urlParts", $urlParts);
55
		$fMessage=new FlashMessage("You are not authorized to access the page <b>".implode("/",$urlParts)."</b> !","Forbidden access","error","warning circle");
56
		$this->noAccessMessage($fMessage);
57
		$message=$this->fMessage($fMessage);		
58
		$this->authLoadView($this->_getFiles()->getViewNoAccess(),["_message"=>$message,"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector()]);
59
	}
60
	
61
	/**
62
	 * Override for modifying the noAccess message
63
	 * @param FlashMessage $fMessage
64
	 */
65
	protected function noAccessMessage(FlashMessage $fMessage){
66
		
67
	}
68
	
69
	/**
70
	 * Override to implement the complete connection procedure 
71
	 */
72
	public function connect(){
73
		if(URequest::isPost()){
74
			if($connected=$this->_connect()){
75
				$this->onConnect($connected);
76
			}else{
77
				$this->onBadCreditentials();
78
			}
79
		}
80
	}
81
	
82
	/**
83
	 * Processes the data posted by the login form
84
	 * Have to return the connected user instance
85
	 */
86
	abstract protected function _connect();
87
	
88
	/**
89
	 * @param object $connected
90
	 */
91
	abstract protected function onConnect($connected);
92
	
93
	/**
94
	 * To override for defining a new action when creditentials are invalid
95
	 */
96
	protected function onBadCreditentials(){
97
		$this->badLogin();
98
	}
99
	
100
	/**
101
	 * Default Action for invalid creditentials
102
	 */
103 View Code Duplication
	public function badLogin(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
		$fMessage=new FlashMessage("Invalid creditentials!","Connection problem","warning","warning circle");
105
		$this->badLoginMessage($fMessage);
106
		$message=$this->fMessage($fMessage);
107
		$this->authLoadView($this->_getFiles()->getViewNoAccess(),["_message"=>$message,"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector()]);
108
	}
109
	
110
	/**
111
	 * To override for modifying the bad login message
112
	 * @param FlashMessage $fMessage
113
	 */
114
	protected function badLoginMessage(FlashMessage $fMessage){
115
		
116
	}
117
	
118
	private function authLoadView($viewName,$vars=[]){
119
		$files=$this->_getFiles();
120
		$mainTemplate=$files->getBaseTemplate();
1 ignored issue
show
Bug introduced by
Are you sure the assignment to $mainTemplate is correct as $files->getBaseTemplate() (which targets Ubiquity\controllers\Aut...iles::getBaseTemplate()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

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

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

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

Loading history...
121
		if(isset($mainTemplate)){
122
			$vars["_viewname"]=$viewName;
123
			$vars["_base"]=$mainTemplate;
124
			$this->loadView($files->getViewBaseTemplate(),$vars);
125
		}else{
126
			$this->loadView($viewName,$vars);
127
		}
128
	}
129
	
130
	/**
131
	 * Logout action
132
	 * Terminate the session and display a logout message
133
	 */
134 View Code Duplication
	public function terminate(){
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
135
		USession::terminate();
136
		$fMessage=new FlashMessage("You have been properly disconnected!","Logout","success","checkmark");
137
		$this->terminateMessage($fMessage);
138
		$message=$this->fMessage($fMessage);
139
		$this->authLoadView($this->_getFiles()->getViewNoAccess(),["_message"=>$message,"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector()]);
140
	}
141
	
142
	public function _disConnected(){
143
		$fMessage=new FlashMessage("You have been disconnected from the application!","Logout","","sign out");
144
		$this->disconnectedMessage($fMessage);
145
		$message=$this->fMessage($fMessage);
146
		$this->jquery->getOnClick("#signin", $this->_getBaseRoute(),$this->_getBodySelector(),["stopPropagation"=>false,"preventDefault"=>false]);
147
		$this->jquery->renderView($this->_getFiles()->getViewDisconnected(),["_title"=>"Session ended","_message"=>$message,"_buttonCaption"=>"Back to authentication"]);
148
	}
149
	
150
	/**
151
	 * To override for modifying the logout message
152
	 * @param FlashMessage $fMessage
153
	 */
154
	protected function terminateMessage(FlashMessage $fMessage){
155
		
156
	}
157
	
158
	/**
159
	 * To override for modifying the disconnect message
160
	 * @param FlashMessage $fMessage
161
	 */
162
	protected function disconnectedMessage(FlashMessage $fMessage){
163
		
164
	}
165
	
166
	/**
167
	 * Action displaying the logged user information 
168
	 * if _displayInfoAsString returns true, use _infoUser var in views to display user info
169
	 * @return string|null
170
	 */
171
	public function info(){
172
		return $this->loadView($this->_getFiles()->getViewInfo(),["connected"=>USession::get($this->_getUserSessionKey()),"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector()],$this->_displayInfoAsString());
173
	}
174
	
175
	protected function fMessage(FlashMessage $fMessage){
176
		return $this->message($fMessage->getType(), $fMessage->getTitle(), $fMessage->getContent(),$fMessage->getIcon());
177
	}
178
	
179
	public function message($type,$header,$body,$icon="info"){
180
		return $this->loadView($this->_getFiles()->getViewMessage(),get_defined_vars(),true);
181
	}
182
	
183
	protected function getOriginalURL(){
184
		return USession::get("urlParts");
185
	}
186
	
187
	/**
188
	 * To override for defining user session key, default : "activeUser"
189
	 * @return string
190
	 */
191
	public function _getUserSessionKey(){
192
		return "activeUser";
193
	}
194
	
195
	public function _checkConnection(){
196
		UResponse::asJSON();
197
		echo "{\"valid\":".UString::getBooleanStr($this->_isValidUser())."}";
198
	}
199
	
200
	/**
201
	 * return boolean true if activeUser is valid
202
	 */
203
	abstract public function _isValidUser();
204
	
205
	/**
206
	 * To override for changing view files
207
	 * @return AuthFiles
208
	 */
209
	protected function getFiles ():AuthFiles{
210
		return new AuthFiles();
211
	}
212
	
213
	/**
214
	 * Override to define if info is displayed as string
215
	 * if set to true, use _infoUser var in views to display user info
216
	 */
217
	public function _displayInfoAsString(){
218
		return true;
219
	}
220
	
221
	public function _checkConnectionTimeout(){
222
		return;
223
	}
224
	
225
	private function _getFiles():AuthFiles{
226
		if(!isset($this->authFiles)){
227
			$this->authFiles=$this->getFiles();
228
		}
229
		return $this->authFiles;
230
	}
231
	
232
	public function _getLoginInputName(){
233
		return "email";
234
	}
235
236
	protected function loginLabel(){
237
		return ucfirst($this->_getLoginInputName());
238
	}
239
	
240
	public function _getPasswordInputName(){
241
		return "password";
242
	}
243
	
244
	protected function passwordLabel(){
245
		return ucfirst($this->_getPasswordInputName());
246
	}
247
	
248
	public function _getBodySelector(){
249
		return "body";
250
	}
251
}
252