PluginProbe
User Access Manager / 2.2.1
User Access Manager v2.2.1
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.2.1, at src/Command/GroupCommand.php

321 lines 9.3 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
17 declare(strict_types=1);
18
19
20 namespace UserAccessManager\Command;
21
22 use Exception;
23 use UserAccessManager\Object\ObjectHandler;
24 use UserAccessManager\UserGroup\UserGroup;
25 use UserAccessManager\UserGroup\UserGroupFactory;
26 use UserAccessManager\UserGroup\UserGroupHandler;
27 use UserAccessManager\UserGroup\UserGroupTypeException;
28 use UserAccessManager\Wrapper\WordpressCli;
29 use WP_CLI\ExitException;
30 use WP_CLI\Formatter;
31 use WP_CLI_Command;
32
33 /**
34 * Class GroupCommand
35 *
36 * @package UserAccessManager\Command
37 */
38 class GroupCommand extends WP_CLI_Command
39 {
40 const FORMATTER_PREFIX = 'uam_user_groups';
41
42 /**
43 * @var array
44 */
45 private static $allowedAccessValues = ['group', 'all'];
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 */
68 public function __construct(
69 WordpressCli $wordpressCli,
70 UserGroupHandler $userGroupHandler,
71 UserGroupFactory $userGroupFactory
72 ) {
73 $this->wordpressCli = $wordpressCli;
74 $this->userGroupHandler = $userGroupHandler;
75 $this->userGroupFactory = $userGroupFactory;
76 parent::__construct();
77 }
78
79 /**
80 * Returns the formatter
81 * @param $assocArguments
82 * @return Formatter
83 */
84 private function getFormatter(&$assocArguments): Formatter
85 {
86 return $this->wordpressCli->createFormatter(
87 $assocArguments,
88 [
89 'ID',
90 'group_name',
91 'group_desc',
92 'read_access',
93 'write_access',
94 'roles',
95 'ip_range',
96 ],
97 self::FORMATTER_PREFIX
98 );
99 }
100
101 /**
102 * list groups
103 * ## OPTIONS
104 * [--format=<format>]
105 * : Accepted values: table, csv, json, count, ids. Default: table
106 * ## EXAMPLES
107 * wp uam groups list
108 * @subcommand ls
109 * @param array $arguments
110 * @param array $assocArguments
111 * @throws ExitException
112 * @throws UserGroupTypeException
113 * @throws Exception
114 */
115 public function ls(array $arguments, array $assocArguments)
116 {
117 if (count($arguments) > 0) {
118 $this->wordpressCli->error('No arguments excepted. Please use the format option.');
119 return;
120 }
121
122 $userGroups = $this->userGroupHandler->getUserGroups();
123
124 if (count($userGroups) <= 0) {
125 $this->wordpressCli->error('No groups defined yet!');
126 return;
127 }
128
129 $groups = [];
130
131 foreach ($userGroups as $userGroup) {
132 $groups[$userGroup->getId()] = [
133 'ID' => $userGroup->getId(),
134 'group_name' => $userGroup->getName(),
135 'group_desc' => $userGroup->getDescription(),
136 'read_access' => $userGroup->getReadAccess(),
137 'write_access' => $userGroup->getWriteAccess(),
138 'roles' => implode(
139 ',',
140 array_keys($userGroup->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE))
141 ),
142 'ip_range' => $userGroup->getIpRange() !== null ? $userGroup->getIpRange() : ''
143 ];
144 }
145
146 $formatter = $this->getFormatter($assocArguments);
147 $formatter->display_items($groups);
148 }
149
150 /**
151 * delete groups
152 * ## OPTIONS
153 * <group_id>
154 * : id of the group(s) to delete; accepts unlimited ids
155 * ## EXAMPLES
156 * wp uam groups del 3 5
157 * @subcommand del
158 * @param array $arguments
159 * @throws ExitException
160 * @throws UserGroupTypeException
161 */
162 public function del(array $arguments)
163 {
164 if (count($arguments) < 1) {
165 $this->wordpressCli->error('Expected: wp uam groups del \<id\> ...');
166 return;
167 }
168
169 foreach ($arguments as $userGroupId) {
170 if ($this->userGroupHandler->deleteUserGroup($userGroupId) === true) {
171 $this->wordpressCli->success("Successfully deleted group with id '{$userGroupId}'.");
172 } else {
173 $this->wordpressCli->error("Group id '{$userGroupId}' doesn't exists.");
174 }
175 }
176 }
177
178 /**
179 * Checks if the user group already exists.
180 * @param $userGroupName
181 * @return bool
182 * @throws ExitException
183 * @throws UserGroupTypeException
184 */
185 private function doesUserGroupExists($userGroupName): bool
186 {
187 $userGroups = $this->userGroupHandler->getUserGroups();
188
189 foreach ($userGroups as $userGroup) {
190 if ($userGroup->getName() === $userGroupName) {
191 $this->wordpressCli->error(
192 "Group with the same name '{$userGroupName}' already exists: {$userGroup->getId()}"
193 );
194
195 return false;
196 }
197 }
198
199 return true;
200 }
201
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
209 {
210 return (isset($arguments[$value]) === true) ? (string) $arguments[$value] : '';
211 }
212
213 /**
214 * Processes the access value.
215 * @param array $arguments
216 * @param string $value
217 * @param bool $porcelain
218 * @return string
219 */
220 private function getAccessValue(array $arguments, string $value, bool $porcelain): string
221 {
222 $accessValue = $this->getArgumentValue($arguments, $value);
223
224 if (in_array($accessValue, self::$allowedAccessValues) === false) {
225 if ($porcelain === true) {
226 $this->wordpressCli->line("setting {$value} to " . self::$allowedAccessValues[0]);
227 }
228
229 $accessValue = self::$allowedAccessValues[0];
230 }
231
232 return $accessValue;
233 }
234
235 /**
236 * Creates the user group.
237 * @param string $userGroupName
238 * @param array $assocArguments
239 * @return UserGroup
240 * @throws UserGroupTypeException
241 * @throws Exception
242 */
243 private function createUserGroup(string $userGroupName, array $assocArguments): UserGroup
244 {
245 $groupDescription = $this->getArgumentValue($assocArguments, 'desc');
246 $ipRange = $this->getArgumentValue($assocArguments, 'ip_range');
247 $porcelain = isset($assocArguments['porcelain']);
248 $readAccess = $this->getAccessValue($assocArguments, 'read_access', $porcelain);
249 $writeAccess = $this->getAccessValue($assocArguments, 'write_access', $porcelain);
250
251 $userGroup = $this->userGroupFactory->createUserGroup();
252 $userGroup->setName($userGroupName);
253 $userGroup->setDescription($groupDescription);
254 $userGroup->setIpRange($ipRange);
255 $userGroup->setReadAccess($readAccess);
256 $userGroup->setWriteAccess($writeAccess);
257
258 // add roles
259 if (isset($assocArguments['roles']) === true) {
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 }
267 }
268
269 $userGroup->save();
270
271 return $userGroup;
272 }
273
274 /**
275 * add group
276 * ## OPTIONS
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
285 * allowed fields and values are:
286 * desc="",
287 * read_access={group,all*},
288 * write_access={group,all*},
289 * ip_range="192.168.0.1-192.168.0.10;192.168.0.20-192.168.0.30"
290 * *=default
291 * ## EXAMPLES
292 * wp uam groups add fighters --read_access=all
293 * @param array $arguments
294 * @param array $assocArguments
295 * @throws ExitException
296 * @throws UserGroupTypeException
297 */
298 public function add(array $arguments, array $assocArguments)
299 {
300 if (isset($arguments[0]) === false) {
301 $this->wordpressCli->error("Please provide a group name.");
302 return;
303 }
304
305 $userGroupName = $arguments[0];
306
307 if ($this->doesUserGroupExists($userGroupName) === false) {
308 return;
309 }
310
311 $userGroup = $this->createUserGroup($userGroupName, $assocArguments);
312 $this->userGroupHandler->addUserGroup($userGroup);
313
314 if (isset($assocArguments['porcelain']) === true) {
315 $this->wordpressCli->line($userGroup->getId());
316 } else {
317 $this->wordpressCli->success("Added new group '{$userGroupName}' with id {$userGroup->getId()}.");
318 }
319 }
320 }
321