|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\controllers\auth; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\http\USession; |
|
6
|
|
|
use Ubiquity\utils\http\URequest; |
|
7
|
|
|
use Ubiquity\utils\flash\FlashMessage; |
|
8
|
|
|
use Ubiquity\controllers\Controller; |
|
9
|
|
|
use Ubiquity\controllers\Auth\AuthFiles; |
|
10
|
|
|
use Ubiquity\utils\http\UResponse; |
|
11
|
|
|
use Ubiquity\utils\base\UString; |
|
12
|
|
|
use Ubiquity\controllers\Startup; |
|
13
|
|
|
use Ajax\service\Javascript; |
|
14
|
|
|
use Ubiquity\utils\http\UCookie; |
|
15
|
|
|
use Ubiquity\controllers\semantic\InsertJqueryTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Controller Auth |
|
19
|
|
|
* |
|
20
|
|
|
* @property \Ajax\php\ubiquity\JsUtils $jquery |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class AuthController extends Controller { |
|
23
|
|
|
use AuthControllerCoreTrait,AuthControllerVariablesTrait,AuthControllerOverrideTrait,InsertJqueryTrait; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* |
|
27
|
|
|
* @var AuthFiles |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $authFiles; |
|
30
|
|
|
protected $_controller; |
|
31
|
|
|
protected $_action; |
|
32
|
|
|
protected $_actionParams; |
|
33
|
|
|
protected $_noAccessMsg; |
|
34
|
|
|
protected $_loginCaption; |
|
35
|
|
|
protected $_attemptsSessionKey = "_attempts"; |
|
36
|
|
|
protected $_controllerInstance; |
|
37
|
|
|
protected $_compileJS = true; |
|
38
|
1 |
|
|
|
39
|
1 |
|
public function __construct($instance = null) { |
|
40
|
1 |
|
parent::__construct (); |
|
41
|
1 |
|
$this->_insertJquerySemantic (); |
|
42
|
1 |
|
$this->_controller = Startup::getController (); |
|
43
|
1 |
|
$this->_action = Startup::getAction (); |
|
44
|
1 |
|
$this->_actionParams = Startup::getActionParams (); |
|
45
|
1 |
|
$this->_noAccessMsg = new FlashMessage ( "You are not authorized to access the page <b>{url}</b> !", "Forbidden access", "error", "warning circle" ); |
|
46
|
1 |
|
$this->_loginCaption = "Log in"; |
|
47
|
1 |
|
$this->_controllerInstance = $instance; |
|
48
|
|
|
if (isset ( $instance )) |
|
49
|
1 |
|
Startup::injectDependences ( $instance ); |
|
50
|
|
|
} |
|
51
|
1 |
|
|
|
52
|
1 |
|
public function index() { |
|
53
|
|
|
if (($nbAttempsMax = $this->attemptsNumber ()) !== null) { |
|
54
|
|
|
$nb = USession::getTmp ( $this->_attemptsSessionKey, $nbAttempsMax ); |
|
55
|
|
|
if ($nb <= 0) { |
|
56
|
|
|
$this->badLogin (); |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
1 |
|
} |
|
60
|
1 |
|
$this->authLoadView ( $this->_getFiles ()->getViewIndex (), [ "action" => $this->getBaseUrl () . "/connect","loginInputName" => $this->_getLoginInputName (),"loginLabel" => $this->loginLabel (),"passwordInputName" => $this->_getPasswordInputName (),"passwordLabel" => $this->passwordLabel (),"rememberCaption" => $this->rememberCaption () ] ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
* @see \Ubiquity\controllers\Controller::isValid() |
|
67
|
1 |
|
*/ |
|
68
|
1 |
|
public final function isValid($action) { |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Action called when the user does not have access rights to a requested resource |
|
74
|
|
|
* |
|
75
|
|
|
* @param array|string $urlParts |
|
76
|
1 |
|
*/ |
|
77
|
1 |
|
public function noAccess($urlParts) { |
|
78
|
|
|
if (! is_array ( $urlParts )) { |
|
79
|
|
|
$urlParts = explode ( ".", $urlParts ); |
|
80
|
1 |
|
} |
|
81
|
1 |
|
USession::set ( "urlParts", $urlParts ); |
|
82
|
1 |
|
$fMessage = $this->_noAccessMsg; |
|
83
|
1 |
|
$this->noAccessMessage ( $fMessage ); |
|
84
|
|
|
$message = $this->fMessage ( $fMessage->parseContent ( [ "url" => implode ( "/", $urlParts ) ] ) ); |
|
85
|
|
|
if (URequest::isAjax ()) { |
|
86
|
|
|
$this->jquery->get ( $this->_getBaseRoute () . "/info/f", "#_userInfo", [ "historize" => false,"jqueryDone" => "replaceWith","hasLoader" => false,"attr" => "" ] ); |
|
87
|
|
|
$this->jquery->compile ( $this->view ); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
$this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ "_message" => $message,"authURL" => $this->getBaseUrl (),"bodySelector" => $this->_getBodySelector (),"_loginCaption" => $this->_loginCaption ] ); |
|
91
|
1 |
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Override to implement the complete connection procedure |
|
95
|
|
|
*/ |
|
96
|
1 |
|
public function connect() { |
|
97
|
1 |
|
if (URequest::isPost ()) { |
|
98
|
1 |
|
if ($connected = $this->_connect ()) { |
|
99
|
1 |
|
if (isset ( $_POST ["ck-remember"] )) { |
|
100
|
|
|
$this->rememberMe ( $connected ); |
|
101
|
|
|
} |
|
102
|
1 |
|
if (USession::exists ( $this->_attemptsSessionKey )) { |
|
103
|
|
|
USession::delete ( $this->_attemptsSessionKey ); |
|
104
|
|
|
} |
|
105
|
1 |
|
$this->onConnect ( $connected ); |
|
106
|
|
|
} else { |
|
107
|
1 |
|
$this->onBadCreditentials (); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
1 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Default Action for invalid creditentials |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function badLogin() { |
|
116
|
1 |
|
$fMessage = new FlashMessage ( "Invalid creditentials!", "Connection problem", "warning", "warning circle" ); |
|
117
|
1 |
|
$this->badLoginMessage ( $fMessage ); |
|
118
|
1 |
|
$attemptsMessage = ""; |
|
119
|
1 |
|
if (($nbAttempsMax = $this->attemptsNumber ()) !== null) { |
|
120
|
|
|
$nb = USession::getTmp ( $this->_attemptsSessionKey, $nbAttempsMax ); |
|
121
|
|
|
$nb --; |
|
122
|
|
|
if ($nb < 0) |
|
123
|
|
|
$nb = 0; |
|
124
|
|
|
if ($nb == 0) { |
|
125
|
|
|
$fAttemptsNumberMessage = $this->noAttempts (); |
|
126
|
|
|
} else { |
|
127
|
|
|
$fAttemptsNumberMessage = new FlashMessage ( "<i class='ui warning icon'></i> You still have {_attemptsCount} attempts to log in.", null, "bottom attached warning", "" ); |
|
128
|
|
|
} |
|
129
|
|
|
USession::setTmp ( $this->_attemptsSessionKey, $nb, $this->attemptsTimeout () ); |
|
130
|
|
|
$this->attemptsNumberMessage ( $fAttemptsNumberMessage, $nb ); |
|
131
|
|
|
$fAttemptsNumberMessage->parseContent ( [ "_attemptsCount" => $nb,"_timer" => "<span id='timer'></span>" ] ); |
|
132
|
|
|
$attemptsMessage = $this->fMessage ( $fAttemptsNumberMessage, "timeout-message" ); |
|
133
|
|
|
$fMessage->addType ( "attached" ); |
|
134
|
|
|
} |
|
135
|
1 |
|
$message = $this->fMessage ( $fMessage, "bad-login" ) . $attemptsMessage; |
|
136
|
1 |
|
$this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ "_message" => $message,"authURL" => $this->getBaseUrl (),"bodySelector" => $this->_getBodySelector (),"_loginCaption" => $this->_loginCaption ] ); |
|
137
|
1 |
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Logout action |
|
141
|
|
|
* Terminate the session and display a logout message |
|
142
|
|
|
*/ |
|
143
|
1 |
|
public function terminate() { |
|
144
|
1 |
|
USession::terminate (); |
|
145
|
1 |
|
$fMessage = new FlashMessage ( "You have been properly disconnected!", "Logout", "success", "checkmark" ); |
|
146
|
1 |
|
$this->terminateMessage ( $fMessage ); |
|
147
|
1 |
|
$message = $this->fMessage ( $fMessage ); |
|
148
|
1 |
|
$this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ "_message" => $message,"authURL" => $this->getBaseUrl (),"bodySelector" => $this->_getBodySelector (),"_loginCaption" => $this->_loginCaption ] ); |
|
149
|
1 |
|
} |
|
150
|
|
|
|
|
151
|
|
|
public function _disConnected() { |
|
152
|
|
|
$fMessage = new FlashMessage ( "You have been disconnected from the application!", "Logout", "", "sign out" ); |
|
153
|
|
|
$this->disconnectedMessage ( $fMessage ); |
|
154
|
|
|
$message = $this->fMessage ( $fMessage ); |
|
155
|
|
|
$this->jquery->getOnClick ( "._signin", $this->getBaseUrl (), $this->_getBodySelector (), [ "stopPropagation" => false,"preventDefault" => false ] ); |
|
156
|
|
|
$this->jquery->execOn ( "click", "._close", "window.open(window.location,'_self').close();" ); |
|
157
|
|
|
return $this->jquery->renderView ( $this->_getFiles ()->getViewDisconnected (), [ "_title" => "Session ended","_message" => $message ], true ); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Action displaying the logged user information |
|
162
|
|
|
* if _displayInfoAsString returns true, use _infoUser var in views to display user info |
|
163
|
|
|
* |
|
164
|
|
|
* @return string|null |
|
165
|
|
|
*/ |
|
166
|
1 |
|
public function info($force = null) { |
|
167
|
1 |
|
if (isset ( $force )) { |
|
168
|
|
|
$displayInfoAsString = ($force === true) ? true : false; |
|
169
|
|
|
} else { |
|
170
|
1 |
|
$displayInfoAsString = $this->_displayInfoAsString (); |
|
171
|
|
|
} |
|
172
|
1 |
|
return $this->loadView ( $this->_getFiles ()->getViewInfo (), [ "connected" => USession::get ( $this->_getUserSessionKey () ),"authURL" => $this->getBaseUrl (),"bodySelector" => $this->_getBodySelector () ], $displayInfoAsString ); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
public function checkConnection() { |
|
176
|
|
|
UResponse::asJSON (); |
|
177
|
|
|
echo "{\"valid\":" . UString::getBooleanStr ( $this->_isValidUser () ) . "}"; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Sets the default noAccess message |
|
182
|
|
|
* Default : "You are not authorized to access the page <b>{url}</b> !" |
|
183
|
|
|
* |
|
184
|
|
|
* @param string $content |
|
185
|
|
|
* @param string $title |
|
186
|
|
|
* @param string $type |
|
187
|
|
|
* @param string $icon |
|
188
|
|
|
*/ |
|
189
|
|
|
public function _setNoAccessMsg($content, $title = NULL, $type = NULL, $icon = null) { |
|
190
|
|
|
$this->_noAccessMsg->setValues ( $content, $title, $type, $icon ); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* |
|
195
|
|
|
* @param string $_loginCaption |
|
196
|
|
|
*/ |
|
197
|
|
|
public function _setLoginCaption($_loginCaption) { |
|
198
|
|
|
$this->_loginCaption = $_loginCaption; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Auto connect the user |
|
203
|
|
|
*/ |
|
204
|
1 |
|
public function _autoConnect() { |
|
205
|
1 |
|
$cookie = $this->getCookieUser (); |
|
206
|
1 |
|
if (isset ( $cookie )) { |
|
207
|
|
|
$user = $this->fromCookie ( $cookie ); |
|
208
|
|
|
if (isset ( $user )) { |
|
209
|
|
|
USession::set ( $this->_getUserSessionKey (), $user ); |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
1 |
|
} |
|
213
|
|
|
|
|
214
|
|
|
/** |
|
215
|
|
|
* Deletes the cookie for auto connection and returns to index |
|
216
|
|
|
*/ |
|
217
|
|
|
public function forgetConnection() { |
|
218
|
|
|
UCookie::delete ( $this->_getUserSessionKey () ); |
|
219
|
|
|
$this->index (); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
/** |
|
223
|
|
|
* |
|
224
|
|
|
* {@inheritdoc} |
|
225
|
|
|
* @see \Ubiquity\controllers\ControllerBase::finalize() |
|
226
|
|
|
*/ |
|
227
|
1 |
|
public function finalize() { |
|
228
|
1 |
|
if (! UResponse::isJSON ()) { |
|
229
|
1 |
|
$this->finalizeAuth (); |
|
230
|
1 |
|
$this->jquery->execAtLast ( "if($('#_userInfo').length){\$('#_userInfo').html(" . preg_replace ( "/$\R?^/m", "", Javascript::prep_element ( $this->info () ) ) . ");}" ); |
|
231
|
|
|
if ($this->_compileJS) { |
|
232
|
|
|
echo $this->jquery->compile (); |
|
233
|
1 |
|
} |
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
1 |
|
|
|
237
|
1 |
|
protected function finalizeAuth() { |
|
238
|
|
|
} |
|
239
|
1 |
|
|
|
240
|
|
|
/** |
|
241
|
|
|
* |
|
242
|
|
|
* {@inheritdoc} |
|
243
|
|
|
* @see \Ubiquity\controllers\ControllerBase::initialize() |
|
244
|
|
|
*/ |
|
245
|
|
|
public function initialize() { |
|
246
|
1 |
|
$this->initializeAuth (); |
|
247
|
1 |
|
} |
|
248
|
1 |
|
|
|
249
|
|
|
protected function initializeAuth() { |
|
250
|
|
|
} |
|
251
|
1 |
|
|
|
252
|
|
|
/** |
|
253
|
|
|
* |
|
254
|
1 |
|
* @param string $url |
|
255
|
|
|
*/ |
|
256
|
|
|
public function _forward($url, $initialize = null, $finalize = null) { |
|
257
|
|
|
if (! isset ( $initialize )) { |
|
258
|
|
|
$initialize = (! isset ( $this->_controllerInstance ) || URequest::isAjax ()); |
|
259
|
|
|
} |
|
260
|
1 |
|
if (! isset ( $finalize )) { |
|
261
|
1 |
|
$finalize = $initialize; |
|
262
|
1 |
|
} |
|
263
|
|
|
Startup::forward ( $url, $initialize, $finalize ); |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|