| 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\AccessHandler\AccessHandler; |
| 19 |
use UserAccessManager\UserGroup\UserGroup; |
| 20 |
use UserAccessManager\Wrapper\WordpressCli; |
| 21 |
|
| 22 |
/** |
| 23 |
* Class ObjectCommand |
| 24 |
* |
| 25 |
* @package UserAccessManager\Command |
| 26 |
*/ |
| 27 |
class ObjectCommand extends \WP_CLI_Command |
| 28 |
{ |
| 29 |
const ACTION_ADD = 'add'; |
| 30 |
const ACTION_UPDATE = 'update'; |
| 31 |
const ACTION_REMOVE = 'remove'; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var WordpressCli |
| 35 |
*/ |
| 36 |
private $wordpressCli; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var AccessHandler |
| 40 |
*/ |
| 41 |
private $accessHandler; |
| 42 |
|
| 43 |
/** |
| 44 |
* ObjectCommand constructor. |
| 45 |
* |
| 46 |
* @param WordpressCli $wordpressCli |
| 47 |
* @param AccessHandler $accessHandler |
| 48 |
*/ |
| 49 |
public function __construct(WordpressCli $wordpressCli, AccessHandler $accessHandler) |
| 50 |
{ |
| 51 |
$this->wordpressCli = $wordpressCli; |
| 52 |
$this->accessHandler = $accessHandler; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* update groups for an object |
| 57 |
* |
| 58 |
* ## OPTIONS |
| 59 |
* |
| 60 |
* <operation> |
| 61 |
* : 'add', 'remove' or 'update' |
| 62 |
* |
| 63 |
* <object_type> |
| 64 |
* : 'page', 'post', 'user', 'role', 'category' or any other term type |
| 65 |
* |
| 66 |
* <object_id> |
| 67 |
* : the id of the object (string for role) |
| 68 |
* |
| 69 |
* <user_groups> |
| 70 |
* : comma separated list of group names or ids to add, remove of update to for the object |
| 71 |
* |
| 72 |
* ## EXAMPLES |
| 73 |
* |
| 74 |
* wp uam object add user 1 fighters,losers |
| 75 |
* wp uam object remove role author fighters |
| 76 |
* wp uam object update category 5 controller |
| 77 |
* |
| 78 |
* @param array $arguments |
| 79 |
* @param array $assocArguments |
| 80 |
*/ |
| 81 |
public function __invoke(array $arguments, array $assocArguments) |
| 82 |
{ |
| 83 |
if (count($arguments) < 4) { |
| 84 |
$this->wordpressCli->error('<operation>, <object_type>, <object_id> and <user_groups> are required'); |
| 85 |
return; |
| 86 |
} |
| 87 |
|
| 88 |
$operation = $arguments[0]; |
| 89 |
|
| 90 |
// check that operation is valid |
| 91 |
if ($operation !== self::ACTION_ADD |
| 92 |
&& $operation !== self::ACTION_UPDATE |
| 93 |
&& $operation !== self::ACTION_REMOVE |
| 94 |
) { |
| 95 |
$this->wordpressCli->error("Operation is not valid: {$operation}"); |
| 96 |
return; |
| 97 |
} |
| 98 |
|
| 99 |
$objectType = $arguments[1]; |
| 100 |
$objectId = $arguments[2]; |
| 101 |
|
| 102 |
// convert the string to and associative array of index and group |
| 103 |
$addUserGroups = []; |
| 104 |
$userGroups = $this->accessHandler->getUserGroups(); |
| 105 |
|
| 106 |
$userGroupNames = array_map( |
| 107 |
function (UserGroup $userGroup) { |
| 108 |
return $userGroup->getName(); |
| 109 |
}, |
| 110 |
$userGroups |
| 111 |
); |
| 112 |
|
| 113 |
foreach ($userGroups as $userGroup) { |
| 114 |
$userGroupNames[$userGroup->getId()] = $userGroup->getName(); |
| 115 |
} |
| 116 |
|
| 117 |
$namesMap = array_flip($userGroupNames); |
| 118 |
|
| 119 |
// groups passes |
| 120 |
$userGroupsArgument = $arguments[3]; |
| 121 |
$userGroupIds = array_unique(explode(',', $userGroupsArgument)); |
| 122 |
|
| 123 |
// find the UserGroup object for the ids or strings given on the commandline |
| 124 |
foreach ($userGroupIds as $identifier) { |
| 125 |
$userGroupId = isset($namesMap[$identifier]) ? $namesMap[$identifier] : $identifier; |
| 126 |
|
| 127 |
if (isset($userGroups[$userGroupId]) === true) { |
| 128 |
$addUserGroups[$userGroupId] = $userGroups[$userGroupId]; |
| 129 |
} else { |
| 130 |
$type = (is_numeric($identifier) === true) ? 'id' : 'name'; |
| 131 |
$this->wordpressCli->error("There is no group with the {$type}: {$identifier}"); |
| 132 |
return; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
$removeUserGroups = ($operation === self::ACTION_UPDATE) ? |
| 137 |
$this->accessHandler->getUserGroupsForObject($objectType, $objectId) : []; |
| 138 |
|
| 139 |
if ($operation === self::ACTION_REMOVE) { |
| 140 |
$removeUserGroups = $addUserGroups; |
| 141 |
$addUserGroups = []; |
| 142 |
} |
| 143 |
|
| 144 |
foreach ($userGroups as $groupId => $uamUserGroup) { |
| 145 |
if (isset($removeUserGroups[$groupId]) === true) { |
| 146 |
$uamUserGroup->removeObject($objectType, $objectId); |
| 147 |
} |
| 148 |
|
| 149 |
if (isset($addUserGroups[$groupId]) === true) { |
| 150 |
$uamUserGroup->addObject($objectType, $objectId); |
| 151 |
} |
| 152 |
|
| 153 |
$uamUserGroup->save(); |
| 154 |
} |
| 155 |
|
| 156 |
if ($operation === self::ACTION_ADD) { |
| 157 |
$this->wordpressCli->success( |
| 158 |
"Groups {$userGroupsArgument} successfully added to {$objectType} {$objectId}" |
| 159 |
); |
| 160 |
} elseif ($operation === self::ACTION_UPDATE) { |
| 161 |
$this->wordpressCli->success( |
| 162 |
"Successfully updated {$objectType} {$objectId} with groups {$userGroupsArgument}" |
| 163 |
); |
| 164 |
} elseif ($operation === self::ACTION_REMOVE) { |
| 165 |
$this->wordpressCli->success( |
| 166 |
"Successfully removed groups: {$userGroupsArgument} from {$objectType} {$objectId}" |
| 167 |
); |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
|