Passed
Push — developer ( 2e4efd...3d8f05 )
by Mariusz
151:22 queued 116:22
created

WebUI::triggerCheckPermission()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 3
nc 3
nop 2
1
<?php
2
/**
3
 * WebUI class.
4
 *
5
 * @package App
6
 *
7
 * @copyright YetiForce Sp. z o.o.
8
 * @license   YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com)
9
 * @author    Mariusz Krzaczkowski <[email protected]>
10
 */
11
12
namespace App;
13
14
use YF\Modules\Base\Model;
15
16
class WebUI
17
{
18
	/**
19
	 * Process.
20
	 *
21
	 * @param Request $request
22
	 *
23
	 * @throws \App\Exceptions\AppException
24
	 */
25
	public function process(Request $request)
26
	{
27
		\App\Log::init();
28
		try {
29
			$module = $request->getModule();
30
			$view = $request->getByType('view', Purifier::ALNUM);
31
			$action = $request->getByType('action', Purifier::ALNUM);
32
			if (false === $this->isInstalled() && 'Install' != $module) {
33
				header('Location:index.php?module=Install&view=Install');
34
				return;
35
			}
36
			$userInstance = User::getUser();
37
			if (empty($module)) {
38
				if ($userInstance && $userInstance->hasLogin()) {
39
					$module = Config::$defaultModule;
40
					$moduleInstance = Model\Module::getInstance($module);
41
					$view = $moduleInstance->getDefaultView();
42
				} else {
43
					$module = 'Users';
44
					$view = 'Login';
45
				}
46
			}
47
			$request->set('module', $module);
48
			$request->set('view', $view);
49
			if (!empty($action)) {
50
				$componentType = 'Action';
51
				$componentName = $action;
52
			} else {
53
				$componentType = 'View';
54
				if (empty($view)) {
55
					$view = 'Index';
56
				}
57
				$componentName = $view;
58
			}
59
			if ('Logout' === $action && 'Users' === $module && !$userInstance->hasLogin() && !$request->isAjax()) {
60
				header('Location:index.php');
61
				return false;
62
			}
63
			$handlerClass = Loader::getModuleClassName($module, $componentType, $componentName);
64
			if (class_exists($handlerClass)) {
65
				$handler = new $handlerClass($request);
66
				$handler->validateRequest();
67
				if ($handler->loginRequired() && !$userInstance->hasLogin()) {
68
					header('Location:index.php');
69
				}
70
				$handler->checkPermission();
71
				$this->triggerPreProcess($handler, $request);
72
				$handler->process();
73
				$this->triggerPostProcess($handler, $request);
74
			} else {
75
				throw new Exceptions\AppException("HANDLER_NOT_FOUND: $handlerClass");
76
			}
77
		} catch (\Throwable $e) {
78
			if (401 === $e->getCode()) {
79
				unset($_SESSION);
80
				session_destroy();
81
				header('Location: ' . \App\Config::$portalUrl);
82
				return;
83
			}
84
			Log::error($e->getMessage());
85
			if ($request->isAjax() && $request->isEmpty('view')) {
86
				$response = new \App\Response();
87
				if (Config::get('displayDetailsException')) {
88
					$result = [
89
						'message' => $e->getMessage(),
90
						'trace' => $e->getTraceAsString()
91
					];
92
				} else {
93
					$result = [
94
						'message' => 'Error',
95
					];
96
				}
97
				$response->setResult($result);
98
				$response->emit();
99
			} else {
100
				Exceptions\AppException::view($e);
101
			}
102
		}
103
	}
104
105
	public function isInstalled(): bool
106
	{
107
		return '__API_PATH__' != Config::$apiUrl;
108
	}
109
110
	protected function triggerPreProcess($handler, $request)
111
	{
112
		if ($request->isAjax()) {
113
			$handler->preProcessAjax();
114
			return;
115
		}
116
		$handler->preProcess();
117
	}
118
119
	protected function triggerPostProcess($handler, $request)
120
	{
121
		if ($request->isAjax()) {
122
			$handler->postProcessAjax();
123
			return;
124
		}
125
		$handler->postProcess();
126
	}
127
}
128