diff --git a/CMakeLists.txt b/CMakeLists.txt index 89b75ca..f81d254 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,6 +67,12 @@ else() set(MULTI_THREADED_VAL true) endif() +if(DEFINED ADD_USER_OPEN) +set(ADD_USER_OPEN_VAL ${ADD_USER_OPEN}) +else() +set(ADD_USER_OPEN_VAL true) +endif() + if(DEFINED RETURN_ON_DEL_NAME) set(RETURN_ON_DEL_VAL true) set(RETURN_ON_DEL_NAME_VAL "\"" + ${RETURN_ON_DEL_NAME} + "\"") diff --git a/ccash_config.hpp.in b/ccash_config.hpp.in index 18141c7..3c6cfca 100644 --- a/ccash_config.hpp.in +++ b/ccash_config.hpp.in @@ -37,4 +37,6 @@ etc */ #define API_VERSION 1 -#define MULTI_THREADED @MULTI_THREADED_VAL@ \ No newline at end of file +#define MULTI_THREADED @MULTI_THREADED_VAL@ + +#define ADD_USER_OPEN @ADD_USER_OPEN_VAL@ \ No newline at end of file diff --git a/docs/building.md b/docs/building.md index d8ced92..c02f185 100644 --- a/docs/building.md +++ b/docs/building.md @@ -66,14 +66,15 @@ cp ../config/config.json config.json ### CMake Flags there are multiple flags responsible configuring CCash: -| name | default | description | pros | cons | -| :--------------------- | :--------------: | ------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | -------------------------------------------------------- | -| USER_SAVE_LOC | "users.dat" | where the users are saved | `N/A` | `N/A` | +| name | default | description | pros | cons | +| :--------------------- | :-----------: | ------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | -------------------------------------------------------- | +| USER_SAVE_LOC | "users.dat" | where the users are saved | `N/A` | `N/A` | | DROGON_CONFIG_LOC | "config.json" | where the config is located | `N/A` | `N/A` | -| MAX_LOG_SIZE | 100 | max number of logs per user, last `n` transactions. If both this and pre log are toggled to 0 logs will not be compiled. | large history | higher memory usage | -| CONSERVATIVE_DISK_SAVE | `true` | when `true` only saves when changes are made | low # of disk operations | some atomic overhead | -| MULTI_THREADED | `true` | when `true` the program is compiled to utilize `n` threads which corresponds to how many Cores your CPU has, plus 1 for saving | speed | memory lock overhead is wasteful on single core machines | -| RETURN_ON_DEL_NAME | `N/A` | when defined, return on delete will be toggled and any accounts deleted will send their funds to the defined account, this prevent currency destruction | prevents destruction of currency | deleting accounts is made slower | +| MAX_LOG_SIZE | 100 | max number of logs per user, last `n` transactions. If both this and pre log are toggled to 0 logs will not be compiled. | large history | higher memory usage | +| CONSERVATIVE_DISK_SAVE | `true` | when `true` only saves when changes are made | low # of disk operations | some atomic overhead | +| MULTI_THREADED | `true` | when `true` the program is compiled to utilize `n` threads which corresponds to how many Cores your CPU has, plus 1 for saving | speed | memory lock overhead is wasteful on single core machines | +| RETURN_ON_DEL_NAME | `N/A` | when defined, return on delete will be toggled and any accounts deleted will send their funds to the defined account, this prevent currency destruction | prevents destruction of currency | deleting accounts is made slower | +| ADD_USER_OPEN | `true` | anybody can make a new account, if set to false only admins can add accounts via `AdminAddUser()` | `N/A` | spamming new users | EXAMPLE: diff --git a/docs/connected_services/how_to/endpoints.md b/docs/connected_services/how_to/endpoints.md index 36cc0f7..eb9c9a8 100644 --- a/docs/connected_services/how_to/endpoints.md +++ b/docs/connected_services/how_to/endpoints.md @@ -102,7 +102,7 @@ Valid ### User Management endpoints | name | purpose | json input | path | HTTP Method | correct status | return type | return value | Jresp | Jreq | A | U | | :----------- | --------------------------------------- | --------------------------------------------- | -------------------------- | :---------: | :------------: | :---------: | :----------: | :----------------: | :----------------------: | :----------------------: | :----------------------: | -| AddUser | adding a user with a balance of 0 | {"name":string,"pass":string} | api/v1/user/register | `POST` | 204 | `N/A` | `N/A` | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | +| AddUser | adding a user with a balance of 0 | {"name":string,"pass":string} | api/v1/user/register | `POST` | 204 | `N/A` | `N/A` | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | | AdminAddUser | adding a user with an arbitrary balance | {"name":string,"amount":uint32,"pass":string} | api/v1/admin/user/register | `POST` | 204 | `N/A` | `N/A` | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | | DelSelf | deletes a user | `N/A` | api/v1/user/delete | `DELETE` | 204 | `N/A` | `N/A` | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | | AdminDelUser | deletes a given user `{name}` | {"name":string} | api/v1/admin/user/delete | `DELETE` | 204 | `N/A` | `N/A` | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | @@ -110,7 +110,7 @@ Valid ### User Management endpoint errors | name | 400 | 401 | 404 | 406 | 409 | | :----------- | :----------------------: | :----------------------: | :----------------------: | :----------------: | :----------------------: | -| AddUser | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_check_mark: | +| AddUser | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | | AdminAddUser | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_check_mark: | | DelSelf | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: | :heavy_check_mark: | :heavy_multiplication_x: | | AdminDelUser | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_check_mark: | :heavy_multiplication_x: | diff --git a/src/bank_api.cpp b/src/bank_api.cpp index 8d8e440..72e1cb5 100644 --- a/src/bank_api.cpp +++ b/src/bank_api.cpp @@ -205,7 +205,7 @@ void api::AdminVerifyAccount(req_args) } void api::ApiProperties(req_args) { - std::string info = "{\"version\":" + std::to_string(API_VERSION) + ",\"max_log\":" + std::to_string(MAX_LOG_SIZE); + std::string info = "{\"version\":" + std::to_string(API_VERSION) + ",\"max_log\":" + std::to_string(MAX_LOG_SIZE) + ",\"add_user_open\":" + std::to_string(ADD_USER_OPEN); if constexpr (RETURN_ON_DEL) { info += ",\"return_on_del\":\"" + std::string(return_account) + "\"}"; @@ -257,28 +257,39 @@ void api::PruneUsers(req_args) void api::AddUser(req_args) { - SIMD_JSON_GEN; - BankResponse res; - if (doc.error()) + if constexpr (ADD_USER_OPEN) { - res = BankResponse{k400BadRequest, "\"Invalid JSON\""}; - } - else - { - auto name = doc["name"].get_string(); - auto pass = doc["pass"].get_string(); - if (name.error() || pass.error()) + + SIMD_JSON_GEN; + BankResponse res; + if (doc.error()) { - res = BankResponse{k400BadRequest, "\"Missing JSON arg(s)\""}; + res = BankResponse{k400BadRequest, "\"Invalid JSON\""}; } else { - StrFromSV_Wrapper name_val(name.value()); - StrFromSV_Wrapper pass_val(pass.value()); - res = Bank::AddUser(name_val.str, 0, pass_val.str); + auto name = doc["name"].get_string(); + auto pass = doc["pass"].get_string(); + if (name.error() || pass.error()) + { + res = BankResponse{k400BadRequest, "\"Missing JSON arg(s)\""}; + } + else + { + StrFromSV_Wrapper name_val(name.value()); + StrFromSV_Wrapper pass_val(pass.value()); + res = Bank::AddUser(name_val.str, 0, pass_val.str); + } } + RESPONSE_PARSE(std::move(res)); + } + else + { + auto resp = HttpResponse::newCustomHttpResponse(BankResponse{k404NotFound, "\"AddUser is Disabled\""}); + CORS; + CACHE_FOREVER; + callback(resp); } - RESPONSE_PARSE(std::move(res)); } void api::AdminAddUser(req_args) {