|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace eXpansion\Framework\AdminGroups\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use eXpansion\Framework\AdminGroups\Exceptions\UnknownGroupException; |
|
6
|
|
|
use eXpansion\Framework\AdminGroups\Services\AdminGroupConfiguration; |
|
7
|
|
|
use eXpansion\Framework\Core\Model\UserGroups\Group; |
|
8
|
|
|
use eXpansion\Framework\Core\Plugins\UserGroups\Factory; |
|
9
|
|
|
use eXpansion\Framework\Core\Storage\Data\Player; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class AdminGroupConfiguration |
|
13
|
|
|
* |
|
14
|
|
|
* @package eXpansion\Bundle\AdminGroupConfiguration\Helpers; |
|
15
|
|
|
* @author oliver de Cramer <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class AdminGroups |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var AdminGroupConfiguration */ |
|
20
|
|
|
protected $adminGroupConfiguration; |
|
21
|
|
|
|
|
22
|
|
|
/** @var Factory */ |
|
23
|
|
|
protected $userGroupFactory; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* GroupsPlugin constructor. |
|
27
|
|
|
* |
|
28
|
|
|
* @param AdminGroupConfiguration $adminGroupConfiguration |
|
29
|
|
|
* @param Factory $userGroupFactory |
|
30
|
|
|
*/ |
|
31
|
6 |
|
public function __construct( |
|
32
|
|
|
AdminGroupConfiguration $adminGroupConfiguration, |
|
33
|
|
|
Factory $userGroupFactory |
|
34
|
|
|
) { |
|
35
|
6 |
|
$this->adminGroupConfiguration = $adminGroupConfiguration; |
|
36
|
6 |
|
$this->userGroupFactory = $userGroupFactory; |
|
37
|
6 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Get list of all user groups. |
|
41
|
|
|
* Can be useful for creating group based GUI widgets. |
|
42
|
|
|
* |
|
43
|
|
|
* @return Group[] |
|
44
|
|
|
*/ |
|
45
|
1 |
|
public function getUserGroups() |
|
46
|
|
|
{ |
|
47
|
1 |
|
$groups = []; |
|
48
|
1 |
|
foreach ($this->adminGroupConfiguration->getGroups() as $groupName) { |
|
49
|
1 |
|
$groups[] = $this->getUserGroup("$groupName"); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
return $groups; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get the group in which a user is. |
|
57
|
|
|
* This is useful for gui actions. |
|
58
|
|
|
* |
|
59
|
|
|
* @param string $login |
|
60
|
|
|
* |
|
61
|
|
|
* @return Group |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function getLoginUserGroups($login) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$groupName = $this->adminGroupConfiguration->getLoginGroupName($login); |
|
66
|
1 |
|
if (empty($groupName)) { |
|
67
|
1 |
|
$groupName = 'guest'; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
return $this->getUserGroup("$groupName"); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get (or create a new) admin user group |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $groupName |
|
77
|
|
|
* |
|
78
|
|
|
* @return Group |
|
79
|
|
|
*/ |
|
80
|
2 |
|
protected function getUserGroup($groupName) |
|
81
|
|
|
{ |
|
82
|
2 |
|
$groupName = "admin:$groupName"; |
|
83
|
|
|
|
|
84
|
2 |
|
$group = $this->userGroupFactory->getGroup($groupName); |
|
85
|
2 |
|
if (!$group) { |
|
86
|
2 |
|
$this->userGroupFactory->create($groupName); |
|
87
|
2 |
|
$group = $this->userGroupFactory->getGroup($groupName); |
|
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
2 |
|
return $group; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Checks if a login or player has a certain permission or not. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|Player $login Login of the player to check for permission. |
|
97
|
|
|
* @param string $permission The permission to check for. |
|
98
|
|
|
* |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
3 |
|
public function hasPermission($login, $permission) |
|
102
|
|
|
{ |
|
103
|
3 |
|
if ($login instanceof Player) { |
|
104
|
|
|
$login = $login->getLogin(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
3 |
|
return $this->adminGroupConfiguration->hasPermission($login, $permission); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Check if a group has a certain permission or not. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $groupName The name of the group to check permissions for. |
|
114
|
|
|
* @param string $permission The permission to check for. |
|
115
|
|
|
* |
|
116
|
|
|
* @return bool |
|
117
|
|
|
* @throws UnknownGroupException Thrown when group isn't an admin group. |
|
118
|
|
|
*/ |
|
119
|
2 |
|
public function hasGroupPermission($groupName, $permission) |
|
120
|
|
|
{ |
|
121
|
|
|
|
|
122
|
2 |
|
if (strpos($groupName, 'admin:') === 0) { |
|
123
|
1 |
|
$groupName = str_replace("admin:", '', $groupName); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
2 |
|
$logins = $this->adminGroupConfiguration->getGroupLogins($groupName); |
|
127
|
|
|
|
|
128
|
2 |
|
if (!empty($logins)) { |
|
129
|
1 |
|
return $this->hasPermission($logins[0], $permission); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
2 |
|
if (is_null($logins)) { |
|
133
|
2 |
|
throw new UnknownGroupException("'$groupName' admin group does not exist."); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param string $groupName |
|
141
|
|
|
* @return string |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getGroupLabel($groupName) |
|
144
|
|
|
{ |
|
145
|
|
|
if (strpos($groupName, 'admin:') === 0) { |
|
146
|
|
|
$groupName = str_replace("admin:", '', $groupName); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $this->adminGroupConfiguration->getGroupLabel($groupName); |
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* gets if the player is admin |
|
155
|
|
|
* |
|
156
|
|
|
* @param string $login |
|
157
|
|
|
* @return bool |
|
158
|
|
|
*/ |
|
159
|
|
|
public function isAdmin($login) |
|
160
|
|
|
{ |
|
161
|
|
|
return (strpos($this->getLoginUserGroups($login)->getName(), 'admin:') === 0) ? true : false; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
} |
|
165
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.