PluginProbe
User Access Manager / 2.1.7
User Access Manager v2.1.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 / ObjectCommand.php

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

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