Completed
Push — master ( 8cf912...d192c7 )
by Alexey
05:24
created

Access::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * Access module
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2015 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Access extends Module
12
{
13
    public $accessGetters = [];
14
15
    public function init()
16
    {
17
        $this->accessCheckers = $this->getSnippets('accessGetter');
0 ignored issues
show
Bug introduced by
The property accessCheckers 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...
18
    }
19
20
    public function getDeniedRedirect($app = false)
21
    {
22
        if (!$app) {
23
            $app = $this->app->type;
24
        }
25
        if (!empty($this->config['access']['accessTree'][$app]['deniedUrl']))
26
            return $this->config['access']['accessTree'][$app]['deniedUrl'];
27
28
        return '/';
29
    }
30
31
    public function checkAccess($element, $user = null)
32
    {
33
        $access = NULL;
34
        foreach ($this->accessCheckers as $getter) {
35
            foreach ($getter['classes'] as $className) {
36
                if ($element instanceof $className) {
37
                    $access = $getter['get']($element);
38
                    break;
39
                }
40
            }
41
        }
42
        if (is_null($access)) {
43
            $access = [];
44
        }
45
        if (is_null($user)) {
46
            $user = Users\User::$cur;
47
        }
48
        if (empty($access)) {
49
            return true;
50
        }
51
52
        if ((!$user->group_id && !empty($access)) || ($user->group_id && !empty($access) && !in_array($user->group_id, $access)))
53
            return false;
54
55
        return true;
56
    }
57
58
    public function resolvePath($array, $path, $element)
59
    {
60
        while ($path) {
61
            $result = $this->pathWalker($array, array_merge($path, [$element]));
62
            if ($result !== null) {
63
                return $result;
64
            }
65
            $path = array_slice($path, 0, -1);
66
        }
67
        return null;
68
    }
69
70
    public function pathWalker($array, $path)
71
    {
72
        if ($path && isset($array[$path[0]])) {
73
            return $this->pathWalker($array[$path[0]], array_slice($path, 1));
74
        } elseif (!$path) {
75
            return $array;
76
        } else {
77
            return NULL;
78
        }
79
    }
80
81
}
82