user-access-manager icon indicating copy to clipboard operation
user-access-manager copied to clipboard

Export Users to CSV by User Group

Open loretoparisi opened this issue 7 years ago • 1 comments

First, thank you very much for this useful plugin! In my web site I'm managing customers with multiple roles (by subscription) that belong to one or more user groups e.g. a certain customer that has a specific role can be in more than one group. When using a Users export plugin (like Export Users to CSV, etc.) normally I can export only by "role", therefore I will miss the User Groups concept, then my export will lack important data i.e. the one to many association between a user and the groups who belongs to. Any way then to export users by their group and the role affiliation?

I'm aware of a public api for the $userGroup instance from a different plugin. So supposed I want to retrieve the groups for a user

global $userAccessManager;

if (isset($userAccessManager)) {
    $userGroupHandler = $userAccessManager->getUserGroupHandler();
    $userGroupsForUser = $userGroupHandler->getUserGroupsForObject(
        \UserAccessManager\Object\ObjectHandler::GENERAL_USER_OBJECT_TYPE,
        $userId
    );
    //Given that you already have the user group.
    $usersInGroup = $userGroupsForUser->getFullUsers();
}

This should give me back the list of the groups for each user, but at this point I'm not sure how to go on with that, while I have seen that the GroupCommand class has a public interface ls to list in several format -format=csv the users with all the information I will need here:

foreach ($userGroups as $userGroup) {
            $groups[$userGroup->getId()] = [
                'ID' => $userGroup->getId(),
                'group_name' => $userGroup->getName(),
                'group_desc' => $userGroup->getDescription(),
                'read_access' => $userGroup->getReadAccess(),
                'write_access' => $userGroup->getWriteAccess(),
                'roles' => implode(
                    ',',
                    array_keys($userGroup->getAssignedObjectsByType(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE))
                ),
                'ip_range' => $userGroup->getIpRange() !== null ? $userGroup->getIpRange() : ''
            ];
        }

It would be possibile to access that public api from another plugin? Thanks a lot!

loretoparisi avatar Feb 22 '18 22:02 loretoparisi

@GM-Alex so far I did some advance in my plugin creating these two function, in order to have this functionality in the WP CLI as well:

private function userAccessGroups() {
		global $userAccessManager;
		//use UserAccessManager\ObjectMembership\UserMembershipHandler;
		
		// get a reference to the UserGroupHandler
		$userGroupHandler = $userAccessManager->getUserGroupHandler();
		$objectHandler = $userAccessManager->getObjectHandler();
		// get a reference to the UserMembershipHandler
		$objectType = \UserAccessManager\Object\ObjectHandler::GENERAL_USER_OBJECT_TYPE;
		$userMembershipHandler = $objectHandler->getObjectMembershipHandler($objectType);

		// get all user groups
		$userGroups = $userGroupHandler->getUserGroups();
		if (count($userGroups) > 0) { // has user groups
			foreach ($userGroups as $userGroup) {
				// get all users in a group 
				$users = $userMembershipHandler->getFullObjects($userGroup, false);
				var_dump( $users );
				if (count($users) > 0) { // has users
					//@TODO:...
				}
			}
		}
	}

and for WP_CLI

	private function addWordpressCLI() {
		global $userAccessManager;
		// get a reference to the UserGroupHandler
		$userGroupHandler = $userAccessManager->getUserGroupHandler();
		$objectHandler = $userAccessManager->getObjectHandler();
		//Add the cli interface to the known commands
		if (defined('WP_CLI') === true && WP_CLI === true) {
			$cliWrapper = new \UserAccessManagerExport\Wrapper\WordpressCli();
			$groupCommand = new \UserAccessManagerExport\Command\GroupCommand($cliWrapper, $userGroupHandler, $objectHandler);
			try {
				\WP_CLI::add_command('uamexport groups', $groupCommand);
			} catch (Exception $exception) {
				// Do nothing
			}
		}
	}

The problem here is that I get an undefined error on the line

$cliWrapper = new \UserAccessManagerExport\Wrapper\WordpressCli();
$groupCommand = new \UserAccessManagerExport\Command\GroupCommand($cliWrapper, $userGroupHandler, $objectHandler);

like the namespace was not defined

loretoparisi avatar Mar 17 '18 11:03 loretoparisi