[CODE-617]: Add env variable in Gitness to allow/disallow sign-ups

jobatzil/rename
Akhilesh Pandey 2023-07-20 16:34:07 +05:30
parent af7c0385f8
commit 4c92c32c0a
5 changed files with 24 additions and 13 deletions
internal
api
controller/user
handler/account
router

View File

@ -1,3 +1,4 @@
GITNESS_TRACE=true
GITNESS_WEBHOOK_ALLOW_LOOPBACK=true
GITNESS_PRINCIPAL_ADMIN_PASSWORD=changeit
GITNESS_PRINCIPAL_ADMIN_PASSWORD=changeit
GITNESS_ALLOW_SIGNUP=true

View File

@ -17,9 +17,15 @@ import (
* 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) (*types.TokenResponse, error) {
func (c *Controller) Register(ctx context.Context, in *CreateInput, config *types.Config) (*types.TokenResponse, error) {
// TODO: allow to configure if open register is allowed.
signUpFlag := config.AllowSignUp
if !signUpFlag {
return nil, fmt.Errorf("user sign-up is disabled")
}
user, err := c.CreateNoAuth(ctx, in, false)
if err != nil {
return nil, fmt.Errorf("failed to create user: %w", err)

View File

@ -5,6 +5,7 @@
package account
import (
"github.com/harness/gitness/types"
"net/http"
"github.com/harness/gitness/internal/api/controller/user"
@ -13,7 +14,7 @@ import (
// HandleRegister returns an http.HandlerFunc that processes an http.Request
// to register the named user account with the system.
func HandleRegister(userCtrl *user.Controller) http.HandlerFunc {
func HandleRegister(userCtrl *user.Controller, config *types.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
@ -24,7 +25,7 @@ func HandleRegister(userCtrl *user.Controller) http.HandlerFunc {
Password: r.FormValue("password"),
}
tokenResponse, err := userCtrl.Register(ctx, in)
tokenResponse, err := userCtrl.Register(ctx, in, config)
if err != nil {
render.TranslatedUserError(w, err)
return

View File

@ -46,7 +46,7 @@ import (
"github.com/rs/zerolog/hlog"
)
// APIHandler is an abstraction of an http handler that handles API calls.
// APIHandler is an abstraction of a http handler that handles API calls.
type APIHandler interface {
http.Handler
}
@ -91,7 +91,7 @@ func NewAPIHandler(
r.Route("/v1", func(r chi.Router) {
setupRoutesV1(r, repoCtrl, spaceCtrl, pullreqCtrl, webhookCtrl, githookCtrl,
saCtrl, userCtrl, principalCtrl, checkCtrl)
saCtrl, userCtrl, principalCtrl, checkCtrl, config)
})
// wrap router in terminatedPath encoder.
@ -121,6 +121,7 @@ func setupRoutesV1(r chi.Router,
userCtrl *user.Controller,
principalCtrl principal.Controller,
checkCtrl *check.Controller,
config *types.Config,
) {
setupSpaces(r, spaceCtrl, repoCtrl)
setupRepos(r, repoCtrl, pullreqCtrl, webhookCtrl, checkCtrl)
@ -129,7 +130,7 @@ func setupRoutesV1(r chi.Router,
setupPrincipals(r, principalCtrl)
setupInternal(r, githookCtrl)
setupAdmin(r, userCtrl)
setupAccount(r, userCtrl)
setupAccount(r, userCtrl, config)
setupSystem(r)
setupResources(r)
}
@ -431,8 +432,8 @@ func setupAdmin(r chi.Router, userCtrl *user.Controller) {
})
}
func setupAccount(r chi.Router, userCtrl *user.Controller) {
func setupAccount(r chi.Router, userCtrl *user.Controller, config *types.Config) {
r.Post("/login", account.HandleLogin(userCtrl))
r.Post("/register", account.HandleRegister(userCtrl))
r.Post("/register", account.HandleRegister(userCtrl, config))
r.Post("/logout", account.HandleLogout(userCtrl))
}

View File

@ -12,10 +12,12 @@ import (
type Config struct {
// InstanceID specifis the ID of the gitness instance.
// NOTE: If the value is not provided the hostname of the machine is used.
InstanceID string `envconfig:"GITNESS_INSTANCE_ID"`
Debug bool `envconfig:"GITNESS_DEBUG"`
Trace bool `envconfig:"GITNESS_TRACE"`
Profiler struct {
InstanceID string `envconfig:"GITNESS_INSTANCE_ID"`
Debug bool `envconfig:"GITNESS_DEBUG"`
Trace bool `envconfig:"GITNESS_TRACE"`
AllowSignUp bool `envconfig:"GITNESS_ALLOW_SIGNUP"`
Profiler struct {
Type string `envconfig:"GITNESS_PROFILER_TYPE"`
ServiceName string `envconfig:"GITNESS_PROFILER_SERVICE_NAME" default:"gitness"`
}