| Total Complexity | 49 |
| Total Lines | 298 |
| Duplicated Lines | 0 % |
| Coverage | 66.2% |
| Changes | 10 | ||
| Bugs | 1 | Features | 4 |
Complex classes like AuthController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AuthController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 28 | abstract class AuthController extends Controller { |
||
| 29 | use AuthControllerCoreTrait,AuthControllerVariablesTrait,AuthControllerOverrideTrait,InsertJqueryTrait,Auth2FATrait,AuthAccountCreationTrait,AuthAccountRecoveryTrait; |
||
|
2 ignored issues
–
show
|
|||
| 30 | |||
| 31 | /** |
||
| 32 | * |
||
| 33 | * @var AuthFiles |
||
| 34 | */ |
||
| 35 | protected $authFiles; |
||
| 36 | protected $_controller; |
||
| 37 | protected $_action; |
||
| 38 | protected $_actionParams; |
||
| 39 | protected $_noAccessMsg; |
||
| 40 | protected $_loginCaption; |
||
| 41 | protected $_attemptsSessionKey = '_attempts'; |
||
| 42 | protected $_controllerInstance; |
||
| 43 | protected $_compileJS = true; |
||
| 44 | protected $_invalid=false; |
||
| 45 | |||
| 46 | 1 | public function __construct($instance = null) { |
|
| 47 | 1 | parent::__construct (); |
|
| 48 | 1 | $this->_insertJquerySemantic (); |
|
| 49 | 1 | $this->_controller = Startup::getController (); |
|
| 50 | 1 | $this->_action = Startup::getAction (); |
|
| 51 | 1 | $this->_actionParams = Startup::getActionParams (); |
|
| 52 | 1 | $this->_noAccessMsg = new FlashMessage ( 'You are not authorized to access the page <b>{url}</b> !', 'Forbidden access', 'error', 'warning circle' ); |
|
| 53 | 1 | $this->_loginCaption = 'Log in'; |
|
| 54 | 1 | $this->_controllerInstance = $instance; |
|
| 55 | 1 | if (isset ( $instance )){ |
|
| 56 | Startup::injectDependences ( $instance ); |
||
| 57 | } |
||
| 58 | 1 | if($this->useAjax() && !URequest::isAjax()) { |
|
| 59 | 1 | $this->_addAjaxBehavior($instance->jquery??$this->jquery); |
|
| 60 | } |
||
| 61 | } |
||
| 62 | |||
| 63 | 1 | public function index() { |
|
| 64 | 1 | if (($nbAttempsMax = $this->attemptsNumber ()) !== null) { |
|
| 65 | $nb = USession::getTmp ( $this->_attemptsSessionKey, $nbAttempsMax ); |
||
| 66 | if ($nb <= 0) { |
||
| 67 | $this->badLogin (); |
||
| 68 | return; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | 1 | if($this->useAjax()){ |
|
| 72 | 1 | $this->_addFrmAjaxBehavior('frm-login'); |
|
| 73 | } |
||
| 74 | 1 | $vData=[ 'action' => $this->getBaseUrl () . '/connect','loginInputName' => $this->_getLoginInputName (),'loginLabel' => $this->loginLabel (),'passwordInputName' => $this->_getPasswordInputName (),'passwordLabel' => $this->passwordLabel (),'rememberCaption' => $this->rememberCaption () ]; |
|
| 75 | 1 | $this->addAccountCreationViewData($vData,true); |
|
| 76 | 1 | $this->authLoadView ( $this->_getFiles ()->getViewIndex (), $vData ); |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * |
||
| 81 | * {@inheritdoc} |
||
| 82 | * @see \Ubiquity\controllers\Controller::isValid() |
||
| 83 | */ |
||
| 84 | 1 | public final function isValid($action) { |
|
| 85 | 1 | return true; |
|
| 86 | } |
||
| 87 | |||
| 88 | /** |
||
| 89 | * Action called when the user does not have access rights to a requested resource |
||
| 90 | * |
||
| 91 | * @param array|string $urlParts |
||
| 92 | */ |
||
| 93 | 1 | public function noAccess($urlParts) { |
|
| 94 | 1 | if (! \is_array ( $urlParts )) { |
|
| 95 | $urlParts = \explode ( '.', $urlParts ); |
||
| 96 | } |
||
| 97 | 1 | USession::set ( 'urlParts', $urlParts ); |
|
| 98 | 1 | $fMessage = $this->_noAccessMsg; |
|
| 99 | 1 | $this->noAccessMessage ( $fMessage ); |
|
| 100 | 1 | $message = $this->fMessage ( $fMessage->parseContent ( [ 'url' => \implode ( '/', $urlParts ) ] ) ); |
|
| 101 | |||
| 102 | 1 | if (URequest::isAjax ()) { |
|
| 103 | $this->jquery->get ( $this->_getBaseRoute () . '/info/f', '#_userInfo', [ 'historize' => false,'jqueryDone' => 'replaceWith','hasLoader' => false,'attr' => '' ] ); |
||
| 104 | $this->jquery->compile ( $this->view ); |
||
| 105 | } |
||
| 106 | 1 | $vData=[ '_message' => $message,'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ]; |
|
| 107 | 1 | $this->addAccountCreationViewData($vData); |
|
| 108 | 1 | $this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), $vData); |
|
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Override to implement the complete connection procedure |
||
| 113 | * |
||
| 114 | * @post |
||
| 115 | */ |
||
| 116 | #[\Ubiquity\attributes\items\router\Post] |
||
| 117 | 1 | public function connect() { |
|
| 118 | 1 | if (URequest::isPost ()) { |
|
| 119 | 1 | if ($connected = $this->_connect ()) { |
|
| 120 | 1 | if (isset ( $_POST ['ck-remember'] )) { |
|
| 121 | $this->rememberMe ( $connected ); |
||
| 122 | } |
||
| 123 | 1 | if (USession::exists ( $this->_attemptsSessionKey )) { |
|
| 124 | USession::delete ( $this->_attemptsSessionKey ); |
||
| 125 | } |
||
| 126 | 1 | if($this->has2FA($connected)){ |
|
| 127 | $this->initializeAuth(); |
||
| 128 | USession::set($this->_getUserSessionKey().'-2FA', $connected); |
||
| 129 | $this->send2FACode(); |
||
| 130 | $this->confirm(); |
||
| 131 | $this->finalizeAuth(); |
||
| 132 | }else{ |
||
| 133 | 1 | $this->onConnect ( $connected ); |
|
| 134 | } |
||
| 135 | } else { |
||
| 136 | 1 | $this->_invalid=true; |
|
| 137 | 1 | $this->initializeAuth(); |
|
| 138 | 1 | $this->onBadCreditentials (); |
|
| 139 | 1 | $this->finalizeAuth(); |
|
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Default Action for invalid creditentials |
||
| 146 | * |
||
| 147 | * @noRoute() |
||
| 148 | */ |
||
| 149 | #[\Ubiquity\attributes\items\router\NoRoute] |
||
| 150 | 1 | public function badLogin() { |
|
| 151 | 1 | $fMessage = new FlashMessage ( 'Invalid creditentials!', 'Connection problem', 'warning', 'warning circle' ); |
|
| 152 | 1 | $this->badLoginMessage ( $fMessage ); |
|
| 153 | 1 | $attemptsMessage = ''; |
|
| 154 | 1 | if (($nbAttempsMax = $this->attemptsNumber ()) !== null) { |
|
| 155 | $nb = USession::getTmp ( $this->_attemptsSessionKey, $nbAttempsMax ); |
||
| 156 | $nb --; |
||
| 157 | if ($nb < 0) { |
||
| 158 | $nb = 0; |
||
| 159 | } |
||
| 160 | if ($nb == 0) { |
||
| 161 | $fAttemptsNumberMessage = $this->noAttempts (); |
||
| 162 | } else { |
||
| 163 | $fAttemptsNumberMessage = new FlashMessage ( '<i class="ui warning icon"></i> You still have {_attemptsCount} attempts to log in.', null, 'bottom attached warning', '' ); |
||
| 164 | } |
||
| 165 | USession::setTmp ( $this->_attemptsSessionKey, $nb, $this->attemptsTimeout () ); |
||
| 166 | $this->attemptsNumberMessage ( $fAttemptsNumberMessage, $nb ); |
||
| 167 | $fAttemptsNumberMessage->parseContent ( [ '_attemptsCount' => $nb,'_timer' => '<span id="timer"></span>' ] ); |
||
| 168 | $attemptsMessage = $this->fMessage ( $fAttemptsNumberMessage, 'timeout-message' ); |
||
| 169 | $fMessage->addType ( "attached" ); |
||
| 170 | } |
||
| 171 | 1 | $message = $this->fMessage ( $fMessage, 'bad-login' ) . $attemptsMessage; |
|
| 172 | 1 | $this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ '_message' => $message,'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ] ); |
|
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Logout action |
||
| 177 | * Terminate the session and display a logout message |
||
| 178 | */ |
||
| 179 | 1 | public function terminate() { |
|
| 180 | 1 | USession::terminate (); |
|
| 181 | 1 | $fMessage = new FlashMessage ( 'You have been properly disconnected!', 'Logout', 'success', 'checkmark' ); |
|
| 182 | 1 | $this->terminateMessage ( $fMessage ); |
|
| 183 | 1 | $message = $this->fMessage ( $fMessage ); |
|
| 184 | 1 | $this->authLoadView ( $this->_getFiles ()->getViewNoAccess (), [ '_message' => $message,'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector (),'_loginCaption' => $this->_loginCaption ] ); |
|
| 185 | } |
||
| 186 | |||
| 187 | public function _disConnected() { |
||
| 188 | $fMessage = new FlashMessage ( 'You have been disconnected from the application!', 'Logout', '', 'sign out' ); |
||
| 189 | $this->disconnectedMessage ( $fMessage ); |
||
| 190 | $message = $this->fMessage ( $fMessage ); |
||
| 191 | $this->jquery->getOnClick ( '._signin', $this->getBaseUrl (), $this->_getBodySelector (), [ 'stopPropagation' => false,'preventDefault' => false ] ); |
||
| 192 | $this->jquery->execOn ( 'click', '._close', "window.open(window.location,'_self').close();" ); |
||
| 193 | return $this->jquery->renderView ( $this->_getFiles ()->getViewDisconnected (), [ "_title" => 'Session ended','_message' => $message ], true ); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Action displaying the logged user information |
||
| 198 | * if _displayInfoAsString returns true, use _infoUser var in views to display user info |
||
| 199 | * |
||
| 200 | * @param null|boolean $force |
||
| 201 | * @return string|null |
||
| 202 | * @throws \Exception |
||
| 203 | */ |
||
| 204 | 1 | public function info($force = null) { |
|
| 205 | 1 | if (isset ( $force )) { |
|
| 206 | $displayInfoAsString = $force === true; |
||
| 207 | } else { |
||
| 208 | 1 | $displayInfoAsString = $this->_displayInfoAsString (); |
|
| 209 | } |
||
| 210 | 1 | return $this->loadView ( $this->_getFiles ()->getViewInfo (), [ 'connected' => USession::get ( $this->_getUserSessionKey () ),'authURL' => $this->getBaseUrl (),'bodySelector' => $this->_getBodySelector () ], $displayInfoAsString ); |
|
| 211 | } |
||
| 212 | |||
| 213 | public function checkConnection() { |
||
| 214 | UResponse::asJSON (); |
||
| 215 | echo \json_encode(['valid'=> UString::getBooleanStr ( $this->_isValidUser () )]); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Sets the default noAccess message |
||
| 220 | * Default : "You are not authorized to access the page <b>{url}</b> !" |
||
| 221 | * |
||
| 222 | * @param string $content |
||
| 223 | * @param string $title |
||
| 224 | * @param string $type |
||
| 225 | * @param string $icon |
||
| 226 | */ |
||
| 227 | public function _setNoAccessMsg($content, $title = NULL, $type = NULL, $icon = null) { |
||
| 228 | $this->_noAccessMsg->setValues ( $content, $title, $type, $icon ); |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * |
||
| 233 | * @param string $_loginCaption |
||
| 234 | */ |
||
| 235 | public function _setLoginCaption($_loginCaption) { |
||
| 236 | $this->_loginCaption = $_loginCaption; |
||
| 237 | } |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Auto connect the user |
||
| 241 | */ |
||
| 242 | 1 | public function _autoConnect() { |
|
| 243 | 1 | $cookie = $this->getCookieUser (); |
|
| 244 | 1 | if (isset ( $cookie )) { |
|
| 245 | $user = $this->fromCookie ( $cookie ); |
||
| 246 | if (isset ( $user )) { |
||
| 247 | USession::set ( $this->_getUserSessionKey (), $user ); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Deletes the cookie for auto connection and returns to index |
||
| 254 | */ |
||
| 255 | public function forgetConnection() { |
||
| 256 | UCookie::delete ( $this->_getUserSessionKey () ); |
||
| 257 | $this->index (); |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * |
||
| 262 | * {@inheritdoc} |
||
| 263 | * @see \Ubiquity\controllers\ControllerBase::finalize() |
||
| 264 | */ |
||
| 265 | 1 | public function finalize() { |
|
| 266 | 1 | if (! UResponse::isJSON ()) { |
|
| 267 | 1 | if(Startup::getAction()!=='connect') { |
|
| 268 | 1 | $this->finalizeAuth(); |
|
| 269 | } |
||
| 270 | 1 | $this->jquery->execAtLast ( "if($('#_userInfo').length){\$('#_userInfo').replaceWith(" . \preg_replace ( "/$\R?^/m", "", Javascript::prep_element ( $this->info () ) ) . ");}" ); |
|
| 271 | 1 | if ($this->_compileJS) { |
|
| 272 | 1 | echo $this->jquery->compile (); |
|
| 273 | } |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | 1 | protected function finalizeAuth() { |
|
| 280 | } |
||
| 281 | } |
||
| 282 | |||
| 283 | /** |
||
| 284 | * |
||
| 285 | * {@inheritdoc} |
||
| 286 | * @see \Ubiquity\controllers\ControllerBase::initialize() |
||
| 287 | */ |
||
| 288 | 1 | public function initialize() { |
|
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | 1 | protected function initializeAuth() { |
|
| 295 | 1 | if (!URequest::isAjax()) { |
|
| 296 | 1 | $this->loadView('@activeTheme/main/vHeader.html'); |
|
| 297 | } |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * |
||
| 302 | * @param string $url |
||
| 303 | */ |
||
| 304 | 1 | public function _forward($url, $initialize = null, $finalize = null) { |
|
| 312 | } |
||
| 313 | |||
| 314 | 1 | public function _addAjaxBehavior(JsUtils $jquery=null,$ajaxParameters=['hasLoader'=>'$(this).children(".button")','historize'=>false,'listenerOn'=>'body']){ |
|
| 315 | 1 | $jquery??=$this->jquery; |
|
| 316 | 1 | $jquery->getHref('.ajax[data-target]','', $ajaxParameters); |
|
| 317 | 1 | $jquery->postFormAction('.ui.form',$this->_getBodySelector(),$ajaxParameters); |
|
| 318 | } |
||
| 319 | |||
| 320 | 1 | public function _addFrmAjaxBehavior($id):HtmlForm{ |
|
| 326 | } |
||
| 327 | } |
||
| 328 |