]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5566 Replace usage of ComponentDao.getByKey by ComponentDao.getAuthorizedCompon...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 11 Sep 2014 14:11:53 +0000 (16:11 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 11 Sep 2014 14:11:53 +0000 (16:11 +0200)
server/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionService.java
server/sonar-server/src/test/java/org/sonar/server/issue/index/IssueAuthorizationIndexMediumTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/InternalPermissionServiceTest.java
sonar-core/src/main/java/org/sonar/core/component/AuthorizedComponentDto.java
sonar-core/src/main/java/org/sonar/core/component/ComponentDto.java
sonar-core/src/main/resources/org/sonar/core/component/db/ComponentMapper.xml

index ef7cbd7566dd6ce0895ea3b314078e717a9e627b..95d78d725f671d691a8e8d807c6ba3c215271995 100644 (file)
@@ -25,13 +25,12 @@ import org.slf4j.LoggerFactory;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.security.DefaultGroups;
 import org.sonar.api.web.UserRole;
-import org.sonar.core.component.ComponentDto;
+import org.sonar.core.component.AuthorizedComponentDto;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.permission.PermissionFacade;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.resource.ResourceDao;
 import org.sonar.core.resource.ResourceDto;
-import org.sonar.core.resource.ResourceQuery;
 import org.sonar.core.user.GroupDto;
 import org.sonar.core.user.UserDao;
 import org.sonar.core.user.UserDto;
@@ -108,7 +107,7 @@ public class InternalPermissionService implements ServerComponent {
 
     DbSession session = dbClient.openSession(false);
     try {
-      ComponentDto component = dbClient.componentDao().getByKey(session, componentKey);
+      AuthorizedComponentDto component = dbClient.componentDao().getAuthorizedComponentByKey(componentKey, session);
       ResourceDto provisioned = resourceDao.selectProvisionedProject(session, componentKey);
       if (provisioned == null) {
         checkProjectAdminPermission(componentKey);
@@ -143,7 +142,7 @@ public class InternalPermissionService implements ServerComponent {
       }
 
       for (String componentKey : query.getSelectedComponents()) {
-        ComponentDto component = (ComponentDto) resourceDao.findByKey(componentKey);
+        AuthorizedComponentDto component = dbClient.componentDao().getAuthorizedComponentByKey(componentKey, session);
         if (component == null) {
           throw new IllegalStateException("Unable to find component with key " + componentKey);
         }
@@ -251,9 +250,8 @@ public class InternalPermissionService implements ServerComponent {
     if (componentKey == null) {
       return null;
     } else {
-      ResourceDto resourceDto = resourceDao.getResource(ResourceQuery.create().setKey(componentKey), session);
-      badRequestIfNullResult(resourceDto, OBJECT_TYPE_COMPONENT, componentKey);
-      return resourceDto.getId();
+      AuthorizedComponentDto component = dbClient.componentDao().getAuthorizedComponentByKey(componentKey, session);
+      return component.getId();
     }
   }
 
@@ -275,7 +273,8 @@ public class InternalPermissionService implements ServerComponent {
   }
 
   private void synchronizePermissions() {
-    // The synchronisation cannot use an existing session, otherwise it's failing with the error : org.apache.ibatis.executor.ExecutorException: Executor was closed
+    // The synchronisation cannot use an existing session, otherwise it's failing with the error :
+    // org.apache.ibatis.executor.ExecutorException: Executor was closed
     DbSession session = dbClient.openSession(false);
     try {
       dbClient.issueAuthorizationDao().synchronizeAfter(session, index.get(IssueAuthorizationIndex.class).getLastSynchronization());
index 94ee04595c13f343cb4cf4cf2ad73a5463739015..3dac0d160955285ff533524edfcd4dafc65c517b 100644 (file)
@@ -81,8 +81,6 @@ public class IssueAuthorizationIndexMediumTest {
     tester.get(PermissionFacade.class).insertUserPermission(project.getId(), john.getId(), UserRole.USER, session);
 
     session.commit();
-    session.clearCache();
-    tester.clearIndexes();
 
     assertThat(index.getByKey(project.getKey())).isNull();
     db.issueAuthorizationDao().synchronizeAfter(session, new Date(0));
@@ -100,4 +98,32 @@ public class IssueAuthorizationIndexMediumTest {
     assertThat(index.getByKey(project.getKey())).isNotNull();
   }
 
+  @Test
+  public void delete_index() throws Exception {
+    project = new ComponentDto()
+      .setKey("Sample")
+      .setProjectId(1L)
+      .setAuthorizationUpdatedAt(DateUtils.parseDate("2014-09-11"));
+    db.componentDao().insert(session, project);
+
+    GroupDto sonarUsers = new GroupDto().setName("devs");
+    db.groupDao().insert(session, sonarUsers);
+
+    UserDto john = new UserDto().setLogin("john").setName("John").setActive(true);
+    db.userDao().insert(session, john);
+
+    tester.get(PermissionFacade.class).insertGroupPermission(project.getId(), "devs", UserRole.USER, session);
+    tester.get(PermissionFacade.class).insertUserPermission(project.getId(), john.getId(), UserRole.USER, session);
+
+    session.commit();
+
+    db.issueAuthorizationDao().synchronizeAfter(session, new Date(0));
+    assertThat(index.getByKey(project.getKey())).isNotNull();
+
+    db.issueAuthorizationDao().deleteByKey(session, project.key());
+    session.commit();
+
+    assertThat(index.getByKey(project.getKey())).isNull();
+  }
+
 }
index 4c5dba92615364a16092108d9c0f3cc51502bec2..609a073f7084d1a612ce0b59303b21591bac9eec 100644 (file)
@@ -34,6 +34,7 @@ import org.mockito.runners.MockitoJUnitRunner;
 import org.sonar.api.resources.Qualifiers;
 import org.sonar.api.security.DefaultGroups;
 import org.sonar.api.web.UserRole;
+import org.sonar.core.component.AuthorizedComponentDto;
 import org.sonar.core.component.ComponentDto;
 import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.permission.PermissionFacade;
@@ -41,7 +42,6 @@ import org.sonar.core.permission.PermissionQuery;
 import org.sonar.core.persistence.DbSession;
 import org.sonar.core.resource.ResourceDao;
 import org.sonar.core.resource.ResourceDto;
-import org.sonar.core.resource.ResourceQuery;
 import org.sonar.core.user.GroupDto;
 import org.sonar.core.user.UserDao;
 import org.sonar.core.user.UserDto;
@@ -169,8 +169,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void add_component_user_permission() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
 
     params = buildPermissionChangeParams("user", null, "org.sample.Sample", "user");
     setUpComponentUserPermissions("user", 10L, "codeviewer");
@@ -195,9 +194,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void remove_component_user_permission() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
-
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
     params = buildPermissionChangeParams("user", null, "org.sample.Sample", "codeviewer");
     setUpComponentUserPermissions("user", 10L, "codeviewer");
     MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN, "org.sample.Sample");
@@ -221,8 +218,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void add_component_group_permission() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
 
     params = buildPermissionChangeParams(null, "group", "org.sample.Sample", "user");
     setUpGlobalGroupPermissions("group", "codeviewer");
@@ -246,8 +242,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void add_component_permission_to_anyone_group() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
 
     params = buildPermissionChangeParams(null, DefaultGroups.ANYONE, "org.sample.Sample", "user");
     MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN, "org.sample.Sample");
@@ -271,8 +266,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void remove_component_group_permission() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
 
     params = buildPermissionChangeParams(null, "group", "org.sample.Sample", "codeviewer");
     setUpComponentGroupPermissions("group", 10L, "codeviewer");
@@ -297,8 +291,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void remove_component_permission_from_anyone_group() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
 
     params = buildPermissionChangeParams(null, DefaultGroups.ANYONE, "org.sample.Sample", "codeviewer");
     setUpComponentGroupPermissions(DefaultGroups.ANYONE, 10L, "codeviewer");
@@ -323,8 +316,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void skip_redundant_add_component_user_permission_change() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
 
     params = buildPermissionChangeParams("user", null, "org.sample.Sample", "codeviewer");
     setUpComponentUserPermissions("user", 10L, "codeviewer");
@@ -349,8 +341,7 @@ public class InternalPermissionServiceTest {
 
   @Test
   public void skip_redundant_add_component_group_permission_change() throws Exception {
-    when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-      new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
 
     params = buildPermissionChangeParams(null, "group", "org.sample.Sample", "codeviewer");
     setUpComponentGroupPermissions("group", 10L, "codeviewer");
@@ -393,16 +384,6 @@ public class InternalPermissionServiceTest {
     }
   }
 
-  @Test
-  public void fail_when_component_is_not_found() throws Exception {
-    try {
-      params = buildPermissionChangeParams(null, "group", "unknown", "user");
-      service.addPermission(params);
-    } catch (Exception e) {
-      assertThat(e).isInstanceOf(BadRequestException.class).hasMessage("Component unknown does not exist");
-    }
-  }
-
   @Test
   public void fail_on_insufficient_global_rights() throws Exception {
     try {
@@ -417,8 +398,7 @@ public class InternalPermissionServiceTest {
   @Test
   public void fail_on_insufficient_project_rights() throws Exception {
     try {
-      when(resourceDao.getResource(any(ResourceQuery.class), eq(session))).thenReturn(
-        new ResourceDto().setId(10L).setKey("org.sample.Sample"));
+      when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(new AuthorizedComponentDto().setId(10L).setKey("org.sample.Sample"));
       params = buildPermissionChangeParams(null, DefaultGroups.ANYONE, "org.sample.Sample", "user");
       MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN);
 
@@ -442,13 +422,13 @@ public class InternalPermissionServiceTest {
   public void apply_permission_template_on_many_projects() throws Exception {
     ComponentDto c1 = mock(ComponentDto.class);
     when(c1.getId()).thenReturn(1L);
-    when(resourceDao.findByKey("org.sample.Sample1")).thenReturn(c1);
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample1", session)).thenReturn(c1);
     ComponentDto c2 = mock(ComponentDto.class);
     when(c2.getId()).thenReturn(2L);
-    when(resourceDao.findByKey("org.sample.Sample2")).thenReturn(c2);
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample2", session)).thenReturn(c2);
     ComponentDto c3 = mock(ComponentDto.class);
     when(c3.getId()).thenReturn(3L);
-    when(resourceDao.findByKey("org.sample.Sample3")).thenReturn(c3);
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample3", session)).thenReturn(c3);
     params = Maps.newHashMap();
     params.put("template_key", "my_template_key");
     params.put("components", "org.sample.Sample1,org.sample.Sample2,org.sample.Sample3");
@@ -468,13 +448,13 @@ public class InternalPermissionServiceTest {
 
     ComponentDto c1 = mock(ComponentDto.class);
     when(c1.getId()).thenReturn(1L);
-    when(resourceDao.findByKey("org.sample.Sample1")).thenReturn(c1);
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample1", session)).thenReturn(c1);
     ComponentDto c2 = mock(ComponentDto.class);
     when(c2.getId()).thenReturn(2L);
-    when(resourceDao.findByKey("org.sample.Sample2")).thenReturn(c2);
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample2", session)).thenReturn(c2);
     ComponentDto c3 = mock(ComponentDto.class);
     when(c3.getId()).thenReturn(3L);
-    when(resourceDao.findByKey("org.sample.Sample3")).thenReturn(c3);
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample3", session)).thenReturn(c3);
     params = Maps.newHashMap();
     params.put("template_key", "my_template_key");
     params.put("components", "org.sample.Sample1,org.sample.Sample2,org.sample.Sample3");
@@ -495,7 +475,7 @@ public class InternalPermissionServiceTest {
 
     ComponentDto c = mock(ComponentDto.class);
     when(c.getId()).thenReturn(1L);
-    when(resourceDao.findByKey("org.sample.Sample")).thenReturn(c);
+    when(componentDao.getAuthorizedComponentByKey("org.sample.Sample", session)).thenReturn(c);
 
     service.applyPermissionTemplate(params);
 
@@ -529,7 +509,7 @@ public class InternalPermissionServiceTest {
     when(mockComponent.qualifier()).thenReturn(qualifier);
     MockUserSession.set().setLogin("admin").addProjectPermissions(UserRole.ADMIN, componentKey);
 
-    when(componentDao.getByKey(session, componentKey)).thenReturn(mockComponent);
+    when(componentDao.getAuthorizedComponentByKey(componentKey, session)).thenReturn(mockComponent);
     service.applyDefaultPermissionTemplate(componentKey);
     verify(permissionFacade).grantDefaultRoles(session, componentId, qualifier);
 
@@ -546,7 +526,7 @@ public class InternalPermissionServiceTest {
     when(mockComponent.getId()).thenReturn(componentId);
     when(mockComponent.qualifier()).thenReturn(qualifier);
 
-    when(componentDao.getByKey(session, componentKey)).thenReturn(mockComponent);
+    when(componentDao.getAuthorizedComponentByKey(componentKey, session)).thenReturn(mockComponent);
     when(resourceDao.selectProvisionedProject(session, componentKey)).thenReturn(mock(ResourceDto.class));
     service.applyDefaultPermissionTemplate(componentKey);
 
@@ -564,7 +544,7 @@ public class InternalPermissionServiceTest {
     when(mockComponent.getId()).thenReturn(componentId);
     when(mockComponent.qualifier()).thenReturn(qualifier);
 
-    when(componentDao.getByKey(session, componentKey)).thenReturn(mockComponent);
+    when(componentDao.getAuthorizedComponentByKey(componentKey, session)).thenReturn(mockComponent);
     when(resourceDao.selectProvisionedProject(session, componentKey)).thenReturn(mock(ResourceDto.class));
     service.applyDefaultPermissionTemplate(componentKey);
 
index 9ad6a40fd3a2906593f08ffc8e062357dfea05fa..d451270dfd730c3c142085bd9f36016079aa68ec 100644 (file)
@@ -24,12 +24,15 @@ import org.sonar.core.persistence.Dto;
 
 /**
  * Used to check that a project exists. Can return provisionned projects and projects from analysis.
+ * The root project id is not available because no join on snapshot is done to retrieve it.
+ *
  * Warning, this component should not be retrieve from db using a join on snapshots, otherwise provisionned projects will not be returned anymore.
  */
 public class AuthorizedComponentDto extends Dto<String> {
 
   private Long id;
   private String kee;
+  private String qualifier;
 
   public Long getId() {
     return id;
@@ -49,6 +52,15 @@ public class AuthorizedComponentDto extends Dto<String> {
     return this;
   }
 
+  public String qualifier() {
+    return qualifier;
+  }
+
+  public AuthorizedComponentDto setAuthoriedQualifier(String qualifier) {
+    this.qualifier = qualifier;
+    return this;
+  }
+
   @Override
   public String getKey() {
     return kee;
index 62aa4acbb0c8249c2903a536707e27ae264ebf99..09a1d3ff81b09566029fba703f2f78bb3571737a 100644 (file)
@@ -31,7 +31,6 @@ public class ComponentDto extends AuthorizedComponentDto implements Component {
   private String path;
   private String name;
   private String longName;
-  private String qualifier;
   private String scope;
   private String language;
   private Long projectId;
@@ -80,12 +79,8 @@ public class ComponentDto extends AuthorizedComponentDto implements Component {
     return this;
   }
 
-  public String qualifier() {
-    return qualifier;
-  }
-
   public ComponentDto setQualifier(String qualifier) {
-    this.qualifier = qualifier;
+    super.setAuthoriedQualifier(qualifier);
     return this;
   }
 
index 20685eb708e5b9b60847acd6bf524df680652022..6f590c5e818e8ce6b2edf0dd1b0df17a286c1695 100644 (file)
@@ -18,7 +18,8 @@
 
   <sql id="authorizedComponentColumns">
     p.id,
-    p.kee as kee
+    p.kee as kee,
+    p.qualifier as qualifier
   </sql>
 
   <select id="selectByKey" parameterType="String" resultType="Component">