Skip to content

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

Merge branch 'main' into mk/db-settings

Matthew Kilgore6a84b69e8221
diff --cc backend/app/api/handlers/v1/controller.go
index 606c5f86,5b154d24..c31a2431
--- a/backend/app/api/handlers/v1/controller.go
+++ b/backend/app/api/handlers/v1/controller.go
@@@ -222,10 -222,9 +222,10 @@@ func (ctrl *V1Controller) HandleCacheWS
}
}
- ctrl.bus.Subscribe(eventbus.EventLabelMutation, factory("label.mutation"))
+ ctrl.bus.Subscribe(eventbus.EventTagMutation, factory("tag.mutation"))
ctrl.bus.Subscribe(eventbus.EventLocationMutation, factory("location.mutation"))
ctrl.bus.Subscribe(eventbus.EventItemMutation, factory("item.mutation"))
+ ctrl.bus.Subscribe(eventbus.EventUserMutation, factory("user.mutation"))
// Persistent asynchronous ticker that keeps all websocket connections alive with periodic pings.
go func() {
diff --cc backend/internal/core/services/reporting/eventbus/eventbus.go
index 44aa6a72,7fc4bfac..ea74a43c
--- a/backend/internal/core/services/reporting/eventbus/eventbus.go
+++ b/backend/internal/core/services/reporting/eventbus/eventbus.go
@@@ -11,10 -11,9 +11,10 @@@ import
type Event string
const (
- EventLabelMutation Event = "label.mutation"
+ EventTagMutation Event = "tags.mutation"
EventLocationMutation Event = "location.mutation"
EventItemMutation Event = "item.mutation"
+ EventUserMutation Event = "user.mutation"
)
type GroupMutationEvent struct {
@@@ -38,10 -37,9 +38,10 @@@ func New() *EventBus
return &EventBus{
ch: make(chan eventData, 100),
subscribers: map[Event][]func(any){
- EventLabelMutation: {},
+ EventTagMutation: {},
EventLocationMutation: {},
EventItemMutation: {},
+ EventUserMutation: {},
},
}
}
diff --cc backend/internal/core/services/service_user.go
index ae168e9e,8d37d6c3..dd586c92
--- a/backend/internal/core/services/service_user.go
+++ b/backend/internal/core/services/service_user.go
@@@ -370,10 -383,31 +383,39 @@@ func (svc *UserService) ChangePassword(
return true
}
+func (svc *UserService) GetSettings(ctx context.Context, uid uuid.UUID) (map[string]interface{}, error) {
+ return svc.repos.Users.GetSettings(ctx, uid)
+}
+
+func (svc *UserService) SetSettings(ctx context.Context, uid uuid.UUID, settings map[string]interface{}) error {
+ return svc.repos.Users.SetSettings(ctx, uid, settings)
+}
++
+ // EnsureUserPassword ensures that the user with the given email has the specified password. If the password does not match, it updates the user's password to the new value.
+ // WARNING: This method bypasses normal checks, it should only be used for demos and/or superuser level administrative processes.
+ func (svc *UserService) EnsureUserPassword(ctx context.Context, email, password string) error {
+ usr, err := svc.repos.Users.GetOneEmailNoEdges(ctx, email)
+ if err != nil {
+ return err
+ }
+ match := false
+ if usr.PasswordHash != "" {
+ match, _ = hasher.CheckPasswordHash(password, usr.PasswordHash)
+ }
+ if !match {
+ hash, herr := hasher.HashPassword(password)
+ if herr != nil {
+ return herr
+ }
+ if cerr := svc.repos.Users.ChangePassword(ctx, usr.ID, hash); cerr != nil {
+ return cerr
+ }
+ }
+ return nil
+ }
+
+ // ExistsByEmail returns true if a user with the given email exists.
+ func (svc *UserService) ExistsByEmail(ctx context.Context, email string) bool {
+ _, err := svc.repos.Users.GetOneEmailNoEdges(ctx, email)
+ return err == nil
+ }
diff --cc backend/internal/data/ent/migrate/schema.go
index 36ece9c7,06231edf..36454611
--- a/backend/internal/data/ent/migrate/schema.go
+++ b/backend/internal/data/ent/migrate/schema.go
@@@ -468,8 -471,7 +471,8 @@@ var
{Name: "activated_on", Type: field.TypeTime, Nullable: true},
{Name: "oidc_issuer", Type: field.TypeString, Nullable: true},
{Name: "oidc_subject", Type: field.TypeString, Nullable: true},
+ {Name: "default_group_id", Type: field.TypeUUID, Nullable: true},
+ {Name: "settings", Type: field.TypeJSON, Nullable: true},
- {Name: "group_users", Type: field.TypeUUID},
}
// UsersTable holds the schema information for the "users" table.
UsersTable = &schema.Table{
diff --cc backend/internal/data/ent/schema/user.go
index 72cbf178,221c7090..32daa88e
--- a/backend/internal/data/ent/schema/user.go
+++ b/backend/internal/data/ent/schema/user.go
@@@ -54,8 -53,10 +53,12 @@@ func (User) Fields() []ent.Field
field.String("oidc_subject").
Optional().
Nillable(),
+ // default_group_id is the user's primary tenant/group
+ field.UUID("default_group_id", uuid.UUID{}).
+ Optional().
+ Nillable(),
+ field.JSON("settings", map[string]interface{}{}).
+ Optional(),
}
}
diff --cc backend/internal/data/ent/user/user.go
index d7877a0e,6ecf60d1..3b06d8b8
--- a/backend/internal/data/ent/user/user.go
+++ b/backend/internal/data/ent/user/user.go
@@@ -38,10 -38,10 +38,12 @@@ const
FieldOidcIssuer = "oidc_issuer"
// FieldOidcSubject holds the string denoting the oidc_subject field in the database.
FieldOidcSubject = "oidc_subject"
+ // FieldDefaultGroupID holds the string denoting the default_group_id field in the database.
+ FieldDefaultGroupID = "default_group_id"
+ // EdgeGroups holds the string denoting the groups edge name in mutations.
+ EdgeGroups = "groups"
+ // FieldSettings holds the string denoting the settings field in the database.
+ FieldSettings = "settings"
- // EdgeGroup holds the string denoting the group edge name in mutations.
- EdgeGroup = "group"
// EdgeAuthTokens holds the string denoting the auth_tokens edge name in mutations.
EdgeAuthTokens = "auth_tokens"
// EdgeNotifiers holds the string denoting the notifiers edge name in mutations.
@@@ -85,14 -83,14 +85,15 @@@ var Columns = []string
FieldActivatedOn,
FieldOidcIssuer,
FieldOidcSubject,
+ FieldDefaultGroupID,
+ FieldSettings,
}
- // ForeignKeys holds the SQL foreign-keys that are owned by the "users"
- // table and are not defined as standalone fields in the schema.
- var ForeignKeys = []string{
- "group_users",
- }
+ var (
+ // GroupsPrimaryKey and GroupsColumn2 are the table columns denoting the
+ // primary key for the groups relation (M2M).
+ GroupsPrimaryKey = []string{"user_id", "group_id"}
+ )
// ValidColumn reports if the column name is valid (part of the table columns).
func ValidColumn(column string) bool {
diff --cc backend/internal/data/ent/user/where.go
index 131394c6,0600426b..f7b552a5
--- a/backend/internal/data/ent/user/where.go
+++ b/backend/internal/data/ent/user/where.go
@@@ -631,18 -636,58 +636,70 @@@ func OidcSubjectContainsFold(v string)
return predicate.User(sql.FieldContainsFold(FieldOidcSubject, v))
}
+// SettingsIsNil applies the IsNil predicate on the "settings" field.
+func SettingsIsNil() predicate.User {
+ return predicate.User(sql.FieldIsNull(FieldSettings))
+}
+
+// SettingsNotNil applies the NotNil predicate on the "settings" field.
+func SettingsNotNil() predicate.User {
+ return predicate.User(sql.FieldNotNull(FieldSettings))
+}
+
+// HasGroup applies the HasEdge predicate on the "group" edge.
+func HasGroup() predicate.User {
+ // DefaultGroupIDEQ applies the EQ predicate on the "default_group_id" field.
+ func DefaultGroupIDEQ(v uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldEQ(FieldDefaultGroupID, v))
+ }
+
+ // DefaultGroupIDNEQ applies the NEQ predicate on the "default_group_id" field.
+ func DefaultGroupIDNEQ(v uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldNEQ(FieldDefaultGroupID, v))
+ }
+
+ // DefaultGroupIDIn applies the In predicate on the "default_group_id" field.
+ func DefaultGroupIDIn(vs ...uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldIn(FieldDefaultGroupID, vs...))
+ }
+
+ // DefaultGroupIDNotIn applies the NotIn predicate on the "default_group_id" field.
+ func DefaultGroupIDNotIn(vs ...uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldNotIn(FieldDefaultGroupID, vs...))
+ }
+
+ // DefaultGroupIDGT applies the GT predicate on the "default_group_id" field.
+ func DefaultGroupIDGT(v uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldGT(FieldDefaultGroupID, v))
+ }
+
+ // DefaultGroupIDGTE applies the GTE predicate on the "default_group_id" field.
+ func DefaultGroupIDGTE(v uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldGTE(FieldDefaultGroupID, v))
+ }
+
+ // DefaultGroupIDLT applies the LT predicate on the "default_group_id" field.
+ func DefaultGroupIDLT(v uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldLT(FieldDefaultGroupID, v))
+ }
+
+ // DefaultGroupIDLTE applies the LTE predicate on the "default_group_id" field.
+ func DefaultGroupIDLTE(v uuid.UUID) predicate.User {
+ return predicate.User(sql.FieldLTE(FieldDefaultGroupID, v))
+ }
+
+ // DefaultGroupIDIsNil applies the IsNil predicate on the "default_group_id" field.
+ func DefaultGroupIDIsNil() predicate.User {
+ return predicate.User(sql.FieldIsNull(FieldDefaultGroupID))
+ }
+
+ // DefaultGroupIDNotNil applies the NotNil predicate on the "default_group_id" field.
+ func DefaultGroupIDNotNil() predicate.User {
+ return predicate.User(sql.FieldNotNull(FieldDefaultGroupID))
+ }
+
+ // HasGroups applies the HasEdge predicate on the "groups" edge.
+ func HasGroups() predicate.User {
return predicate.User(func(s *sql.Selector) {
step := sqlgraph.NewStep(
sqlgraph.From(Table, FieldID),