Modern Docker Management, Designed for Everyone
Go composecontainer-managementcontainersdockerdocker-composedocker-managementgoself-hostedsveltekittypescriptweb-ui
fix(auth): stop stale user writes from overwriting profiles
Kyle Mendellae7641d7f477
diff --git a/backend/internal/auth/service.go b/backend/internal/auth/service.goindex 08a999bfd..d8e7c3c7e 100644--- a/backend/internal/auth/service.go+++ b/backend/internal/auth/service.go@@ -239,11 +239,12 @@ func (s *AuthService) AuthenticateLocalPrimary(ctx context.Context, username, pareturn nil, ErrInvalidCredentials}- localUser.LastLogin = new(time.Now())- userCopy := new(*localUser)+ userID := localUser.ID+ now := time.Now()+ localUser.LastLogin = &nows.runInBackground(ctx, "update_last_login", func(ctx context.Context) error {- if _, updateUserErr := s.userService.UpdateUser(ctx, userCopy, nil); updateUserErr != nil {- return fmt.Errorf("failed to update user's last login time: %w", updateUserErr)+ if touchErr := s.userService.TouchLastLogin(ctx, userID, now); touchErr != nil {+ return fmt.Errorf("failed to update user's last login time: %w", touchErr)}return nil})@@ -402,7 +403,7 @@ func (s *AuthService) findOrCreateOidcUser(ctx context.Context, userInfo auth.Oislog.WarnContext(ctx, "failed to sync OIDC role assignments on user merge", "error", syncErr, "userId", merged.ID)}}- return existingUser, false, nil+ return merged, false, nil}}}@@ -508,23 +509,23 @@ func (s *AuthService) resolveOidcUsername(ctx context.Context, userInfo auth.Oid}func (s *AuthService) updateOidcUser(ctx context.Context, localUser *user.User, userInfo auth.OidcUserInfo, tokenResp *auth.OidcTokenResponse) (*user.User, error) {- if userInfo.Name != "" && localUser.DisplayName == nil {- localUser.DisplayName = new(userInfo.Name)- }- if userInfo.Email != "" && localUser.Email == nil {- localUser.Email = new(userInfo.Email)- }-- s.persistOidcTokens(localUser, tokenResp)-- localUser.LastLogin = new(time.Now())- if _, err := s.userService.UpdateUser(ctx, localUser, nil); err != nil {+ updated, err := s.userService.AttachOidcSubjectTransactional(ctx, localUser.ID, userInfo.Subject, func(u *user.User) {+ if userInfo.Name != "" && u.DisplayName == nil {+ u.DisplayName = new(userInfo.Name)+ }+ if userInfo.Email != "" && u.Email == nil {+ u.Email = new(userInfo.Email)+ }+ s.persistOidcTokens(u, tokenResp)+ u.LastLogin = new(time.Now())+ })+ if err != nil {return nil, err}- if err := s.syncOidcRoleAssignments(ctx, localUser, userInfo, tokenResp); err != nil {- slog.WarnContext(ctx, "failed to sync OIDC role assignments on user update", "error", err, "userId", localUser.ID)+ if syncErr := s.syncOidcRoleAssignments(ctx, updated, userInfo, tokenResp); syncErr != nil {+ slog.WarnContext(ctx, "failed to sync OIDC role assignments on user update", "error", syncErr, "userId", updated.ID)}- return localUser, nil+ return updated, nil}// syncOidcRoleAssignments rebuilds the user's `source='oidc'` role assignmentsdiff --git a/backend/internal/user/service.go b/backend/internal/user/service.goindex 7d25716e9..9da95e30d 100644--- a/backend/internal/user/service.go+++ b/backend/internal/user/service.go@@ -11,6 +11,7 @@ import ("maps""slices""strings"+ "time""github.com/getarcaneapp/arcane/types/v2/user""go.getarcane.app/kit/normalization"@@ -292,6 +293,20 @@ func (s *UserService) UpdateUser(ctx context.Context, localUser *User, actorPermreturn localUser, nil}+// TouchLastLogin updates only the user's last login time.+func (s *UserService) TouchLastLogin(ctx context.Context, userID string, at time.Time) error {+ err := s.db.DB.WithContext(ctx).+ Model(&User{}).+ Where("id = ?", userID).+ Updates(map[string]any{+ "last_login": at,+ }).Error+ if err != nil {+ return fmt.Errorf("failed to update user last login: %w", err)+ }+ return nil+}+func normalizeUsernameInternal(username string) (string, error) {username = strings.TrimSpace(username)if username == "" {diff --git a/frontend/src/lib/components/sheets/user-form-sheet.svelte b/frontend/src/lib/components/sheets/user-form-sheet.svelteindex a9ea7b157..bbe9114d7 100644--- a/frontend/src/lib/components/sheets/user-form-sheet.svelte+++ b/frontend/src/lib/components/sheets/user-form-sheet.svelte@@ -123,11 +123,16 @@}const userData: UserSubmission = {- displayName: data.displayName,roleAssignments: data.roleAssignments};- if (data.email) {+ // When editing, only send profile fields that changed so a stale form+ // can't overwrite newer values+ if (!isEditMode || data.displayName.trim() !== (userToEdit?.displayName ?? '').trim()) {+ userData.displayName = data.displayName;+ }++ if (data.email && (!isEditMode || data.email.trim() !== (userToEdit?.email ?? '').trim())) {userData.email = data.email;}diff --git a/frontend/src/routes/(app)/account/+page.svelte b/frontend/src/routes/(app)/account/+page.svelteindex a02da96c1..3864c02ad 100644--- a/frontend/src/routes/(app)/account/+page.svelte+++ b/frontend/src/routes/(app)/account/+page.svelte@@ -1,4 +1,5 @@<script lang="ts">+ import { useQueryClient } from '@tanstack/svelte-query';import { toast } from 'svelte-sonner';import { Temporal } from 'temporal-polyfill';@@ -12,6 +13,7 @@import { useUrlTab } from '#lib/hooks/use-url-tab.svelte.js';import { UserIcon, SettingsIcon, TestIcon } from '#lib/icons/index.js';import { m } from '#lib/paraglide/messages.js';+ import { queryKeys } from '#lib/query/query-keys.js';import { userService } from '#lib/services/user-service.js';import settingsStore from '#lib/stores/config-store.svelte.js';import userStore from '#lib/stores/user-store.svelte.js';@@ -53,6 +55,7 @@return BUILT_IN_ROLE_LABELS[roleId]?.() ?? roleId.replace(/^role_/, '').replace(/_/g, ' ');}+ const queryClient = useQueryClient();const currentUser = $derived(userStore.current);const isOidcUser = $derived(Boolean(currentUser?.oidcSubjectId));@@ -119,6 +122,7 @@email: profileEmail.trim()});await userStore.setUser(updated);+ await queryClient.invalidateQueries({ queryKey: queryKeys.users.all });if (profileDraft === submittedDraft) profileDraft = null;toast.success(m.account_profile_updated());})()