wordpressCli->createFormatter( $assocArguments, [ 'ID', 'group_name', 'group_desc', 'read_access', 'write_access', 'roles', 'ip_range', ], self::FORMATTER_PREFIX ); } /** * List groups * ## OPTIONS * [--format=]: Accepted values: table, csv, JSON, count, ids. Default: table * ## EXAMPLES * wp uam groups list * @subcommand ls * @throws ExitException * @throws UserGroupTypeException * @throws Exception */ public function ls(array $arguments, array $assocArguments): void { if (count($arguments) > 0) { $this->wordpressCli->error('No arguments excepted. Please use the format option.'); return; } $userGroups = $this->userGroupHandler->getUserGroups(); if (count($userGroups) <= 0) { $this->wordpressCli->error('No groups defined yet!'); return; } $groups = []; 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() ?? '' ]; } $this->getFormatter($assocArguments)->display_items($groups); } /** * delete groups * ## OPTIONS * : id of the group(s) to delete; accepts unlimited ids * ## EXAMPLES * wp uam groups del 3 5 * @subcommand del * @throws ExitException * @throws UserGroupTypeException */ public function del(array $arguments): void { if (count($arguments) < 1) { $this->wordpressCli->error('Expected: wp uam groups del \ ...'); return; } foreach ($arguments as $userGroupId) { if ($this->userGroupHandler->deleteUserGroup($userGroupId) === true) { $this->wordpressCli->success("Successfully deleted group with id '$userGroupId'."); } else { $this->wordpressCli->error("Group id '$userGroupId' doesn't exists."); } } } /** * Reports the conflicting group and returns false when the name is already taken. * @throws ExitException * @throws UserGroupTypeException */ private function isUserGroupNameAvailable(mixed $userGroupName): bool { foreach ($this->userGroupHandler->getUserGroups() as $userGroup) { if ($userGroup->getName() === $userGroupName) { $this->wordpressCli->error( "Group with the same name '$userGroupName' already exists: {$userGroup->getId()}" ); return false; } } return true; } private function getArgumentValue(array $arguments, string $name): string { return isset($arguments[$name]) ? (string) $arguments[$name] : ''; } /** * @throws ExitException */ private function getAccessValue(array $arguments, string $name, bool $porcelain): string { $accessValue = $this->getArgumentValue($arguments, $name); if (in_array($accessValue, self::ALLOWED_ACCESS_VALUES) === true) { return $accessValue; } if ($porcelain === true) { $this->wordpressCli->line("setting $name to " . self::DEFAULT_ACCESS_VALUE); } return self::DEFAULT_ACCESS_VALUE; } /** * @throws UserGroupTypeException */ private function assignRoles(UserGroup $userGroup, string $commaSeparatedRoles): void { $userGroup->removeObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE); foreach (explode(',', $commaSeparatedRoles) as $role) { $userGroup->addObject(ObjectHandler::GENERAL_ROLE_OBJECT_TYPE, trim($role)); } } /** * @throws UserGroupTypeException * @throws Exception */ private function createUserGroup(string $userGroupName, array $assocArguments): UserGroup { $groupDescription = $this->getArgumentValue($assocArguments, 'desc'); $ipRange = $this->getArgumentValue($assocArguments, 'ip_range'); $porcelain = isset($assocArguments['porcelain']); $readAccess = $this->getAccessValue($assocArguments, 'read_access', $porcelain); $writeAccess = $this->getAccessValue($assocArguments, 'write_access', $porcelain); $userGroup = $this->userGroupFactory->createUserGroup(); $userGroup->setName($userGroupName); $userGroup->setDescription($groupDescription); $userGroup->setIpRange($ipRange); $userGroup->setReadAccess($readAccess); $userGroup->setWriteAccess($writeAccess); if (isset($assocArguments['roles']) === true) { $this->assignRoles($userGroup, $assocArguments['roles']); } $userGroup->save(); return $userGroup; } /** * add group * ## OPTIONS * : the name of the new group * [--porcelain]: Output just the new post id. * [--roles=]: comma separated list of group associated roles * [--=]: Associative args for a new UamUserGroup object * allowed fields and values are: * desc="", * read_access={group,all*}, * write_access={group,all*}, * ip_range="192.168.0.1-192.168.0.10;192.168.0.20-192.168.0.30" * *=default * ## EXAMPLES * wp uam groups add fighters --read_access=all * @throws ExitException * @throws UserGroupTypeException */ public function add(array $arguments, array $assocArguments): void { if (isset($arguments[0]) === false) { $this->wordpressCli->error("Please provide a group name."); return; } $userGroupName = $arguments[0]; if ($this->isUserGroupNameAvailable($userGroupName) === false) { return; } $userGroup = $this->createUserGroup($userGroupName, $assocArguments); $this->userGroupHandler->addUserGroup($userGroup); if (isset($assocArguments['porcelain']) === true) { $this->wordpressCli->line($userGroup->getId()); } else { $this->wordpressCli->success("Added new group '$userGroupName' with id {$userGroup->getId()}."); } } }