From cb8e66c1b1f1c8076053d71347d0b1f96ca0bca1 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 29 May 2009 18:02:12 +0200 Subject: Requests UserGroupList und GroupUserList implementiert --- src/Client/CommandManager.cpp | 58 +++++++++++++++++++++++++++++++++++++++++-- src/Client/CommandManager.h | 2 ++ src/Client/CommandParser.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++- src/Client/CommandParser.h | 2 ++ 4 files changed, 117 insertions(+), 3 deletions(-) (limited to 'src/Client') diff --git a/src/Client/CommandManager.cpp b/src/Client/CommandManager.cpp index b427db7..603f679 100644 --- a/src/Client/CommandManager.cpp +++ b/src/Client/CommandManager.cpp @@ -235,7 +235,7 @@ void CommandManager::userListRequestFinished(const Common::Request &request) { std::cout << "User list is empty." << std::endl; } else { - std::cout << "Found " << users.getSize() << " users:" << std::endl; + std::cout << "Found " << users.getSize() << " user" << (users.getSize()==1 ? "" : "s") << ":" << std::endl; for(size_t i = 0; i < users.getSize(); ++i) @@ -251,6 +251,33 @@ void CommandManager::userListRequestFinished(const Common::Request &request) { requestFinished(); } +void CommandManager::userGroupListRequestFinished(const Common::Request &request) { + try { + const Common::XmlPacket &packet = request.getResult(); + + const Common::XmlPacket::Element &groups = packet["groups"]; + + if(groups.isEmpty()) { + std::cout << "The user isn't member of any group." << std::endl; + } + else { + + std::cout << "User is member of " << groups.getSize() << " group" << (groups.getSize()==1 ? "" : "s") << ":" << std::endl; + + + for(size_t i = 0; i < groups.getSize(); ++i) + std::cout << " " << (unsigned long)groups[i]["gid"] << std::endl; + } + + std::cout << std::endl; + } + catch(Net::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + void CommandManager::groupListRequestFinished(const Common::Request &request) { try { const Common::XmlPacket &packet = request.getResult(); @@ -261,7 +288,7 @@ void CommandManager::groupListRequestFinished(const Common::Request &request) { std::cout << "Group list is empty." << std::endl; } else { - std::cout << "Found " << groups.getSize() << " groups:" << std::endl; + std::cout << "Found " << groups.getSize() << " group" << (groups.getSize()==1 ? "" : "s") << ":" << std::endl; for(size_t i = 0; i < groups.getSize(); ++i) @@ -277,5 +304,32 @@ void CommandManager::groupListRequestFinished(const Common::Request &request) { requestFinished(); } +void CommandManager::groupUserListRequestFinished(const Common::Request &request) { + try { + const Common::XmlPacket &packet = request.getResult(); + + const Common::XmlPacket::Element &users = packet["users"]; + + if(users.isEmpty()) { + std::cout << "The group doesn't have any members." << std::endl; + } + else { + + std::cout << "The group has " << users.getSize() << " member" << (users.getSize()==1 ? "" : "s") << ":" << std::endl; + + + for(size_t i = 0; i < users.getSize(); ++i) + std::cout << " " << (unsigned long)users[i]["uid"] << std::endl; + } + + std::cout << std::endl; + } + catch(Net::Exception &exception) { + Common::Logger::logf(Common::Logger::ERROR, "An error occurred during your request: %s.", exception.strerror().c_str()); + } + + requestFinished(); +} + } } diff --git a/src/Client/CommandManager.h b/src/Client/CommandManager.h index 3cfc502..137ec8e 100644 --- a/src/Client/CommandManager.h +++ b/src/Client/CommandManager.h @@ -60,7 +60,9 @@ class CommandManager { void statusRequestFinished(const Common::Request &request); void userInfoRequestFinished(const Common::Request &request); void userListRequestFinished(const Common::Request &request); + void userGroupListRequestFinished(const Common::Request &request); void groupListRequestFinished(const Common::Request &request); + void groupUserListRequestFinished(const Common::Request &request); CommandManager() : activeRequests(0), disconnect(false) {} diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index 044a64c..1c0403d 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -27,9 +27,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -49,8 +51,10 @@ const CommandParser::Command CommandParser::commands[] = { {{"status", "st", 0}, "status [host]", "Display status information", "Display host status information. If no host is given, display server status information.", &CommandParser::statusCommand}, {{"user_info", "user", 0}, "user_info uid", "Search for a user id", "Search for a user id.", &CommandParser::userInfoCommand}, {{"list_users", "users", 0}, "list_users", "Show the user account database", "Show the user account database.", &CommandParser::listUsersCommand}, + {{"list_user_groups", "user_groups", 0}, "list_user_groups uid", "List user's groups", "List the groups that the user is member of.", &CommandParser::listUserGroupsCommand}, {{"list_groups", "groups", 0}, "list_groups", "Show the user group database", "Show the user group database.", &CommandParser::listGroupsCommand}, - {{"exit", "quit", 0}, "exit", "Close the connection and quit the client", "Closes the connection and quits the client.", &CommandParser::exitCommand}, + {{"list_group_users", "group_users", 0}, "list_group_users gid", "List group's users", "List the users that are members of the group.", &CommandParser::listGroupUsersCommand}, + {{"exit", "quit", 0}, "exit", "Close the connection and quit the client", "Close the connection and quit the client.", &CommandParser::exitCommand}, {{0}, 0, 0, 0, 0} }; @@ -292,6 +296,32 @@ void CommandParser::listUsersCommand(const std::vector&) { boost::bind(&CommandManager::userListRequestFinished, CommandManager::get(), _1)); } +void CommandParser::listUserGroupsCommand(const std::vector &args) { + if(args.size() == 1) { + Common::Logger::logf(Common::Logger::ERROR, "%s: No user id given.", args[0].c_str()); + printUsage("list_user_groups"); + return; + } + if(args.size() > 2) { + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + printUsage("list_user_groups"); + return; + } + + char *endptr; + unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); + if(args[1].empty() || *endptr != '\0') { + Common::Logger::logf(Common::Logger::ERROR, "%s: Unable to parse user id.", args[0].c_str()); + printUsage("list_user_groups"); + return; + } + + ++CommandManager::get()->activeRequests; + + Common::RequestManager::get()->sendRequest(connection, + boost::bind(&CommandManager::userGroupListRequestFinished, CommandManager::get(), _1), uid); +} + void CommandParser::listGroupsCommand(const std::vector&) { ++CommandManager::get()->activeRequests; @@ -299,6 +329,32 @@ void CommandParser::listGroupsCommand(const std::vector&) { boost::bind(&CommandManager::groupListRequestFinished, CommandManager::get(), _1)); } +void CommandParser::listGroupUsersCommand(const std::vector &args) { + if(args.size() == 1) { + Common::Logger::logf(Common::Logger::ERROR, "%s: No group id given.", args[0].c_str()); + printUsage("list_group_users"); + return; + } + if(args.size() > 2) { + Common::Logger::logf(Common::Logger::ERROR, "%s: Too many arguments.", args[0].c_str()); + printUsage("list_group_users"); + return; + } + + char *endptr; + unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10); + if(args[1].empty() || *endptr != '\0') { + Common::Logger::logf(Common::Logger::ERROR, "%s: Unable to parse group id.", args[0].c_str()); + printUsage("list_group_users"); + return; + } + + ++CommandManager::get()->activeRequests; + + Common::RequestManager::get()->sendRequest(connection, + boost::bind(&CommandManager::groupUserListRequestFinished, CommandManager::get(), _1), gid); +} + void CommandParser::exitCommand(const std::vector&) { ++CommandManager::get()->activeRequests; diff --git a/src/Client/CommandParser.h b/src/Client/CommandParser.h index 727181e..692eb7e 100644 --- a/src/Client/CommandParser.h +++ b/src/Client/CommandParser.h @@ -65,7 +65,9 @@ class CommandParser { void statusCommand(const std::vector &args); void userInfoCommand(const std::vector &args); void listUsersCommand(const std::vector &args); + void listUserGroupsCommand(const std::vector &args); void listGroupsCommand(const std::vector &args); + void listGroupUsersCommand(const std::vector &args); void exitCommand(const std::vector&); CommandParser() : connection(0) {} -- cgit v1.2.3