DashboardPanelAccess   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 2
dl 0
loc 62
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B userHasAccess() 0 21 5
B loadAllowRoles() 0 24 6
1
<?php
2
namespace cornernote\dashboard\components;
3
4
use cornernote\dashboard\Module;
5
6
/**
7
 * Dashboard acces controll
8
 *
9
 * @author Uldis Nelsons
10
 */
11
class DashboardPanelAccess
12
{
13
14
	private static $viewRoles = [];
15
16
	/**
17
	 *
18
	 * @param string $panelName
19
	 * @return boolean
20
	 */
21
	public static function userHasAccess($panelName)
22
	{
23
		if (!isset(self::$viewRoles[$panelName])) {
24
			self::$viewRoles[$panelName] = self::loadAllowRoles($panelName);
25
		}
26
27
		/**
28
		 * if not defined allow rules, any has access
29
		 */
30
		if (!self::$viewRoles[$panelName]) {
31
			return true;
32
		}
33
34
		foreach (self::$viewRoles[$panelName] as $role) {
35
			if (\Yii::$app->user->can($role)) {
36
				return true;
37
			}
38
		}
39
40
		return false;
41
	}
42
43
	/**
44
	 * 
45
	 * @param string $panelName
46
	 * @return array
47
	 */
48
	private static function loadAllowRoles($panelName)
49
	{
50
51
		if (!$panels = Module::getInstance()->panels) {
52
			return [];
53
		}
54
		if (!isset($panels[$panelName])) {
55
			return [];
56
		}
57
		if (!is_array($panels[$panelName])) {
58
			return [];
59
		}
60
		if (!isset($panels[$panelName]['allowRoles'])) {
61
			return [];
62
		}
63
64
		$viewRoles = $panels[$panelName]['allowRoles'];
65
66
		if ($updateRoles = Module::getInstance()->updateRoles) {
67
			$viewRoles = array_merge($updateRoles, $viewRoles);
68
		}
69
70
		return $viewRoles;
71
	}
72
}
73