Skip to content

A continuation of HomeBox the inventory and organization system built for the Home User

Add backend to store user settings in the DB

Matthew Kilgore24216d888a0b
diff --git a/backend/app/api/handlers/v1/v1_ctrl_user.go b/backend/app/api/handlers/v1/v1_ctrl_user.go
index f1dbefa2..580dde7f 100644
--- a/backend/app/api/handlers/v1/v1_ctrl_user.go
+++ b/backend/app/api/handlers/v1/v1_ctrl_user.go
@@ -9,6 +9,7 @@ import (
"github.com/hay-kot/httpkit/server"
"github.com/rs/zerolog/log"
"github.com/sysadminsmedia/homebox/backend/internal/core/services"
+ "github.com/sysadminsmedia/homebox/backend/internal/data/ent/schema"
"github.com/sysadminsmedia/homebox/backend/internal/data/repo"
"github.com/sysadminsmedia/homebox/backend/internal/sys/validate"
)
@@ -121,6 +122,57 @@ func (ctrl *V1Controller) HandleUserSelfDelete() errchain.HandlerFunc {
}
}
+// HandleUserSelfSettingsGet godoc
+//
+// @Summary Get user settings
+// @Tags User
+// @Produce json
+// @Success 200 {object} Wrapped{item=map[string]interface{}}
+// @Router /v1/users/self/settings [GET]
+// @Security Bearer
+func (ctrl *V1Controller) HandleUserSelfSettingsGet() errchain.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) error {
+ actor := services.UseUserCtx(r.Context())
+ settings, err := ctrl.svc.User.GetSettings(r.Context(), actor.ID)
+ if err != nil {
+ return validate.NewRequestError(err, http.StatusInternalServerError)
+ }
+
+ return server.JSON(w, http.StatusOK, Wrap(settings))
+ }
+}
+
+// HandleUserSelfSettingsUpdate godoc
+//
+// @Summary Get user settings
+// @Tags User
+// @Produce json
+// @Success 200 {object} Wrapped{item=map[string]interface{}}
+// @Router /v1/users/self/settings [PUT]
+// @Param payload body map[string]interface{} true "Settings Data"
+// @Security Bearer
+func (ctrl *V1Controller) HandleUserSelfSettingsUpdate() errchain.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) error {
+ var settings schema.UserSettings
+ if err := server.Decode(r, &settings); err != nil {
+ log.Err(err).Msg("failed to decode user settings data")
+ return validate.NewRequestError(err, http.StatusBadRequest)
+ }
+
+ actor := services.UseUserCtx(r.Context())
+ if err := ctrl.svc.User.SetSettings(r.Context(), actor.ID, settings); err != nil {
+ return validate.NewRequestError(err, http.StatusInternalServerError)
+ }
+
+ newSettings, err := ctrl.svc.User.GetSettings(r.Context(), actor.ID)
+ if err != nil {
+ return validate.NewRequestError(err, http.StatusInternalServerError)
+ }
+
+ return server.JSON(w, http.StatusOK, Wrap(newSettings))
+ }
+}
+
type (
ChangePassword struct {
Current string `json:"current,omitempty"`
diff --git a/backend/app/api/routes.go b/backend/app/api/routes.go
index f655a543..dc621add 100644
--- a/backend/app/api/routes.go
+++ b/backend/app/api/routes.go
@@ -89,6 +89,8 @@ func (a *app) mountRoutes(r *chi.Mux, chain *errchain.ErrChain, repos *repo.AllR
r.Get("/users/self", chain.ToHandlerFunc(v1Ctrl.HandleUserSelf(), userMW...))
r.Put("/users/self", chain.ToHandlerFunc(v1Ctrl.HandleUserSelfUpdate(), userMW...))
r.Delete("/users/self", chain.ToHandlerFunc(v1Ctrl.HandleUserSelfDelete(), userMW...))
+ r.Get("/users/self/settings", chain.ToHandlerFunc(v1Ctrl.HandleUserSelfSettingsGet(), userMW...))
+ r.Put("/users/self/settings", chain.ToHandlerFunc(v1Ctrl.HandleUserSelfSettingsUpdate(), userMW...))
r.Post("/users/logout", chain.ToHandlerFunc(v1Ctrl.HandleAuthLogout(), userMW...))
r.Get("/users/refresh", chain.ToHandlerFunc(v1Ctrl.HandleAuthRefresh(), userMW...))
r.Put("/users/self/change-password", chain.ToHandlerFunc(v1Ctrl.HandleUserSelfChangePassword(), userMW...))
diff --git a/backend/app/api/static/docs/swagger.yaml b/backend/app/api/static/docs/swagger.yaml
index 0d234331..2a190f54 100644
--- a/backend/app/api/static/docs/swagger.yaml
+++ b/backend/app/api/static/docs/swagger.yaml
@@ -747,6 +747,10 @@ definitions:
allOf:
- $ref: '#/definitions/user.Role'
description: Role holds the value of the "role" field.
+ settings:
+ allOf:
+ - $ref: '#/definitions/schema.UserSettings'
+ description: Settings holds the value of the "settings" field.
superuser:
description: Superuser holds the value of the "superuser" field.
type: boolean
@@ -1710,6 +1714,44 @@ definitions:
value:
type: number
type: object
+ schema.DuplicateSettings:
+ properties:
+ copyAttachments:
+ type: boolean
+ copyCustomFields:
+ type: boolean
+ copyMaintenance:
+ type: boolean
+ copyPrefixOverride:
+ type: string
+ type: object
+ schema.UserSettings:
+ properties:
+ displayLegacyHeader:
+ type: boolean
+ duplicateSettings:
+ $ref: '#/definitions/schema.DuplicateSettings'
+ editorAdvancedView:
+ type: boolean
+ itemDisplayView:
+ type: string
+ itemsPerPage:
+ type: integer
+ itemsPerTablePage:
+ type: integer
+ language:
+ type: string
+ locale:
+ type: string
+ overrideFormatLocale:
+ type: string
+ showDetails:
+ type: boolean
+ showEmpty:
+ type: boolean
+ theme:
+ type: string
+ type: object
services.Latest:
properties:
date:
@@ -3403,6 +3445,53 @@ paths:
summary: Update Account
tags:
- User
+ /v1/users/self/settings:
+ get:
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/v1.Wrapped'
+ - properties:
+ item:
+ additionalProperties: true
+ type: object
+ type: object
+ security:
+ - Bearer: []
+ summary: Get user settings
+ tags:
+ - User
+ put:
+ parameters:
+ - description: Settings Data
+ in: body
+ name: payload
+ required: true
+ schema:
+ additionalProperties: true
+ type: object
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/v1.Wrapped'
+ - properties:
+ item:
+ additionalProperties: true
+ type: object
+ type: object
+ security:
+ - Bearer: []
+ summary: Get user settings
+ tags:
+ - User
schemes:
- https
- http
diff --git a/backend/internal/core/services/service_user.go b/backend/internal/core/services/service_user.go
index 8b13a653..c3235a27 100644
--- a/backend/internal/core/services/service_user.go
+++ b/backend/internal/core/services/service_user.go
@@ -10,6 +10,7 @@ import (
"github.com/rs/zerolog/log"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/authroles"
+ "github.com/sysadminsmedia/homebox/backend/internal/data/ent/schema"
"github.com/sysadminsmedia/homebox/backend/internal/data/repo"
"github.com/sysadminsmedia/homebox/backend/pkgs/hasher"
)
@@ -369,3 +370,11 @@ func (svc *UserService) ChangePassword(ctx Context, current string, new string)
return true
}
+
+func (svc *UserService) GetSettings(ctx context.Context, uid uuid.UUID) (schema.UserSettings, error) {
+ return svc.repos.Users.GetSettings(ctx, uid)
+}
+
+func (svc *UserService) SetSettings(ctx context.Context, uid uuid.UUID, settings schema.UserSettings) error {
+ return svc.repos.Users.SetSettings(ctx, uid, settings)
+}
diff --git a/backend/internal/data/ent/schema/user.go b/backend/internal/data/ent/schema/user.go
index c1ff9c29..382a11f4 100644
--- a/backend/internal/data/ent/schema/user.go
+++ b/backend/internal/data/ent/schema/user.go
@@ -54,6 +54,8 @@ func (User) Fields() []ent.Field {
field.String("oidc_subject").
Optional().
Nillable(),
+ field.JSON("settings", UserSettings{}).
+ Optional(),
}
}
@@ -77,6 +79,28 @@ func (User) Edges() []ent.Edge {
}
}
+type UserSettings struct {
+ Theme string `json:"theme"`
+ ItemsPerPage int `json:"itemsPerPage"`
+ Locale string `json:"locale"`
+ ShowDetails bool `json:"showDetails"`
+ ShowEmpty bool `json:"showEmpty"`
+ EditorAdvancedView bool `json:"editorAdvancedView"`
+ ItemDisplayView string `json:"itemDisplayView"`
+ ItemsPerTablePage int `json:"itemsPerTablePage"`
+ DisplayLegacyHeader bool `json:"displayLegacyHeader"`
+ Language string `json:"language"`
+ OverrideFormatLocale string `json:"overrideFormatLocale"`
+ DuplicateSettings DuplicateSettings `json:"duplicateSettings"`
+}
+
+type DuplicateSettings struct {
+ CopyMaintenance bool `json:"copyMaintenance"`
+ CopyAttachments bool `json:"copyAttachments"`
+ CopyCustomFields bool `json:"copyCustomFields"`
+ CopyPrefixOverride string `json:"copyPrefixOverride"`
+}
+
// UserMixin when embedded in an ent.Schema, adds a reference to
// the Group entity.
type UserMixin struct {
diff --git a/backend/internal/data/migrations/postgres/20250823141600_user_settings.sql b/backend/internal/data/migrations/postgres/20250823141600_user_settings.sql
new file mode 100644
index 00000000..2c3be410
--- /dev/null
+++ b/backend/internal/data/migrations/postgres/20250823141600_user_settings.sql
@@ -0,0 +1,2 @@
+-- +goose Up
+ALTER TABLE users ADD COLUMN settings JSONB DEFAULT '{}';
\ No newline at end of file
diff --git a/backend/internal/data/migrations/sqlite3/20250823141400_user_settings.sql b/backend/internal/data/migrations/sqlite3/20250823141400_user_settings.sql
new file mode 100644
index 00000000..3f75af8d
--- /dev/null
+++ b/backend/internal/data/migrations/sqlite3/20250823141400_user_settings.sql
@@ -0,0 +1,2 @@
+-- +goose Up
+ALTER TABLE users ADD COLUMN settings JSON DEFAULT '{}';
\ No newline at end of file
diff --git a/backend/internal/data/repo/repo_users.go b/backend/internal/data/repo/repo_users.go
index 9af394fa..ef920f0d 100644
--- a/backend/internal/data/repo/repo_users.go
+++ b/backend/internal/data/repo/repo_users.go
@@ -5,6 +5,7 @@ import (
"github.com/google/uuid"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent"
+ "github.com/sysadminsmedia/homebox/backend/internal/data/ent/schema"
"github.com/sysadminsmedia/homebox/backend/internal/data/ent/user"
)
@@ -176,6 +177,18 @@ func (r *UserRepository) ChangePassword(ctx context.Context, uid uuid.UUID, pw s
return r.db.User.UpdateOneID(uid).SetPassword(pw).Exec(ctx)
}
+func (r *UserRepository) SetSettings(ctx context.Context, uid uuid.UUID, settings schema.UserSettings) error {
+ return r.db.User.UpdateOneID(uid).SetSettings(settings).Exec(ctx)
+}
+
+func (r *UserRepository) GetSettings(ctx context.Context, uid uuid.UUID) (schema.UserSettings, error) {
+ usr, err := r.db.User.Get(ctx, uid)
+ if err != nil {
+ return schema.UserSettings{}, err
+ }
+ return usr.Settings, nil
+}
+
func (r *UserRepository) SetOIDCIdentity(ctx context.Context, uid uuid.UUID, issuer, subject string) error {
return r.db.User.UpdateOneID(uid).SetOidcIssuer(issuer).SetOidcSubject(subject).Exec(ctx)
}
diff --git a/docs/en/api/openapi-2.0.yaml b/docs/en/api/openapi-2.0.yaml
index 0d234331..2a190f54 100644
--- a/docs/en/api/openapi-2.0.yaml
+++ b/docs/en/api/openapi-2.0.yaml
@@ -747,6 +747,10 @@ definitions:
allOf:
- $ref: '#/definitions/user.Role'
description: Role holds the value of the "role" field.
+ settings:
+ allOf:
+ - $ref: '#/definitions/schema.UserSettings'
+ description: Settings holds the value of the "settings" field.
superuser:
description: Superuser holds the value of the "superuser" field.
type: boolean
@@ -1710,6 +1714,44 @@ definitions:
value:
type: number
type: object
+ schema.DuplicateSettings:
+ properties:
+ copyAttachments:
+ type: boolean
+ copyCustomFields:
+ type: boolean
+ copyMaintenance:
+ type: boolean
+ copyPrefixOverride:
+ type: string
+ type: object
+ schema.UserSettings:
+ properties:
+ displayLegacyHeader:
+ type: boolean
+ duplicateSettings:
+ $ref: '#/definitions/schema.DuplicateSettings'
+ editorAdvancedView:
+ type: boolean
+ itemDisplayView:
+ type: string
+ itemsPerPage:
+ type: integer
+ itemsPerTablePage:
+ type: integer
+ language:
+ type: string
+ locale:
+ type: string
+ overrideFormatLocale:
+ type: string
+ showDetails:
+ type: boolean
+ showEmpty:
+ type: boolean
+ theme:
+ type: string
+ type: object
services.Latest:
properties:
date:
@@ -3403,6 +3445,53 @@ paths:
summary: Update Account
tags:
- User
+ /v1/users/self/settings:
+ get:
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/v1.Wrapped'
+ - properties:
+ item:
+ additionalProperties: true
+ type: object
+ type: object
+ security:
+ - Bearer: []
+ summary: Get user settings
+ tags:
+ - User
+ put:
+ parameters:
+ - description: Settings Data
+ in: body
+ name: payload
+ required: true
+ schema:
+ additionalProperties: true
+ type: object
+ produces:
+ - application/json
+ responses:
+ "200":
+ description: OK
+ schema:
+ allOf:
+ - $ref: '#/definitions/v1.Wrapped'
+ - properties:
+ item:
+ additionalProperties: true
+ type: object
+ type: object
+ security:
+ - Bearer: []
+ summary: Get user settings
+ tags:
+ - User
schemes:
- https
- http
diff --git a/frontend/lib/api/types/data-contracts.ts b/frontend/lib/api/types/data-contracts.ts
index 3f7e283c..00732e83 100644
--- a/frontend/lib/api/types/data-contracts.ts
+++ b/frontend/lib/api/types/data-contracts.ts
@@ -527,6 +527,8 @@ export interface EntUser {
oidc_subject: string;
/** Role holds the value of the "role" field. */
role: UserRole;
+ /** Settings holds the value of the "settings" field. */
+ settings: SchemaUserSettings;
/** Superuser holds the value of the "superuser" field. */
superuser: boolean;
/** UpdatedAt holds the value of the "updated_at" field. */
@@ -1057,6 +1059,28 @@ export interface ValueOverTimeEntry {
value: number;
}
+export interface SchemaDuplicateSettings {
+ copyAttachments: boolean;
+ copyCustomFields: boolean;
+ copyMaintenance: boolean;
+ copyPrefixOverride: string;
+}
+
+export interface SchemaUserSettings {
+ displayLegacyHeader: boolean;
+ duplicateSettings: SchemaDuplicateSettings;
+ editorAdvancedView: boolean;
+ itemDisplayView: string;
+ itemsPerPage: number;
+ itemsPerTablePage: number;
+ language: string;
+ locale: string;
+ overrideFormatLocale: string;
+ showDetails: boolean;
+ showEmpty: boolean;
+ theme: string;
+}
+
export interface Latest {
date: Date | string;
version: string;