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

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