Passed
Push — master ( d5c6e8...acf3eb )
by Jean-Christophe
06:08
created

AuthController::info()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 7
ccs 4
cts 5
cp 0.8
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
crap 3.072
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\ControllerBase;
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 ControllerBase {
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
38 3
	public function __construct($instance = null) {
39 3
		parent::__construct ();
40 3
		$this->insertJquerySemantic ();
41 3
		$this->_controller = Startup::getController ();
42 3
		$this->_action = Startup::getAction ();
43 3
		$this->_actionParams = Startup::getActionParams ();
44 3
		$this->_noAccessMsg = new FlashMessage ( "You are not authorized to access the page <b>{url}</b> !", "Forbidden access", "error", "warning circle" );
45 3
		$this->_loginCaption = "Log in";
46 3
		$this->_controllerInstance = $instance;
47 3
		if (isset ( $instance ))
48
			Startup::injectDependences ( $instance );
49 3
	}
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 \controllers\ControllerBase::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
		/*
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
			if (! URequest::isAjax ()) {
230 1
				if (isset ( $this->_controllerInstance )) {
231
					call_user_func_array ( array ($this->_controllerInstance,'parent::finalize' ), [ ] );
232
				} else {
233 1
					parent::finalize ();
234
				}
235
			}
236 1
			$this->jquery->execAtLast ( "if($('#_userInfo').length){\$('#_userInfo').html(" . preg_replace ( "/$\R?^/m", "", Javascript::prep_element ( $this->info () ) ) . ");}" );
237 1
			echo $this->jquery->compile ();
238
		}
239 1
	}
240
241
	/**
242
	 *
243
	 * {@inheritdoc}
244
	 * @see \Ubiquity\controllers\ControllerBase::initialize()
245
	 */
246 1
	public function initialize() {
247 1
		if (! URequest::isAjax ()) {
248 1
			if (isset ( $this->_controllerInstance )) {
249
				call_user_func_array ( array ($this->_controllerInstance,'parent::initialize' ), [ ] );
250
			} else {
251 1
				parent::initialize ();
252
			}
253
		}
254 1
	}
255
256
	/**
257
	 *
258
	 * @param string $url
259
	 */
260 1
	public function _forward($url) {
261 1
		$initFinalize = true;
262 1
		if (isset ( $this->_controllerInstance ) && ! URequest::isAjax ()) {
263
			$initFinalize = false;
264
		}
265 1
		Startup::forward ( $url, $initFinalize, $initFinalize );
266 1
	}
267
}
268