PluginProbe
User Access Manager / 2.3.11
User Access Manager v2.3.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
user-access-manager / src / Command / GroupCommand.php

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

252 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 declare(strict_types=1);
4
5 namespace UserAccessManager\Command;
6
7 use Exception;
8 use UserAccessManager\Object\ObjectHandler;
9 use UserAccessManager\UserGroup\UserGroup;
10 use UserAccessManager\UserGroup\UserGroupFactory;
11 use UserAccessManager\UserGroup\UserGroupHandler;
12 use UserAccessManager\UserGroup\UserGroupTypeException;
13 use UserAccessManager\Wrapper\WordpressCli;
14 use WP_CLI\ExitException;
15 use WP_CLI\Formatter;
16 use WP_CLI_Command;
17
18 class GroupCommand extends WP_CLI_Command
19 {
20 const FORMATTER_PREFIX = 'uam_user_groups';
21
22 private static array $allowedAccessValues = ['group', 'all'];
23
24 public function __construct(
25 private WordpressCli $wordpressCli,
26 private UserGroupHandler $userGroupHandler,
27 private UserGroupFactory $userGroupFactory
28 ) {
29 }
30
31 /**
32 * Returns the formatter
33 * @param $assocArguments
34 * @return Formatter
35 */
36 private function getFormatter(&$assocArguments): Formatter
37 {
38 return $this->wordpressCli->createFormatter(
39 $assocArguments,
40 [
41 'ID',
42 'group_name',
43 'group_desc',
44 'read_access',
45 'write_access',
46 'roles',
47 'ip_range',
48 ],
49 self::FORMATTER_PREFIX
50 );
51 }
52
53 /**
54 * List groups
55 * ## OPTIONS
56 * [--format=<format>]: Accepted values: table, csv, JSON, count, ids. Default: table
57 * ## EXAMPLES
58 * wp uam groups list
59 * @subcommand ls
60 * @throws ExitException
61 * @throws UserGroupTypeException
62 * @throws Exception
63 */
64 public function ls(array $arguments, array $assocArguments): void
65 {
66 if (count($arguments) > 0) {
67 $this->wordpressCli->error('No arguments excepted. Please use the format option.');
68 return;
69 }
70
71 $userGroups = $this->userGroupHandler->getUserGroups();
72
73 if (count($userGroups) <= 0) {
74 $this->wordpressCli->error('No groups defined yet!');
75 return;
76 }
77
78 $groups = [];
79
80 foreach ($userGroups as $userGroup) {
81 $groups[$userGroup->getId()] = [
82 'ID' => $userGroup->getId(),
83 'group_name' => $userGroup->getName(),
84 'group_desc' => $userGroup->getDescription(),
85 'read_access' => $userGroup->getReadAccess(),
86 'write_access' => $userGroup->getWriteAccess(),
87 'roles' => implode(
88 ',',
89 array_keys($userGroup->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE))
90 ),
91 'ip_range' => $userGroup->getIpRange() !== null ? $userGroup->getIpRange() : ''
92 ];
93 }
94
95 $formatter = $this->getFormatter($assocArguments);
96 $formatter->display_items($groups);
97 }
98
99 /**
100 * delete groups
101 * ## OPTIONS
102 * <group_id>: id of the group(s) to delete; accepts unlimited ids
103 * ## EXAMPLES
104 * wp uam groups del 3 5
105 * @subcommand del
106 * @throws ExitException
107 * @throws UserGroupTypeException
108 */
109 public function del(array $arguments): void
110 {
111 if (count($arguments) < 1) {
112 $this->wordpressCli->error('Expected: wp uam groups del \<id\> ...');
113 return;
114 }
115
116 foreach ($arguments as $userGroupId) {
117 if ($this->userGroupHandler->deleteUserGroup($userGroupId) === true) {
118 $this->wordpressCli->success("Successfully deleted group with id '$userGroupId'.");
119 } else {
120 $this->wordpressCli->error("Group id '$userGroupId' doesn't exists.");
121 }
122 }
123 }
124
125 /**
126 * Checks if the user group already exists.
127 * @param mixed $userGroupName
128 * @return bool
129 * @throws ExitException
130 * @throws UserGroupTypeException
131 */
132 private function doesUserGroupExists(mixed $userGroupName): bool
133 {
134 $userGroups = $this->userGroupHandler->getUserGroups();
135
136 foreach ($userGroups as $userGroup) {
137 if ($userGroup->getName() === $userGroupName) {
138 $this->wordpressCli->error(
139 "Group with the same name '$userGroupName' already exists: {$userGroup->getId()}"
140 );
141
142 return false;
143 }
144 }
145
146 return true;
147 }
148
149 /**
150 * Returns the argument value.
151 */
152 private function getArgumentValue(array $arguments, string $value): string
153 {
154 return (isset($arguments[$value]) === true) ? (string) $arguments[$value] : '';
155 }
156
157 /**
158 * Processes the access value.
159 */
160 private function getAccessValue(array $arguments, string $value, bool $porcelain): string
161 {
162 $accessValue = $this->getArgumentValue($arguments, $value);
163
164 if (in_array($accessValue, self::$allowedAccessValues) === false) {
165 if ($porcelain === true) {
166 $this->wordpressCli->line("setting $value to " . self::$allowedAccessValues[0]);
167 }
168
169 $accessValue = self::$allowedAccessValues[0];
170 }
171
172 return $accessValue;
173 }
174
175 /**
176 * Creates the user group.
177 * @throws UserGroupTypeException
178 * @throws Exception
179 */
180 private function createUserGroup(string $userGroupName, array $assocArguments): UserGroup
181 {
182 $groupDescription = $this->getArgumentValue($assocArguments, 'desc');
183 $ipRange = $this->getArgumentValue($assocArguments, 'ip_range');
184 $porcelain = isset($assocArguments['porcelain']);
185 $readAccess = $this->getAccessValue($assocArguments, 'read_access', $porcelain);
186 $writeAccess = $this->getAccessValue($assocArguments, 'write_access', $porcelain);
187
188 $userGroup = $this->userGroupFactory->createUserGroup();
189 $userGroup->setName($userGroupName);
190 $userGroup->setDescription($groupDescription);
191 $userGroup->setIpRange($ipRange);
192 $userGroup->setReadAccess($readAccess);
193 $userGroup->setWriteAccess($writeAccess);
194
195 // add roles
196 if (isset($assocArguments['roles']) === true) {
197 $roles = explode(',', $assocArguments['roles']);
198
199 $userGroup->removeObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE);
200
201 foreach ($roles as $role) {
202 $userGroup->addObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, trim($role));
203 }
204 }
205
206 $userGroup->save();
207
208 return $userGroup;
209 }
210
211 /**
212 * add group
213 * ## OPTIONS
214 * <group_name>: the name of the new group
215 * [--porcelain]: Output just the new post id.
216 * [--roles=<list>]: comma separated list of group associated roles
217 * [--<field>=<value>]: Associative args for a new UamUserGroup object
218 * allowed fields and values are:
219 * desc="",
220 * read_access={group,all*},
221 * write_access={group,all*},
222 * ip_range="192.168.0.1-192.168.0.10;192.168.0.20-192.168.0.30"
223 * *=default
224 * ## EXAMPLES
225 * wp uam groups add fighters --read_access=all
226 * @throws ExitException
227 * @throws UserGroupTypeException
228 */
229 public function add(array $arguments, array $assocArguments): void
230 {
231 if (isset($arguments[0]) === false) {
232 $this->wordpressCli->error("Please provide a group name.");
233 return;
234 }
235
236 $userGroupName = $arguments[0];
237
238 if ($this->doesUserGroupExists($userGroupName) === false) {
239 return;
240 }
241
242 $userGroup = $this->createUserGroup($userGroupName, $assocArguments);
243 $this->userGroupHandler->addUserGroup($userGroup);
244
245 if (isset($assocArguments['porcelain']) === true) {
246 $this->wordpressCli->line($userGroup->getId());
247 } else {
248 $this->wordpressCli->success("Added new group '$userGroupName' with id {$userGroup->getId()}.");
249 }
250 }
251 }
252