formatting and fixes

This commit is contained in:
Anthony Stirling 2025-08-06 16:35:09 +01:00
parent d1c8802890
commit 0dd0e0c71e
9 changed files with 50 additions and 66 deletions

View File

@ -221,42 +221,49 @@ public class InitialSecuritySetup {
private void migrateDeprecatedRolesToUser() {
String[] deprecatedRoles = {
"ROLE_WEB_ONLY_USER",
"ROLE_EXTRA_LIMITED_API_USER",
"ROLE_LIMITED_API_USER"
"ROLE_WEB_ONLY_USER", "ROLE_EXTRA_LIMITED_API_USER", "ROLE_LIMITED_API_USER"
};
int totalMigrated = 0;
for (String deprecatedRole : deprecatedRoles) {
List<User> usersWithDeprecatedRole = userService.findByRole(deprecatedRole);
if (!usersWithDeprecatedRole.isEmpty()) {
log.info("Found {} users with role {}. Converting to USER...",
usersWithDeprecatedRole.size(), deprecatedRole);
log.info(
"Found {} users with role {}. Converting to USER...",
usersWithDeprecatedRole.size(),
deprecatedRole);
int migratedCount = 0;
for (User user : usersWithDeprecatedRole) {
try {
user.setUserRole(Role.USER);
userService.saveUser(user);
log.debug("Converted user '{}' from {} to USER",
user.getUsername(), deprecatedRole);
log.debug(
"Converted user '{}' from {} to USER",
user.getUsername(),
deprecatedRole);
migratedCount++;
} catch (Exception e) {
log.error("Failed to migrate user '{}' from {} to USER: {}",
user.getUsername(), deprecatedRole, e.getMessage());
log.error(
"Failed to migrate user '{}' from {} to USER: {}",
user.getUsername(),
deprecatedRole,
e.getMessage());
}
}
if (migratedCount > 0) {
log.info("Successfully migrated {} users from {} to USER",
migratedCount, deprecatedRole);
log.info(
"Successfully migrated {} users from {} to USER",
migratedCount,
deprecatedRole);
totalMigrated += migratedCount;
}
}
}
if (totalMigrated == 0) {
log.debug("No users with deprecated roles found - migration not needed");
} else {

View File

@ -220,15 +220,13 @@ public class AccountWebController {
List<User> allUsers = userRepository.findAllWithTeam();
Iterator<User> iterator = allUsers.iterator();
Map<String, String> roleDetails = Role.getAllRoleDetails();
// Filter role details to only show SYSTEM_ADMIN, USER, and DEMO_USER in UI
Map<String, String> filteredRoleDetails = new LinkedHashMap<>();
String[] allowedRoles = {
Role.SYSTEM_ADMIN.getRoleId(),
Role.USER.getRoleId(),
Role.DEMO_USER.getRoleId()
Role.SYSTEM_ADMIN.getRoleId(), Role.USER.getRoleId(), Role.DEMO_USER.getRoleId()
};
for (String roleId : allowedRoles) {
if (roleDetails.containsKey(roleId)) {
filteredRoleDetails.put(roleId, roleDetails.get(roleId));

View File

@ -30,7 +30,8 @@ import stirling.software.proprietary.security.service.RoleBasedAuthorizationServ
@Slf4j
@RequiredArgsConstructor
@PremiumEndpoint
@PreAuthorize("@roleBasedAuthorizationService.canManageOrgUsers() or @roleBasedAuthorizationService.canManageOrgTeams()")
@PreAuthorize(
"@roleBasedAuthorizationService.canManageOrgUsers() or @roleBasedAuthorizationService.canManageOrgTeams()")
public class OrgAdminController {
private final TeamRepository teamRepository;

View File

@ -34,7 +34,8 @@ public class OrganizationController {
}
@GetMapping("/{id}")
@PreAuthorize("@roleBasedAuthorizationService.canViewOrganization(@organizationRepository.findById(#id).orElse(null))")
@PreAuthorize(
"@roleBasedAuthorizationService.canViewOrganization(@organizationRepository.findById(#id).orElse(null))")
public ResponseEntity<Organization> getOrganization(@PathVariable Long id) {
Optional<Organization> organizationOpt = organizationRepository.findById(id);
if (organizationOpt.isEmpty()) {

View File

@ -62,7 +62,8 @@ public class TeamController {
}
@PostMapping("/rename")
@PreAuthorize("@roleBasedAuthorizationService.canManageTeam(@teamRepository.findById(#teamId).orElse(null))")
@PreAuthorize(
"@roleBasedAuthorizationService.canManageTeam(@teamRepository.findById(#teamId).orElse(null))")
public RedirectView renameTeam(
@RequestParam("teamId") Long teamId, @RequestParam("newName") String newName) {
Optional<Team> existing = teamRepository.findById(teamId);
@ -88,7 +89,8 @@ public class TeamController {
@PostMapping("/delete")
@Transactional
@PreAuthorize("@roleBasedAuthorizationService.canManageTeam(@teamRepository.findById(#teamId).orElse(null))")
@PreAuthorize(
"@roleBasedAuthorizationService.canManageTeam(@teamRepository.findById(#teamId).orElse(null))")
public RedirectView deleteTeam(@RequestParam("teamId") Long teamId) {
Optional<Team> teamOpt = teamRepository.findById(teamId);
if (teamOpt.isEmpty()) {
@ -113,7 +115,8 @@ public class TeamController {
@PostMapping("/addUser")
@Transactional
@PreAuthorize("@roleBasedAuthorizationService.canAddUserToTeam(#userId, @teamRepository.findById(#teamId).orElse(null))")
@PreAuthorize(
"@roleBasedAuthorizationService.canAddUserToTeam(#userId, @teamRepository.findById(#teamId).orElse(null))")
public RedirectView addUserToTeam(
@RequestParam("teamId") Long teamId, @RequestParam("userId") Long userId) {

View File

@ -116,26 +116,6 @@ public class TeamLeadController {
return ResponseEntity.ok().body("User removed from team successfully");
}
/** Get users that can be added to the team (within same organization, not in any team) */
@GetMapping("/available-users")
public ResponseEntity<List<User>> getAvailableUsers() {
if (!authorizationService.canManageTeamUsers()) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
User currentUser = authorizationService.getCurrentUser();
if (currentUser == null || currentUser.getOrganization() == null) {
return ResponseEntity.badRequest().build();
}
// Find users in the same organization who are not in any team
List<User> availableUsers =
userRepository.findUsersInOrganizationWithoutTeam(
currentUser.getOrganization().getId());
return ResponseEntity.ok(availableUsers);
}
/** Update a team member's role (team leads can only assign USER role) */
@PostMapping("/update-member-role")
@Transactional

View File

@ -24,9 +24,6 @@ public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByAuthenticationTypeIgnoreCase(String authenticationType);
@Query("SELECT u FROM User u WHERE u.team IS NULL")
List<User> findAllWithoutTeam();
@Query(value = "SELECT u FROM User u LEFT JOIN FETCH u.team")
List<User> findAllWithTeam();
@ -38,8 +35,8 @@ public interface UserRepository extends JpaRepository<User, Long> {
List<User> findByTeam(Team team);
@Query("SELECT u FROM User u WHERE u.team IS NULL AND u.organization.id = :organizationId")
List<User> findUsersInOrganizationWithoutTeam(@Param("organizationId") Long organizationId);
@Query("SELECT u FROM User u WHERE u.team IS NULL")
List<User> findUsersWithoutTeam();
@Query("SELECT u FROM User u JOIN u.authorities a WHERE a.authority = :role")
List<User> findByRole(@Param("role") String role);

View File

@ -624,7 +624,7 @@ public class UserService implements UserServiceInterface {
}
public List<User> getUsersWithoutTeam() {
return userRepository.findAllWithoutTeam();
return userRepository.findUsersWithoutTeam();
}
public void saveAll(List<User> users) {

View File

@ -11,25 +11,19 @@ import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import stirling.software.proprietary.model.Organization;
import stirling.software.proprietary.model.Team;
import stirling.software.proprietary.security.repository.TeamRepository;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class TeamServiceTest {
@Mock private TeamRepository teamRepository;
@Mock
private OrganizationService organizationService;
@Mock private OrganizationService organizationService;
@InjectMocks
private TeamService teamService;
@InjectMocks private TeamService teamService;
@Test
void getDefaultTeam() {
@ -42,7 +36,8 @@ class TeamServiceTest {
team.setOrganization(organization);
when(organizationService.getOrCreateDefaultOrganization()).thenReturn(organization);
when(teamRepository.findByNameAndOrganizationId(TeamService.DEFAULT_TEAM_NAME, organization.getId()))
when(teamRepository.findByNameAndOrganizationId(
TeamService.DEFAULT_TEAM_NAME, organization.getId()))
.thenReturn(Optional.of(team));
Team result = teamService.getOrCreateDefaultTeam();
@ -83,8 +78,9 @@ class TeamServiceTest {
team.setOrganization(organization);
when(organizationService.getOrCreateInternalOrganization()).thenReturn(organization);
when(teamRepository.findByNameAndOrganizationId(TeamService.INTERNAL_TEAM_NAME, organization.getId()))
.thenReturn(Optional.of(team));
when(teamRepository.findByNameAndOrganizationId(
TeamService.INTERNAL_TEAM_NAME, organization.getId()))
.thenReturn(Optional.of(team));
Team result = teamService.getOrCreateInternalTeam();
@ -104,8 +100,9 @@ class TeamServiceTest {
internalTeam.setOrganization(organization);
when(organizationService.getOrCreateInternalOrganization()).thenReturn(organization);
when(teamRepository.findByNameAndOrganizationId(TeamService.INTERNAL_TEAM_NAME, organization.getId()))
.thenReturn(Optional.empty());
when(teamRepository.findByNameAndOrganizationId(
TeamService.INTERNAL_TEAM_NAME, organization.getId()))
.thenReturn(Optional.empty());
when(teamRepository.save(any(Team.class))).thenReturn(internalTeam);
Team result = teamService.getOrCreateInternalTeam();