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