]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8278 Support organizations in web service api/permissions/search_templates
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 21 Oct 2016 08:50:08 +0000 (10:50 +0200)
committerSimon Brandhof <simon.brandhof@sonarsource.com>
Fri, 21 Oct 2016 13:58:40 +0000 (15:58 +0200)
12 files changed:
server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java
server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java
server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/BulkApplyTemplateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/SearchTemplatesActionTest.java
sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java
sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java
sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml
sonar-db/src/test/java/org/sonar/db/permission/template/PermissionTemplateDaoTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/SearchTemplatesWsRequest.java

index 5bc1ae7769358c27ad2a65b977418fd3c8eded2d..41f1509dd3b3f17dc402479e2e7de745a3501785 100644 (file)
@@ -44,6 +44,7 @@ import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
 import org.sonar.db.permission.template.PermissionTemplateDto;
 import org.sonar.db.permission.template.PermissionTemplateGroupDto;
 import org.sonar.db.permission.template.PermissionTemplateUserDto;
+import org.sonar.server.organization.DefaultOrganizationProvider;
 import org.sonar.server.permission.index.PermissionIndexer;
 import org.sonar.server.user.UserSession;
 
@@ -60,12 +61,15 @@ public class PermissionTemplateService {
   private final Settings settings;
   private final PermissionIndexer permissionIndexer;
   private final UserSession userSession;
+  private final DefaultOrganizationProvider defaultOrganizationProvider;
 
-  public PermissionTemplateService(DbClient dbClient, Settings settings, PermissionIndexer permissionIndexer, UserSession userSession) {
+  public PermissionTemplateService(DbClient dbClient, Settings settings, PermissionIndexer permissionIndexer, UserSession userSession,
+    DefaultOrganizationProvider defaultOrganizationProvider) {
     this.dbClient = dbClient;
     this.settings = settings;
     this.permissionIndexer = permissionIndexer;
     this.userSession = userSession;
+    this.defaultOrganizationProvider = defaultOrganizationProvider;
   }
 
   /**
@@ -101,7 +105,8 @@ public class PermissionTemplateService {
     applyDefault(session, component, userId);
   }
 
-  public boolean wouldUserHavePermissionWithDefaultTemplate(DbSession dbSession, @Nullable Long userId, String permission, @Nullable String branch, String projectKey, String qualifier) {
+  public boolean wouldUserHavePermissionWithDefaultTemplate(DbSession dbSession, @Nullable Long userId, String permission, @Nullable String branch, String projectKey,
+    String qualifier) {
     if (userSession.hasPermission(permission)) {
       return true;
     }
@@ -199,8 +204,7 @@ public class PermissionTemplateService {
    */
   @CheckForNull
   private PermissionTemplateDto findDefaultTemplate(DbSession dbSession, ComponentDto component) {
-    // FIXME performance issue here, we should not load all templates
-    List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession);
+    List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession, defaultOrganizationProvider.get().getUuid(), null);
     List<PermissionTemplateDto> matchingTemplates = new ArrayList<>();
     for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) {
       String keyPattern = permissionTemplateDto.getKeyPattern();
index 1a9de3dabc55ac636b21baba9b45e24a094ce800..8c6d6a82ca0e72da7839e98e8223a9e4ec4f9c6f 100644 (file)
@@ -26,7 +26,11 @@ import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
 import org.sonar.api.server.ws.WebService.Param;
 import org.sonar.core.permission.ProjectPermissions;
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.template.PermissionTemplateDto;
+import org.sonar.server.permission.ws.PermissionWsSupport;
 import org.sonar.server.permission.ws.PermissionsWsAction;
 import org.sonar.server.user.UserSession;
 import org.sonarqube.ws.WsPermissions;
@@ -37,48 +41,54 @@ import org.sonarqube.ws.WsPermissions.SearchTemplatesWsResponse.TemplateIdQualif
 import org.sonarqube.ws.client.permission.SearchTemplatesWsRequest;
 
 import static org.sonar.api.utils.DateUtils.formatDateTime;
+import static org.sonar.server.permission.ws.PermissionsWsParametersBuilder.createOrganizationParameter;
 import static org.sonar.server.ws.WsUtils.writeProtobuf;
+import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_ORGANIZATION_KEY;
 
 public class SearchTemplatesAction implements PermissionsWsAction {
   private static final String PROPERTY_PREFIX = "projects_role.";
   private static final String DESCRIPTION_SUFFIX = ".desc";
 
+  private final DbClient dbClient;
   private final UserSession userSession;
   private final I18n i18n;
+  private final PermissionWsSupport support;
   private final SearchTemplatesDataLoader dataLoader;
 
-  public SearchTemplatesAction(UserSession userSession, I18n i18n, SearchTemplatesDataLoader dataLoader) {
+  public SearchTemplatesAction(DbClient dbClient, UserSession userSession, I18n i18n, PermissionWsSupport support, SearchTemplatesDataLoader dataLoader) {
+    this.dbClient = dbClient;
     this.userSession = userSession;
     this.i18n = i18n;
+    this.support = support;
     this.dataLoader = dataLoader;
   }
 
   @Override
   public void define(WebService.NewController context) {
-    context.createAction("search_templates")
+    WebService.NewAction action = context.createAction("search_templates")
       .setDescription("List permission templates.<br />" +
         "It requires to be authenticated.")
       .setResponseExample(getClass().getResource("search_templates-example.json"))
       .setSince("5.2")
       .addSearchQuery("defau", "permission template names")
       .setHandler(this);
+
+    createOrganizationParameter(action);
   }
 
   @Override
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     userSession.checkLoggedIn();
 
-    SearchTemplatesWsResponse searchTemplatesWsResponse = doHandle(toSearchTemplatesWsRequest(wsRequest));
-    writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
-  }
+    try (DbSession dbSession = dbClient.openSession(false)) {
+      OrganizationDto org = support.findOrganization(dbSession, wsRequest.param(PARAM_ORGANIZATION_KEY));
+      SearchTemplatesWsRequest request = new SearchTemplatesWsRequest()
+        .setOrganizationUuid(org.getUuid())
+        .setQuery(wsRequest.param(Param.TEXT_QUERY));
 
-  private SearchTemplatesWsResponse doHandle(SearchTemplatesWsRequest wsRequest) {
-    SearchTemplatesData data = dataLoader.load(wsRequest);
-    return buildResponse(data);
-  }
-
-  private static SearchTemplatesWsRequest toSearchTemplatesWsRequest(Request request) {
-    return new SearchTemplatesWsRequest().setQuery(request.param(Param.TEXT_QUERY));
+      SearchTemplatesWsResponse searchTemplatesWsResponse = buildResponse(dataLoader.load(dbSession, request));
+      writeProtobuf(searchTemplatesWsResponse, wsRequest, wsResponse);
+    }
   }
 
   private WsPermissions.SearchTemplatesWsResponse buildResponse(SearchTemplatesData data) {
index 1a9176e8f7980f7e3580adbc9a3755f8b6b0e70d..c14d0a2be897356a8a7c755b3404567d9bf223bf 100644 (file)
@@ -42,28 +42,23 @@ public class SearchTemplatesDataLoader {
     this.defaultPermissionTemplateFinder = defaultPermissionTemplateFinder;
   }
 
-  public SearchTemplatesData load(SearchTemplatesWsRequest request) {
-    try (DbSession dbSession = dbClient.openSession(false)) {
-      SearchTemplatesData.Builder data = builder();
-      List<PermissionTemplateDto> templates = searchTemplates(dbSession, request);
-      List<Long> templateIds = Lists.transform(templates, PermissionTemplateDto::getId);
-      List<TemplateUuidQualifier> defaultTemplates = defaultPermissionTemplateFinder.getDefaultTemplatesByQualifier();
-
-      data.templates(templates)
-        .defaultTemplates(defaultTemplates)
-        .userCountByTemplateIdAndPermission(userCountByTemplateIdAndPermission(dbSession, templateIds))
-        .groupCountByTemplateIdAndPermission(groupCountByTemplateIdAndPermission(dbSession, templateIds))
-        .withProjectCreatorByTemplateIdAndPermission(withProjectCreatorsByTemplateIdAndPermission(dbSession, templateIds));
-
-      return data.build();
-    }
+  public SearchTemplatesData load(DbSession dbSession, SearchTemplatesWsRequest request) {
+    SearchTemplatesData.Builder data = builder();
+    List<PermissionTemplateDto> templates = searchTemplates(dbSession, request);
+    List<Long> templateIds = Lists.transform(templates, PermissionTemplateDto::getId);
+    List<TemplateUuidQualifier> defaultTemplates = defaultPermissionTemplateFinder.getDefaultTemplatesByQualifier();
+
+    data.templates(templates)
+      .defaultTemplates(defaultTemplates)
+      .userCountByTemplateIdAndPermission(userCountByTemplateIdAndPermission(dbSession, templateIds))
+      .groupCountByTemplateIdAndPermission(groupCountByTemplateIdAndPermission(dbSession, templateIds))
+      .withProjectCreatorByTemplateIdAndPermission(withProjectCreatorsByTemplateIdAndPermission(dbSession, templateIds));
+
+    return data.build();
   }
 
   private List<PermissionTemplateDto> searchTemplates(DbSession dbSession, SearchTemplatesWsRequest request) {
-    String nameMatch = request.getQuery();
-
-    return nameMatch == null ? dbClient.permissionTemplateDao().selectAll(dbSession)
-      : dbClient.permissionTemplateDao().selectAll(dbSession, nameMatch);
+    return dbClient.permissionTemplateDao().selectAll(dbSession, request.getOrganizationUuid(), request.getQuery());
   }
 
   private Table<Long, String, Integer> userCountByTemplateIdAndPermission(DbSession dbSession, List<Long> templateIds) {
index acadfff16e401d3d87fc40b95a4499f42953b273..3d247f954de391d472dc6af8d74fe7ebbdd1ced6 100644 (file)
@@ -37,6 +37,8 @@ import org.sonar.db.permission.template.PermissionTemplateDbTester;
 import org.sonar.db.permission.template.PermissionTemplateDto;
 import org.sonar.db.user.GroupDto;
 import org.sonar.db.user.UserDto;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.permission.index.PermissionIndexer;
 import org.sonar.server.tester.UserSessionRule;
 
@@ -49,7 +51,6 @@ import static org.sonar.core.permission.GlobalPermissions.SCAN_EXECUTION;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
 import static org.sonar.db.user.GroupTesting.newGroupDto;
 
-
 public class PermissionTemplateServiceTest {
 
   private static final String DEFAULT_TEMPLATE = "default_20130101_010203";
@@ -69,7 +70,9 @@ public class PermissionTemplateServiceTest {
   private DbSession session = dbTester.getSession();
   private Settings settings = new MapSettings();
   private PermissionIndexer permissionIndexer = mock(PermissionIndexer.class);
-  private PermissionTemplateService underTest = new PermissionTemplateService(dbTester.getDbClient(), settings, permissionIndexer, userSession);
+  private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
+  private PermissionTemplateService underTest = new PermissionTemplateService(dbTester.getDbClient(), settings,
+    permissionIndexer, userSession, defaultOrganizationProvider);
 
   @Before
   public void setUp() {
index 27cb72abf97b33184c205236512e8e90d55ecaf5..957a3235c6b05660b40a36523f1a89f455047ba4 100644 (file)
@@ -40,6 +40,8 @@ import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.issue.index.IssueIndexDefinition;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.permission.index.PermissionIndexer;
 import org.sonar.server.permission.index.PermissionIndexerTester;
@@ -68,14 +70,14 @@ public class ApplyTemplateActionTest extends BasePermissionWsTest<ApplyTemplateA
   private ComponentDto project;
   private PermissionTemplateDto template1;
   private PermissionTemplateDto template2;
-
   private PermissionIndexerTester authorizationIndexerTester = new PermissionIndexerTester(esTester);
-
   private PermissionIndexer permissionIndexer = new PermissionIndexer(db.getDbClient(), esTester.client());
+  private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
 
   @Override
   protected ApplyTemplateAction buildWsAction() {
-    PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(), new MapSettings(), permissionIndexer, userSession);
+    PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(),
+      new MapSettings(), permissionIndexer, userSession, defaultOrganizationProvider);
     return new ApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport());
   }
 
index 4629c3ff422f20148ab2a4b58ad6f595c590f806..9758ee790a4b52357e486a2960f4b8676de25cde 100644 (file)
@@ -34,6 +34,8 @@ import org.sonar.db.user.UserDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.i18n.I18nRule;
+import org.sonar.server.organization.DefaultOrganizationProvider;
+import org.sonar.server.organization.TestDefaultOrganizationProvider;
 import org.sonar.server.permission.PermissionTemplateService;
 import org.sonar.server.permission.index.PermissionIndexer;
 import org.sonar.server.permission.ws.BasePermissionWsTest;
@@ -56,10 +58,12 @@ public class BulkApplyTemplateActionTest extends BasePermissionWsTest<BulkApplyT
   private PermissionTemplateDto template1;
   private PermissionTemplateDto template2;
   private PermissionIndexer issuePermissionIndexer = mock(PermissionIndexer.class);
+  private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db);
 
   @Override
   protected BulkApplyTemplateAction buildWsAction() {
-    PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(), new MapSettings(), issuePermissionIndexer, userSession);
+    PermissionTemplateService permissionTemplateService = new PermissionTemplateService(db.getDbClient(), new MapSettings(),
+      issuePermissionIndexer, userSession, defaultOrganizationProvider);
     return new BulkApplyTemplateAction(db.getDbClient(), userSession, permissionTemplateService, newPermissionWsSupport(), new I18nRule(), newRootResourceTypes());
   }
 
index 6bec7b28b36eb01263120f739421d6e1a0290175..4434d2c8d1cfbb78184402aecae23a9568cc5b71 100644 (file)
@@ -22,18 +22,15 @@ package org.sonar.server.permission.ws.template;
 import java.util.Date;
 import javax.annotation.Nullable;
 import org.junit.Before;
-import org.junit.Rule;
 import org.junit.Test;
-import org.junit.rules.ExpectedException;
 import org.sonar.api.config.MapSettings;
 import org.sonar.api.config.Settings;
 import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
-import org.sonar.db.DbTester;
 import org.sonar.db.component.ResourceTypesRule;
+import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.template.PermissionTemplateCharacteristicDto;
 import org.sonar.db.permission.template.PermissionTemplateDto;
 import org.sonar.db.user.GroupDto;
@@ -41,8 +38,8 @@ import org.sonar.db.user.UserDto;
 import org.sonar.server.exceptions.UnauthorizedException;
 import org.sonar.server.i18n.I18nRule;
 import org.sonar.server.permission.ws.BasePermissionWsTest;
-import org.sonar.server.permission.ws.PermissionsWsAction;
-import org.sonar.server.tester.UserSessionRule;
+import org.sonarqube.ws.MediaTypes;
+import org.sonarqube.ws.WsPermissions;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
@@ -55,15 +52,7 @@ import static org.sonar.db.user.UserTesting.newUserDto;
 import static org.sonar.server.permission.DefaultPermissionTemplates.defaultRootQualifierTemplateProperty;
 import static org.sonar.test.JsonAssert.assertJson;
 
-public class SearchTemplatesActionTest extends BasePermissionWsTest {
-  @Rule
-  public DbTester db = DbTester.create(System2.INSTANCE);
-
-  @Rule
-  public ExpectedException expectedException = ExpectedException.none();
-
-  @Rule
-  public UserSessionRule userSession = UserSessionRule.standalone();
+public class SearchTemplatesActionTest extends BasePermissionWsTest<SearchTemplatesAction> {
 
   private I18nRule i18n = new I18nRule();
   private DbClient dbClient = db.getDbClient();
@@ -71,8 +60,7 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest {
   private ResourceTypesRule resourceTypes = new ResourceTypesRule().setRootQualifiers(Qualifiers.PROJECT, Qualifiers.VIEW, "DEV");
 
   @Override
-  protected PermissionsWsAction buildWsAction() {
-
+  protected SearchTemplatesAction buildWsAction() {
     Settings settings = new MapSettings();
     settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.PROJECT), UUID_EXAMPLE_01);
     settings.setProperty(defaultRootQualifierTemplateProperty(Qualifiers.VIEW), UUID_EXAMPLE_02);
@@ -80,7 +68,7 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest {
 
     DefaultPermissionTemplateFinder defaultPermissionTemplateFinder = new DefaultPermissionTemplateFinder(settings, resourceTypes);
     SearchTemplatesDataLoader dataLoader = new SearchTemplatesDataLoader(dbClient, defaultPermissionTemplateFinder);
-    return new SearchTemplatesAction(userSession, i18n, dataLoader);
+    return new SearchTemplatesAction(dbClient, userSession, i18n, newPermissionWsSupport(), dataLoader);
   }
 
   @Before
@@ -91,7 +79,7 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest {
 
   @Test
   public void search_project_permissions() {
-    PermissionTemplateDto projectTemplate = insertProjectTemplate();
+    PermissionTemplateDto projectTemplate = insertProjectTemplateOnDefaultOrganization();
     PermissionTemplateDto viewsTemplate = insertViewsTemplate();
     PermissionTemplateDto developerTemplate = insertDeveloperTemplate();
 
@@ -138,8 +126,8 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest {
   }
 
   @Test
-  public void search_by_name() {
-    insertProjectTemplate();
+  public void search_by_name_in_default_organization() {
+    insertProjectTemplateOnDefaultOrganization();
     insertViewsTemplate();
     insertDeveloperTemplate();
     db.commit();
@@ -154,6 +142,23 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest {
       .doesNotContain("developers");
   }
 
+  @Test
+  public void search_in_organization() throws Exception {
+    OrganizationDto org = db.organizations().insert();
+    PermissionTemplateDto templateInOrg = insertProjectTemplate(org);
+    PermissionTemplateDto templateInDefaultOrg = insertProjectTemplateOnDefaultOrganization();
+    db.commit();
+
+    WsPermissions.SearchTemplatesWsResponse result = WsPermissions.SearchTemplatesWsResponse.parseFrom(newRequest()
+      .setParam("organization", org.getKey())
+      .setMediaType(MediaTypes.PROTOBUF)
+      .execute()
+      .getInputStream());
+
+    assertThat(result.getPermissionTemplatesCount()).isEqualTo(1);
+    assertThat(result.getPermissionTemplates(0).getId()).isEqualTo(templateInOrg.getUuid());
+  }
+
   @Test
   public void fail_if_not_logged_in() {
     expectedException.expect(UnauthorizedException.class);
@@ -172,9 +177,13 @@ public class SearchTemplatesActionTest extends BasePermissionWsTest {
       .isSimilarTo(getClass().getResource("SearchTemplatesActionTest/display_all_project_permissions.json"));
   }
 
-  private PermissionTemplateDto insertProjectTemplate() {
+  private PermissionTemplateDto insertProjectTemplateOnDefaultOrganization() {
+    return insertProjectTemplate(db.getDefaultOrganization());
+  }
+
+  private PermissionTemplateDto insertProjectTemplate(OrganizationDto org) {
     return insertTemplate(newPermissionTemplateDto()
-      .setOrganizationUuid(db.getDefaultOrganization().getUuid())
+      .setOrganizationUuid(org.getUuid())
       .setUuid(UUID_EXAMPLE_01)
       .setName("Default template for Projects")
       .setDescription("Template for new projects")
index 9beeb044d2c489137c497d98700446ef93d62a21..ac6bb49fefde057d293d66c8dd6c335c2ec128d1 100644 (file)
@@ -101,21 +101,9 @@ public class PermissionTemplateDao implements Dao {
     return mapper(session).selectByUuid(templateUuid);
   }
 
-  /**
-   * @deprecated does not support organizations. Should return group ids.
-   */
-  @Deprecated
-  public List<PermissionTemplateDto> selectAll(DbSession session, String nameMatch) {
-    String uppercaseNameMatch = toUppercaseSqlQuery(nameMatch);
-    return mapper(session).selectAll(uppercaseNameMatch);
-  }
-
-  /**
-   * @deprecated does not support organizations. Should return group ids.
-   */
-  @Deprecated
-  public List<PermissionTemplateDto> selectAll(DbSession session) {
-    return mapper(session).selectAll(null);
+  public List<PermissionTemplateDto> selectAll(DbSession session, String organizationUuid, @Nullable String nameMatch) {
+    String upperCaseNameLikeSql = nameMatch != null ? toUppercaseSqlQuery(nameMatch) : null;
+    return mapper(session).selectAll(organizationUuid, upperCaseNameLikeSql);
   }
 
   private static String toUppercaseSqlQuery(String nameMatch) {
index 64d4626e7b3d947e55e92c4d1e6ce8c7d3931b28..55b3d2569ecd3d0aa1bd8e78017e493b980c3215 100644 (file)
@@ -74,7 +74,7 @@ public interface PermissionTemplateMapper {
 
   int countGroupNamesByQueryAndTemplate(@Param("query") PermissionQuery query, @Param("templateId") long templateId);
 
-  List<PermissionTemplateDto> selectAll(@Nullable @Param("nameMatch") String nameMatch);
+  List<PermissionTemplateDto> selectAll(@Param("organizationUuid") String organizationUuid, @Nullable @Param("upperCaseNameLikeSql") String upperCaseNameLikeSql);
 
   void usersCountByTemplateIdAndPermission(Map<String, Object> parameters, ResultHandler resultHandler);
 
index e60f18a5a82bb7d1d3c75e77541a6afeb7e099bc..0001f3e18f49a38af573c943a59a95edfed4f8a5 100644 (file)
   </select>
 
   <select id="selectAll" parameterType="map" resultType="PermissionTemplate">
-    SELECT
+    select
     <include refid="templateColumns"/>
-    FROM permission_templates
-    <where>
-      <if test="nameMatch!=null">
-        AND (UPPER(name) LIKE #{nameMatch} ESCAPE '/')
-      </if>
-    </where>
-    ORDER BY UPPER(name), name
+    from permission_templates
+    where
+    organization_uuid = #{organizationUuid,jdbcType=VARCHAR}
+    <if test="upperCaseNameLikeSql != null">
+        and upper(name) like #{upperCaseNameLikeSql} escape '/'
+    </if>
+    order by upper(name), name
   </select>
 
   <select id="selectByName" parameterType="map" resultType="PermissionTemplate">
index b4d3ed70416681fc5282e529053df5f2cd21910b..8707fcdb930d45c45564496423469ee6640e1d9d 100644 (file)
@@ -94,17 +94,29 @@ public class PermissionTemplateDaoTest {
   }
 
   @Test
-  public void should_select_all_permission_templates() {
+  public void selectAll_without_name_filtering() {
     db.prepareDbUnit(getClass(), "selectAllPermissionTemplates.xml");
     commit();
 
-    List<PermissionTemplateDto> permissionTemplates = underTest.selectAll(dbSession);
-
+    List<PermissionTemplateDto> permissionTemplates = underTest.selectAll(dbSession, "org1", null);
     assertThat(permissionTemplates).hasSize(3);
     assertThat(permissionTemplates).extracting("id").containsOnly(1L, 2L, 3L);
     assertThat(permissionTemplates).extracting("name").containsOnly("template1", "template2", "template3");
     assertThat(permissionTemplates).extracting("kee").containsOnly("template1_20130102_030405", "template2_20130102_030405", "template3_20130102_030405");
     assertThat(permissionTemplates).extracting("description").containsOnly("description1", "description2", "description3");
+
+    assertThat(underTest.selectAll(dbSession, "missingOrg", null)).isEmpty();
+  }
+
+  @Test
+  public void selectAll_with_name_filtering() {
+    PermissionTemplateDto t1InOrg1 = templateDb.insertTemplate(newPermissionTemplateDto().setName("aBcDeF").setOrganizationUuid("org1"));
+    PermissionTemplateDto t2InOrg1 = templateDb.insertTemplate(newPermissionTemplateDto().setName("cdefgh").setOrganizationUuid("org1"));
+    PermissionTemplateDto t3InOrg1 = templateDb.insertTemplate(newPermissionTemplateDto().setName("hijkl").setOrganizationUuid("org2"));
+    PermissionTemplateDto t4InOrg2 = templateDb.insertTemplate(newPermissionTemplateDto().setName("cdefgh").setOrganizationUuid("org2"));
+
+    assertThat(underTest.selectAll(dbSession, "org1", "def")).extracting(PermissionTemplateDto::getId).containsExactly(t1InOrg1.getId(), t2InOrg1.getId());
+    assertThat(underTest.selectAll(dbSession, "org1", "missing")).isEmpty();
   }
 
   @Test
@@ -239,16 +251,6 @@ public class PermissionTemplateDaoTest {
     assertThat(result).extracting("count").containsOnly(3, 1);
   }
 
-  @Test
-  public void select_by_name_query_and_pagination() {
-    templateDb.insertTemplate(newPermissionTemplateDto().setName("aaabbb"));
-    templateDb.insertTemplate(newPermissionTemplateDto().setName("aaaccc"));
-
-    List<PermissionTemplateDto> templates = underTest.selectAll(dbSession, "aaa");
-
-    assertThat(templates.get(0).getName()).isEqualTo("aaabbb");
-  }
-
   @Test
   public void selectPotentialPermissions_with_unknown_template_and_no_user() {
     List<String> result = underTest.selectPotentialPermissionsByUserIdAndTemplateId(dbSession, null, 42L);
index 92494039cd013d8b0c1007b7c9cc0305ed473671..faa9c5f150e8659e64f16178b9398d30a629053a 100644 (file)
@@ -24,6 +24,7 @@ import javax.annotation.Nullable;
 
 public class SearchTemplatesWsRequest {
   private String query;
+  private String organizationUuid;
 
   @CheckForNull
   public String getQuery() {
@@ -34,4 +35,13 @@ public class SearchTemplatesWsRequest {
     this.query = query;
     return this;
   }
+
+  public String getOrganizationUuid() {
+    return organizationUuid;
+  }
+
+  public SearchTemplatesWsRequest setOrganizationUuid(String s) {
+    this.organizationUuid = s;
+    return this;
+  }
 }