Completed
Push — master ( ab7f39...f31c26 )
by Jean-Christophe
02:08
created

WithAuthTrait::onInvalidControl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 0
1
<?php
2
namespace Ubiquity\controllers\auth;
3
4
use Ubiquity\utils\http\URequest;
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * ControllerBase
9
 * @property \Ajax\php\ubiquity\JsUtils $jquery
10
 **/
11
trait WithAuthTrait{
12
	
13
	/**
14
	 * @var AuthController
15
	 */
16
	protected $authController;
17
	
18
	public function initialize(){
19
		parent::initialize();
20
		$authController=$this->_getAuthController();
21
		if(!URequest::isAjax() && !$authController->_displayInfoAsString()){
22
			$authController->info();
23
			if($this->isValid()){
24
				$this->checkConnection($authController);
25
			}else{
26
				if($authController->_checkConnectionTimeout()!=null)
27
					$this->jquery->clearInterval("_checkConnection");
28
			}
29
		}
30
	}
31
	
32
	/**
33
	 * {@inheritDoc}
34
	 * @see \Ubiquity\controllers\Controller::loadView()
35
	 */
36
	public function loadView($viewName, $pData = NULL, $asString = false) {
37
		if(!URequest::isAjax() && $this->_getAuthController()->_displayInfoAsString()){
38
			$this->view->setVar("_userInfo",$this->_getAuthController()->info());
0 ignored issues
show
Bug introduced by
The property view does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
39
		}
40
		return parent::loadView ($viewName,$pData,$asString);
41
	}
42
43
	/**
44
	 * {@inheritDoc}
45
	 * @see \Ubiquity\controllers\Controller::isValid()
46
	 */
47
	public function isValid() {
48
		return $this->_getAuthController()->_isValidUser();
49
	}
50
51
	/**
52
	 * {@inheritDoc}
53
	 * @see \Ubiquity\controllers\Controller::onInvalidControl()
54
	 */
55
	public function onInvalidControl() {
56
		$auth=$this->_getAuthController();
57
		if(URequest::isAjax()){
58
			Startup::injectDependences($this, Startup::getConfig());
59
			$this->jquery->get($auth->_getBaseRoute()."/noAccess/".implode(".", Startup::$urlParts),$auth->_getBodySelector(),["historize"=>false]);	
60
			echo $this->jquery->compile($this->view);
61
		}else{
62
			$auth->initialize();
63
			$auth->noAccess(Startup::$urlParts);
64
			$auth->finalize();
65
		}
66
		exit();
67
	}
68
	
69
	/**
70
	 * @return \Ubiquity\controllers\auth\AuthController
71
	 */
72
	protected function _getAuthController():AuthController{
73
		if(!isset($this->authController)){
74
			$this->authController=$this->getAuthController();
75
			Startup::injectDependences($this->authController, Startup::getConfig());
76
		}
77
		return $this->authController;
78
	}
79
	
80
	protected abstract function getAuthController():AuthController;
81
	
82
	
83
	protected function checkConnection($authController){
84
		if($authController->_checkConnectionTimeout()!=null){
85
			$authController->_disconnected();
86
			$this->jquery->ajaxInterval("get",$authController->_getBaseRoute()."/_checkConnection",$authController->_checkConnectionTimeout(),"_checkConnection","",["jsCallback"=>"data=($.isPlainObject(data))?data:JSON.parse(data);if(!data.valid){ $('#disconnected-modal').modal({closable: false}).modal('show');clearInterval(window._checkConnection);}"]);
87
		}
88
			
89
	}
90
91
}
92