summaryrefslogtreecommitdiffstats
path: root/src/Client/UserCommands.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/Client/UserCommands.cpp')
-rw-r--r--src/Client/UserCommands.cpp194
1 files changed, 95 insertions, 99 deletions
diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp
index de5b7ea..fec2a33 100644
--- a/src/Client/UserCommands.cpp
+++ b/src/Client/UserCommands.cpp
@@ -23,11 +23,7 @@
#include <Common/RequestManager.h>
-#include <Common/Requests/GroupListRequest.h>
-#include <Common/Requests/GroupUserListRequest.h>
-#include <Common/Requests/UserInfoRequest.h>
-#include <Common/Requests/UserListRequest.h>
-#include <Common/Requests/UserGroupListRequest.h>
+#include <Common/UserManager.h>
#include <iostream>
@@ -35,6 +31,29 @@
namespace Mad {
namespace Client {
+void UserCommands::listUsersCommand(CommandParser *commandParser, const std::vector<std::string> &args _UNUSED_PARAMETER_) {
+ try {
+ boost::shared_ptr<const std::map<unsigned long, Common::UserInfo> > users = commandParser->application->getUserManager()->getUserList();
+
+ if(users->empty()) {
+ std::cout << "User list is empty." << std::endl;
+ }
+ else {
+ std::cout << "Found " << users->size() << " user" << (users->size()==1 ? "" : "s") << ":" << std::endl;
+
+ for(std::map<unsigned long, Common::UserInfo>::const_iterator user = users->begin(); user != users->end(); ++user) {
+ std::cout << " " << user->second.getUid() << ", " << user->second.getGid() << ", "
+ << user->second.getUsername() << ", " << user->second.getFullName() << std::endl;
+ }
+ }
+
+ std::cout << std::endl;
+ }
+ catch(Core::Exception e) {
+ std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;
+ }
+}
+
void UserCommands::userInfoCommand(CommandParser *commandParser, const std::vector<std::string> &args) {
if(args.size() == 1) {
std::cerr << args[0] << ": No user id given." << std::endl;
@@ -47,58 +66,23 @@ void UserCommands::userInfoCommand(CommandParser *commandParser, const std::vect
return;
}
- char *endptr;
- unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10);
- if(args[1].empty() || *endptr != '\0') {
- std::cerr << args[0] << ": Unable to parse user id." << std::endl;
- commandParser->printUsage("user_info");
- return;
- }
-
- boost::shared_ptr<Common::Requests::UserInfoRequest> request(new Common::Requests::UserInfoRequest(commandParser->application, uid));
- commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request);
- request->wait();
-
- std::pair<boost::shared_ptr<const Common::XmlPacket>, Core::Exception> result = request->getResult();
-
- if(!result.first || result.second) {
- std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl;
- }
- else {
- const Common::XmlPacket &packet = *result.first;
-
- std::cout << " " << packet.get<unsigned long>("uid") << ", " << packet.get<unsigned long>("gid") << ", "
- << packet.get<const std::string&>("username") << ", " << packet.get<const std::string&>("fullName") << std::endl << std::endl;
- }
-}
-
-void UserCommands::listUsersCommand(CommandParser *commandParser, const std::vector<std::string> &args _UNUSED_PARAMETER_) {
- boost::shared_ptr<Common::Requests::UserListRequest> request(new Common::Requests::UserListRequest(commandParser->application));
-
- commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request);
- request->wait();
-
- std::pair<boost::shared_ptr<const Common::XmlPacket>, Core::Exception> result = request->getResult();
-
- if(!result.first || result.second) {
- std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl;
- }
- else {
- const Common::XmlPacket::List *users = result.first->getList("users");
-
- if(!users || users->isEmpty()) {
- std::cout << "User list is empty." << std::endl;
- }
- else {
- std::cout << "Found " << users->getSize() << " user" << (users->getSize()==1 ? "" : "s") << ":" << std::endl;
+ try {
+ char *endptr;
+ unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10);
+ boost::shared_ptr<const Common::UserInfo> userInfo;
- for(Common::XmlPacket::List::const_iterator user = users->begin(); user != users->end(); ++user)
- std::cout << " " << user->get<unsigned long>("uid") << ", " << user->get<unsigned long>("gid")
- << ", " << user->get<const std::string&>("username") << ", " << user->get<const std::string&>("fullName") << std::endl;
+ if(args[1].empty() || *endptr != '\0') {
+ userInfo = commandParser->application->getUserManager()->getUserInfoByName(args[1]);
}
+ else
+ userInfo = commandParser->application->getUserManager()->getUserInfo(uid);
- std::cout << std::endl;
+ std::cout << " " << userInfo->getUid() << ", " << userInfo->getGid() << ", "
+ << userInfo->getUsername() << ", " << userInfo->getFullName() << std::endl << std::endl;
+ }
+ catch(Core::Exception e) {
+ std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;
}
}
@@ -122,59 +106,77 @@ void UserCommands::listUserGroupsCommand(CommandParser *commandParser, const std
return;
}
- boost::shared_ptr<Common::Requests::UserGroupListRequest> request(new Common::Requests::UserGroupListRequest(commandParser->application, uid));
- commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request);
- request->wait();
+ try {
+ boost::shared_ptr<const std::set<unsigned long> > groups = commandParser->application->getUserManager()->getUserGroupList(uid);
- std::pair<boost::shared_ptr<const Common::XmlPacket>, Core::Exception> result = request->getResult();
-
- if(!result.first || result.second) {
- std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl;
- }
- else {
- const Common::XmlPacket::List *groups = result.first->getList("groups");
-
- if(!groups || groups->isEmpty()) {
+ if(groups->empty()) {
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;
+ std::cout << "User is member of " << groups->size() << " group" << (groups->size()==1 ? "" : "s") << ":" << std::endl;
- for(Common::XmlPacket::List::const_iterator group = groups->begin(); group != groups->end(); ++group)
- std::cout << " " << group->get<unsigned long>("gid") << std::endl;
+ for(std::set<unsigned long>::const_iterator group = groups->begin(); group != groups->end(); ++group)
+ std::cout << " " << *group << std::endl;
}
std::cout << std::endl;
}
+ catch(Core::Exception e) {
+ std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;
+ }
}
void UserCommands::listGroupsCommand(CommandParser *commandParser, const std::vector<std::string> &args _UNUSED_PARAMETER_) {
- boost::shared_ptr<Common::Requests::GroupListRequest> request(new Common::Requests::GroupListRequest(commandParser->application));
+ try {
+ boost::shared_ptr<const std::map<unsigned long, Common::GroupInfo> > groups = commandParser->application->getUserManager()->getGroupList();
- commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request);
- request->wait();
+ if(groups->empty()) {
+ std::cout << "Group list is empty." << std::endl;
+ }
+ else {
+ std::cout << "Found " << groups->size() << " group" << (groups->size()==1 ? "" : "s") << ":" << std::endl;
- std::pair<boost::shared_ptr<const Common::XmlPacket>, Core::Exception> result = request->getResult();
+ for(std::map<unsigned long, Common::GroupInfo>::const_iterator group = groups->begin(); group != groups->end(); ++group) {
+ std::cout << " " << group->second.getGid() << ", " << group->second.getName() << std::endl;
+ }
+ }
- if(!result.first || result.second) {
- std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl;
+ std::cout << std::endl;
+ }
+ catch(Core::Exception e) {
+ std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;
}
- else {
- const Common::XmlPacket::List *groups = result.first->getList("groups");
+}
- if(!groups || groups->isEmpty()) {
- std::cout << "Group list is empty." << std::endl;
- }
- else {
- std::cout << "Found " << groups->getSize() << " group" << (groups->getSize()==1 ? "" : "s") << ":" << std::endl;
+void UserCommands::groupInfoCommand(CommandParser *commandParser, const std::vector<std::string> &args) {
+ if(args.size() == 1) {
+ std::cerr << args[0] << ": No group id given." << std::endl;
+ commandParser->printUsage("group_info");
+ return;
+ }
+ if(args.size() > 2) {
+ std::cerr << args[0] << ": Too many arguments." << std::endl;
+ commandParser->printUsage("group_info");
+ return;
+ }
+
+ try {
+ char *endptr;
+ unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10);
+ boost::shared_ptr<const Common::GroupInfo> groupInfo;
- for(Common::XmlPacket::List::const_iterator group = groups->begin(); group != groups->end(); ++group)
- std::cout << " " << group->get<unsigned long>("gid") << ", " << group->get<const std::string&>("name") << std::endl;
+ if(args[1].empty() || *endptr != '\0') {
+ groupInfo = commandParser->application->getUserManager()->getGroupInfoByName(args[1]);
}
+ else
+ groupInfo = commandParser->application->getUserManager()->getGroupInfo(gid);
- std::cout << std::endl;
+ std::cout << " " << groupInfo->getGid() << ", " << groupInfo->getName() << std::endl << std::endl;
+ }
+ catch(Core::Exception e) {
+ std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;
}
}
@@ -198,31 +200,25 @@ void UserCommands::listGroupUsersCommand(CommandParser *commandParser, const std
return;
}
- boost::shared_ptr<Common::Requests::GroupUserListRequest> request(new Common::Requests::GroupUserListRequest(commandParser->application, gid));
- commandParser->application->getRequestManager()->sendRequest(commandParser->connection, request);
- request->wait();
+ try {
+ boost::shared_ptr<const std::set<unsigned long> > users = commandParser->application->getUserManager()->getGroupUserList(gid);
- std::pair<boost::shared_ptr<const Common::XmlPacket>, Core::Exception> result = request->getResult();
-
- if(!result.first || result.second) {
- std::cerr << "An error occurred during your request: " << result.second.strerror() << "." << std::endl;
- }
- else {
- const Common::XmlPacket::List *users = result.first->getList("users");
-
- if(!users || users->isEmpty()) {
+ if(users->empty()) {
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;
+ std::cout << "The group has " << users->size() << " member" << (users->size()==1 ? "" : "s") << ":" << std::endl;
- for(Common::XmlPacket::List::const_iterator user = users->begin(); user != users->end(); ++user)
- std::cout << " " << user->get<unsigned long>("uid") << std::endl;
+ for(std::set<unsigned long>::const_iterator user = users->begin(); user != users->end(); ++user)
+ std::cout << " " << *user << std::endl;
}
std::cout << std::endl;
}
+ catch(Core::Exception e) {
+ std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;
+ }
}
}