#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
fix(saas): personal teams must be allowed to share a name (new signups get no team) (#7180)
ConnorYoh66b80a80c0b5
diff --git a/app/proprietary/src/main/java/stirling/software/proprietary/model/Team.java b/app/proprietary/src/main/java/stirling/software/proprietary/model/Team.javaindex 0009ee386e..a54959b0ff 100644--- a/app/proprietary/src/main/java/stirling/software/proprietary/model/Team.java+++ b/app/proprietary/src/main/java/stirling/software/proprietary/model/Team.java@@ -29,7 +29,9 @@ public class Team implements Serializable {@Column(name = "team_id")private Long id;- @Column(name = "name", unique = true, nullable = false)+ // Not unique: SaaS personal teams all share the name "My Team". TeamController enforces+ // uniqueness for admin-created teams.+ @Column(name = "name", nullable = false)private String name;@OneToMany(mappedBy = "team", cascade = CascadeType.ALL, orphanRemoval = true)diff --git a/app/saas/src/main/java/stirling/software/saas/security/SupabaseAuthenticationFilter.java b/app/saas/src/main/java/stirling/software/saas/security/SupabaseAuthenticationFilter.javaindex 890bc081a2..684e22a98f 100644--- a/app/saas/src/main/java/stirling/software/saas/security/SupabaseAuthenticationFilter.java+++ b/app/saas/src/main/java/stirling/software/saas/security/SupabaseAuthenticationFilter.java@@ -239,7 +239,7 @@ public class SupabaseAuthenticationFilter extends OncePerRequestFilter {&& !supabaseUser.isAnonymous()) {user = upgradeAnonymousUser(user, supabaseUser, jwt);}- return user;+ return recoverMissingTeam(user);}return createUser(jwt, supabaseId, email, appMetadata);@@ -406,6 +406,28 @@ public class SupabaseAuthenticationFilter extends OncePerRequestFilter {return savedUser;}+ /**+ * Recover an account stranded without a team: signup is the only other place one is assigned,+ * so a null team_id is otherwise permanent — and portal access derives from leading a team.+ * Guests get none by design.+ */+ private User recoverMissingTeam(User user) {+ if (user.getTeam() != null+ || ANONYMOUS.toString().equalsIgnoreCase(user.getAuthenticationType())) {+ return user;+ }+ try {+ user.setTeam(saasTeamService.ensurePersonalTeam(user));+ log.info("Assigned a personal team to user {} which had none", user.getId());+ } catch (Exception e) {+ log.warn(+ "Could not assign a personal team to user {}: {}",+ user.getId(),+ e.getMessage());+ }+ return user;+ }+private boolean apiKeyAuthenticated(HttpServletRequest request) throws AuthenticationException {Authentication existing = SecurityContextHolder.getContext().getAuthentication();if (existing != null && existing.isAuthenticated()) {diff --git a/app/saas/src/test/java/stirling/software/saas/security/SupabaseAuthenticationFilterMoreTest.java b/app/saas/src/test/java/stirling/software/saas/security/SupabaseAuthenticationFilterMoreTest.javaindex 87e100375f..3b8a0292c7 100644--- a/app/saas/src/test/java/stirling/software/saas/security/SupabaseAuthenticationFilterMoreTest.java+++ b/app/saas/src/test/java/stirling/software/saas/security/SupabaseAuthenticationFilterMoreTest.java@@ -585,4 +585,78 @@ class SupabaseAuthenticationFilterMoreTest {verify(userService, times(1)).saveUser(any(User.class));}}++ @Nested+ @DisplayName("Team recovery for existing accounts")+ class TeamRecovery {++ private User existingWebUser(UUID supabaseId) {+ User local = newUser("[email protected]");+ local.setSupabaseId(supabaseId);+ local.setAuthenticationType(AuthenticationType.WEB);+ return local;+ }++ @Test+ @DisplayName("an existing account with no team is given a personal team")+ void assignsTeamWhenMissing() throws Exception {+ UUID supabaseId = UUID.randomUUID();+ when(jwtDecoder.decode("tok"))+ .thenReturn(fullJwt(supabaseId, "[email protected]", false, "email"));+ when(supabaseUserService.getUser(supabaseId))+ .thenReturn(supabaseUser(supabaseId, "[email protected]", false));++ User local = existingWebUser(supabaseId);+ Team recovered = new Team();+ when(userService.findBySupabaseId(supabaseId)).thenReturn(Optional.of(local));+ when(saasTeamService.ensurePersonalTeam(local)).thenReturn(recovered);++ bearer("tok");+ filter.doFilter(request, response, chain);++ verify(saasTeamService).ensurePersonalTeam(local);+ assertThat(local.getTeam()).isSameAs(recovered);+ }++ @Test+ @DisplayName("an account that already has a team is left alone")+ void noOpWhenTeamPresent() throws Exception {+ UUID supabaseId = UUID.randomUUID();+ when(jwtDecoder.decode("tok"))+ .thenReturn(fullJwt(supabaseId, "[email protected]", false, "email"));+ when(supabaseUserService.getUser(supabaseId))+ .thenReturn(supabaseUser(supabaseId, "[email protected]", false));++ User local = existingWebUser(supabaseId);+ Team existing = new Team();+ local.setTeam(existing);+ when(userService.findBySupabaseId(supabaseId)).thenReturn(Optional.of(local));++ bearer("tok");+ filter.doFilter(request, response, chain);++ verify(saasTeamService, never()).ensurePersonalTeam(any(User.class));+ assertThat(local.getTeam()).isSameAs(existing);+ }++ @Test+ @DisplayName("a guest session is never given a team")+ void guestStaysTeamless() throws Exception {+ UUID supabaseId = UUID.randomUUID();+ when(jwtDecoder.decode("tok")).thenReturn(fullJwt(supabaseId, null, true, "email"));+ when(supabaseUserService.getUser(supabaseId))+ .thenReturn(supabaseUser(supabaseId, null, true));++ User local = newUser("anon_guest");+ local.setSupabaseId(supabaseId);+ local.setAuthenticationType(AuthenticationType.ANONYMOUS);+ when(userService.findBySupabaseId(supabaseId)).thenReturn(Optional.of(local));++ bearer("tok");+ filter.doFilter(request, response, chain);++ verify(saasTeamService, never()).ensurePersonalTeam(any(User.class));+ assertThat(local.getTeam()).isNull();+ }+ }}