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