| 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\Access\AccessHandler; |
| 19 |
use UserAccessManager\UserGroup\AbstractUserGroup; |
| 20 |
use UserAccessManager\UserGroup\UserGroup; |
| 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 AccessHandler |
| 41 |
*/ |
| 42 |
private $accessHandler; |
| 43 |
|
| 44 |
/** |
| 45 |
* ObjectCommand constructor. |
| 46 |
* |
| 47 |
* @param WordpressCli $wordpressCli |
| 48 |
* @param AccessHandler $accessHandler |
| 49 |
*/ |
| 50 |
public function __construct(WordpressCli $wordpressCli, AccessHandler $accessHandler) |
| 51 |
{ |
| 52 |
$this->wordpressCli = $wordpressCli; |
| 53 |
$this->accessHandler = $accessHandler; |
| 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->accessHandler->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->accessHandler->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 => $uamUserGroup) { |
| 210 |
if (isset($removeUserGroups[$groupId]) === true) { |
| 211 |
$uamUserGroup->removeObject($objectType, $objectId); |
| 212 |
} |
| 213 |
|
| 214 |
if (isset($addUserGroups[$groupId]) === true) { |
| 215 |
$uamUserGroup->addObject($objectType, $objectId); |
| 216 |
} |
| 217 |
|
| 218 |
$uamUserGroup->save(); |
| 219 |
} |
| 220 |
|
| 221 |
$this->wordpressCli->success(sprintf($messages[$operation], $userGroupsArgument, $objectType, $objectId)); |
| 222 |
} |
| 223 |
} |
| 224 |
|