mirror of
https://github.com/harness/drone.git
synced 2025-05-06 21:52:03 +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 {
|
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
|
||||||
|
}
|
||||||
|
@ -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.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user