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;
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;
}
/**
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;
}
*/
@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();
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;
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) {
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) {
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;
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";
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() {
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;
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());
}
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;
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());
}
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;
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;
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();
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);
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
@Test
public void search_project_permissions() {
- PermissionTemplateDto projectTemplate = insertProjectTemplate();
+ PermissionTemplateDto projectTemplate = insertProjectTemplateOnDefaultOrganization();
PermissionTemplateDto viewsTemplate = insertViewsTemplate();
PermissionTemplateDto developerTemplate = insertDeveloperTemplate();
}
@Test
- public void search_by_name() {
- insertProjectTemplate();
+ public void search_by_name_in_default_organization() {
+ insertProjectTemplateOnDefaultOrganization();
insertViewsTemplate();
insertDeveloperTemplate();
db.commit();
.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);
.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")
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) {
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);
</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">
}
@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
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);
public class SearchTemplatesWsRequest {
private String query;
+ private String organizationUuid;
@CheckForNull
public String getQuery() {
this.query = query;
return this;
}
+
+ public String getOrganizationUuid() {
+ return organizationUuid;
+ }
+
+ public SearchTemplatesWsRequest setOrganizationUuid(String s) {
+ this.organizationUuid = s;
+ return this;
+ }
}