diff --git a/doc/api.oa3 b/doc/api.oa3 index d3672468..f08ff0b4 100644 --- a/doc/api.oa3 +++ b/doc/api.oa3 @@ -266,8 +266,32 @@ paths: fileName: type: string format: binary - + /account/available: + get: + tags: + - account + description: Check if any public accounts are available + operationId: get-account-available + parameters: + - name: public + in: query + description: for open access accounts + required: false + schema: + type: boolean + responses: + '200': + description: available public accounts + content: + application/json: + schema: + type: integer + format: uint32 + '500': + description: internal server error + + /account/username: get: tags: - account @@ -2963,7 +2987,8 @@ components: type: object required: - domain - - publicLimit + - openAccess + - accountLimit - accountStorage properties: domain: @@ -2971,6 +2996,11 @@ components: accountStorage: type: integer format: int64 + openAccess + type: boolean + accountLimit + type: integer + format: int64 AccountStatus: type: object diff --git a/net/server/internal/api_getAccountAvailable.go b/net/server/internal/api_getAccountAvailable.go new file mode 100644 index 00000000..c7cd97ed --- /dev/null +++ b/net/server/internal/api_getAccountAvailable.go @@ -0,0 +1,27 @@ +package databag + +import ( + "net/http" + "databag/internal/store" +) + +func GetAccountAvailable(w http.ResponseWriter, r *http.Request) { + + public := r.FormValue("public") == "true" + open := getBoolConfigValue(CONFIG_OPENACCESS, true) + limit := getNumConfigValue(CONFIG_ACCOUNTLIMIT, 16) + + var count int64 + if err := store.DB.Model(&store.Account{}).Count(&count).Error; err != nil { + ErrResponse(w, http.StatusInternalServerError, err) + return + } + + var available int64 + if (!public || open) && limit > count { + available = limit - count + } + + WriteResponse(w, &available) +} + diff --git a/net/server/internal/api_getNodeConfig.go b/net/server/internal/api_getNodeConfig.go index 5657b705..6463c069 100644 --- a/net/server/internal/api_getNodeConfig.go +++ b/net/server/internal/api_getNodeConfig.go @@ -15,7 +15,8 @@ func GetNodeConfig(w http.ResponseWriter, r *http.Request) { // get node config fields var config NodeConfig; config.Domain = getStrConfigValue(CONFIG_DOMAIN, ""); - config.PublicLimit = getNumConfigValue(CONFIG_PUBLICLIMIT, 0); + config.AccountLimit = getNumConfigValue(CONFIG_ACCOUNTLIMIT, 16); + config.OpenAccess = getBoolConfigValue(CONFIG_OPENACCESS, true); config.AccountStorage = getNumConfigValue(CONFIG_STORAGE, 0); WriteResponse(w, config); diff --git a/net/server/internal/api_setNodeConfig.go b/net/server/internal/api_setNodeConfig.go index 288b5858..96bc8825 100644 --- a/net/server/internal/api_setNodeConfig.go +++ b/net/server/internal/api_setNodeConfig.go @@ -33,11 +33,19 @@ func SetNodeConfig(w http.ResponseWriter, r *http.Request) { return res } - // upsert public limit config + // upsert account limit config if res := tx.Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "config_id"}}, DoUpdates: clause.AssignmentColumns([]string{"num_value"}), - }).Create(&store.Config{ConfigId: CONFIG_PUBLICLIMIT, NumValue: config.PublicLimit}).Error; res != nil { + }).Create(&store.Config{ConfigId: CONFIG_ACCOUNTLIMIT, NumValue: config.AccountLimit}).Error; res != nil { + return res + } + + // upsert account open access + if res := tx.Clauses(clause.OnConflict{ + Columns: []clause.Column{{Name: "config_id"}}, + DoUpdates: clause.AssignmentColumns([]string{"bool_value"}), + }).Create(&store.Config{ConfigId: CONFIG_ACCOUNTLIMIT, BoolValue: config.OpenAccess}).Error; res != nil { return res } diff --git a/net/server/internal/configUtil.go b/net/server/internal/configUtil.go index 75f1a519..796fbcc5 100644 --- a/net/server/internal/configUtil.go +++ b/net/server/internal/configUtil.go @@ -6,11 +6,12 @@ import ( "databag/internal/store" ) +const CONFIG_OPENACCESS = "open_access" +const CONFIG_ACCOUNTLIMIT = "account_limit" const CONFIG_CONFIGURED = "configured" const CONFIG_USERNAME = "username" const CONFIG_PASSWORD = "password" const CONFIG_DOMAIN = "domain" -const CONFIG_PUBLICLIMIT = "public_limit" const CONFIG_STORAGE = "storage" const CONFIG_ASSETPATH = "asset_path" const CONFIG_SCRIPTPATH = "script_path" diff --git a/net/server/internal/main_test.go b/net/server/internal/main_test.go index 958a5e89..37b9ffb9 100644 --- a/net/server/internal/main_test.go +++ b/net/server/internal/main_test.go @@ -54,7 +54,7 @@ func TestMain(m *testing.M) { } // config server - config := NodeConfig{Domain: "example.com", PublicLimit: 1024, AccountStorage: 4096} + config := NodeConfig{Domain: "example.com", AccountLimit: 1024, OpenAccess: true, AccountStorage: 4096} r, w, _ = NewRequest("PUT", "/admin/config", &config) SetBasicAuth(r, "admin:pass") SetNodeConfig(w, r) @@ -73,8 +73,11 @@ func TestMain(m *testing.M) { if check.Domain != "example.com" { panic("failed to set config domain"); } - if check.PublicLimit != 1024 { - panic("failed to set public limit"); + if check.AccountLimit != 1024 { + panic("failed to set account limit"); + } + if check.OpenAccess != true { + panic("failed to set open access"); } if check.AccountStorage != 4096 { panic("failed to set account storage"); diff --git a/net/server/internal/models.go b/net/server/internal/models.go index 8387b640..377c95d7 100644 --- a/net/server/internal/models.go +++ b/net/server/internal/models.go @@ -322,7 +322,9 @@ type NodeConfig struct { Domain string `json:"domain"` - PublicLimit int64 `json:"publicLimit"` + OpenAccess bool `json:"openAccess"` + + AccountLimit int64 `json:"accountLimit"` AccountStorage int64 `json:"accountStorage"` } diff --git a/net/server/internal/routers.go b/net/server/internal/routers.go index 4cb78835..7a1eb91f 100644 --- a/net/server/internal/routers.go +++ b/net/server/internal/routers.go @@ -119,9 +119,16 @@ var routes = Routes{ }, Route{ - "GetAccountUsername", + "GetAccountAvailable", strings.ToUpper("Get"), "/account/available", + GetAccountAvailable, + }, + + Route{ + "GetAccountUsername", + strings.ToUpper("Get"), + "/account/username", GetAccountUsername, }, diff --git a/net/web/src/App.js b/net/web/src/App.js index 3c821760..f0fda75a 100644 --- a/net/web/src/App.js +++ b/net/web/src/App.js @@ -22,14 +22,14 @@ function App() {