Completed
Push — dev ( ade326...e1b211 )
by
unknown
03:23
created

AdminGroupConfiguration::hasPermission()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
ccs 8
cts 8
cp 1
cc 3
eloc 8
nc 3
nop 2
crap 3
1
<?php
2
3
namespace eXpansion\Framework\AdminGroups\Services;
4
5
/**
6
 * Class AdminGroupConfiguration
7
 *
8
 * @package eXpansion\Bundle\AdminGroupConfiguration\Services;
9
 * @author oliver de Cramer <[email protected]>
10
 */
11
class AdminGroupConfiguration
12
{
13
    protected $config;
14
15
    protected $loginGroups = [];
16
17
    /**
18
     * AdminGroupConfiguration constructor.
19
     * @param $config
20
     */
21 13
    public function __construct($config)
22
    {
23 13
        $this->config = $config;
24
25 13
        foreach ($this->config as $groupName => $groupData) {
26 13
            if (!empty($groupData['logins'])) {
27 13
                foreach ($groupData['logins'] as $login) {
28 13
                    $this->loginGroups[$login] = $groupName;
29
                }
30
            }
31
        }
32 13
    }
33
34
    /**
35
     * Get the list of all admin groups.
36
     *
37
     * @return string[]
38
     */
39 4
    public function getGroups()
40
    {
41 4
        return array_keys($this->config);
42
    }
43
44
    /**
45
     * Get list of all users in a group (not just connected).
46
     *
47
     * @param $groupName
48
     *
49
     * @return string[]|null
50
     */
51 3 View Code Duplication
    public function getGroupLogins($groupName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53 3
        if (!isset($this->config[$groupName])) {
54 3
            return null;
55
        }
56
57 2
        return $this->config[$groupName]['logins'];
58
    }
59
60
    /**
61
     * Get list of all permissions given to a group.
62
     *
63
     * @param string $groupName
64
     *
65
     * @return string[]
66
     */
67 5 View Code Duplication
    public function getGroupPermissions($groupName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
    {
69 5
        if (!isset($this->config[$groupName])) {
70 1
            return [];
71
        }
72
73 5
        return $this->config[$groupName]['permissions'];
74
    }
75
76
    /**
77
     * Get admin group label
78
     *
79
     * @param string $groupName
80
     * @return string
81
     */
82 View Code Duplication
    public function getGroupLabel($groupName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84
        if (!isset($this->config[$groupName])) {
85
            return "";
86
        }
87
88
        return $this->config[$groupName]['label'];
89
    }
90
91
92
    /**
93
     * @param string $login
94
     *
95
     * @return string|null
96
     */
97 7
    public function getLoginGroupName($login)
98
    {
99 7
        return isset($this->loginGroups[$login]) ? $this->loginGroups[$login] : null;
100
    }
101
102
    /**
103
     * @param string $login
104
     * @param string $permission
105
     *
106
     * @return bool
107
     */
108 4
    public function hasPermission($login, $permission)
109
    {
110 4
        $groupName = $this->getLoginGroupName($login);
111
112
        // if login has no groups, no permission
113 4
        if ($groupName === null) {
114 3
            return false;
115
        }
116
        // master admin has all permissions
117 4
        if ($groupName == 'master_admin') {
118 4
            return true;
119
        }
120
121 4
        $permissions = $this->getGroupPermissions($groupName);
122
123 4
        return in_array($permission, $permissions);
124
    }
125
}
126