fix: Update the code to use registerInput along with createInput for user registration and subsequent creation

This commit is contained in:
Akhilesh Pandey 2023-08-11 00:52:05 +05:30
parent 084443f81e
commit a8f6f9528a
12 changed files with 33 additions and 22 deletions

View File

@ -35,7 +35,7 @@ func (c *registerCommand) run(*kingpin.ParseContext) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Minute) ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel() defer cancel()
input := &user.CreateInput{ input := &user.RegisterInput{
UID: uid, UID: uid,
Email: email, Email: email,
DisplayName: displayName, DisplayName: displayName,

View File

@ -67,7 +67,7 @@ func (c *HTTPClient) Login(ctx context.Context, input *user.LoginInput) (*types.
} }
// Register registers a new user and returns a JWT token. // Register registers a new user and returns a JWT token.
func (c *HTTPClient) Register(ctx context.Context, input *user.CreateInput) (*types.TokenResponse, error) { func (c *HTTPClient) Register(ctx context.Context, input *user.RegisterInput) (*types.TokenResponse, error) {
out := new(types.TokenResponse) out := new(types.TokenResponse)
uri := fmt.Sprintf("%s/api/v1/register", c.base) uri := fmt.Sprintf("%s/api/v1/register", c.base)
err := c.post(ctx, uri, true, input, out) err := c.post(ctx, uri, true, input, out)

View File

@ -17,7 +17,7 @@ type Client interface {
Login(ctx context.Context, input *user.LoginInput) (*types.TokenResponse, error) Login(ctx context.Context, input *user.LoginInput) (*types.TokenResponse, error)
// Register registers a new user and returns a JWT token. // Register registers a new user and returns a JWT token.
Register(ctx context.Context, input *user.CreateInput) (*types.TokenResponse, error) Register(ctx context.Context, input *user.RegisterInput) (*types.TokenResponse, error)
// Self returns the currently authenticated user. // Self returns the currently authenticated user.
Self(ctx context.Context) (*types.User, error) Self(ctx context.Context) (*types.User, error)

View File

@ -20,6 +20,14 @@ import (
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
) )
type RegisterInput struct {
Email string `json:"email"`
DisplayName string `json:"display_name"`
UID string `json:"uid"`
Password string `json:"password"`
}
// CreateInput is the input used for create operations. // CreateInput is the input used for create operations.
// On purpose don't expose admin, has to be enabled explicitly. // On purpose don't expose admin, has to be enabled explicitly.
type CreateInput struct { type CreateInput struct {

View File

@ -18,7 +18,7 @@ import (
// This differs from the Create method as it doesn't require auth, but has limited // This differs from the Create method as it doesn't require auth, but has limited
// functionalities (unable to create admin user for example). // functionalities (unable to create admin user for example).
func (c *Controller) Register(ctx context.Context, sysCtrl *system.Controller, func (c *Controller) Register(ctx context.Context, sysCtrl *system.Controller,
in *CreateInput) (*types.TokenResponse, error) { in *RegisterInput) (*types.TokenResponse, error) {
signUpAllowed, err := sysCtrl.IsUserRegistrationAllowed(ctx) signUpAllowed, err := sysCtrl.IsUserRegistrationAllowed(ctx)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -19,7 +19,7 @@ func HandleRegister(userCtrl *user.Controller, sysCtrl *system.Controller) http.
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
in := new(user.CreateInput) in := new(user.RegisterInput)
err := json.NewDecoder(r.Body).Decode(in) err := json.NewDecoder(r.Body).Decode(in)
if err != nil { if err != nil {
render.BadRequestf(w, "Invalid request body: %s.", err) render.BadRequestf(w, "Invalid request body: %s.", err)

View File

@ -11,13 +11,13 @@ import (
"github.com/harness/gitness/internal/api/render" "github.com/harness/gitness/internal/api/render"
) )
type ConfigsOutput struct { type ConfigOutput struct {
SignUpAllowed bool `json:"sign_up_allowed"` SignUpAllowed bool `json:"sign_up_allowed"`
} }
// HandleListConfig returns an http.HandlerFunc that processes an http.Request // HandleGetConfig returns an http.HandlerFunc that processes an http.Request
// and returns a struct containing all system configs exposed to the users. // and returns a struct containing all system configs exposed to the users.
func HandleListConfig(sysCtrl *system.Controller) http.HandlerFunc { func HandleGetConfig(sysCtrl *system.Controller) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context() ctx := r.Context()
@ -26,7 +26,7 @@ func HandleListConfig(sysCtrl *system.Controller) http.HandlerFunc {
render.TranslatedUserError(w, err) render.TranslatedUserError(w, err)
return return
} }
render.JSON(w, http.StatusOK, ConfigsOutput{ render.JSON(w, http.StatusOK, ConfigOutput{
SignUpAllowed: signUpAllowedCheck, SignUpAllowed: signUpAllowedCheck,
}) })
} }

View File

@ -21,7 +21,7 @@ type loginRequest struct {
// request to register an account. // request to register an account.
type registerRequest struct { type registerRequest struct {
user.CreateInput user.RegisterInput
} }
// helper function that constructs the openapi specification // helper function that constructs the openapi specification

View File

@ -16,12 +16,12 @@ import (
// helper function that constructs the openapi specification // helper function that constructs the openapi specification
// for the system registration config endpoints. // for the system registration config endpoints.
func buildSystem(reflector *openapi3.Reflector) { func buildSystem(reflector *openapi3.Reflector) {
onListConfigs := openapi3.Operation{} onGetConfig := openapi3.Operation{}
onListConfigs.WithTags("system") onGetConfig.WithTags("system")
onListConfigs.WithMapOfAnything(map[string]interface{}{"operationId": "onListConfigs"}) onGetConfig.WithMapOfAnything(map[string]interface{}{"operationId": "getSystemConfig"})
_ = reflector.SetRequest(&onListConfigs, nil, http.MethodGet) _ = reflector.SetRequest(&onGetConfig, nil, http.MethodGet)
_ = reflector.SetJSONResponse(&onListConfigs, new(system.ConfigsOutput), http.StatusOK) _ = reflector.SetJSONResponse(&onGetConfig, new(system.ConfigOutput), http.StatusOK)
_ = reflector.SetJSONResponse(&onListConfigs, new(usererror.Error), http.StatusInternalServerError) _ = reflector.SetJSONResponse(&onGetConfig, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&onListConfigs, new(usererror.Error), http.StatusBadRequest) _ = reflector.SetJSONResponse(&onGetConfig, new(usererror.Error), http.StatusBadRequest)
_ = reflector.Spec.AddOperation(http.MethodGet, "/system/config", onListConfigs) _ = reflector.Spec.AddOperation(http.MethodGet, "/system/config", onGetConfig)
} }

View File

@ -408,7 +408,7 @@ func setupSystem(r chi.Router, sysCtrl *system.Controller) {
r.Route("/system", func(r chi.Router) { r.Route("/system", func(r chi.Router) {
r.Get("/health", handlersystem.HandleHealth) r.Get("/health", handlersystem.HandleHealth)
r.Get("/version", handlersystem.HandleVersion) r.Get("/version", handlersystem.HandleVersion)
r.Get("/config", handlersystem.HandleListConfig(sysCtrl)) r.Get("/config", handlersystem.HandleGetConfig(sysCtrl))
}) })
} }

View File

@ -52,7 +52,7 @@ func (mr *MockClientMockRecorder) Login(arg0, arg1 interface{}) *gomock.Call {
} }
// Register mocks base method. // Register mocks base method.
func (m *MockClient) Register(arg0 context.Context, arg1 *user.CreateInput) (*types.TokenResponse, error) { func (m *MockClient) Register(arg0 context.Context, arg1 *user.RegisterInput) (*types.TokenResponse, error) {
m.ctrl.T.Helper() m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Register", arg0, arg1) ret := m.ctrl.Call(m, "Register", arg0, arg1)
ret0, _ := ret[0].(*types.TokenResponse) ret0, _ := ret[0].(*types.TokenResponse)

View File

@ -13,8 +13,11 @@ type Config struct {
// InstanceID specifis the ID of the gitness instance. // InstanceID specifis the ID of the gitness instance.
// NOTE: If the value is not provided the hostname of the machine is used. // NOTE: If the value is not provided the hostname of the machine is used.
InstanceID string `envconfig:"GITNESS_INSTANCE_ID"` InstanceID string `envconfig:"GITNESS_INSTANCE_ID"`
Debug bool `envconfig:"GITNESS_DEBUG"`
Trace bool `envconfig:"GITNESS_TRACE"` // GracefulShutdownTime defines the max time we wait when shutting down a server. Debug bool `envconfig:"GITNESS_DEBUG"`
Trace bool `envconfig:"GITNESS_TRACE"`
// GracefulShutdownTime defines the max time we wait when shutting down a server.
// 5min should be enough for most git clones to complete. // 5min should be enough for most git clones to complete.
GracefulShutdownTime time.Duration `envconfig:"GITNESS_GRACEFUL_SHUTDOWN_TIME" default:"300s"` GracefulShutdownTime time.Duration `envconfig:"GITNESS_GRACEFUL_SHUTDOWN_TIME" default:"300s"`