diff options
Diffstat (limited to 'src/Client')
-rw-r--r-- | src/Client/CommandParser.cpp | 1 | ||||
-rw-r--r-- | src/Client/UserCommands.cpp | 64 | ||||
-rw-r--r-- | src/Client/UserCommands.h | 2 |
3 files changed, 66 insertions, 1 deletions
diff --git a/src/Client/CommandParser.cpp b/src/Client/CommandParser.cpp index b06f4b4..56ecbba 100644 --- a/src/Client/CommandParser.cpp +++ b/src/Client/CommandParser.cpp @@ -55,6 +55,7 @@ const CommandParser::Command CommandParser::commands[] = { {{"delete_group", 0}, "delete_group did", "Delete group", "Delete a user group from the account database.", &UserCommands::deleteGroupCommand}, {{"add_user_to_group", 0}, "add_user_to_group uid gid", "Add a user to a group", "Add a user to a group.", &UserCommands::addUserToGroupCommand}, {{"delete_user_from_group", 0}, "delete_user_from_group uid gid", "Remove a user from a group", "Remove a user from a group.", &UserCommands::deleteUserFromGroupCommand}, + {{"set_password", "password", 0}, "set_password uid", "Set the password of a user", "Set the password of a user.", &UserCommands::setPasswordCommand}, {{"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} }; diff --git a/src/Client/UserCommands.cpp b/src/Client/UserCommands.cpp index b93de94..21259d0 100644 --- a/src/Client/UserCommands.cpp +++ b/src/Client/UserCommands.cpp @@ -22,10 +22,10 @@ #include "CommandParser.h" #include <Common/RequestManager.h> - #include <Common/UserManager.h> #include <iostream> +#include <termios.h> namespace Mad { @@ -491,5 +491,67 @@ void UserCommands::deleteUserFromGroupCommand(CommandParser *commandParser, cons } } +void UserCommands::setPasswordCommand(CommandParser *commandParser, const std::vector<std::string> &args) { + if(args.size() < 2) { + std::cerr << args[0] << ": Too few arguments." << std::endl; + commandParser->printUsage("set_password"); + return; + } + if(args.size() > 2) { + std::cerr << args[0] << ": Too many arguments." << std::endl; + commandParser->printUsage("set_password"); + 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("set_password"); + return; + } + + // TODO Extract this as a method + struct termios termold, termnew; + + if(tcgetattr(STDIN_FILENO, &termold) != 0) { + std::cerr << "Unable to set up terminal for password entry." << std::endl; + return; + } + + termnew = termold; + termnew.c_lflag &= ~ECHO; + if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &termnew) != 0) { + std::cerr << "Unable to set up terminal for password entry." << std::endl; + return; + } + + std::string password, password2; + + std::cout << "Password: " << std::flush; + std::getline(std::cin, password); + std::cout << std::endl; + + std::cout << "Verify password: " << std::flush; + std::getline(std::cin, password2); + std::cout << std::endl; + + tcsetattr(STDIN_FILENO, TCSAFLUSH, &termold); + + if(password != password2) { + std::cerr << "Passwords do not match." << std::endl; + return; + } + + try { + commandParser->application->getUserManager()->setPassword(uid, password); + + std::cout << "Password set." << std::endl; + } + catch(Core::Exception e) { + std::cerr << "An error occurred during your request: " << e.strerror() << "." << std::endl; + } +} + } } diff --git a/src/Client/UserCommands.h b/src/Client/UserCommands.h index 62d42c1..d5422da 100644 --- a/src/Client/UserCommands.h +++ b/src/Client/UserCommands.h @@ -52,6 +52,8 @@ class UserCommands { static void addUserToGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args); static void deleteUserFromGroupCommand(CommandParser *commandParser, const std::vector<std::string> &args); + + static void setPasswordCommand(CommandParser *commandParser, const std::vector<std::string> &args); }; } |