]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8893 WS api/organizations/remove_member remove from organization groups
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 14 Mar 2017 15:22:59 +0000 (16:22 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 21 Mar 2017 12:05:50 +0000 (13:05 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserGroupMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserGroupMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/user/UserGroupDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/organization/ws/RemoveMemberAction.java
server/sonar-server/src/test/java/org/sonar/server/organization/ws/RemoveMemberActionTest.java

index 1e53f6a312e7ae50a7dc8353fcb35910a0c7a03b..a056671b5431c471c2873393d3a5b2d9582e0138 100644 (file)
@@ -37,6 +37,10 @@ public class UserGroupDao implements Dao {
     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);
   }
index ce0d053d701ab51abd1e87ee42cb752f5839496a..df021f0af4dd85d46a5fe3f92058540a7d7e3c45 100644 (file)
@@ -28,4 +28,6 @@ public interface UserGroupMapper {
   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);
 }
index 4ff5a60ffa3205b83a024c2b6e06b52ca5b26323..5ac413f7351ecd7a1c14cb1404617cad3fb38115 100644 (file)
     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>
index ca36ee4e574171dcb44ec2fb6a56ff0095d0b253..6fab25df6a3580eda549704fbbd58dab14193915 100644 (file)
@@ -22,12 +22,19 @@ package org.sonar.db.user;
 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();
 
@@ -47,4 +54,23 @@ public class UserGroupDaoTest {
     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());
+  }
 }
index 6ff2a10c59cabe6cae6594d4d279688bc10d9aee..2c9ef9f9c13b0c807a6771c94a8cef27cfa55fdc 100644 (file)
@@ -87,7 +87,8 @@ public class RemoveMemberAction implements OrganizationsWsAction {
       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();
index aaf68807295e3fcd6f609d76ccd68d981a900155..1fc3f9aa6e2bcbeeef78779ccb7b893a4c338eca 100644 (file)
@@ -32,6 +32,7 @@ import org.sonar.db.DbTester;
 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;
@@ -101,7 +102,16 @@ public class RemoveMemberActionTest {
   }
 
   @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);
@@ -130,6 +140,23 @@ public class RemoveMemberActionTest {
     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();
@@ -141,7 +168,7 @@ public class RemoveMemberActionTest {
   }
 
   @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());