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