diff --git a/internal/api/controller/user/register.go b/internal/api/controller/user/register.go index e71b22d13..3230568c2 100644 --- a/internal/api/controller/user/register.go +++ b/internal/api/controller/user/register.go @@ -7,19 +7,17 @@ package user import ( "context" "fmt" + "github.com/harness/gitness/internal/api/usererror" "github.com/harness/gitness/internal/token" "github.com/harness/gitness/types" ) -/* - * Register creates a new user and returns a new session token on success. - * This differs from the Create method as it doesn't require auth, but has limited - * functionalities (unable to create admin user for example). - */ +// Register creates a new user and returns a new session token on success. +// This differs from the Create method as it doesn't require auth, but has limited +// functionalities (unable to create admin user for example). func (c *Controller) Register(ctx context.Context, in *CreateInput, config *types.Config) (*types.TokenResponse, error) { - signUpFlag := config.AllowSignUp if !signUpFlag { return nil, usererror.BadRequest("User sign-up is disabled.") diff --git a/internal/api/controller/user/register_check.go b/internal/api/controller/user/register_check.go index 371fa0796..04fd3cd90 100644 --- a/internal/api/controller/user/register_check.go +++ b/internal/api/controller/user/register_check.go @@ -11,11 +11,8 @@ import ( ) // RegisterCheck checks the DB and env config flag to return boolean -// which represents if a user sign-up is allowed or not - -func (c *Controller) RegisterCheck(ctx context.Context, - config *types.Config) (*bool, error) { - +// which represents if a user sign-up is allowed or not. +func (c *Controller) RegisterCheck(ctx context.Context, config *types.Config) (*bool, error) { usrCount, err := c.principalStore.CountUsers(ctx) if err != nil { return nil, err diff --git a/internal/api/handler/account/register_check.go b/internal/api/handler/account/register_check.go index d30edf1b5..30a96abcb 100644 --- a/internal/api/handler/account/register_check.go +++ b/internal/api/handler/account/register_check.go @@ -13,7 +13,7 @@ import ( ) // HandleRegisterCheck returns an http.HandlerFunc that processes an http.Request -// and returns a boolean true/false if user registration is allowed or not +// and returns a boolean true/false if user registration is allowed or not. func HandleRegisterCheck(userCtrl *user.Controller, config *types.Config) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { ctx := r.Context() diff --git a/internal/api/openapi/account.go b/internal/api/openapi/account.go index d8deef248..13733fb20 100644 --- a/internal/api/openapi/account.go +++ b/internal/api/openapi/account.go @@ -24,6 +24,10 @@ type registerRequest struct { user.CreateInput } +type registrationCheckResponse struct { + Allowed bool `json:"allowed"` +} + // helper function that constructs the openapi specification // for the account registration and login endpoints. func buildAccount(reflector *openapi3.Reflector) { @@ -55,4 +59,13 @@ func buildAccount(reflector *openapi3.Reflector) { _ = reflector.SetJSONResponse(&onRegister, new(usererror.Error), http.StatusInternalServerError) _ = reflector.SetJSONResponse(&onRegister, new(usererror.Error), http.StatusBadRequest) _ = reflector.Spec.AddOperation(http.MethodPost, "/register", onRegister) + + onRegistrationCheck := openapi3.Operation{} + onRegistrationCheck.WithTags("account") + onRegistrationCheck.WithMapOfAnything(map[string]interface{}{"operationId": "onRegistrationCheck"}) + _ = reflector.SetRequest(&onRegistrationCheck, nil, http.MethodGet) + _ = reflector.SetJSONResponse(&onRegistrationCheck, new(registrationCheckResponse), http.StatusOK) + _ = reflector.SetJSONResponse(&onRegistrationCheck, new(usererror.Error), http.StatusInternalServerError) + _ = reflector.SetJSONResponse(&onRegistrationCheck, new(usererror.Error), http.StatusBadRequest) + _ = reflector.Spec.AddOperation(http.MethodGet, "/registration-check", onRegistrationCheck) }