mirror of
https://github.com/harness/drone.git
synced 2025-05-06 17:19:13 +08:00
[CODE-617]: Create common function for registration check to reuse the logic
This commit is contained in:
parent
b8c239ed6d
commit
ac375ff3fa
@ -48,3 +48,17 @@ func findUserFromEmail(ctx context.Context,
|
||||
func isUserTokenType(tokenType enum.TokenType) bool {
|
||||
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
|
||||
}
|
||||
|
@ -18,8 +18,12 @@ import (
|
||||
// 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 {
|
||||
signUpAllowed, err := isUserRegistrationAllowed(ctx, c.principalStore, config.AllowSignUp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if !*signUpAllowed {
|
||||
return nil, usererror.BadRequest("User sign-up is disabled.")
|
||||
}
|
||||
|
||||
|
@ -13,15 +13,10 @@ 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) {
|
||||
usrCount, err := c.principalStore.CountUsers(ctx)
|
||||
check, err := isUserRegistrationAllowed(ctx, c.principalStore, config.AllowSignUp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
check := false
|
||||
if usrCount == 0 || config.AllowSignUp {
|
||||
check = true
|
||||
}
|
||||
|
||||
return &check, nil
|
||||
return check, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user