mirror of
https://github.com/Expand-sys/CCash
synced 2026-03-23 04:57:09 +11:00
🐛 fixed unintuitive design of AddUser having password in body
This commit is contained in:
parent
6d9d7cff71
commit
eed41a1de1
7 changed files with 29 additions and 29 deletions
2
help.md
2
help.md
|
|
@ -39,7 +39,7 @@
|
||||||
# User Management
|
# User Management
|
||||||
| Name | Path | Method | A | Description |
|
| Name | Path | Method | A | Description |
|
||||||
| :----------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
| :----------: | :------------------------------------- | :----: | :---: | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
||||||
| AddUser | /user/{name} | POST | false | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the body |
|
| AddUser | /user/{name} | POST | true | registers a user with the name `{name}`, balance of 0 and a password of the password supplied in the header |
|
||||||
| AdminAddUser | /admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password supplied by the body of the request |
|
| AdminAddUser | /admin/user/{name}?init_bal={init_bal} | POST | true | if the password supplied in the header matches the admin password, then it registers a user with the name `{name}`, balance of `init_bal` and a password supplied by the body of the request |
|
||||||
| DelUser | /user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted |
|
| DelUser | /user/{name} | DELETE | true | if the password supplied in the header matches the user `{name}`'s password, then the user is deleted |
|
||||||
| AdminDelUser | /admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted |
|
| AdminDelUser | /admin/user/{name} | DELETE | true | if the password supplied in the header matches the admin password, then the user is deleted |
|
||||||
|
|
@ -33,24 +33,24 @@ private:
|
||||||
public:
|
public:
|
||||||
std::string admin_pass;
|
std::string admin_pass;
|
||||||
|
|
||||||
int_fast8_t AddUser(const std::string &name, std::string &&init_pass);
|
int_fast8_t AddUser(const std::string &name, const std::string &init_pass);
|
||||||
int_fast8_t AdminAddUser(std::string_view attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass);
|
int_fast8_t AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass);
|
||||||
|
|
||||||
int_fast8_t DelUser(const std::string &name, std::string_view attempt);
|
int_fast8_t DelUser(const std::string &name, const std::string &attempt);
|
||||||
int_fast8_t AdminDelUser(const std::string &name, std::string_view attempt);
|
int_fast8_t AdminDelUser(const std::string &name, const std::string &attempt);
|
||||||
|
|
||||||
int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, std::string_view attempt);
|
int_fast8_t SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt);
|
||||||
|
|
||||||
bool Contains(const std::string &name) const;
|
bool Contains(const std::string &name) const;
|
||||||
bool AdminVerifyPass(std::string_view attempt);
|
bool AdminVerifyPass(const std::string &attempt);
|
||||||
|
|
||||||
int_fast8_t SetBal(const std::string &name, std::string_view attempt, uint32_t amount);
|
int_fast8_t SetBal(const std::string &name, const std::string &attempt, uint32_t amount);
|
||||||
int_fast64_t GetBal(const std::string &name) const;
|
int_fast64_t GetBal(const std::string &name) const;
|
||||||
|
|
||||||
int_fast8_t VerifyPassword(const std::string &name, std::string_view attempt) const;
|
int_fast8_t VerifyPassword(const std::string &name, const std::string &attempt) const;
|
||||||
int_fast8_t ChangePassword(const std::string &name, std::string_view attempt, std::string &&new_pass);
|
int_fast8_t ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass);
|
||||||
|
|
||||||
Json::Value GetLogs(const std::string &name, std::string_view attempt);
|
Json::Value GetLogs(const std::string &name, const std::string &attempt);
|
||||||
|
|
||||||
void Save();
|
void Save();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ public:
|
||||||
BankF(Bank *b);
|
BankF(Bank *b);
|
||||||
void Help(req_args) const;
|
void Help(req_args) const;
|
||||||
void Close(req_args) const;
|
void Close(req_args) const;
|
||||||
void AddUser(req_args, std::string &&name) const;
|
void AddUser(req_args, const std::string &name) const;
|
||||||
void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const;
|
void AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const;
|
||||||
void DelUser(req_args, const std::string &name) const;
|
void DelUser(req_args, const std::string &name) const;
|
||||||
void AdminDelUser(req_args, const std::string &name) const;
|
void AdminDelUser(req_args, const std::string &name) const;
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,8 @@ struct User
|
||||||
uint64_t password;
|
uint64_t password;
|
||||||
Log log;
|
Log log;
|
||||||
|
|
||||||
User(std::string &&init_pass);
|
User(const std::string &init_pass);
|
||||||
User(uint32_t init_bal, std::string &&init_pass);
|
User(uint32_t init_bal, const std::string &init_pass);
|
||||||
User(uint32_t init_bal, uint64_t init_pass);
|
User(uint32_t init_bal, uint64_t init_pass);
|
||||||
User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j);
|
User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j);
|
||||||
|
|
||||||
|
|
|
||||||
22
src/bank.cpp
22
src/bank.cpp
|
|
@ -1,6 +1,6 @@
|
||||||
#include "bank.h"
|
#include "bank.h"
|
||||||
|
|
||||||
int_fast8_t Bank::AddUser(const std::string &name, std::string &&init_pass)
|
int_fast8_t Bank::AddUser(const std::string &name, const std::string &init_pass)
|
||||||
{
|
{
|
||||||
if (name.size() > max_name_size)
|
if (name.size() > max_name_size)
|
||||||
{
|
{
|
||||||
|
|
@ -9,7 +9,7 @@ int_fast8_t Bank::AddUser(const std::string &name, std::string &&init_pass)
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
if (users.try_emplace_l(
|
if (users.try_emplace_l(
|
||||||
name, [](User &) {}, std::move(init_pass)))
|
name, [](User &) {}, init_pass))
|
||||||
{
|
{
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -19,7 +19,7 @@ int_fast8_t Bank::AddUser(const std::string &name, std::string &&init_pass)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int_fast8_t Bank::AdminAddUser(std::string_view attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass)
|
int_fast8_t Bank::AdminAddUser(const std::string &attempt, std::string &&name, uint32_t init_bal, std::string &&init_pass)
|
||||||
{
|
{
|
||||||
if (name.size() > max_name_size)
|
if (name.size() > max_name_size)
|
||||||
{
|
{
|
||||||
|
|
@ -42,7 +42,7 @@ int_fast8_t Bank::AdminAddUser(std::string_view attempt, std::string &&name, uin
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int_fast8_t Bank::DelUser(const std::string &name, std::string_view attempt)
|
int_fast8_t Bank::DelUser(const std::string &name, const std::string &attempt)
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
bool state = false;
|
bool state = false;
|
||||||
|
|
@ -62,7 +62,7 @@ int_fast8_t Bank::DelUser(const std::string &name, std::string_view attempt)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
int_fast8_t Bank::AdminDelUser(const std::string &name, std::string_view attempt)
|
int_fast8_t Bank::AdminDelUser(const std::string &name, const std::string &attempt)
|
||||||
{
|
{
|
||||||
std::shared_lock<std::shared_mutex> lock{size_l};
|
std::shared_lock<std::shared_mutex> lock{size_l};
|
||||||
bool state = false;
|
bool state = false;
|
||||||
|
|
@ -83,7 +83,7 @@ int_fast8_t Bank::AdminDelUser(const std::string &name, std::string_view attempt
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, std::string_view attempt)
|
int_fast8_t Bank::SendFunds(const std::string &a_name, const std::string &b_name, uint32_t amount, const std::string &attempt)
|
||||||
{
|
{
|
||||||
//cant send money to self, from self or amount is 0
|
//cant send money to self, from self or amount is 0
|
||||||
if (a_name == b_name || !amount)
|
if (a_name == b_name || !amount)
|
||||||
|
|
@ -159,12 +159,12 @@ bool Bank::Contains(const std::string &name) const
|
||||||
{
|
{
|
||||||
return users.contains(name);
|
return users.contains(name);
|
||||||
}
|
}
|
||||||
bool Bank::AdminVerifyPass(std::string_view attempt)
|
bool Bank::AdminVerifyPass(const std::string &attempt)
|
||||||
{
|
{
|
||||||
return (admin_pass == attempt);
|
return (admin_pass == attempt);
|
||||||
}
|
}
|
||||||
|
|
||||||
int_fast8_t Bank::SetBal(const std::string &name, std::string_view attempt, uint32_t amount)
|
int_fast8_t Bank::SetBal(const std::string &name, const std::string &attempt, uint32_t amount)
|
||||||
{
|
{
|
||||||
if (admin_pass != attempt)
|
if (admin_pass != attempt)
|
||||||
{
|
{
|
||||||
|
|
@ -190,7 +190,7 @@ int_fast64_t Bank::GetBal(const std::string &name) const
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
int_fast8_t Bank::VerifyPassword(const std::string &name, std::string_view attempt) const
|
int_fast8_t Bank::VerifyPassword(const std::string &name, const std::string &attempt) const
|
||||||
{
|
{
|
||||||
int_fast8_t res = ErrorResponse::UserNotFound;
|
int_fast8_t res = ErrorResponse::UserNotFound;
|
||||||
users.if_contains(name, [&res, &attempt](const User &u) {
|
users.if_contains(name, [&res, &attempt](const User &u) {
|
||||||
|
|
@ -198,7 +198,7 @@ int_fast8_t Bank::VerifyPassword(const std::string &name, std::string_view attem
|
||||||
});
|
});
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
int_fast8_t Bank::ChangePassword(const std::string &name, std::string_view attempt, std::string &&new_pass)
|
int_fast8_t Bank::ChangePassword(const std::string &name, const std::string &attempt, std::string &&new_pass)
|
||||||
{
|
{
|
||||||
int_fast8_t res = ErrorResponse::UserNotFound;
|
int_fast8_t res = ErrorResponse::UserNotFound;
|
||||||
users.modify_if(name, [&res, &attempt, &new_pass](User &u) {
|
users.modify_if(name, [&res, &attempt, &new_pass](User &u) {
|
||||||
|
|
@ -214,7 +214,7 @@ int_fast8_t Bank::ChangePassword(const std::string &name, std::string_view attem
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
Json::Value Bank::GetLogs(const std::string &name, std::string_view attempt)
|
Json::Value Bank::GetLogs(const std::string &name, const std::string &attempt)
|
||||||
{
|
{
|
||||||
Json::Value res;
|
Json::Value res;
|
||||||
if (!users.if_contains(name, [&res, &attempt](const User &u) {
|
if (!users.if_contains(name, [&res, &attempt](const User &u) {
|
||||||
|
|
|
||||||
|
|
@ -52,9 +52,9 @@ void BankF::Close(req_args) const
|
||||||
}
|
}
|
||||||
JSON(res);
|
JSON(res);
|
||||||
}
|
}
|
||||||
void BankF::AddUser(req_args, std::string &&name) const
|
void BankF::AddUser(req_args, const std::string &name) const
|
||||||
{
|
{
|
||||||
JSON(bank.AddUser(std::move(name), std::string(req->getBody())));
|
JSON(bank.AddUser(std::move(name), PASS_HEADER));
|
||||||
}
|
}
|
||||||
void BankF::AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const
|
void BankF::AdminAddUser(req_args, std::string &&name, uint32_t init_bal) const
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
*
|
*
|
||||||
* @param init_pass initial password
|
* @param init_pass initial password
|
||||||
*/
|
*/
|
||||||
User::User(std::string &&init_pass) : password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
|
User::User(const std::string &init_pass) : password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief User Constructor for admins
|
* @brief User Constructor for admins
|
||||||
|
|
@ -13,7 +13,7 @@ User::User(std::string &&init_pass) : password(XXH3_64bits(init_pass.data(), ini
|
||||||
* @param init_bal initial balance
|
* @param init_bal initial balance
|
||||||
* @param init_pass initial password
|
* @param init_pass initial password
|
||||||
*/
|
*/
|
||||||
User::User(uint32_t init_bal, std::string &&init_pass) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
|
User::User(uint32_t init_bal, const std::string &init_pass) : balance(init_bal), password(XXH3_64bits(init_pass.data(), init_pass.size())) {}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief User Constructor for loading
|
* @brief User Constructor for loading
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue