PluginProbe
User Access Manager / 2.1.7
User Access Manager v2.1.7
2.3.20 2.3.19 2.3.18 2.3.17 2.3.16 2.3.15 2.3.14 2.3.13 trunk 0.6 0.6.1 0.6.2 0.7 0.7 Beta 0.7.0.1 0.8 0.8.0.1 0.8.0.2 0.9 0.9.1 0.9.1.1 0.9.1.2 0.9.1.3 0.9.1.4 1.0 All 136 releases
user-access-manager / src / Command / GroupCommand.php

GroupCommand.php in User Access Manager 2.1.7, at src/Command/GroupCommand.php

330 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * GroupCommand.php
4 *
5 * The GroupCommand class file.
6 *
7 * PHP versions 5
8 *
9 * @author Alexander Schneider <alexanderschneider85@gmail.com>
10 * @author Nils Woetzel nils.woetzel@h-its.org
11 * @copyright 2008-2017 Alexander Schneider
12 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
13 * @version SVN: $id$
14 * @link http://wordpress.org/extend/plugins/user-access-manager/
15 */
16 namespace UserAccessManager\Command;
17
18 use UserAccessManager\Object\ObjectHandler;
19 use UserAccessManager\UserGroup\UserGroupFactory;
20 use UserAccessManager\UserGroup\UserGroupHandler;
21 use UserAccessManager\Wrapper\WordpressCli;
22 use WP_CLI\CommandWithDBObject;
23
24 /**
25 * Class GroupCommand
26 *
27 * @package UserAccessManager\Command
28 */
29 class GroupCommand extends CommandWithDBObject
30 {
31 const FORMATTER_PREFIX = 'uam_user_groups';
32
33 /**
34 * @var array
35 */
36 private static $allowedAccessValues = ['group', 'all'];
37
38 /**
39 * @var WordpressCli
40 */
41 private $wordpressCli;
42
43 /**
44 * @var UserGroupHandler
45 */
46 private $userGroupHandler;
47
48 /**
49 * @var UserGroupFactory
50 */
51 private $userGroupFactory;
52
53 /**
54 * ObjectCommand constructor.
55 *
56 * @param WordpressCli $wordpressCli
57 * @param UserGroupHandler $userGroupHandler
58 * @param UserGroupFactory $userGroupFactory
59 */
60 public function __construct(
61 WordpressCli $wordpressCli,
62 UserGroupHandler $userGroupHandler,
63 UserGroupFactory $userGroupFactory
64 ) {
65 $this->wordpressCli = $wordpressCli;
66 $this->userGroupHandler = $userGroupHandler;
67 $this->userGroupFactory = $userGroupFactory;
68 }
69
70 /**
71 * Returns the formatter
72 *
73 * @param $assocArguments
74 *
75 * @return \WP_CLI\Formatter
76 */
77 private function getFormatter(&$assocArguments)
78 {
79 return $this->wordpressCli->createFormatter(
80 $assocArguments,
81 [
82 'ID',
83 'group_name',
84 'group_desc',
85 'read_access',
86 'write_access',
87 'roles',
88 'ip_range',
89 ],
90 self::FORMATTER_PREFIX
91 );
92 }
93
94 /**
95 * list groups
96 *
97 * ## OPTIONS
98 *
99 * [--format=<format>]
100 * : Accepted values: table, csv, json, count, ids. Default: table
101 *
102 * ## EXAMPLES
103 *
104 * wp uam groups list
105 *
106 * @subcommand ls
107 *
108 * @param array $arguments
109 * @param array $assocArguments
110 */
111 public function ls(array $arguments, array $assocArguments)
112 {
113 if (count($arguments) > 0) {
114 $this->wordpressCli->error('No arguments excepted. Please use the format option.');
115 return;
116 }
117
118 $userGroups = $this->userGroupHandler->getUserGroups();
119
120 if (count($userGroups) <= 0) {
121 $this->wordpressCli->error('No groups defined yet!');
122 return;
123 }
124
125 $groups = [];
126
127 foreach ($userGroups as $userGroup) {
128 $groups[$userGroup->getId()] = [
129 'ID' => $userGroup->getId(),
130 'group_name' => $userGroup->getName(),
131 'group_desc' => $userGroup->getDescription(),
132 'read_access' => $userGroup->getReadAccess(),
133 'write_access' => $userGroup->getWriteAccess(),
134 'roles' => implode(
135 ',',
136 array_keys($userGroup->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE))
137 ),
138 'ip_range' => $userGroup->getIpRange() !== null ? $userGroup->getIpRange() : ''
139 ];
140 }
141
142 $formatter = $this->getFormatter($assocArguments);
143 $formatter->display_items($groups);
144 }
145
146 /**
147 * delete groups
148 *
149 * ## OPTIONS
150 * <group_id>
151 * : id of the group(s) to delete; accepts unlimited ids
152 *
153 * ## EXAMPLES
154 *
155 * wp uam groups del 3 5
156 *
157 * @subcommand del
158 *
159 * @param array $arguments
160 */
161 public function del(array $arguments)
162 {
163 if (count($arguments) < 1) {
164 $this->wordpressCli->error('Expected: wp uam groups del \<id\> ...');
165 return;
166 }
167
168 foreach ($arguments as $userGroupId) {
169 if ($this->userGroupHandler->deleteUserGroup($userGroupId) === true) {
170 $this->wordpressCli->success("Successfully deleted group with id '{$userGroupId}'.");
171 } else {
172 $this->wordpressCli->error("Group id '{$userGroupId}' doesn't exists.");
173 }
174 }
175 }
176
177 /**
178 * Checks if the user group already exists.
179 *
180 * @param $userGroupName
181 *
182 * @return bool
183 */
184 private function doesUserGroupExists($userGroupName)
185 {
186 $userGroups = $this->userGroupHandler->getUserGroups();
187
188 foreach ($userGroups as $userGroup) {
189 if ($userGroup->getName() === $userGroupName) {
190 $this->wordpressCli->error(
191 "Group with the same name '{$userGroupName}' already exists: {$userGroup->getId()}"
192 );
193
194 return false;
195 }
196 }
197
198 return true;
199 }
200
201 /**
202 * Returns the argument value.
203 *
204 * @param array $arguments
205 * @param string $value
206 *
207 * @return string
208 */
209 private function getArgumentValue(array $arguments, $value)
210 {
211 return (isset($arguments[$value]) === true) ? (string)$arguments[$value] : '';
212 }
213
214 /**
215 * Processes the access value.
216 *
217 * @param array $arguments
218 * @param string $value
219 * @param bool $porcelain
220 *
221 * @return string
222 */
223 private function getAccessValue(array $arguments, $value, $porcelain)
224 {
225 $accessValue = $this->getArgumentValue($arguments, $value);
226
227 if (in_array($accessValue, self::$allowedAccessValues) === false) {
228 if ($porcelain === true) {
229 $this->wordpressCli->line("setting {$value} to ".self::$allowedAccessValues[0]);
230 }
231
232 $accessValue = self::$allowedAccessValues[0];
233 }
234
235 return $accessValue;
236 }
237
238 /**
239 * Creates the user group.
240 *
241 * @param string $userGroupName
242 * @param array $assocArguments
243 *
244 * @return \UserAccessManager\UserGroup\UserGroup
245 */
246 private function createUserGroup($userGroupName, array $assocArguments)
247 {
248 $groupDescription = $this->getArgumentValue($assocArguments, 'desc');
249 $ipRange = $this->getArgumentValue($assocArguments, 'ip_range');
250 $porcelain = isset($assocArguments['porcelain']);
251 $readAccess = $this->getAccessValue($assocArguments, 'read_access', $porcelain);
252 $writeAccess = $this->getAccessValue($assocArguments, 'write_access', $porcelain);
253
254 $userGroup = $this->userGroupFactory->createUserGroup();
255 $userGroup->setName($userGroupName);
256 $userGroup->setDescription($groupDescription);
257 $userGroup->setIpRange($ipRange);
258 $userGroup->setReadAccess($readAccess);
259 $userGroup->setWriteAccess($writeAccess);
260
261 // add roles
262 if (isset($assocArguments['roles']) === true) {
263 $roles = explode(',', $assocArguments['roles']);
264
265 $userGroup->removeObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
266
267 foreach ($roles as $role) {
268 $userGroup->addObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, trim($role));
269 }
270 }
271
272 $userGroup->save();
273
274 return $userGroup;
275 }
276
277 /**
278 * add group
279 *
280 * ## OPTIONS
281 * <group_name>
282 * : the name of the new group
283 *
284 * [--porcelain]
285 * : Output just the new post id.
286 *
287 * [--roles=<list>]
288 * : comma separated list of group associated roles
289 *
290 * [--<field>=<value>]
291 * : Associative args for new UamUserGroup object
292 * allowed fields and values are:
293 * desc="",
294 * read_access={group,all*},
295 * write_access={group,all*},
296 * ip_range="192.168.0.1-192.168.0.10;192.168.0.20-192.168.0.30"
297 *
298 * *=default
299 *
300 * ## EXAMPLES
301 *
302 * wp uam groups add fighters --read_access=all
303 *
304 * @param array $arguments
305 * @param array $assocArguments
306 */
307 public function add(array $arguments, array $assocArguments)
308 {
309 if (isset($arguments[0]) === false) {
310 $this->wordpressCli->error("Please provide a group name.");
311 return;
312 }
313
314 $userGroupName = $arguments[0];
315
316 if ($this->doesUserGroupExists($userGroupName) === false) {
317 return;
318 }
319
320 $userGroup = $this->createUserGroup($userGroupName, $assocArguments);
321 $this->userGroupHandler->addUserGroup($userGroup);
322
323 if (isset($assocArguments['porcelain']) === true) {
324 $this->wordpressCli->line($userGroup->getId());
325 } else {
326 $this->wordpressCli->success("Added new group '{$userGroupName}' with id {$userGroup->getId()}.");
327 }
328 }
329 }
330