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.cpp136
1 files changed, 58 insertions, 78 deletions
diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp
index d58eaca..cd2ff5a 100644
--- a/src/Client/UserCommands.cpp
+++ b/src/Client/UserCommands.cpp
@@ -33,19 +33,58 @@
namespace Mad {
namespace Client {
+std::string UserCommands::getUserName(CommandParser *commandParser, unsigned long uid, bool withId) {
+ std::ostringstream stream;
+
+ try {
+ stream << commandParser->application->getUserManager()->getUserInfo(uid)->getUsername();
+ }
+ catch(...) {
+ stream << uid;
+ }
+
+ if(withId) {
+ stream << " (";
+ stream << uid;
+ stream << ")";
+ }
+
+ return stream.str();
+}
+
+std::string UserCommands::getGroupName(CommandParser *commandParser, unsigned long gid, bool withId) {
+ std::ostringstream stream;
+
+ try {
+ stream << commandParser->application->getUserManager()->getGroupInfo(gid)->getName();
+ }
+ catch(...) {
+ stream << gid;
+ }
+
+ if(withId) {
+ stream << " (";
+ stream << gid;
+ stream << ")";
+ }
+
+ return stream.str();
+}
+
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;
+ std::cout << "The 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 << " " << user->second.getUsername() << ":"
+ << getGroupName(commandParser, user->second.getGid(), false) << " ("
+ << user->second.getFullName() << ")" << std::endl;
}
}
@@ -80,49 +119,19 @@ void UserCommands::userInfoCommand(CommandParser *commandParser, const std::vect
else
userInfo = commandParser->application->getUserManager()->getUserInfo(uid);
- 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;
- }
-}
-
-void UserCommands::listUserGroupsCommand(CommandParser *commandParser, const std::vector<std::string> &args) {
- if(args.size() == 1) {
- std::cerr << args[0] << ": No user id given." << std::endl;
- commandParser->printUsage("list_user_groups");
- return;
- }
- if(args.size() > 2) {
- std::cerr << args[0] << ": Too many arguments." << std::endl;
- commandParser->printUsage("list_user_groups");
- return;
- }
+ std::cout << " User name: " << userInfo->getUsername() << " (" << userInfo->getUid() << ")" << std::endl;
+ std::cout << " Group: " << getGroupName(commandParser, userInfo->getGid(), true) << std::endl;
+ std::cout << " Full name: " << userInfo->getFullName() << std::endl << std::endl;
- 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("list_user_groups");
- return;
- }
-
- try {
boost::shared_ptr<const std::set<unsigned long> > groups = commandParser->application->getUserManager()->getUserGroupList(uid);
- if(groups->empty()) {
- std::cout << "The user isn't member of any group." << std::endl;
- }
- else {
- std::cout << "User is member of " << groups->size() << " group" << (groups->size()==1 ? "" : "s") << ":" << std::endl;
-
-
+ if(!groups->empty()) {
+ std::cout << " The user is member of " << groups->size() << " group" << (groups->size()==1 ? "" : "s") << ":" << std::endl;
for(std::set<unsigned long>::const_iterator group = groups->begin(); group != groups->end(); ++group)
- std::cout << " " << *group << std::endl;
- }
+ std::cout << " " << getGroupName(commandParser, *group, true) << std::endl;
- std::cout << std::endl;
+ std::cout << std::endl;
+ }
}
catch(Core::Exception e) {
std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;
@@ -134,13 +143,13 @@ void UserCommands::listGroupsCommand(CommandParser *commandParser, const std::ve
boost::shared_ptr<const std::map<unsigned long, Common::GroupInfo> > groups = commandParser->application->getUserManager()->getGroupList();
if(groups->empty()) {
- std::cout << "Group list is empty." << std::endl;
+ std::cout << "The group list is empty." << std::endl;
}
else {
std::cout << "Found " << groups->size() << " group" << (groups->size()==1 ? "" : "s") << ":" << std::endl;
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;
+ std::cout << " " << group->second.getName() << " (" << group->second.getGid() << ")" << std::endl;
}
}
@@ -175,48 +184,19 @@ void UserCommands::groupInfoCommand(CommandParser *commandParser, const std::vec
else
groupInfo = commandParser->application->getUserManager()->getGroupInfo(gid);
- 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;
- }
-}
+ std::cout << " Group name: " << groupInfo->getName() << " (" << groupInfo->getGid() << ")" << std::endl << std::endl;
-void UserCommands::listGroupUsersCommand(CommandParser *commandParser, const std::vector<std::string> &args) {
- if(args.size() == 1) {
- std::cerr << args[0] << ": No group id given." << std::endl;
- commandParser->printUsage("list_group_users");
- return;
- }
- if(args.size() > 2) {
- std::cerr << args[0] << ": Too many arguments." << std::endl;
- commandParser->printUsage("list_group_users");
- return;
- }
-
- char *endptr;
- unsigned long gid = std::strtoul(args[1].c_str(), &endptr, 10);
- if(args[1].empty() || *endptr != '\0') {
- std::cerr << args[0] << ": Unable to parse group id." << std::endl;
- commandParser->printUsage("list_group_users");
- return;
- }
-
- try {
boost::shared_ptr<const std::set<unsigned long> > users = commandParser->application->getUserManager()->getGroupUserList(gid);
- if(users->empty()) {
- std::cout << "The group doesn't have any members." << std::endl;
- }
- else {
- std::cout << "The group has " << users->size() << " member" << (users->size()==1 ? "" : "s") << ":" << std::endl;
+ if(!users->empty()) {
+ std::cout << " The group has " << users->size() << " member" << (users->size()==1 ? "" : "s") << ":" << std::endl;
for(std::set<unsigned long>::const_iterator user = users->begin(); user != users->end(); ++user)
- std::cout << " " << *user << std::endl;
- }
+ std::cout << " " << getUserName(commandParser, *user, true) << std::endl;
- std::cout << std::endl;
+ std::cout << std::endl;
+ }
}
catch(Core::Exception e) {
std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl;