|
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
|
|
|
use Ubiquity\controllers\Startup; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Controller Auth |
|
16
|
|
|
* @property \Ajax\php\ubiquity\JsUtils $jquery |
|
17
|
|
|
**/ |
|
18
|
|
|
abstract class AuthController extends ControllerBase{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var AuthFiles |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $authFiles; |
|
23
|
|
|
protected $_controller; |
|
24
|
|
|
protected $_action; |
|
25
|
|
|
protected $_actionParams; |
|
26
|
|
|
protected $_noAccessMsg; |
|
27
|
|
|
protected $_loginCaption; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(){ |
|
30
|
|
|
parent::__construct(); |
|
31
|
|
|
$this->_controller=Startup::getController(); |
|
32
|
|
|
$this->_action=Startup::getAction(); |
|
33
|
|
|
$this->_actionParams=Startup::getActionParams(); |
|
34
|
|
|
$this->_noAccessMsg=new FlashMessage("You are not authorized to access the page <b>{url}</b> !","Forbidden access","error","warning circle"); |
|
35
|
|
|
$this->_loginCaption="Log in"; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function index(){ |
|
39
|
|
|
$this->authLoadView($this->_getFiles()->getViewIndex(),["action"=>$this->_getBaseRoute()."/connect", |
|
40
|
|
|
"loginInputName"=>$this->_getLoginInputName(),"loginLabel"=>$this->loginLabel(), |
|
41
|
|
|
"passwordInputName"=>$this->_getPasswordInputName(),"passwordLabel"=>$this->passwordLabel() |
|
42
|
|
|
]); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* To override |
|
47
|
|
|
* Return the base route for this Auth controller |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
public function _getBaseRoute(){ |
|
51
|
|
|
return ClassUtils::getClassSimpleName(get_class($this)); |
|
52
|
|
|
} |
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
* @see \controllers\ControllerBase::isValid() |
|
56
|
|
|
*/ |
|
57
|
|
|
public final function isValid($action) { |
|
|
|
|
|
|
58
|
|
|
return true; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Action called when the user does not have access rights to a requested resource |
|
63
|
|
|
* @param array|string $urlParts |
|
64
|
|
|
*/ |
|
65
|
|
|
public function noAccess($urlParts){ |
|
66
|
|
|
if(!is_array($urlParts)){ |
|
67
|
|
|
$urlParts=explode(".", $urlParts); |
|
68
|
|
|
} |
|
69
|
|
|
USession::set("urlParts", $urlParts); |
|
70
|
|
|
$fMessage=$this->_noAccessMsg->parseContent(["url"=>implode("/",$urlParts)]); |
|
71
|
|
|
$this->noAccessMessage($fMessage); |
|
72
|
|
|
$message=$this->fMessage($fMessage); |
|
73
|
|
|
$this->authLoadView($this->_getFiles()->getViewNoAccess(),["_message"=>$message,"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector(),"_loginCaption"=>$this->_loginCaption]); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Override for modifying the noAccess message |
|
78
|
|
|
* @param FlashMessage $fMessage |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function noAccessMessage(FlashMessage $fMessage){ |
|
81
|
|
|
|
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Override to implement the complete connection procedure |
|
86
|
|
|
*/ |
|
87
|
|
|
public function connect(){ |
|
88
|
|
|
if(URequest::isPost()){ |
|
89
|
|
|
if($connected=$this->_connect()){ |
|
90
|
|
|
$this->onConnect($connected); |
|
91
|
|
|
}else{ |
|
92
|
|
|
$this->onBadCreditentials(); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Processes the data posted by the login form |
|
99
|
|
|
* Have to return the connected user instance |
|
100
|
|
|
*/ |
|
101
|
|
|
abstract protected function _connect(); |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param object $connected |
|
105
|
|
|
*/ |
|
106
|
|
|
abstract protected function onConnect($connected); |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* To override for defining a new action when creditentials are invalid |
|
110
|
|
|
*/ |
|
111
|
|
|
protected function onBadCreditentials(){ |
|
112
|
|
|
$this->badLogin(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Default Action for invalid creditentials |
|
117
|
|
|
*/ |
|
118
|
|
View Code Duplication |
public function badLogin(){ |
|
|
|
|
|
|
119
|
|
|
$fMessage=new FlashMessage("Invalid creditentials!","Connection problem","warning","warning circle"); |
|
120
|
|
|
$this->badLoginMessage($fMessage); |
|
121
|
|
|
$message=$this->fMessage($fMessage); |
|
122
|
|
|
$this->authLoadView($this->_getFiles()->getViewNoAccess(),["_message"=>$message,"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector(),"_loginCaption"=>$this->_loginCaption]); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* To override for modifying the bad login message |
|
127
|
|
|
* @param FlashMessage $fMessage |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function badLoginMessage(FlashMessage $fMessage){ |
|
130
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
private function authLoadView($viewName,$vars=[]){ |
|
134
|
|
|
$files=$this->_getFiles(); |
|
135
|
|
|
$mainTemplate=$files->getBaseTemplate(); |
|
|
|
|
|
|
136
|
|
|
if(isset($mainTemplate)){ |
|
137
|
|
|
$vars["_viewname"]=$viewName; |
|
138
|
|
|
$vars["_base"]=$mainTemplate; |
|
139
|
|
|
$this->loadView($files->getViewBaseTemplate(),$vars); |
|
140
|
|
|
}else{ |
|
141
|
|
|
$this->loadView($viewName,$vars); |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Logout action |
|
147
|
|
|
* Terminate the session and display a logout message |
|
148
|
|
|
*/ |
|
149
|
|
View Code Duplication |
public function terminate(){ |
|
|
|
|
|
|
150
|
|
|
USession::terminate(); |
|
151
|
|
|
$fMessage=new FlashMessage("You have been properly disconnected!","Logout","success","checkmark"); |
|
152
|
|
|
$this->terminateMessage($fMessage); |
|
153
|
|
|
$message=$this->fMessage($fMessage); |
|
154
|
|
|
$this->authLoadView($this->_getFiles()->getViewNoAccess(),["_message"=>$message,"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector(),"_loginCaption"=>$this->_loginCaption]); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function _disConnected(){ |
|
158
|
|
|
$fMessage=new FlashMessage("You have been disconnected from the application!","Logout","","sign out"); |
|
159
|
|
|
$this->disconnectedMessage($fMessage); |
|
160
|
|
|
$message=$this->fMessage($fMessage); |
|
161
|
|
|
$this->jquery->getOnClick("._signin", $this->_getBaseRoute(),$this->_getBodySelector(),["stopPropagation"=>false,"preventDefault"=>false]); |
|
162
|
|
|
$this->jquery->execOn("click", "._close", "window.open(window.location,'_self').close();"); |
|
163
|
|
|
$this->jquery->renderView($this->_getFiles()->getViewDisconnected(),["_title"=>"Session ended","_message"=>$message]); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* To override for modifying the logout message |
|
168
|
|
|
* @param FlashMessage $fMessage |
|
169
|
|
|
*/ |
|
170
|
|
|
protected function terminateMessage(FlashMessage $fMessage){ |
|
171
|
|
|
|
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* To override for modifying the disconnect message |
|
176
|
|
|
* @param FlashMessage $fMessage |
|
177
|
|
|
*/ |
|
178
|
|
|
protected function disconnectedMessage(FlashMessage $fMessage){ |
|
179
|
|
|
|
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Action displaying the logged user information |
|
184
|
|
|
* if _displayInfoAsString returns true, use _infoUser var in views to display user info |
|
185
|
|
|
* @return string|null |
|
186
|
|
|
*/ |
|
187
|
|
|
public function info(){ |
|
188
|
|
|
return $this->loadView($this->_getFiles()->getViewInfo(),["connected"=>USession::get($this->_getUserSessionKey()),"authURL"=>$this->_getBaseRoute(),"bodySelector"=>$this->_getBodySelector()],$this->_displayInfoAsString()); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
protected function fMessage(FlashMessage $fMessage){ |
|
192
|
|
|
return $this->message($fMessage->getType(), $fMessage->getTitle(), $fMessage->getContent(),$fMessage->getIcon()); |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
public function message($type,$header,$body,$icon="info"){ |
|
196
|
|
|
return $this->loadView($this->_getFiles()->getViewMessage(),get_defined_vars(),true); |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
protected function getOriginalURL(){ |
|
200
|
|
|
return USession::get("urlParts"); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* To override for defining user session key, default : "activeUser" |
|
205
|
|
|
* @return string |
|
206
|
|
|
*/ |
|
207
|
|
|
public function _getUserSessionKey(){ |
|
208
|
|
|
return "activeUser"; |
|
209
|
|
|
} |
|
210
|
|
|
|
|
211
|
|
|
public function _checkConnection(){ |
|
212
|
|
|
UResponse::asJSON(); |
|
213
|
|
|
echo "{\"valid\":".UString::getBooleanStr($this->_isValidUser())."}"; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* return boolean true if activeUser is valid |
|
218
|
|
|
*/ |
|
219
|
|
|
abstract public function _isValidUser(); |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* To override for changing view files |
|
223
|
|
|
* @return AuthFiles |
|
224
|
|
|
*/ |
|
225
|
|
|
protected function getFiles ():AuthFiles{ |
|
226
|
|
|
return new AuthFiles(); |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* Override to define if info is displayed as string |
|
231
|
|
|
* if set to true, use _infoUser var in views to display user info |
|
232
|
|
|
*/ |
|
233
|
|
|
public function _displayInfoAsString(){ |
|
234
|
|
|
return false; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
public function _checkConnectionTimeout(){ |
|
238
|
|
|
return; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
private function _getFiles():AuthFiles{ |
|
242
|
|
|
if(!isset($this->authFiles)){ |
|
243
|
|
|
$this->authFiles=$this->getFiles(); |
|
244
|
|
|
} |
|
245
|
|
|
return $this->authFiles; |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
public function _getLoginInputName(){ |
|
249
|
|
|
return "email"; |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
protected function loginLabel(){ |
|
253
|
|
|
return ucfirst($this->_getLoginInputName()); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
public function _getPasswordInputName(){ |
|
257
|
|
|
return "password"; |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
protected function passwordLabel(){ |
|
261
|
|
|
return ucfirst($this->_getPasswordInputName()); |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
|
public function _getBodySelector(){ |
|
265
|
|
|
return "body"; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* Sets the default noAccess message |
|
270
|
|
|
* Default : "You are not authorized to access the page <b>{url}</b> !" |
|
271
|
|
|
* @param string $content |
|
272
|
|
|
* @param string $title |
|
273
|
|
|
* @param string $type |
|
274
|
|
|
* @param string $icon |
|
275
|
|
|
*/ |
|
276
|
|
|
public function _setNoAccessMsg($content,$title=NULL,$type=NULL,$icon=null) { |
|
277
|
|
|
$this->_noAccessMsg->setValues($content,$title,$type,$icon); |
|
278
|
|
|
} |
|
279
|
|
|
/** |
|
280
|
|
|
* @param string $_loginCaption |
|
281
|
|
|
*/ |
|
282
|
|
|
public function _setLoginCaption($_loginCaption) { |
|
283
|
|
|
$this->_loginCaption = $_loginCaption; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
} |
|
288
|
|
|
|