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 / ObjectCommand.php

ObjectCommand.php in User Access Manager 2.1.12, at src/Command/ObjectCommand.php

229 lines 6.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * ObjectsCommand.php
4 *
5 * The ObjectCommand 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\UserGroup\AbstractUserGroup;
19 use UserAccessManager\UserGroup\UserGroup;
20 use UserAccessManager\UserGroup\UserGroupHandler;
21 use UserAccessManager\Wrapper\WordpressCli;
22 use WP_CLI\ExitException;
23
24 /**
25 * Class ObjectCommand
26 *
27 * @package UserAccessManager\Command
28 */
29 class ObjectCommand extends \WP_CLI_Command
30 {
31 const ACTION_ADD = 'add';
32 const ACTION_UPDATE = 'update';
33 const ACTION_REMOVE = 'remove';
34
35 /**
36 * @var WordpressCli
37 */
38 private $wordpressCli;
39
40 /**
41 * @var UserGroupHandler
42 */
43 private $userGroupHandler;
44
45 /**
46 * ObjectCommand constructor.
47 *
48 * @param WordpressCli $wordpressCli
49 * @param UserGroupHandler $userGroupHandler
50 */
51 public function __construct(WordpressCli $wordpressCli, UserGroupHandler $userGroupHandler)
52 {
53 $this->wordpressCli = $wordpressCli;
54 $this->userGroupHandler = $userGroupHandler;
55 }
56
57 /**
58 * Converts the string to and associative array of index and group
59 *
60 * @param AbstractUserGroup[] $userGroups
61 *
62 * @return array
63 */
64 private function getUserGroupNameMap(array $userGroups)
65 {
66 $userGroupNames = array_map(
67 function (UserGroup $userGroup) {
68 return $userGroup->getName();
69 },
70 $userGroups
71 );
72
73 foreach ($userGroups as $userGroup) {
74 $userGroupNames[$userGroup->getId()] = $userGroup->getName();
75 }
76
77 $userGroupNames = array_flip($userGroupNames);
78 return (is_array($userGroupNames) === true) ? $userGroupNames : [];
79 }
80
81 /**
82 * Returns the user group id and the type of the id.
83 *
84 * @param array $namesMap
85 * @param string $identifier
86 * @param string $type
87 *
88 * @return mixed
89 */
90 private function getUserGroupIdAndType(array $namesMap, $identifier, &$type)
91 {
92 $type = (is_numeric($identifier) === true) ? 'id' : 'name';
93 return isset($namesMap[$identifier]) ? $namesMap[$identifier] : $identifier;
94 }
95
96 /**
97 * Returns the add and remove user groups by reference.
98 *
99 * @param string $operation
100 * @param string $objectType
101 * @param string $objectId
102 * @param string $userGroupsArgument
103 * @param AbstractUserGroup[] $userGroups
104 * @param array $addUserGroups
105 * @param array $removeUserGroups
106 *
107 * @return bool
108 *
109 * @throws ExitException
110 */
111 private function getAddRemoveUserGroups(
112 $operation,
113 $objectType,
114 $objectId,
115 $userGroupsArgument,
116 array $userGroups,
117 &$addUserGroups,
118 &$removeUserGroups
119 ) {
120 $addUserGroups = [];
121 $userGroupIds = array_unique(explode(',', $userGroupsArgument));
122 $namesMap = $this->getUserGroupNameMap($userGroups);
123
124 // find the UserGroup object for the ids or strings given on the commandline
125 foreach ($userGroupIds as $identifier) {
126 $userGroupId = $this->getUserGroupIdAndType($namesMap, $identifier, $type);
127
128 if (isset($userGroups[$userGroupId]) !== true) {
129 $this->wordpressCli->error("There is no group with the {$type}: {$identifier}");
130 return false;
131 }
132
133 $addUserGroups[$userGroupId] = $userGroups[$userGroupId];
134 }
135
136 $removeUserGroups = ($operation === self::ACTION_UPDATE) ?
137 $this->userGroupHandler->getUserGroupsForObject($objectType, $objectId) : [];
138
139 if ($operation === self::ACTION_REMOVE) {
140 $removeUserGroups = $addUserGroups;
141 $addUserGroups = [];
142 }
143
144 return true;
145 }
146
147 /**
148 * update groups for an object
149 *
150 * ## OPTIONS
151 *
152 * <operation>
153 * : 'add', 'remove' or 'update'
154 *
155 * <object_type>
156 * : 'page', 'post', 'user', 'role', 'category' or any other term type
157 *
158 * <object_id>
159 * : the id of the object (string for role)
160 *
161 * <user_groups>
162 * : comma separated list of group names or ids to add, remove of update to for the object
163 *
164 * ## EXAMPLES
165 *
166 * wp uam object add user 1 fighters,losers
167 * wp uam object remove role author fighters
168 * wp uam object update category 5 controller
169 *
170 * @param array $arguments
171 * @param array $assocArguments
172 *
173 * @throws ExitException
174 */
175 public function __invoke(array $arguments, array $assocArguments)
176 {
177 if (count($arguments) < 4) {
178 $this->wordpressCli->error('<operation>, <object_type>, <object_id> and <user_groups> are required');
179 return;
180 }
181
182 $operation = $arguments[0];
183 $messages = [
184 self::ACTION_ADD => 'Groups %1$s successfully added to %2$s %3$s',
185 self::ACTION_UPDATE => 'Successfully updated %2$s %3$s with groups %1$s',
186 self::ACTION_REMOVE => 'Successfully removed groups: %1$s from %2$s %3$s'
187 ];
188
189 // check that operation is valid
190 if (isset($messages[$operation]) === false) {
191 $this->wordpressCli->error("Operation is not valid: {$operation}");
192 return;
193 }
194
195 $objectType = $arguments[1];
196 $objectId = $arguments[2];
197 $userGroupsArgument = $arguments[3];
198 $userGroups = $this->userGroupHandler->getUserGroups();
199
200 $success = $this->getAddRemoveUserGroups(
201 $operation,
202 $objectType,
203 $objectId,
204 $userGroupsArgument,
205 $userGroups,
206 $addUserGroups,
207 $removeUserGroups
208 );
209
210 if ($success === false) {
211 return;
212 }
213
214 foreach ($userGroups as $groupId => $userGroup) {
215 if (isset($removeUserGroups[$groupId]) === true) {
216 $userGroup->removeObject($objectType, $objectId);
217 }
218
219 if (isset($addUserGroups[$groupId]) === true) {
220 $userGroup->addObject($objectType, $objectId);
221 }
222
223 $userGroup->save();
224 }
225
226 $this->wordpressCli->success(sprintf($messages[$operation], $userGroupsArgument, $objectType, $objectId));
227 }
228 }
229