From dea2dcfde81f9000d81c8004ecf3470f45dab00f Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Fri, 14 Oct 2022 14:02:16 -0800 Subject: [PATCH] feat: allow disable registration (#71) --- backend/app/api/routes.go | 1 + backend/app/api/v1/controller.go | 16 ++++++++++++---- backend/app/api/v1/v1_ctrl_user.go | 5 +++++ backend/internal/config/conf.go | 15 ++++++++------- docs/docs/quick-start.md | 5 ++++- frontend/lib/api/types/data-contracts.ts | 6 ++++++ 6 files changed, 36 insertions(+), 12 deletions(-) diff --git a/backend/app/api/routes.go b/backend/app/api/routes.go index ab14c76..9cfd289 100644 --- a/backend/app/api/routes.go +++ b/backend/app/api/routes.go @@ -44,6 +44,7 @@ func (a *app) newRouter(repos *repo.AllRepos) *chi.Mux { v1Base := v1.BaseUrlFunc(prefix) v1Ctrl := v1.NewControllerV1(a.services, v1.WithMaxUploadSize(a.conf.Web.MaxUploadSize), + v1.WithRegistration(a.conf.AllowRegistration), v1.WithDemoStatus(a.conf.Demo), // Disable Password Change in Demo Mode ) r.Get(v1Base("/status"), v1Ctrl.HandleBase(func() bool { return true }, v1.Build{ diff --git a/backend/app/api/v1/controller.go b/backend/app/api/v1/controller.go index 365feef..349daf5 100644 --- a/backend/app/api/v1/controller.go +++ b/backend/app/api/v1/controller.go @@ -19,10 +19,17 @@ func WithDemoStatus(demoStatus bool) func(*V1Controller) { } } +func WithRegistration(allowRegistration bool) func(*V1Controller) { + return func(ctrl *V1Controller) { + ctrl.allowRegistration = allowRegistration + } +} + type V1Controller struct { - svc *services.AllServices - maxUploadSize int64 - isDemo bool + svc *services.AllServices + maxUploadSize int64 + isDemo bool + allowRegistration bool } type ( @@ -53,7 +60,8 @@ func BaseUrlFunc(prefix string) func(s string) string { func NewControllerV1(svc *services.AllServices, options ...func(*V1Controller)) *V1Controller { ctrl := &V1Controller{ - svc: svc, + svc: svc, + allowRegistration: true, } for _, opt := range options { diff --git a/backend/app/api/v1/v1_ctrl_user.go b/backend/app/api/v1/v1_ctrl_user.go index dc2779a..9ea4fae 100644 --- a/backend/app/api/v1/v1_ctrl_user.go +++ b/backend/app/api/v1/v1_ctrl_user.go @@ -27,6 +27,11 @@ func (ctrl *V1Controller) HandleUserRegistration() http.HandlerFunc { return } + if !ctrl.allowRegistration && regData.GroupToken == "" { + server.RespondError(w, http.StatusForbidden, nil) + return + } + _, err := ctrl.svc.User.RegisterUser(r.Context(), regData) if err != nil { log.Err(err).Msg("failed to register user") diff --git a/backend/internal/config/conf.go b/backend/internal/config/conf.go index a648bcc..f45e19c 100644 --- a/backend/internal/config/conf.go +++ b/backend/internal/config/conf.go @@ -16,13 +16,14 @@ const ( ) type Config struct { - Mode string `yaml:"mode" conf:"default:development"` // development or production - Web WebConfig `yaml:"web"` - Storage Storage `yaml:"storage"` - Log LoggerConf `yaml:"logger"` - Mailer MailerConf `yaml:"mailer"` - Swagger SwaggerConf `yaml:"swagger"` - Demo bool `yaml:"demo"` + Mode string `yaml:"mode" conf:"default:development"` // development or production + Web WebConfig `yaml:"web"` + Storage Storage `yaml:"storage"` + Log LoggerConf `yaml:"logger"` + Mailer MailerConf `yaml:"mailer"` + Swagger SwaggerConf `yaml:"swagger"` + Demo bool `yaml:"demo"` + AllowRegistration bool `yaml:"disable_registration" conf:"default:true"` } type SwaggerConf struct { diff --git a/docs/docs/quick-start.md b/docs/docs/quick-start.md index 21fdba9..d3e97a2 100644 --- a/docs/docs/quick-start.md +++ b/docs/docs/quick-start.md @@ -42,9 +42,10 @@ volumes: | HBOX_MODE | production | application mode used for runtime behavior can be one of: development, production | | HBOX_WEB_PORT | 7745 | port to run the web server on, in you're using docker do not change this | | HBOX_WEB_HOST | | host to run the web server on, in you're using docker do not change this | +| HBOX_ALLOW_REGISTRATION | true | allow users to register themselves | +| HBOX_WEB_MAX_UPLOAD_SIZE | 10 | maximum file upload size supported in MB | | HBOX_STORAGE_DATA | /data/ | path to the data directory, do not change this if you're using docker | | HBOX_STORAGE_SQLITE_URL | /data/homebox.db?_fk=1 | sqlite database url, in you're using docker do not change this | -| HBOX_WEB_MAX_UPLOAD_SIZE | 10 | maximum file upload size supported in MB | | HBOX_LOG_LEVEL | info | log level to use, can be one of: trace, debug, info, warn, error, critical | | HBOX_LOG_FORMAT | text | log format to use, can be one of: text, json | | HBOX_MAILER_HOST | | email host to use, if not set no email provider will be used | @@ -77,6 +78,8 @@ volumes: --mailer-from/$HBOX_MAILER_FROM --swagger-host/$HBOX_SWAGGER_HOST (default: localhost:7745) --swagger-scheme/$HBOX_SWAGGER_SCHEME (default: http) + --demo/$HBOX_DEMO + --allow-registration/$HBOX_ALLOW_REGISTRATION (default: true) --help/-h display this help message ``` \ No newline at end of file diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts index e160c5d..585feb1 100644 --- a/frontend/lib/api/types/data-contracts.ts +++ b/frontend/lib/api/types/data-contracts.ts @@ -187,6 +187,12 @@ export interface LocationSummary { updatedAt: Date; } +export interface PaginationResultRepoItemSummary { + items: ItemSummary[]; + page: number; + pageSize: number; + total: number; +} export interface UserOut { email: string;