diff options
author | Michal Duda <michal.duda@sonarsource.com> | 2020-12-14 21:18:50 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-12-22 20:09:36 +0000 |
commit | a8cd264afd2a7532ee0f3d9809db2d5cb8c32e6e (patch) | |
tree | 66ff12305b5060bc93b844dae11649540de14b3f /server/sonar-webserver-auth | |
parent | 31c59b718a3c074deb51bfadf8152fb6317a826a (diff) | |
download | sonarqube-a8cd264afd2a7532ee0f3d9809db2d5cb8c32e6e.tar.gz sonarqube-a8cd264afd2a7532ee0f3d9809db2d5cb8c32e6e.zip |
SONAR-13999 remove organizations from user WS
Diffstat (limited to 'server/sonar-webserver-auth')
7 files changed, 51 insertions, 99 deletions
diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java index f24d55a9cc0..63c66c7e334 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/organization/MemberUpdater.java @@ -28,7 +28,6 @@ import org.sonar.db.DbSession; import org.sonar.db.alm.ALM; import org.sonar.db.alm.OrganizationAlmBindingDto; import org.sonar.db.organization.OrganizationDto; -import org.sonar.db.organization.OrganizationMemberDto; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserGroupDto; import org.sonar.server.user.index.UserIndexer; @@ -56,12 +55,12 @@ public class MemberUpdater { this.userIndexer = userIndexer; } - public void addMember(DbSession dbSession, OrganizationDto organization, UserDto user) { - addMembers(dbSession, organization, singletonList(user)); + public void addMember(DbSession dbSession, UserDto user) { + addMembers(dbSession, singletonList(user)); } - public void addMembers(DbSession dbSession, OrganizationDto organization, List<UserDto> users) { - Set<String> currentMemberUuids = new HashSet<>(dbClient.organizationMemberDao().selectUserUuidsByOrganizationUuid(dbSession, organization.getUuid())); + public void addMembers(DbSession dbSession, List<UserDto> users) { + Set<String> currentMemberUuids = dbClient.userGroupDao().selectUserUuidsInGroup(dbSession, defaultGroupFinder.findDefaultGroup(dbSession).getUuid()); List<UserDto> usersToAdd = users.stream() .filter(UserDto::isActive) .filter(u -> !currentMemberUuids.contains(u.getUuid())) @@ -69,22 +68,21 @@ public class MemberUpdater { if (usersToAdd.isEmpty()) { return; } - usersToAdd.forEach(u -> addMemberInDb(dbSession, organization, u)); + usersToAdd.forEach(u -> addMemberInDb(dbSession, u)); userIndexer.commitAndIndex(dbSession, usersToAdd); } - private void addMemberInDb(DbSession dbSession, OrganizationDto organization, UserDto user) { - dbClient.organizationMemberDao().insert(dbSession, new OrganizationMemberDto() - .setOrganizationUuid(organization.getUuid()) - .setUserUuid(user.getUuid())); + private void addMemberInDb(DbSession dbSession, UserDto user) { dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setGroupUuid(defaultGroupFinder.findDefaultGroup(dbSession).getUuid()).setUserUuid(user.getUuid())); } + // TODO remove org param like it was removed from addMembers public void removeMember(DbSession dbSession, OrganizationDto organization, UserDto user) { removeMembers(dbSession, organization, singletonList(user)); } + // TODO remove org param like it was removed from addMembers public void removeMembers(DbSession dbSession, OrganizationDto organization, List<UserDto> users) { Set<String> currentMemberIds = new HashSet<>(dbClient.organizationMemberDao().selectUserUuidsByOrganizationUuid(dbSession, organization.getUuid())); List<UserDto> usersToRemove = users.stream() @@ -126,7 +124,7 @@ public class MemberUpdater { allOrganizationsByUuid.entrySet().stream() .filter(entry -> organizationUuidsToBeAdded.contains(entry.getKey())) - .forEach(entry -> addMemberInDb(dbSession, entry.getValue(), user)); + .forEach(entry -> addMemberInDb(dbSession, user)); allOrganizationsByUuid.entrySet().stream() .filter(entry -> organizationUuidsToBeRemoved.contains(entry.getKey())) .forEach(entry -> removeMemberInDb(dbSession, entry.getValue(), user)); diff --git a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java index ba996626e59..bce94f4ee31 100644 --- a/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java +++ b/server/sonar-webserver-auth/src/main/java/org/sonar/server/user/UserUpdater.java @@ -35,12 +35,10 @@ import org.sonar.api.platform.NewUserHandler; import org.sonar.api.server.ServerSide; import org.sonar.db.DbClient; import org.sonar.db.DbSession; -import org.sonar.db.organization.OrganizationMemberDto; import org.sonar.db.user.GroupDto; import org.sonar.db.user.UserDto; import org.sonar.db.user.UserGroupDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; -import org.sonar.server.organization.DefaultOrganizationProvider; import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; import org.sonar.server.util.Validation; @@ -75,18 +73,15 @@ public class UserUpdater { private final NewUserNotifier newUserNotifier; private final DbClient dbClient; private final UserIndexer userIndexer; - private final DefaultOrganizationProvider defaultOrganizationProvider; private final DefaultGroupFinder defaultGroupFinder; private final Configuration config; private final CredentialsLocalAuthentication localAuthentication; - public UserUpdater(NewUserNotifier newUserNotifier, DbClient dbClient, UserIndexer userIndexer, - DefaultOrganizationProvider defaultOrganizationProvider, DefaultGroupFinder defaultGroupFinder, Configuration config, + public UserUpdater(NewUserNotifier newUserNotifier, DbClient dbClient, UserIndexer userIndexer, DefaultGroupFinder defaultGroupFinder, Configuration config, CredentialsLocalAuthentication localAuthentication) { this.newUserNotifier = newUserNotifier; this.dbClient = dbClient; this.userIndexer = userIndexer; - this.defaultOrganizationProvider = defaultOrganizationProvider; this.defaultGroupFinder = defaultGroupFinder; this.config = config; this.localAuthentication = localAuthentication; @@ -120,7 +115,7 @@ public class UserUpdater { setOnboarded(reactivatedUser); updateDto(dbSession, updateUser, reactivatedUser); updateUser(dbSession, reactivatedUser); - addUserToDefaultOrganizationAndDefaultGroup(dbSession, reactivatedUser); + addUserToDefaultGroup(dbSession, reactivatedUser); } public void updateAndCommit(DbSession dbSession, UserDto dto, UpdateUser updateUser, Consumer<UserDto> beforeCommit, UserDto... otherUsersToIndex) { @@ -416,7 +411,7 @@ public class UserUpdater { private UserDto saveUser(DbSession dbSession, UserDto userDto) { userDto.setActive(true); UserDto res = dbClient.userDao().insert(dbSession, userDto); - addUserToDefaultOrganizationAndDefaultGroup(dbSession, userDto); + addUserToDefaultGroup(dbSession, userDto); return res; } @@ -437,16 +432,10 @@ public class UserUpdater { return userGroups.stream().anyMatch(group -> defaultGroup.getUuid().equals(group.getUuid())); } - private void addUserToDefaultOrganizationAndDefaultGroup(DbSession dbSession, UserDto userDto) { - addUserToDefaultOrganization(dbSession, userDto); + private void addUserToDefaultGroup(DbSession dbSession, UserDto userDto) { addDefaultGroup(dbSession, userDto); } - private void addUserToDefaultOrganization(DbSession dbSession, UserDto userDto) { - String defOrgUuid = defaultOrganizationProvider.get().getUuid(); - dbClient.organizationMemberDao().insert(dbSession, new OrganizationMemberDto().setOrganizationUuid(defOrgUuid).setUserUuid(userDto.getUuid())); - } - private void addDefaultGroup(DbSession dbSession, UserDto userDto) { List<GroupDto> userGroups = dbClient.groupDao().selectByUserLogin(dbSession, userDto.getLogin()); GroupDto defaultGroup = defaultGroupFinder.findDefaultGroup(dbSession); diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java index c97df1924a5..c5daa4ab305 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/HttpHeadersAuthenticationTest.java @@ -40,8 +40,6 @@ import org.sonar.db.user.UserDto; import org.sonar.server.authentication.event.AuthenticationEvent; import org.sonar.server.authentication.event.AuthenticationEvent.Source; import org.sonar.server.es.EsTester; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; import org.sonar.server.user.index.UserIndexer; @@ -63,7 +61,7 @@ import static org.sonar.server.authentication.event.AuthenticationExceptionMatch public class HttpHeadersAuthenticationTest { - private MapSettings settings = new MapSettings(); + private final MapSettings settings = new MapSettings(); @Rule public ExpectedException expectedException = none(); @@ -93,24 +91,20 @@ public class HttpHeadersAuthenticationTest { private GroupDto group2; private GroupDto sonarUsers; - private System2 system2 = mock(System2.class); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); + private final System2 system2 = mock(System2.class); + private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); - private UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); + private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); private final DefaultGroupFinder defaultGroupFinder = new DefaultGroupFinder(db.getDbClient()); - private final UserRegistrarImpl userIdentityAuthenticator = new UserRegistrarImpl( - db.getDbClient(), - new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), userIndexer, defaultOrganizationProvider, defaultGroupFinder, settings.asConfig(), - localAuthentication), + private final UserRegistrarImpl userIdentityAuthenticator = new UserRegistrarImpl(db.getDbClient(), + new UserUpdater(mock(NewUserNotifier.class), db.getDbClient(), userIndexer, defaultGroupFinder, settings.asConfig(), localAuthentication), defaultGroupFinder); - - private HttpServletResponse response = mock(HttpServletResponse.class); - private JwtHttpHandler jwtHttpHandler = mock(JwtHttpHandler.class); - private AuthenticationEvent authenticationEvent = mock(AuthenticationEvent.class); - - private HttpHeadersAuthentication underTest = new HttpHeadersAuthentication(system2, settings.asConfig(), userIdentityAuthenticator, jwtHttpHandler, authenticationEvent); + private final HttpServletResponse response = mock(HttpServletResponse.class); + private final JwtHttpHandler jwtHttpHandler = mock(JwtHttpHandler.class); + private final AuthenticationEvent authenticationEvent = mock(AuthenticationEvent.class); + private final HttpHeadersAuthentication underTest = new HttpHeadersAuthentication(system2, settings.asConfig(), userIdentityAuthenticator, jwtHttpHandler, + authenticationEvent); @Before public void setUp() { diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java index f73873bc814..438361bd358 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/authentication/UserRegistrarImplTest.java @@ -37,8 +37,6 @@ import org.sonar.server.authentication.event.AuthenticationEvent; import org.sonar.server.authentication.event.AuthenticationEvent.Source; import org.sonar.server.authentication.exception.EmailAlreadyExistsRedirectionException; import org.sonar.server.es.EsTester; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.user.NewUserNotifier; import org.sonar.server.user.UserUpdater; import org.sonar.server.user.index.UserIndexer; @@ -54,22 +52,22 @@ import static org.sonar.server.authentication.event.AuthenticationEvent.Method.B import static org.sonar.server.authentication.event.AuthenticationExceptionMatcher.authenticationException; public class UserRegistrarImplTest { - private static String USER_LOGIN = "johndoo"; + private static final String USER_LOGIN = "johndoo"; - private static UserIdentity USER_IDENTITY = UserIdentity.builder() + private static final UserIdentity USER_IDENTITY = UserIdentity.builder() .setProviderId("ABCD") .setProviderLogin("johndoo") .setName("John") .setEmail("john@email.com") .build(); - private static TestIdentityProvider IDENTITY_PROVIDER = new TestIdentityProvider() + private static final TestIdentityProvider IDENTITY_PROVIDER = new TestIdentityProvider() .setKey("github") .setName("name of github") .setEnabled(true) .setAllowsUsersToSignUp(true); - private MapSettings settings = new MapSettings(); + private final MapSettings settings = new MapSettings(); @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -77,20 +75,18 @@ public class UserRegistrarImplTest { public DbTester db = DbTester.create(new AlwaysIncreasingSystem2()); @Rule public EsTester es = EsTester.create(); - private UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); + private final UserIndexer userIndexer = new UserIndexer(db.getDbClient(), es.client()); + private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); private final DefaultGroupFinder groupFinder = new DefaultGroupFinder(db.getDbClient()); - private UserUpdater userUpdater = new UserUpdater( + private final UserUpdater userUpdater = new UserUpdater( mock(NewUserNotifier.class), db.getDbClient(), userIndexer, - defaultOrganizationProvider, groupFinder, settings.asConfig(), localAuthentication); - private UserRegistrarImpl underTest = new UserRegistrarImpl(db.getDbClient(), userUpdater, groupFinder); + private final UserRegistrarImpl underTest = new UserRegistrarImpl(db.getDbClient(), userUpdater, groupFinder); private GroupDto defaultGroup; @Before diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java index 897f737c37f..b2c279063ef 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterCreateTest.java @@ -41,8 +41,6 @@ import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.authentication.CredentialsLocalAuthentication.HashMethod; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.user.index.UserIndexDefinition; import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; @@ -62,7 +60,7 @@ public class UserUpdaterCreateTest { private static final String DEFAULT_LOGIN = "marius"; - private System2 system2 = new AlwaysIncreasingSystem2(); + private final System2 system2 = new AlwaysIncreasingSystem2(); @Rule public ExpectedException expectedException = ExpectedException.none(); @@ -71,16 +69,14 @@ public class UserUpdaterCreateTest { @Rule public DbTester db = DbTester.create(system2); - private DbClient dbClient = db.getDbClient(); - private NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); - private ArgumentCaptor<NewUserHandler.Context> newUserHandler = ArgumentCaptor.forClass(NewUserHandler.Context.class); - private DbSession session = db.getSession(); - private UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private MapSettings settings = new MapSettings(); - private CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); - - private UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, defaultOrganizationProvider, + private final DbClient dbClient = db.getDbClient(); + private final NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); + private final ArgumentCaptor<NewUserHandler.Context> newUserHandler = ArgumentCaptor.forClass(NewUserHandler.Context.class); + private final DbSession session = db.getSession(); + private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); + private final MapSettings settings = new MapSettings(); + private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); + private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, new DefaultGroupFinder(dbClient), settings.asConfig(), localAuthentication); @Test @@ -300,7 +296,7 @@ public class UserUpdaterCreateTest { } @Test - public void does_not_set_notifications_readDate_setting_when_creating_user_when_not_on_and_organization_disabled() { + public void does_not_set_notifications_readDate_setting_when_creating_user_when_not_on() { createDefaultGroup(); UserDto user = underTest.createAndCommit(db.getSession(), NewUser.builder() @@ -589,21 +585,6 @@ public class UserUpdaterCreateTest { }); } - @Test - public void add_user_as_member_of_default_organization_when_creating_user_and_organizations_are_disabled() { - createDefaultGroup(); - - UserDto dto = underTest.createAndCommit(db.getSession(), NewUser.builder() - .setLogin("user") - .setName("User") - .setEmail("user@mail.com") - .setPassword("PASSWORD") - .build(), u -> { - }); - - assertThat(dbClient.organizationMemberDao().select(db.getSession(), defaultOrganizationProvider.get().getUuid(), dto.getUuid())).isPresent(); - } - private GroupDto createDefaultGroup() { return db.users().insertDefaultGroup(); } diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java index f64f25069a8..98ba2cac36c 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterReactivateTest.java @@ -35,8 +35,6 @@ import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.authentication.CredentialsLocalAuthentication.HashMethod; import org.sonar.server.es.EsTester; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; @@ -61,10 +59,9 @@ public class UserUpdaterReactivateTest { private final NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); private final DbSession session = db.getSession(); private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); - private final DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); private final MapSettings settings = new MapSettings(); private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); - private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, defaultOrganizationProvider, + private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, new DefaultGroupFinder(dbClient), settings.asConfig(), localAuthentication); diff --git a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java index 63995f29ce7..f195d3d4ca4 100644 --- a/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java +++ b/server/sonar-webserver-auth/src/test/java/org/sonar/server/user/UserUpdaterUpdateTest.java @@ -39,8 +39,6 @@ import org.sonar.db.user.UserDto; import org.sonar.server.authentication.CredentialsLocalAuthentication; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.organization.DefaultOrganizationProvider; -import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.user.index.UserIndexDefinition; import org.sonar.server.user.index.UserIndexer; import org.sonar.server.usergroups.DefaultGroupFinder; @@ -63,21 +61,20 @@ public class UserUpdaterUpdateTest { private static final Consumer<UserDto> EMPTY_USER_CONSUMER = userDto -> { }; - private System2 system2 = new AlwaysIncreasingSystem2(); + private final System2 system2 = new AlwaysIncreasingSystem2(); @Rule public EsTester es = EsTester.create(); @Rule public DbTester db = DbTester.create(system2); - private DbClient dbClient = db.getDbClient(); - private NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); - private DbSession session = db.getSession(); - private UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); - private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); - private MapSettings settings = new MapSettings(); - private CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); - private UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, defaultOrganizationProvider, + private final DbClient dbClient = db.getDbClient(); + private final NewUserNotifier newUserNotifier = mock(NewUserNotifier.class); + private final DbSession session = db.getSession(); + private final UserIndexer userIndexer = new UserIndexer(dbClient, es.client()); + private final MapSettings settings = new MapSettings(); + private final CredentialsLocalAuthentication localAuthentication = new CredentialsLocalAuthentication(db.getDbClient()); + private final UserUpdater underTest = new UserUpdater(newUserNotifier, dbClient, userIndexer, new DefaultGroupFinder(dbClient), settings.asConfig(), localAuthentication); @Test |