[CODE-617]: Create common function for registration check to reuse the logic

This commit is contained in:
Akhilesh Pandey 2023-07-20 22:36:52 +05:30
parent b8c239ed6d
commit ac375ff3fa
3 changed files with 22 additions and 9 deletions

View File

@ -48,3 +48,17 @@ func findUserFromEmail(ctx context.Context,
func isUserTokenType(tokenType enum.TokenType) bool { func isUserTokenType(tokenType enum.TokenType) bool {
return tokenType == enum.TokenTypePAT || tokenType == enum.TokenTypeSession return tokenType == enum.TokenTypePAT || tokenType == enum.TokenTypeSession
} }
func isUserRegistrationAllowed(ctx context.Context, principalStore store.PrincipalStore,
allowSignUpFlag bool) (*bool, error) {
usrCount, err := principalStore.CountUsers(ctx)
if err != nil {
return nil, err
}
check := false
if usrCount == 0 || allowSignUpFlag {
check = true
}
return &check, nil
}

View File

@ -18,8 +18,12 @@ import (
// functionalities (unable to create admin user for example). // functionalities (unable to create admin user for example).
func (c *Controller) Register(ctx context.Context, func (c *Controller) Register(ctx context.Context,
in *CreateInput, config *types.Config) (*types.TokenResponse, error) { in *CreateInput, config *types.Config) (*types.TokenResponse, error) {
signUpFlag := config.AllowSignUp signUpAllowed, err := isUserRegistrationAllowed(ctx, c.principalStore, config.AllowSignUp)
if !signUpFlag { if err != nil {
return nil, err
}
if !*signUpAllowed {
return nil, usererror.BadRequest("User sign-up is disabled.") return nil, usererror.BadRequest("User sign-up is disabled.")
} }

View File

@ -13,15 +13,10 @@ import (
// RegisterCheck checks the DB and env config flag to return boolean // RegisterCheck checks the DB and env config flag to return boolean
// which represents if a user sign-up is allowed or not. // which represents if a user sign-up is allowed or not.
func (c *Controller) RegisterCheck(ctx context.Context, config *types.Config) (*bool, error) { func (c *Controller) RegisterCheck(ctx context.Context, config *types.Config) (*bool, error) {
usrCount, err := c.principalStore.CountUsers(ctx) check, err := isUserRegistrationAllowed(ctx, c.principalStore, config.AllowSignUp)
if err != nil { if err != nil {
return nil, err return nil, err
} }
check := false return check, nil
if usrCount == 0 || config.AllowSignUp {
check = true
}
return &check, nil
} }