PluginProbe
User Access Manager / 1.2.13
User Access Manager v1.2.13
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 / includes / wp-cli.php

wp-cli.php in User Access Manager 1.2.13, at includes/wp-cli.php

378 lines 11.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * wp-cli.php
4 *
5 * The wp cli file.
6 *
7 * PHP versions 5
8 *
9 * @category UserAccessManager
10 * @package UserAccessManager
11 * @author Nils Woetzel nils.woetzel@h-its.org
12 * @author Alexander Schneider <alexanderschneider85@googlemail.com>
13 * @copyright 2008-2016 Alexander Schneider
14 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
15 * @version SVN: $Id$
16 * @link http://wordpress.org/extend/plugins/user-access-manager/
17 */
18
19 if (!defined('ABSPATH')) {
20 die();
21 }
22
23 if (!defined('WP_CLI') || !WP_CLI) {
24 die();
25 }
26
27 /**
28 * The group command class.
29 *
30 * @category UserAccessManager
31 * @package UserAccessManager
32 * @author Alexander Schneider <alexanderschneider85@gmail.com>
33 * @author Nils Woetzel nils.woetzel@h-its.org
34 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
35 * @link http://wordpress.org/extend/plugins/user-access-manager/
36 */
37 class Groups_Command extends \WP_CLI\CommandWithDBObject
38 {
39 private static $aAllowedAccessValues = array('group', 'all');
40
41 protected $sObjectType = 'uam_accessgroup';
42 protected $aObjectFields = array(
43 'ID',
44 'groupname',
45 'groupdesc',
46 'read_access',
47 'write_access',
48 'roles',
49 'ip_range',
50 );
51
52 private $oUserAccessManager;
53
54 /**
55 * Groups_Command constructor
56 */
57 public function __construct()
58 {
59 $this->oUserAccessManager = new UserAccessManager();
60 }
61
62 /**
63 * list groups
64 *
65 * ## OPTIONS
66 *
67 * [--format=<format>]
68 * : Accepted values: table, csv, json, count, ids. Default: table
69 *
70 * ## EXAMPLES
71 *
72 * wp uam groups list
73 *
74 * @subcommand list
75 */
76 public function list_($_, $assoc_args)
77 {
78 $aUamUserGroups = $this->oUserAccessManager->getAccessHandler()->getUserGroups();
79
80 if (!isset($aUamUserGroups)) {
81 WP_CLI:
82 error("no groups defined yet!");
83 }
84
85 $aGroups = array();
86
87 foreach ($aUamUserGroups as $oUamUserGroup) {
88 $aGroup = array(
89 'ID' => $oUamUserGroup->getId(),
90 'groupname' => $oUamUserGroup->getGroupName(),
91 'groupdesc' => $oUamUserGroup->getGroupDesc(),
92 'read_access' => $oUamUserGroup->getReadAccess(),
93 'write_access' => $oUamUserGroup->getWriteAccess(),
94 'roles' => implode(',', array_keys($oUamUserGroup->getObjectsFromType(UserAccessManager::ROLE_OBJECT_TYPE))),
95 'ip_range' => $oUamUserGroup->getIpRange() === null ? '' : $oUamUserGroup->getIpRange()
96 );
97
98 // add current group
99 $aGroups[] = $aGroup;
100 }
101
102 $oFormatter = $this->getFormatter($assoc_args);
103 $oFormatter->display_items($aGroups);
104 }
105
106 /**
107 * delete groups
108 *
109 * ## OPTIONS
110 * <group_id>
111 * : id of the group(s) to delete; accepts unlimited ids
112 *
113 * ## EXAMPLES
114 *
115 * wp uam groups del 3 5
116 *
117 * @subcommand del
118 */
119 public function del($_, $assoc_args)
120 {
121 if (count($_) < 1) {
122 WP_CLI::error('Expected: wp uam groups del <id> ..');
123 }
124
125 foreach ($_ as $delId) {
126 if ($this->oUserAccessManager->getAccessHandler()->getUserGroups($delId) == null) {
127 WP_CLI::error('no group with this id: ' . $delId);
128 }
129 $this->oUserAccessManager->getAccessHandler()->deleteUserGroup($delId);
130 }
131 WP_CLI::success('successfully deleted groups: ' . implode(' ', $_));
132 }
133
134 /**
135 * Returns the formatter
136 *
137 * @param $assoc_args
138 *
139 * @return \WP_CLI\Formatter
140 */
141 protected function getFormatter(&$assoc_args)
142 {
143 return new \WP_CLI\Formatter($assoc_args, $this->aObjectFields, $this->sObjectType);
144 }
145
146 /**
147 * add group
148 *
149 * ## OPTIONS
150 * <groupname>
151 * : the name of the new group
152 *
153 * [--porcelain]
154 * : Output just the new post id.
155 *
156 * [--roles=<list>]
157 * : comma seperated list of group associated roles
158 *
159 * [--<field>=<value>]
160 * : Associative args for new UamUserGroup object
161 * allowed fields and values are: groupdesc="", read_access={group,all*}, write_access={group,all*}, ip_range="192.168.0.1-192.168.0.10;192.168.0.20-192.168.0.30"
162 * *=default
163 *
164 * ## EXAMPLES
165 *
166 * wp uam groups add fighters --read_access=all
167 *
168 */
169 public function add($_, $assoc_args)
170 {
171 $blPorcelain = isset($assoc_args['porcelain']);
172 $sGroupName = $_[0];
173 $aUamUserGroups = $this->oUserAccessManager->getAccessHandler()->getUserGroups();
174
175 foreach ($aUamUserGroups as $oUamUserGroup) {
176 if ($oUamUserGroup->getGroupName() == $sGroupName) {
177 WP_CLI::error('group with the same name already exists: ' . $oUamUserGroup->getId());
178 }
179 }
180
181 $sGroupDescription = $assoc_args['groupdesc'];
182 $read_access = $assoc_args['read_access'];
183 $write_access = $assoc_args['write_access'];
184 $ip_range = $assoc_args['ip_range'];
185
186 $oUamUserGroup = new UamUserGroup($this->oUserAccessManager->getAccessHandler(), null);
187
188 if (!in_array($read_access, self::$aAllowedAccessValues)) {
189 if (!$blPorcelain) {
190 WP_CLI::line('setting read_access to ' . self::$aAllowedAccessValues[0]);
191 }
192 $read_access = self::$aAllowedAccessValues[0];
193 }
194
195 if (!in_array($write_access, self::$aAllowedAccessValues)) {
196 if (!$blPorcelain) {
197 WP_CLI::line('setting write_access to ' . self::$aAllowedAccessValues[0]);
198 }
199 $write_access = self::$aAllowedAccessValues[0];
200 }
201
202 $oUamUserGroup->setGroupName($sGroupName);
203 $oUamUserGroup->setGroupDesc($sGroupDescription);
204 $oUamUserGroup->setReadAccess($read_access);
205 $oUamUserGroup->setWriteAccess($write_access);
206 $oUamUserGroup->setIpRange($ip_range);
207
208 // add roles
209 if (isset($assoc_args['roles'])) {
210 $roles = explode(',', $assoc_args['roles']);
211
212 $oUamUserGroup->unsetObjects(UserAccessManager::ROLE_OBJECT_TYPE, true);
213
214 foreach ($roles as $role) {
215 $oUamUserGroup->addObject(UserAccessManager::ROLE_OBJECT_TYPE, $role);
216 }
217 }
218
219 $oUamUserGroup->save();
220
221 $this->oUserAccessManager->getAccessHandler()->addUserGroup($oUamUserGroup);
222
223 if ($blPorcelain) {
224 WP_CLI::line($oUamUserGroup->getId());
225 } else {
226 WP_CLI::success('added new group ' . $sGroupName . ' with id ' . $oUamUserGroup->getId());
227 }
228 }
229 }
230
231 /**
232 * The object command class.
233 *
234 * @category UserAccessManager
235 * @package UserAccessManager
236 * @author Alexander Schneider <alexanderschneider85@gmail.com>
237 * @author Nils Woetzel nils.woetzel@h-its.org
238 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU General Public License, version 2
239 * @link http://wordpress.org/extend/plugins/user-access-manager/
240 */
241 class Objects_Command extends WP_CLI_Command
242 {
243
244 private $oUserAccessManager;
245
246 /**
247 * Objects_Command constructor.
248 */
249 public function __construct()
250 {
251 $this->oUserAccessManager = new UserAccessManager();
252 }
253
254 /**
255 * update groups for an object
256 *
257 * ## OPTIONS
258 *
259 * <operation>
260 * : 'add', 'remove' or 'update'
261 *
262 * <object_type>
263 * : 'page', 'post', 'user', 'role', 'category' or any other term type
264 *
265 * <object_id>
266 * : the id of the object (string for role)
267 *
268 * [--groups=<list>]
269 * : comma seperated list of group names or ids to add,remove of upate to for the object
270 *
271 * ## EXAMPLES
272 *
273 * wp uam object add user 1 --groups=fighters,loosers
274 * wp uam object remove role author --groups=figthers
275 * wp uam object update category 5 --groups=controller
276 *
277 */
278 public function __invoke($_, $assoc_args)
279 {
280 $sOperation = $_[0];
281 $sObjectType = $_[1];
282 $sObjectId = $_[2];
283
284 // check that operation is valid
285 switch ($sOperation) {
286 case "add":
287 break;
288 case "update":
289 break;
290 case "remove":
291 break;
292 default:
293 WP_CLI::error("operation is not valid: " . $sOperation);
294 }
295
296 $oUamAccessHandler = $this->oUserAccessManager->getAccessHandler();
297
298 // groups passes
299 $aGroups = array_unique(explode(',', $assoc_args['groups']));
300
301 // convert the string to and associative array of index and group
302 $aAddUserGroups = array();
303 $aUamUserGroups = $oUamAccessHandler->getUserGroups();
304
305 // find the UserGroup object for the ids or strings given on the commandline
306 foreach ($aGroups as $sGroup) {
307 if (is_numeric($sGroup)) {
308 $oUamUserGroup = $oUamAccessHandler->getUserGroups($sGroup);
309
310 if (!$oUamUserGroup) {
311 WP_CLI::error("there is no group with the id: " . $sGroup);
312 }
313
314 $aAddUserGroups[$sGroup] = $oUamUserGroup;
315 } else {
316 $blFound = false;
317
318 foreach ($aUamUserGroups as $oUamUserGroup) {
319 if ($oUamUserGroup->getGroupName() == $sGroup) {
320 $aAddUserGroups[$oUamUserGroup->getId()] = $oUamUserGroup;
321 $blFound = true;
322 break;
323 }
324 }
325
326 if (!$blFound) {
327 WP_CLI::error("there is no group with the name: " . $sGroup);
328 }
329 }
330 }
331
332 $aRemoveUserGroups = $oUamAccessHandler->getUserGroupsForObject($sObjectType, $sObjectId);
333 $blRemoveOldAssignments = true;
334
335 switch ($sOperation) {
336 case "add":
337 $blRemoveOldAssignments = false;
338 break;
339 case "update":
340 break;
341 case "remove":
342 $aRemoveUserGroups = $aAddUserGroups;
343 $aAddUserGroups = array();
344 break;
345 default:
346 WP_CLI::error("operation is not valid: " . $sOperation);
347 }
348
349 foreach ($aUamUserGroups as $sGroupId => $oUamUserGroup) {
350 if (isset($aRemoveUserGroups[$sGroupId])) {
351 $oUamUserGroup->removeObject($sObjectType, $sObjectId);
352 }
353
354 if (isset($aAddUserGroups[$sGroupId])) {
355 $oUamUserGroup->addObject($sObjectType, $sObjectId);
356 }
357
358 $oUamUserGroup->save($blRemoveOldAssignments);
359 }
360
361 switch ($sOperation) {
362 case "add":
363 WP_CLI::success(implode(" ", array("groups:", $assoc_args['groups'], "sucessfully added to", $sObjectType, $sObjectId)));
364 break;
365 case "update":
366 WP_CLI::success(implode(" ", array("sucessfully updated", $sObjectType, $sObjectId, "with groups:", $assoc_args['groups'])));
367 break;
368 case "remove":
369 WP_CLI::success(implode(" ", array("sucessfully removed groups:", $assoc_args['groups'], "from", $sObjectType, $sObjectId)));
370 break;
371 default:
372 }
373 }
374 }
375
376 // add the command
377 WP_CLI::add_command('uam groups', 'Groups_Command');
378 WP_CLI::add_command('uam objects', 'Objects_Command');