From 50e8ca5c7c8f5f08e3b0d7a7bc1a8839358f4542 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Mon, 19 Apr 2021 03:56:41 -0700 Subject: [PATCH] :fire: simplified via removing allusers --- include/bank.hpp | 28 +++++----------------------- include/bank_f.hpp | 7 +------ 2 files changed, 6 insertions(+), 29 deletions(-) diff --git a/include/bank.hpp b/include/bank.hpp index 9680fe1..5c92dc9 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -35,7 +35,7 @@ public: bool AddUser(const std::string &name, std::string &&init_pass) { - std::unique_lock lock{size_lock}; + std::shared_lock lock{size_lock}; return users.try_emplace_l( name, [](User &) {}, std::move(init_pass)); } @@ -44,7 +44,7 @@ public: bool state = (admin_pass == attempt); if (state) { - std::unique_lock lock{size_lock}; + std::shared_lock lock{size_lock}; state = users.try_emplace_l( name, [](User &) {}, init_bal, std::move(init_pass)); } @@ -53,12 +53,12 @@ public: bool DelUser(const std::string &name, const std::string &attempt) { - std::unique_lock lock{size_lock}; + std::shared_lock lock{size_lock}; return users.erase_if(name, [&attempt](const User &u) { return (XXH64(attempt.data(), attempt.size(), 1) == u.password); }); } bool AdminDelUser(const std::string &name, const std::string &attempt) { - std::unique_lock lock{size_lock}; + std::shared_lock lock{size_lock}; return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); }); } @@ -147,23 +147,6 @@ public: return res; } - Json::Value AllUsers() - { - Json::Value temp; - Json::UInt i = 0; - std::shared_lock lock{size_lock}; //gives readers of users the lock - for (const auto &u : users) - { - //we know it contains this key but we call this func to grab mutex - temp[i]["name"] = u.first; - users.if_contains(u.first, [&temp, i](const User &u) { - temp[i]["balance"] = (Json::UInt)u.balance; - }); - ++i; - } - return temp; - } - void Save() { Json::StreamWriterBuilder builder; @@ -174,8 +157,7 @@ public: //loading info into json temp { - std::shared_lock lock{size_lock}; //gives readers of users the lock - std::unique_lock halt_funds{send_funds_l}; //halts all send fund requests + std::scoped_lock lock{size_lock, send_funds_l}; for (const auto &u : users) { //we know it contains this key but we call this func to grab mutex diff --git a/include/bank_f.hpp b/include/bank_f.hpp index a0be8fa..0b91229 100644 --- a/include/bank_f.hpp +++ b/include/bank_f.hpp @@ -37,7 +37,7 @@ public: { auto resp = HttpResponse::newHttpResponse(); auto handlerInfo = app().getHandlersInfo(); - resp->setBody("

ALL FUNCTIONS (that have args) ARE EXPECTING JSON AS DATA TYPE

/BankF/admin/close (POST)

 attempt  - admin password

Closes and Saves the server.

/BankF/user (POST)

 name  - name of the user being added

 init_pass  - initial password for the user being added

Adds a user to the bank

/BankF/admin/user (POST)

 name  - name of the user being added

 attempt  - admin password required to add user with balance

 init_bal  - initial balance for user being added

 init_pass  - initial password for user being added

Adds a user with initial balance

/BankF/sendfunds (POST)

 a_name  - sender's name

 b_name  - reciever's name

 amount  - amount being sent

 attempt  - password of sender

Sends money from one user to another

/BankF/changepass (PATCH)

 name  - name of user's password being changes

 attempt  - password of user being changed

 new_pass  - new password to replace the current user's password

Changes password of a user, returns -1 if user doesnt exist

/BankF/admin/{name}/bal (PATCH)

 name  - the name of the user being set

 attempt  - the admin password required

 amount  - the new balance of the user

Sets the balance of a user

/BankF/help (GET)

the page you're looking at right now!

/BankF/vpass (POST)

 name  - name of user being verified

 attempt  - password being verified

returns 0 or 1 based on if [attempt] is equal to the password of the user [name], or -1 if user does not exist. The intended usage for this function is for connected services

/BankF/contains/{name} (GET)

returns a 0 or 1 based on if the bank contains the user

/BankF/{name}/bal (GET)

returns the balance of a given user's name, if -1 that means the user does not exist

/BankF/allusers/ (GET)

returns an array of all users in the bank, please consider checking out vpass or contains instead of this function when you can.

/BankF/admin/vpass (POST)

 attempt  - admin password

Verifies if password entered is admin password

/BankF/user (DELETE)

 name  - name of user being deleted

 attempt  - password of user being deleted

Deletes a user with the password of the user as verification

/BankF/admin/user (DELETE)

 name  - name of user being deleted

 attempt  - admin password

Deletes a user with admin password as verification

"); + resp->setBody("

ALL FUNCTIONS (that have args) ARE EXPECTING JSON AS DATA TYPE

/BankF/admin/close (POST)

 attempt  - admin password

Closes and Saves the server.

/BankF/user (POST)

 name  - name of the user being added

 init_pass  - initial password for the user being added

Adds a user to the bank

/BankF/admin/user (POST)

 name  - name of the user being added

 attempt  - admin password required to add user with balance

 init_bal  - initial balance for user being added

 init_pass  - initial password for user being added

Adds a user with initial balance

/BankF/sendfunds (POST)

 a_name  - sender's name

 b_name  - reciever's name

 amount  - amount being sent

 attempt  - password of sender

Sends money from one user to another

/BankF/changepass (PATCH)

 name  - name of user's password being changes

 attempt  - password of user being changed

 new_pass  - new password to replace the current user's password

Changes password of a user, returns -1 if user doesnt exist

/BankF/admin/{name}/bal (PATCH)

 name  - the name of the user being set

 attempt  - the admin password required

 amount  - the new balance of the user

Sets the balance of a user

/BankF/help (GET)

the page you're looking at right now!

/BankF/vpass (POST)

 name  - name of user being verified

 attempt  - password being verified

returns 0 or 1 based on if [attempt] is equal to the password of the user [name], or -1 if user does not exist. The intended usage for this function is for connected services

/BankF/contains/{name} (GET)

returns a 0 or 1 based on if the bank contains the user

/BankF/{name}/bal (GET)

returns the balance of a given user's name, if -1 that means the user does not exist

/BankF/admin/vpass (POST)

 attempt  - admin password

Verifies if password entered is admin password

/BankF/user (DELETE)

 name  - name of user being deleted

 attempt  - password of user being deleted

Deletes a user with the password of the user as verification

/BankF/admin/user (DELETE)

 name  - name of user being deleted

 attempt  - admin password

Deletes a user with admin password as verification

"); resp->setExpiredTime(0); callback(resp); } @@ -106,10 +106,6 @@ public: GEN_BODY JSON(bank.SetBal(name, body["attempt"].asCString(), body["amount"].asUInt())); } - void AllUsers(req_args) - { - JSON(bank.AllUsers()); - } void AdminVerifyPass(req_args) { GEN_BODY @@ -129,7 +125,6 @@ public: METHOD_ADD(BankF::VerifyPassword, "/vpass", Post, Options); METHOD_ADD(BankF::Contains, "/contains/{name}", Get, Options); METHOD_ADD(BankF::GetBal, "/{name}/bal", Get, Options); - METHOD_ADD(BankF::AllUsers, "/allusers", Get, Options); METHOD_ADD(BankF::AdminVerifyPass, "/admin/vpass", Post, Options); METHOD_ADD(BankF::DelUser, "/user", Delete, Options);