mapper(session).deleteByGroupId(groupId);
}
+ public void deleteByOrganizationAndUser(DbSession dbSession, String organizationUuid, int userId) {
+ mapper(dbSession).deleteByOrganizationAndUser(organizationUuid, userId);
+ }
+
private static UserGroupMapper mapper(DbSession session) {
return session.getMapper(UserGroupMapper.class);
}
void delete(@Param("groupId") int groupId, @Param("userId") int userId);
void deleteByGroupId(@Param("groupId") int groupId);
+
+ void deleteByOrganizationAndUser(@Param("organizationUuid") String organizationUuid, @Param("userId") int userId);
}
where group_id = #{groupId,jdbcType=INTEGER}
</delete>
+ <delete id="deleteByOrganizationAndUser" parameterType="map">
+ delete from groups_users
+ where user_id = #{userId,jdbcType=INTEGER} and
+ group_id in (select id from groups where organization_uuid=#{organizationUuid,jdbcType=VARCHAR})
+ </delete>
+
</mapper>
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.utils.System2;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
+import org.sonar.db.organization.OrganizationDto;
+
+import static org.assertj.core.api.Assertions.assertThat;
public class UserGroupDaoTest {
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
+ private DbClient dbClient = dbTester.getDbClient();
+ private DbSession dbSession = dbTester.getSession();
private UserGroupDao underTest = dbTester.getDbClient().userGroupDao();
dbTester.getSession().commit();
dbTester.assertDbUnit(getClass(), "delete_members_by_group_id-result.xml", "groups_users");
}
+
+ @Test
+ public void delete_organization_member() {
+ OrganizationDto organization = dbTester.organizations().insert();
+ OrganizationDto anotherOrganization = dbTester.organizations().insert();
+ UserDto user = dbTester.users().insertUser();
+ UserDto anotherUser = dbTester.users().insertUser();
+ GroupDto group = dbTester.users().insertGroup(organization);
+ GroupDto anotherGroup = dbTester.users().insertGroup(anotherOrganization);
+ dbTester.users().insertMembers(group, user, anotherUser);
+ dbTester.users().insertMembers(anotherGroup, user, anotherUser);
+
+ underTest.deleteByOrganizationAndUser(dbSession, organization.getUuid(), user.getId());
+
+ assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, user.getId()))
+ .containsOnly(anotherGroup.getId());
+ assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, anotherUser.getId()))
+ .containsOnly(group.getId(), anotherGroup.getId());
+ }
}
OrganizationMemberDto organizationMember = dbClient.organizationMemberDao().select(dbSession, organization.getUuid(), user.getId())
.orElseThrow(() -> BadRequestException.create(format("User '%s' is not a member of organization '%s'", user.getLogin(), organization.getKey())));
- dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, organizationMember.getOrganizationUuid(), organizationMember.getUserId());
+ dbClient.userPermissionDao().deleteOrganizationMemberPermissions(dbSession, organization.getUuid(), user.getId());
+ dbClient.userGroupDao().deleteByOrganizationAndUser(dbSession, organization.getUuid(), user.getId());
dbClient.organizationMemberDao().delete(dbSession, organizationMember.getOrganizationUuid(), organizationMember.getUserId());
dbSession.commit();
import org.sonar.db.component.ComponentDto;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.permission.OrganizationPermission;
+import org.sonar.db.user.GroupDto;
import org.sonar.db.user.UserDto;
import org.sonar.server.exceptions.BadRequestException;
import org.sonar.server.exceptions.ForbiddenException;
}
@Test
- public void remove_member_from_db_and_all_dependencies() {
+ public void remove_member_from_db() {
+ assertMember(organization.getUuid(), user.getId());
+
+ call(organization.getKey(), user.getLogin());
+
+ assertNotAMember(organization.getUuid(), user.getId());
+ }
+
+ @Test
+ public void remove_organization_permissions() {
UserDto anotherUser = db.users().insertUser();
OrganizationDto anotherOrganization = db.organizations().insert();
ComponentDto anotherProject = db.components().insertProject(anotherOrganization);
assertProjectPermissionsOfUser(anotherUser, project, CODEVIEWER, USER);
}
+ @Test
+ public void remove_from_organization_groups() {
+ OrganizationDto anotherOrganization = db.organizations().insert();
+ UserDto anotherUser = db.users().insertUser();
+ GroupDto group = db.users().insertGroup(organization);
+ GroupDto anotherGroup = db.users().insertGroup(anotherOrganization);
+ db.users().insertMembers(group, user, anotherUser);
+ db.users().insertMembers(anotherGroup, user, anotherUser);
+
+ call(organization.getKey(), user.getLogin());
+
+ assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, user.getId()))
+ .containsOnly(anotherGroup.getId());
+ assertThat(dbClient.groupMembershipDao().selectGroupIdsByUserId(dbSession, anotherUser.getId()))
+ .containsOnly(group.getId(), anotherGroup.getId());
+ }
+
@Test
public void user_is_removed_only_from_designated_organization() {
OrganizationDto anotherOrg = db.organizations().insert();
}
@Test
- public void add_member_as_organization_admin() {
+ public void remove_member_as_organization_admin() {
userSession.logIn().addPermission(ADMINISTER, organization);
call(organization.getKey(), user.getLogin());