1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
#include <iostream>
#include <string>
#include <fstream>
#include <set>
#include <map>
#include <sstream>
#include <iomanip>
#include <limits>
struct UserData {
std::string name;
std::string curpass;
std::set<std::string> prevpass;
};
using Users = std::map<std::string, UserData>;
enum Passerr {OK, UPPER, LOWER, LENGTH, DUPLICATE, LAST};
const std::string PassErrs[LAST] {"", "Insufficient upper case chars", "Insufficient lower case chars", "Not enough chars", "Duplicate"};
const std::string userfile {"users.txt"};
// Format of file is
//userid,name,curpass,oldpass,...
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && (!(std::cin >> i) || std::cin.peek() != '\n')) {
std::cout << "Not an integer\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return i;
}
// true OK, false bad
bool readUsers(Users& users)
{
std::ifstream ifs(userfile);
if (!ifs)
return false;
for (std::string line; std::getline(ifs, line); ) {
UserData user;
std::string id;
std::istringstream iss(line);
std::getline(iss, id, ',');
std::getline(iss, user.name, ',');
std::getline(iss, user.curpass, ',');
const bool ok {iss};
for (std::string pass; std::getline(iss, pass, ','); user.prevpass.insert(pass));
// Assume data on file is correct - no duplicates etc
if (ok)
users[id] = user;
}
return true;
}
// true OK, false bad
bool writeUsers(const Users& users)
{
std::ofstream ofs(userfile);
if (!ofs)
return false;
for (const auto& user : users) {
ofs << user.first << ',' << user.second.name << ',' << user.second.curpass << ',';
for (size_t pasno = 0; const auto & pass : user.second.prevpass)
ofs << (pasno++ ? "," : "") << pass;
ofs << '\n';
}
return true;
}
//true OK, false bad
Passerr checkPass(const UserData& user, const std::string& pass)
{
// INCLUDE HERE OTHER REQUIRED TESTS FOR VALID PASSWORD
size_t lowerCount {}, upperCount {};
for (const auto& ch : pass) {
upperCount += isupper(static_cast<unsigned char>(ch));
lowerCount += islower(static_cast<unsigned char>(ch));
}
if (pass.size() < 6)
return LENGTH;
if (lowerCount == 0)
return LOWER;
if (upperCount == 0)
return UPPER;
if ((pass == user.curpass) || user.prevpass.count(pass))
return DUPLICATE;
return OK;
}
auto getUser(Users& users, std::string& id)
{
std::cout << "\nEnter userid: ";
std::getline(std::cin, id);
return users.find(id);
}
auto getUser(Users& users)
{
std::string id;
return getUser(users, id);
}
std::string getPass(const UserData& data)
{
std::string pass;
Passerr passerr;
do {
std::cout << "Enter password (CR to terminate): ";
std::getline(std::cin, pass);
} while (!pass.empty() && ((passerr = checkPass(data, pass)) != OK) && (std::cout << "Invalid password - " << PassErrs[passerr] << '\n'));
return pass;
}
// true OK, false bad
bool addUser(Users& users)
{
std::string id;
if (const auto itr {getUser(users, id)}; itr != users.end()) {
std::cout << "Id already exists.\n";
} else {
UserData data;
std::cout << "Enter name: ";
std::getline(std::cin, data.name);
if (const auto pass {getPass(data)}; !pass.empty()) {
data.curpass = pass;
users[id] = data;
return true;
}
}
return false;
}
// true OK, false bad
bool changePass(Users& users)
{
if (const auto iditr {getUser(users)}; iditr == users.end()) {
std::cout << "Id does not exist\n";
} else {
if (const auto pass {getPass(iditr->second)}; !pass.empty()) {
iditr->second.prevpass.insert(iditr->second.curpass);
iditr->second.curpass = pass;
return true;
}
}
return false;
}
// true OK, false bad
bool login(const Users& users)
{
const auto itr {getUser(const_cast<Users&>(users))};
std::string pass;
std::cout << "Password: ";
std::getline(std::cin, pass);
if (itr != users.end())
if (itr->second.curpass == pass)
return true;
return false;
}
void displayUsers(const Users& users)
{
std::cout << '\n' << std::right << std::setw(24) << "USER" << std::setw(16) << "PASS\n";
std::cout << std::left << std::setw(20) << "NAME" << std::setw(15) << "NAME" << std::setw(10) << "WORD" << " PREVIOUS PASSWORDS\n";
std::cout << std::setw(75) << std::setfill('-') << '-' << std::setfill(' ') << '\n';
for (const auto& user : users) {
std::cout << std::setw(20) << user.second.name << std::setw(15) << user.first << std::setw(10) << user.second.curpass;
for (const auto& old : user.second.prevpass)
std::cout << " " << old;
std::cout << '\n';
}
std::cout << '\n';
}
// true OK, false bad
bool deleteUser(Users& users)
{
if (const auto iditr {getUser(users)}; iditr == users.end()) {
std::cout << "Id does not exist\n";
return false;
} else
users.erase(iditr);
return true;
}
int main() {
Users users;
unsigned opt {};
if (!readUsers(users))
std::cout << "Problem reading users\n";
do {
std::cout << "\n1. Login\n";
std::cout << "2. Change password\n";
std::cout << "\n3. Add user\n";
std::cout << "4. Delete user\n";
std::cout << "5. Display all users\n";
std::cout << "0. Exit\n";
switch (opt = getInt("\nEnter options: ")) {
case 3:
addUser(users);
break;
case 4:
deleteUser(users);
break;
case 2:
changePass(users);
break;
case 1:
if (login(users))
std::cout << "OK\n";
else
std::cout << "Invalid\n";
break;
case 5:
displayUsers(users);
break;
case 0:
if (!writeUsers(users))
std::cout << "Problem writing users\n";
break;
default:
std::cout << "Invalid option\n";
}
} while (opt);
}
|