From: Simon Brandhof Date: Fri, 21 Oct 2016 08:50:08 +0000 (+0200) Subject: SONAR-8278 Support organizations in web service api/permissions/search_templates X-Git-Tag: 6.2-RC1~294 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=f34266f025c3f862c5f1bdf58a0983545334c4ae;p=sonarqube.git SONAR-8278 Support organizations in web service api/permissions/search_templates --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java index 5bc1ae77693..41f1509dd3b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/PermissionTemplateService.java @@ -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 allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession); + List allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession, defaultOrganizationProvider.get().getUuid(), null); List matchingTemplates = new ArrayList<>(); for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) { String keyPattern = permissionTemplateDto.getKeyPattern(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java index 1a9de3dabc5..8c6d6a82ca0 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesAction.java @@ -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.
" + "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) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java index 1a9176e8f79..c14d0a2be89 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java +++ b/server/sonar-server/src/main/java/org/sonar/server/permission/ws/template/SearchTemplatesDataLoader.java @@ -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 templates = searchTemplates(dbSession, request); - List templateIds = Lists.transform(templates, PermissionTemplateDto::getId); - List 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 templates = searchTemplates(dbSession, request); + List templateIds = Lists.transform(templates, PermissionTemplateDto::getId); + List 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 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 userCountByTemplateIdAndPermission(DbSession dbSession, List templateIds) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java index acadfff16e4..3d247f954de 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/PermissionTemplateServiceTest.java @@ -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() { diff --git a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java index 27cb72abf97..957a3235c6b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/permission/ws/template/ApplyTemplateActionTest.java @@ -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 { 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") diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java index 9beeb044d2c..ac6bb49fefd 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateDao.java @@ -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 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 selectAll(DbSession session) { - return mapper(session).selectAll(null); + public List 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) { diff --git a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java index 64d4626e7b3..55b3d2569ec 100644 --- a/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java +++ b/sonar-db/src/main/java/org/sonar/db/permission/template/PermissionTemplateMapper.java @@ -74,7 +74,7 @@ public interface PermissionTemplateMapper { int countGroupNamesByQueryAndTemplate(@Param("query") PermissionQuery query, @Param("templateId") long templateId); - List selectAll(@Nullable @Param("nameMatch") String nameMatch); + List selectAll(@Param("organizationUuid") String organizationUuid, @Nullable @Param("upperCaseNameLikeSql") String upperCaseNameLikeSql); void usersCountByTemplateIdAndPermission(Map parameters, ResultHandler resultHandler); diff --git a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml index e60f18a5a82..0001f3e18f4 100644 --- a/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/permission/template/PermissionTemplateMapper.xml @@ -195,15 +195,15 @@