Skip to content

Commit

Permalink
Merge pull request #56 from xxanxnie/auth-client
Browse files Browse the repository at this point in the history
Save client auth token
  • Loading branch information
suranimaria authored Dec 5, 2024
2 parents d57fcaf + ae46641 commit 6af5abc
Show file tree
Hide file tree
Showing 25 changed files with 639 additions and 393 deletions.
8 changes: 4 additions & 4 deletions include/Counseling.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class Counseling {
public:
Counseling(DatabaseManager& dbManager, const std::string& collection_name);
void cleanCache();
std::string checkInputFormat(std::string content);
virtual std::string addCounselor(std::string request_body);
virtual std::string deleteCounselor(const std::string& counselorId);
std::string checkInputFormat(std::string content, std::string request_auth);
virtual std::string addCounselor(std::string request_body, std::string request_auth);
virtual std::string deleteCounselor(const std::string& counselorId, std::string request_auth);
virtual std::string searchCounselorsAll(int start = 0);
virtual std::string updateCounselor(std::string request_body);
virtual std::string updateCounselor(std::string request_body, std::string request_auth);
std::vector<std::pair<std::string, std::string>> createDBContent();

private:
Expand Down
3 changes: 2 additions & 1 deletion include/DatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ class DatabaseManager {
const std::string& collectionName,
const std::vector<std::pair<std::string, std::string>>& keyValues);
virtual bool deleteResource(const std::string& collectionName,
const std::string& resourceId);
const std::string& resourceId,
const std::string &authToken);
virtual void deleteCollection(const std::string& collectionName);
virtual void updateResource(
const std::string& collectionName, const std::string& resourceId,
Expand Down
8 changes: 4 additions & 4 deletions include/Food.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class Food {
public:
Food(DatabaseManager& db, const std::string& collection_name);
void cleanCache();
std::string checkInputFormat(std::string content);
std::string checkInputFormat(std::string content, std::string request_auth);
std::unordered_map<std::string, std::string> format;
virtual std::string addFood(std::string request_body);
virtual std::string addFood(std::string request_body, std::string request_auth);
std::vector<std::pair<std::string, std::string>> createDBContent();
virtual std::string getAllFood(int start = 0);

virtual std::string updateFood(std::string request_body);
virtual std::string updateFood(std::string request_body, std::string request_auth);

virtual std::string deleteFood(const std::string& id);
virtual std::string deleteFood(const std::string& id, std::string request_auth);
};

#endif
8 changes: 4 additions & 4 deletions include/Healthcare.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Healthcare {

Healthcare(DatabaseManager& dbManager, const std::string& collection_name);
void cleanCache();
std::string checkInputFormat(std::string content);
virtual std::string addHealthcareService(std::string request_body);
std::string checkInputFormat(std::string content, std::string authToken);
virtual std::string addHealthcareService(std::string request_body, std::string request_auth);

virtual std::string getAllHealthcareServices(int start = 0);

virtual std::string deleteHealthcare(std::string id);
virtual std::string updateHealthcare(std::string request_body);
virtual std::string deleteHealthcare(std::string id, std::string request_auth);
virtual std::string updateHealthcare(std::string request_body, std::string request_auth);

// virtual std::string validateHealthcareServiceInput(
// const std::map<std::string, std::string>& content);
Expand Down
3 changes: 2 additions & 1 deletion include/MockDatabaseManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class MockDatabaseManager : public DatabaseManager {
(override));

MOCK_METHOD(bool, deleteResource,
(const std::string &collectionName, (const std::string &id)),
(const std::string &collectionName, (const std::string &id),
(const std::string &authToken)),
(override));
};

Expand Down
8 changes: 4 additions & 4 deletions include/Outreach.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ class Outreach {

std::string collection_name;
void cleanCache();
std::string checkInputFormat(std::string content);
std::string checkInputFormat(std::string content, std::string request_auth);
std::unordered_map<std::string, std::string> format;
virtual std::string addOutreachService(std::string request_body);
virtual std::string addOutreachService(std::string request_bod, std::string request_auth);

std::vector<std::pair<std::string, std::string>> createDBContent();

virtual std::string getAllOutreachServices(int start = 0);
virtual std::string deleteOutreach(std::string id);
virtual std::string updateOutreach(std::string request_body);
virtual std::string deleteOutreach(std::string id, std::string request_auth);
virtual std::string updateOutreach(std::string request_body, std::string request_auth);

std::string printOutreachServices(
const std::vector<bsoncxx::document::value>& services) const;
Expand Down
8 changes: 4 additions & 4 deletions include/Shelter.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class Shelter {
public:
Shelter(DatabaseManager& dbManager, std::string collection_name);
void cleanCache();
std::string checkInputFormat(std::string content);
virtual std::string addShelter(std::string request_body);
virtual std::string deleteShelter(std::string id);
std::string checkInputFormat(std::string content, std::string request_auth);
virtual std::string addShelter(std::string request_body, std::string request_auth);
virtual std::string deleteShelter(std::string id, std::string request_auth);
virtual std::string searchShelterAll(int start = 0);
virtual std::string updateShelter(std::string request_body);
virtual std::string updateShelter(std::string request_body, std::string request_auth);
std::vector<std::pair<std::string, std::string>> createDBContent();
std::string printShelters(
std::vector<bsoncxx::document::value>& shelters) const;
Expand Down
42 changes: 35 additions & 7 deletions src/DatabaseManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ void DatabaseManager::findCollection(
std::vector<bsoncxx::document::value> &result) {
auto collection = (*conn)["GitGud"][collectionName];
mongocxx::options::find options;
options.limit(20); // Limit results to 20 documents
options.skip(start); // Skip the first 10 documents
options.limit(20); // Limit results to 20 documents
options.skip(start);

std::string excludeField = "authToken";
bsoncxx::builder::stream::document projectionBuilder;
projectionBuilder << excludeField
<< 0; // Exclude the field by setting it to 0
options.projection(projectionBuilder.view());

// Perform the query
auto cursor = collection.find(createDocument(keyValues).view(), options);

for (auto &&doc : cursor) {
Expand Down Expand Up @@ -70,20 +78,39 @@ std::string DatabaseManager::insertResource(
}

bool DatabaseManager::deleteResource(const std::string &collectionName,
const std::string &resourceId) {
const std::string &resourceId,
const std::string &authToken) {
auto collection = (*conn)["GitGud"][collectionName];

// Build the filter to find the document by _id
bsoncxx::builder::stream::document filter_builder;
bsoncxx::oid oid(resourceId);
filter_builder << "_id" << oid;

// Retrieve the document first
auto document = collection.find_one(filter_builder.view());
if (!document) {
std::cout << "No document found with the given _id.\n";
return false;
}

// Check if the document contains the expected auth token
auto doc_view = document->view();
auto auth_field = doc_view["authToken"];
if (!auth_field || std::string(auth_field.get_utf8().value) != authToken) {
std::cout << "Invalid permissions: auth token mismatch.\n";
return false;
}

// Proceed with deletion if the auth token matches
auto result = collection.delete_one(filter_builder.view());
if (result && result->deleted_count() > 0) {
std::cout << "Document deleted successfully.\n";
return 1;
return true;
} else {
std::cout << "No document found with the given _id.\n";
return 0;
std::cout
<< "Failed to delete document. It might have already been deleted.\n";
return false;
}
}

Expand All @@ -108,7 +135,8 @@ void DatabaseManager::updateResource(
<< "_id" << oid
<< bsoncxx::builder::stream::finalize);
if (!check) {
throw std::invalid_argument("The request with wrong id.");
throw std::invalid_argument(
"The request with wrong id or invalid permissions.");
}
auto result = collection.update_one(bsoncxx::builder::stream::document{}
<< "_id" << oid
Expand Down
Loading

0 comments on commit 6af5abc

Please sign in to comment.