Passed
Pull Request — master (#201)
by Jean-Christophe
26:40
created

WithAuthTrait::onInvalidControl()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.1922

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 10
c 1
b 1
f 0
dl 0
loc 13
ccs 7
cts 11
cp 0.6364
rs 9.9332
cc 2
nc 2
nop 0
crap 2.1922
1
<?php
2
namespace Ubiquity\controllers\auth;
3
4
use Ubiquity\utils\http\URequest;
5
use Ubiquity\controllers\Startup;
6
7
/**
8
 * 
9
 * For adding authentification on a controller.
10
 * 
11
 * Ubiquity\controllers\auth$WithAuthTrait
12
 * This class is part of Ubiquity
13
 * @author jc
14
 * @version 1.0.0
15
 * 
16
 * @property \Ajax\php\ubiquity\JsUtils $jquery
17
 * @property \Ubiquity\views\View $view
18
 *
19
 */
20
trait WithAuthTrait {
21
22
	protected $_checkConnectionContent;
23
24
	/**
25
	 *
26
	 * @var AuthController
27
	 */
28
	protected $authController;
29
30 2
	public function initialize() {
31 2
		$authController = $this->_getAuthController();
32 2
		$authController->_init();
33 2
		parent::initialize();
34 2
		if (! URequest::isAjax() || URequest::has('_userInfo')) {
35 2
			if (! $authController->_displayInfoAsString()) {
36 2
				$authController->info();
37
			}
38 2
			if ($this->isValid(Startup::getAction())) {
39 2
				$this->_checkConnectionContent = $this->checkConnection($authController);
40
			} else {
41 2
				if ($authController->_checkConnectionTimeout() !== null)
42
					$this->jquery->clearInterval('_checkConnection');
43
			}
44
		}
45
	}
46
47
	/**
48
	 *
49
	 * {@inheritdoc}
50
	 * @see \Ubiquity\controllers\Controller::loadView()
51
	 */
52 2
	public function loadView(string $viewName, $pData = NULL, bool $asString = false) {
53 2
		if ((! URequest::isAjax() && $this->_getAuthController()->_displayInfoAsString()) || URequest::has('_userInfo')) {
54
			$this->view->setVar('_userInfo', $this->_getAuthController()
55
				->info());
56
		}
57 2
		return parent::loadView($viewName, $pData, $asString);
58
	}
59
60
	/**
61
	 *
62
	 * {@inheritdoc}
63
	 * @see \Ubiquity\controllers\Controller::isValid()
64
	 */
65 2
	public function isValid($action) {
66 2
		$authCtrl = $this->_getAuthController();
67 2
		$isValid = $authCtrl->_isValidUser($action);
68 2
		if (! $isValid) {
69 2
			$authCtrl->_autoConnect();
70 2
			return $authCtrl->_isValidUser($action);
71
		}
72 2
		return $isValid;
73
	}
74
75
	/**
76
	 *
77
	 * {@inheritdoc}
78
	 * @see \Ubiquity\controllers\Controller::onInvalidControl()
79
	 */
80 2
	public function onInvalidControl() {
81 2
		$auth = $this->_getAuthController();
82 2
		if (URequest::isAjax()) {
83
			$this->jquery->get($auth->_getBaseRoute() . '/noAccess/' . \implode('.', Startup::$urlParts), $auth->_getBodySelector(), [
84
				'historize' => false
85
			]);
86
			echo $this->jquery->compile($this->view);
87
		} else {
88 2
			$this->initialize();
89 2
			$auth->noAccess(Startup::$urlParts);
90 2
			$this->finalize();
91
		}
92 2
		exit();
93
	}
94
95
	/**
96
	 *
97
	 * @return \Ubiquity\controllers\auth\AuthController
98
	 */
99 2
	protected function _getAuthController(): AuthController {
100 2
		if (! isset($this->authController)) {
101 2
			$this->authController = $this->getAuthController();
102 2
			Startup::injectDependencies($this->authController);
103
		}
104 2
		return $this->authController;
105
	}
106
107
	protected abstract function getAuthController(): AuthController;
108
109 2
	protected function checkConnection($authController) {
110 2
		if ($authController->_checkConnectionTimeout() !== null) {
111
			$ret = $authController->_disconnected();
112
			$this->jquery->ajaxInterval("get", $authController->_getBaseRoute() . '/checkConnection/', $authController->_checkConnectionTimeout(), '_checkConnection', '', [
113
				'historize' => false,
114
				'jsCallback' => "data=($.isPlainObject(data))?data:JSON.parse(data);if(!data.valid){ $('#disconnected-modal').modal({closable: false}).modal('show');clearInterval(window._checkConnection);}"
115
			]);
116
			return $ret;
117
		}
118
	}
119
120 2
	public function finalize() {
121 2
		parent::finalize();
122 2
		if (isset($this->_checkConnectionContent)) {
123
			echo $this->_checkConnectionContent;
124
		}
125
	}
126
}
127