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

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