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

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