diff --git a/ComplaintBox.cpp b/ComplaintBox.cpp index 6abd894..e19d356 100644 --- a/ComplaintBox.cpp +++ b/ComplaintBox.cpp @@ -3,9 +3,41 @@ #include #include #include +#include +#include using namespace std; +string getHiddenPassword() { + string pass; + char ch; + while ((ch = _getch()) != '\r') { // Enter key + if (ch == '\b') { // Backspace + if (!pass.empty()) { + cout << "\b \b"; + pass.pop_back(); + } + } else { + pass += ch; + cout << '*'; + } + } + cout << endl; + return pass; +} + +string hashPassword(const string& password) { + unsigned char hash[SHA256_DIGEST_LENGTH]; + SHA256((const unsigned char*)password.c_str(), password.size(), hash); + + stringstream ss; + for (int i = 0; i < SHA256_DIGEST_LENGTH; ++i) + ss << hex << setw(2) << setfill('0') << (int)hash[i]; + + return ss.str(); +} + + ComplaintBox::ComplaintBox() { sqlite3_open("complaints.db", &db); createTables(); @@ -19,10 +51,12 @@ void ComplaintBox::createTables() { string sqlUsers = "CREATE TABLE IF NOT EXISTS users (username TEXT PRIMARY KEY, password TEXT);"; string sqlAdmins = "CREATE TABLE IF NOT EXISTS adminusers (username TEXT PRIMARY KEY, password TEXT);"; string sqlComplaints = "CREATE TABLE IF NOT EXISTS complaints (" - "complaint_id INTEGER PRIMARY KEY AUTOINCREMENT, " - "category TEXT, " - "subCategory TEXT, " - "message TEXT);"; + "complaint_id INTEGER PRIMARY KEY AUTOINCREMENT, " + "category TEXT, " + "subCategory TEXT, " + "message TEXT, " + "status TEXT DEFAULT 'Pending', " + "timestamp DATETIME DEFAULT CURRENT_TIMESTAMP);"; sqlite3_exec(db, sqlUsers.c_str(), 0, 0, &errMsg); sqlite3_exec(db, sqlAdmins.c_str(), 0, 0, &errMsg); @@ -30,7 +64,7 @@ void ComplaintBox::createTables() { } void ComplaintBox::registerUser(bool isAdmin) { - string uname, pass; + string uname; cout << PURPLE << "Enter username: " << RESET; cin >> uname; @@ -48,12 +82,21 @@ void ComplaintBox::registerUser(bool isAdmin) { } cout << PURPLE << "Enter password: " << RESET; - cin >> pass; + string pass1 = getHiddenPassword(); + + cout << PURPLE << "Confirm password: " << RESET; + string pass2 = getHiddenPassword(); + + if (pass1 != pass2) { + cout << RED << "Passwords do not match. Registration failed.\n" << RESET; + return; + } string table = isAdmin ? "adminusers" : "users"; - string sql = "INSERT INTO " + table + " (username, password) VALUES ('" + uname + "', '" + pass + "');"; + string hashedPass = hashPassword(pass1); + string insertSql = "INSERT INTO " + table + " (username, password) VALUES ('" + uname + "', '" + hashedPass + "');"; - if (sqlite3_exec(db, sql.c_str(), 0, 0, &errMsg) != SQLITE_OK) { + if (sqlite3_exec(db, insertSql.c_str(), 0, 0, &errMsg) != SQLITE_OK) { cout << RED << "Error: " << errMsg << RESET << endl; sqlite3_free(errMsg); } else { @@ -61,15 +104,18 @@ void ComplaintBox::registerUser(bool isAdmin) { } } + bool ComplaintBox::loginUser(bool isAdmin) { string uname, pass; cout << CYAN << "Enter username: " << RESET; cin >> uname; cout << CYAN << "Enter password: " << RESET; - cin >> pass; + pass = getHiddenPassword(); string table = isAdmin ? "adminusers" : "users"; - string sql = "SELECT * FROM " + table + " WHERE username = '" + uname + "' AND password = '" + pass + "';"; + string hashedPass = hashPassword(pass); + string sql = "SELECT * FROM " + table + " WHERE username = '" + uname + "' AND password = '" + hashedPass + "';"; + bool success = false; sqlite3_exec(db, sql.c_str(), [](void *successPtr, int, char **, char **) -> int { @@ -79,6 +125,7 @@ bool ComplaintBox::loginUser(bool isAdmin) { if (success) { cout << GREEN << "Login successful!\n" << RESET; + admin_logged_in = isAdmin; return true; } else { cout << RED << "Invalid credentials!\n" << RESET; @@ -86,6 +133,7 @@ bool ComplaintBox::loginUser(bool isAdmin) { } } + void ComplaintBox::fileComplaint() { string category, subCategory, message; cout << YELLOW << "Enter category: " << RESET; @@ -96,24 +144,28 @@ void ComplaintBox::fileComplaint() { cout << YELLOW << "Enter complaint message: " << RESET; getline(cin, message); - string sql = "INSERT INTO complaints (category, subCategory, message) VALUES ('" + category + "', '" + subCategory + "', '" + message + "');"; + string sql = "INSERT INTO complaints (category, subCategory, message, status) VALUES ('" + + category + "', '" + subCategory + "', '" + message + "', 'Pending');"; + if (sqlite3_exec(db, sql.c_str(), 0, 0, &errMsg) != SQLITE_OK) { cout << RED << "Error: " << errMsg << RESET << endl; sqlite3_free(errMsg); } else { - cout << BOLDGREEN << "Complaint filed successfully!\n" << RESET; + cout << BOLDGREEN << "Complaint filed successfully with status 'Pending'!\n" << RESET; } } + void ComplaintBox::exportComplaintsToCSV() { ofstream file("complaints_export.csv"); if (!file.is_open()) { cout << RED << "Failed to create CSV file.\n" << RESET; return; } - - file << "complaint_id,category,subCategory,message\n"; - string sql = "SELECT complaint_id, category, subCategory, message FROM complaints;"; + + file << "complaint_id,category,subCategory,message,status,timestamp\n"; // Include timestamp in header + string sql = "SELECT complaint_id, category, subCategory, message, status, timestamp FROM complaints;"; // Include timestamp + auto callback = [](void *data, int argc, char **argv, char **colName) -> int { ofstream *f = static_cast(data); for (int i = 0; i < argc; i++) { @@ -121,14 +173,14 @@ void ComplaintBox::exportComplaintsToCSV() { } return 0; }; - + if (sqlite3_exec(db, sql.c_str(), callback, &file, &errMsg) != SQLITE_OK) { cout << RED << "Export failed: " << errMsg << RESET << endl; sqlite3_free(errMsg); } else { - cout << BOLDGREEN << "Complaints exported to 'complaints_export.csv'!\n" << RESET; + cout << BOLDGREEN << "Complaints exported to 'complaints_export.csv' with timestamp!\n" << RESET; } - + file.close(); } @@ -138,10 +190,11 @@ void ComplaintBox::searchComplaints() { cin.ignore(); getline(cin, keyword); - string sql = "SELECT complaint_id, category, subCategory, message FROM complaints " + string sql = "SELECT complaint_id, category, subCategory, message, status FROM complaints " "WHERE category LIKE '%" + keyword + "%' OR " "subCategory LIKE '%" + keyword + "%' OR " - "message LIKE '%" + keyword + "%';"; + "message LIKE '%" + keyword + "%' OR " + "status LIKE '%" + keyword + "%';"; cout << CYAN << "\nSearch Results:\n" << RESET; auto callback = [](void *data, int argc, char **argv, char **colName) -> int { @@ -157,3 +210,29 @@ void ComplaintBox::searchComplaints() { sqlite3_free(errMsg); } } + + +void ComplaintBox::updateComplaintStatus(int complaint_id, const std::string& new_status) { + if (!admin_logged_in) { + cout << RED << "Only admins can update complaint status.\n" << RESET; + return; + } + + sqlite3_stmt* stmt; + string sql = "UPDATE complaints SET status = ? WHERE complaint_id = ?"; + + if (sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr) == SQLITE_OK) { + sqlite3_bind_text(stmt, 1, new_status.c_str(), -1, SQLITE_STATIC); + sqlite3_bind_int(stmt, 2, complaint_id); + + if (sqlite3_step(stmt) == SQLITE_DONE) { + cout << GREEN << "Status updated successfully.\n" << RESET; + } else { + cerr << RED << "Failed to update status.\n" << RESET; + } + } else { + cerr << RED << "SQL Prepare Failed: " << sqlite3_errmsg(db) << "\n" << RESET; + } + + sqlite3_finalize(stmt); +} \ No newline at end of file diff --git a/ComplaintBox.h b/ComplaintBox.h index 4dbc09a..9dd08c8 100644 --- a/ComplaintBox.h +++ b/ComplaintBox.h @@ -26,10 +26,13 @@ class ComplaintBox { void fileComplaint(); void exportComplaintsToCSV(); void searchComplaints(); + void updateComplaintStatus(int complaint_id, const string& new_status); + bool isAdminLoggedIn() const { return admin_logged_in; } private: sqlite3 *db; char *errMsg; + bool admin_logged_in = false; void createTables(); }; diff --git a/complaints_export.csv b/complaints_export.csv index eb444d4..20d4075 100644 --- a/complaints_export.csv +++ b/complaints_export.csv @@ -1,4 +1,2 @@ -complaint_id,category,subCategory,message -1,Infrastructural,Hostel,Washrooms are not being regularly cleaned. -2,Management,College Events,Admin paisa kha gaya bc -3,Infrastructure,Hostel,MMC change karo mc +complaint_id,category,subCategory,message,status,timestamp +1,College,Fests,Admin chor mc,Pending,2025-04-11 14:43:14 diff --git a/main.cpp b/main.cpp index 8eaffc2..5963d2a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,87 +1,74 @@ +// g++ main.cpp ComplaintBox.cpp -lssl -lcrypto -lsqlite3 -o complaintbox + #include #include "ComplaintBox.h" using namespace std; int main() { ComplaintBox cb; - string choice; - int choiceNum = 0; + int choice = 0; do { cout << BOLDVIOLET << "\n==== Complaint Box Menu ====\n" << RESET; - cout << CYAN << "1. Register User\n" + cout << CYAN + << "1. Register User\n" << "2. Register Admin\n" << "3. User Login\n" << "4. Admin Login\n" << "5. File Complaint\n" << "6. Export Complaints to CSV\n" << "7. Search Complaints\n" - << "8. Exit\n" << RESET; - + << "8. Update Complaint Status (Admin Only)\n" + << "9. Exit\n" + << RESET; + cout << WHITE << "Choice: " << RESET; cin >> choice; -// <<<<<<< Fix/crash-main-menu - try { - choiceNum = stoi(choice); - switch (choiceNum) - { - case 1: - cb.registerUser(); - break; - case 2: - cb.registerUser(true); - break; - case 3: - cb.loginUser(); - break; - case 4: - cb.loginUser(true); - break; - case 5: - cb.fileComplaint(); - break; - case 6: - cout << "Exiting..." << endl; - break; - default: - cout << "Invalid choice!\n"; + switch (choice) { + case 1: + cb.registerUser(); + break; + case 2: + cb.registerUser(true); + break; + case 3: + cb.loginUser(); + break; + case 4: + cb.loginUser(true); + break; + case 5: + cb.fileComplaint(); + break; + case 6: + cb.exportComplaintsToCSV(); + break; + case 7: + cb.searchComplaints(); + break; + case 8: + if (!cb.isAdminLoggedIn()) { + cout << RED << "Only admins can update complaint status. Please login as admin first.\n" << RESET; + break; + } else { + int id; + string status; + cout << YELLOW << "Enter Complaint ID to update: " << RESET; + cin >> id; + cin.ignore(); + cout << YELLOW << "Enter new status (Pending/In Progress/Resolved): " << RESET; + getline(cin, status); + cb.updateComplaintStatus(id, status); + break; + } + case 9: + cout << BOLDGREEN << "Exiting..." << RESET << endl; + break; + default: + cout << BOLDRED << "Invalid choice!\n" << RESET; } - } catch (exception& e) { // stoi throw an exception when input is non-numeric string - cout << "Invalid input! Please enter a number."<>>>>>> main + } while (choice != 9); return 0; -} +} \ No newline at end of file