Passed
Push — master ( 13c59c...11a6ef )
by Jean-Christophe
09:53
created

AuthController::_setLoginCaption()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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