@@ -56,9 +56,12 @@ type keycloakUser struct {
5656
5757type keycloakUserAttributes struct {
5858 TermsAcceptedDate []string `json:"terms_and_conditions,omitempty"`
59- Profile * UserProfile `json:"profile"`
59+ Profile * UserProfile `json:"profile,omitempty"`
60+ Preferences * Preferences `json:"preferences,omitempty"`
61+ Settings * Settings `json:"settings,omitempty"`
6062}
6163
64+ // keycloakClient is a wrapper around gocloak for simplified access to keycloak
6265type keycloakClient struct {
6366 cfg * KeycloakConfig
6467 adminToken * oauth2.Token
@@ -202,6 +205,14 @@ func (c *keycloakClient) UpdateUser(ctx context.Context, user *keycloakUser) err
202205 profileAttrs := user .Attributes .Profile .ToAttributes ()
203206 maps .Copy (attrs , profileAttrs )
204207 }
208+ if user .Attributes .Settings != nil {
209+ settingsAttrs := user .Attributes .Settings .ToAttributes ()
210+ maps .Copy (attrs , settingsAttrs )
211+ }
212+ if user .Attributes .Preferences != nil {
213+ prefsAttrs := user .Attributes .Preferences .ToAttributes ()
214+ maps .Copy (attrs , prefsAttrs )
215+ }
205216
206217 gocloakUser .Attributes = & attrs
207218 if err := c .keycloak .UpdateUser (ctx , token .AccessToken , c .cfg .Realm , gocloakUser ); err != nil {
@@ -237,6 +248,54 @@ func (c *keycloakClient) DeleteUserProfile(ctx context.Context, id string) error
237248 return c .UpdateUser (ctx , user )
238249}
239250
251+ func (c * keycloakClient ) UpdateUserPreferences (ctx context.Context , id string , p * Preferences ) error {
252+ user , err := c .GetUserById (ctx , id )
253+ if err != nil {
254+ return err
255+ }
256+ if user == nil {
257+ return ErrUserNotFound
258+ }
259+ user .Attributes .Preferences = p
260+ return c .UpdateUser (ctx , user )
261+ }
262+
263+ func (c * keycloakClient ) DeleteUserPreferences (ctx context.Context , id string ) error {
264+ user , err := c .GetUserById (ctx , id )
265+ if err != nil {
266+ return err
267+ }
268+ if user == nil {
269+ return ErrUserNotFound
270+ }
271+ user .Attributes .Preferences = nil
272+ return c .UpdateUser (ctx , user )
273+ }
274+
275+ func (c * keycloakClient ) UpdateUserSettings (ctx context.Context , id string , s * Settings ) error {
276+ user , err := c .GetUserById (ctx , id )
277+ if err != nil {
278+ return err
279+ }
280+ if user == nil {
281+ return ErrUserNotFound
282+ }
283+ user .Attributes .Settings = s
284+ return c .UpdateUser (ctx , user )
285+ }
286+
287+ func (c * keycloakClient ) DeleteUserSettings (ctx context.Context , id string ) error {
288+ user , err := c .GetUserById (ctx , id )
289+ if err != nil {
290+ return err
291+ }
292+ if user == nil {
293+ return ErrUserNotFound
294+ }
295+ user .Attributes .Settings = nil
296+ return c .UpdateUser (ctx , user )
297+ }
298+
240299func (c * keycloakClient ) UpdateUserPassword (ctx context.Context , id , password string ) error {
241300 token , err := c .getAdminToken (ctx )
242301 if err != nil {
@@ -528,6 +587,12 @@ func newKeycloakUser(gocloakUser *gocloak.User) *keycloakUser {
528587 if prof , ok := profileFromAttributes (attrs ); ok {
529588 user .Attributes .Profile = prof
530589 }
590+ if prefs , ok := preferencesFromAttributes (attrs ); ok {
591+ user .Attributes .Preferences = prefs
592+ }
593+ if settings , ok := settingsFromAttributes (attrs ); ok {
594+ user .Attributes .Settings = settings
595+ }
531596 }
532597
533598 if gocloakUser .RealmRoles != nil {
@@ -556,6 +621,8 @@ func newUserFromKeycloakUser(keycloakUser *keycloakUser) *FullUser {
556621 IsMigrated : true ,
557622 Enabled : keycloakUser .Enabled ,
558623 Profile : attrs .Profile ,
624+ Settings : attrs .Settings ,
625+ Preferences : attrs .Preferences ,
559626 }
560627
561628 // All non-custodial users have a password and it's important to set the hash to a non-empty value.
@@ -591,6 +658,12 @@ func userToKeycloakUser(u *FullUser) *keycloakUser {
591658 if u .Profile != nil {
592659 keycloakUser .Attributes .Profile = u .Profile
593660 }
661+ if u .Preferences != nil {
662+ keycloakUser .Attributes .Preferences = u .Preferences
663+ }
664+ if u .Settings != nil {
665+ keycloakUser .Attributes .Settings = u .Settings
666+ }
594667
595668 return keycloakUser
596669}
0 commit comments