/* * UserCommands.cpp * * Copyright (C) 2009 Matthias Schiffer * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published by the * Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * See the GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License along * with this program. If not, see . */ #include #include "UserCommands.h" #include "Application.h" #include "CommandParser.h" #include #include #include namespace Mad { namespace Client { void UserCommands::listUsersCommand(CommandParser *commandParser, const std::vector &args _UNUSED_PARAMETER_) { try { boost::shared_ptr > 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::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 &args) { if(args.size() == 1) { std::cerr << args[0] << ": No user id given." << std::endl; commandParser->printUsage("user_info"); return; } if(args.size() > 2) { std::cerr << args[0] << ": Too many arguments." << std::endl; commandParser->printUsage("user_info"); return; } try { char *endptr; unsigned long uid = std::strtoul(args[1].c_str(), &endptr, 10); boost::shared_ptr userInfo; if(args[1].empty() || *endptr != '\0') { userInfo = commandParser->application->getUserManager()->getUserInfoByName(args[1]); } 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 &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; } 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 > 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; for(std::set::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 &args _UNUSED_PARAMETER_) { try { boost::shared_ptr > groups = commandParser->application->getUserManager()->getGroupList(); if(groups->empty()) { std::cout << "Group list is empty." << std::endl; } else { std::cout << "Found " << groups->size() << " group" << (groups->size()==1 ? "" : "s") << ":" << std::endl; for(std::map::const_iterator group = groups->begin(); group != groups->end(); ++group) { std::cout << " " << group->second.getGid() << ", " << group->second.getName() << std::endl; } } std::cout << std::endl; } catch(Core::Exception e) { std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; } } void UserCommands::groupInfoCommand(CommandParser *commandParser, const std::vector &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 groupInfo; if(args[1].empty() || *endptr != '\0') { groupInfo = commandParser->application->getUserManager()->getGroupInfoByName(args[1]); } 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; } } void UserCommands::listGroupUsersCommand(CommandParser *commandParser, const std::vector &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 > 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; for(std::set::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; } } void UserCommands::addUserCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() < 5) { std::cerr << args[0] << ": Too few arguments." << std::endl; commandParser->printUsage("add_user"); return; } if(args.size() > 5) { std::cerr << args[0] << ": Too many arguments." << std::endl; commandParser->printUsage("add_user"); 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("add_user"); return; } unsigned long gid = std::strtoul(args[2].c_str(), &endptr, 10); if(args[2].empty() || *endptr != '\0') { std::cerr << args[0] << ": Unable to parse group id." << std::endl; commandParser->printUsage("add_user"); return; } try { Common::UserInfo user(uid, args[3]); user.setGid(gid); user.setFullName(args[4]); commandParser->application->getUserManager()->addUser(user); std::cout << "User added." << std::endl; } catch(Core::Exception e) { std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; } } void UserCommands::updateUserCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() < 6) { std::cerr << args[0] << ": Too few arguments." << std::endl; commandParser->printUsage("update_user"); return; } if(args.size() > 6) { std::cerr << args[0] << ": Too many arguments." << std::endl; commandParser->printUsage("update_user"); return; } char *endptr; unsigned long origUid = std::strtoul(args[1].c_str(), &endptr, 10); if(args[1].empty() || *endptr != '\0') { std::cerr << args[0] << ": Unable to parse the old user id." << std::endl; commandParser->printUsage("update_user"); return; } unsigned long uid = std::strtoul(args[2].c_str(), &endptr, 10); if(args[2].empty() || *endptr != '\0') { std::cerr << args[0] << ": Unable to parse the new user id." << std::endl; commandParser->printUsage("update_user"); return; } unsigned long gid = std::strtoul(args[3].c_str(), &endptr, 10); if(args[3].empty() || *endptr != '\0') { std::cerr << args[0] << ": Unable to parse group id." << std::endl; commandParser->printUsage("update_user"); return; } try { Common::UserInfo user(uid, args[4]); user.setGid(gid); user.setFullName(args[5]); commandParser->application->getUserManager()->updateUser(origUid, user); std::cout << "User updated." << std::endl; } catch(Core::Exception e) { std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; } } void UserCommands::deleteUserCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() < 2) { std::cerr << args[0] << ": No user id given." << std::endl; commandParser->printUsage("delete_user"); return; } if(args.size() > 2) { std::cerr << args[0] << ": Too many arguments." << std::endl; commandParser->printUsage("delete_user"); 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("delete_user"); return; } try { commandParser->application->getUserManager()->deleteUser(uid); std::cout << "User deleted." << std::endl; } catch(Core::Exception e) { std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; } } void UserCommands::addUserToGroupCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() < 3) { std::cerr << args[0] << ": Too few arguments." << std::endl; commandParser->printUsage("add_user_to_group"); return; } if(args.size() > 3) { std::cerr << args[0] << ": Too many arguments." << std::endl; commandParser->printUsage("add_user_to_group"); 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("add_user_to_group"); return; } unsigned long gid = std::strtoul(args[2].c_str(), &endptr, 10); if(args[2].empty() || *endptr != '\0') { std::cerr << args[0] << ": Unable to parse group id." << std::endl; commandParser->printUsage("add_user_to_group"); return; } try { commandParser->application->getUserManager()->addUserToGroup(uid, gid); std::cout << "Added." << std::endl; } catch(Core::Exception e) { std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; } } void UserCommands::deleteUserFromGroupCommand(CommandParser *commandParser, const std::vector &args) { if(args.size() < 3) { std::cerr << args[0] << ": Too few arguments." << std::endl; commandParser->printUsage("delete_user_from_group"); return; } if(args.size() > 3) { std::cerr << args[0] << ": Too many arguments." << std::endl; commandParser->printUsage("delete_user_from_group"); 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("delete_user_from_group"); return; } unsigned long gid = std::strtoul(args[2].c_str(), &endptr, 10); if(args[2].empty() || *endptr != '\0') { std::cerr << args[0] << ": Unable to parse group id." << std::endl; commandParser->printUsage("delete_user_from_group"); return; } try { commandParser->application->getUserManager()->deleteUserFromGroup(uid, gid); std::cout << "Removed." << std::endl; } catch(Core::Exception e) { std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; } } } }