Skip to content

Commit

Permalink
Correctly extract mysql users.settings json type to `UserSettings…
Browse files Browse the repository at this point in the history
…` type
  • Loading branch information
Jacob Shandling committed Jan 6, 2025
1 parent 14d71cf commit d4d65e7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
5 changes: 2 additions & 3 deletions server/datastore/mysql/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,11 +326,10 @@ func (ds *Datastore) UserSettings(ctx context.Context, userID uint) (*fleet.User
`
err := sqlx.GetContext(ctx, ds.reader(ctx), &bytes, stmt, userID)
if err != nil {
// okay for a user not to have any settings
if err == sql.ErrNoRows {
return settings, nil
return nil, ctxerr.Wrap(ctx, notFound("UserSettings").WithID(userID))
}
return nil, ctxerr.Wrap(ctx, err, "selecting app config")
return nil, ctxerr.Wrap(ctx, err, "selecting user settings")
}

if len(bytes) == 0 {
Expand Down
15 changes: 14 additions & 1 deletion server/fleet/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,20 @@ type User struct {
}

type UserSettings struct {
HiddenHostsTableColumns *[]string `json:"hidden_hosts_table_columns,omitempty"`
HiddenHostsTableColumns []string `json:"hidden_hosts_table_columns,omitempty"`
}

// Scan implements the sql.Scanner interface, tells DB driver how to convert MySQL type (json) to
// custom Go type (UserSettings).
func (us *UserSettings) Scan(val interface{}) error {
switch v := val.(type) {
case []byte:
return json.Unmarshal(v, us)
case string:
return json.Unmarshal([]byte(v), us)
default:
return fmt.Errorf("unsupported type: %T", v)
}
}

// IsGlobalObserver returns true if user is either a Global Observer or a Global Observer+
Expand Down

0 comments on commit d4d65e7

Please sign in to comment.