Search in sources :

Example 1 with ResolvedDefaultTemplates

use of org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates in project sonarqube by SonarSource.

the class SearchTemplatesAction method load.

private SearchTemplatesData load(DbSession dbSession, SearchTemplatesRequest request) {
    SearchTemplatesData.Builder data = builder();
    List<PermissionTemplateDto> templates = searchTemplates(dbSession, request);
    List<String> templateUuids = templates.stream().map(PermissionTemplateDto::getUuid).collect(Collectors.toList());
    ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(dbSession);
    data.templates(templates).defaultTemplates(resolvedDefaultTemplates).userCountByTemplateUuidAndPermission(userCountByTemplateUuidAndPermission(dbSession, templateUuids)).groupCountByTemplateUuidAndPermission(groupCountByTemplateUuidAndPermission(dbSession, templateUuids)).withProjectCreatorByTemplateUuidAndPermission(withProjectCreatorsByTemplateUuidAndPermission(dbSession, templateUuids));
    return data.build();
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) ResolvedDefaultTemplates(org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates)

Example 2 with ResolvedDefaultTemplates

use of org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates in project sonarqube by SonarSource.

the class DeleteTemplateAction method checkTemplateUuidIsNotDefault.

private void checkTemplateUuidIsNotDefault(DbSession dbSession, PermissionTemplateDto template) {
    ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(dbSession);
    checkRequest(!resolvedDefaultTemplates.getProject().equals(template.getUuid()), "It is not possible to delete the default permission template for projects");
    resolvedDefaultTemplates.getApplication().ifPresent(defaultApplicationTemplate -> checkRequest(!defaultApplicationTemplate.equals(template.getUuid()), "It is not possible to delete the default permission template for applications"));
    resolvedDefaultTemplates.getPortfolio().ifPresent(defaultPortfolioTemplate -> checkRequest(!defaultPortfolioTemplate.equals(template.getUuid()), "It is not possible to delete the default permission template for portfolios"));
}
Also used : ResolvedDefaultTemplates(org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates)

Example 3 with ResolvedDefaultTemplates

use of org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates in project sonarqube by SonarSource.

the class PermissionTemplateService method findTemplate.

/**
 * Return the permission template for the given component. If no template key pattern match then consider default
 * template for the component qualifier.
 */
@CheckForNull
private PermissionTemplateDto findTemplate(DbSession dbSession, ComponentDto component) {
    List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession, null);
    List<PermissionTemplateDto> matchingTemplates = new ArrayList<>();
    for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) {
        String keyPattern = permissionTemplateDto.getKeyPattern();
        if (StringUtils.isNotBlank(keyPattern) && component.getDbKey().matches(keyPattern)) {
            matchingTemplates.add(permissionTemplateDto);
        }
    }
    checkAtMostOneMatchForComponentKey(component.getDbKey(), matchingTemplates);
    if (matchingTemplates.size() == 1) {
        return matchingTemplates.get(0);
    }
    String qualifier = component.qualifier();
    ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(dbSession);
    switch(qualifier) {
        case Qualifiers.PROJECT:
            return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
        case Qualifiers.VIEW:
            String portDefaultTemplateUuid = resolvedDefaultTemplates.getPortfolio().orElseThrow(() -> new IllegalStateException("Failed to find default template for portfolios"));
            return dbClient.permissionTemplateDao().selectByUuid(dbSession, portDefaultTemplateUuid);
        case Qualifiers.APP:
            String appDefaultTemplateUuid = resolvedDefaultTemplates.getApplication().orElseThrow(() -> new IllegalStateException("Failed to find default template for applications"));
            return dbClient.permissionTemplateDao().selectByUuid(dbSession, appDefaultTemplateUuid);
        default:
            throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
    }
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) ResolvedDefaultTemplates(org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates) ArrayList(java.util.ArrayList) CheckForNull(javax.annotation.CheckForNull)

Example 4 with ResolvedDefaultTemplates

use of org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates in project sonarqube by SonarSource.

the class SearchTemplatesAction method buildDefaultTemplatesResponse.

private static void buildDefaultTemplatesResponse(SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
    TemplateIdQualifier.Builder templateUuidQualifierBuilder = TemplateIdQualifier.newBuilder();
    ResolvedDefaultTemplates resolvedDefaultTemplates = data.defaultTemplates();
    response.addDefaultTemplates(templateUuidQualifierBuilder.setQualifier(Qualifiers.PROJECT).setTemplateId(resolvedDefaultTemplates.getProject()));
    resolvedDefaultTemplates.getApplication().ifPresent(viewDefaultTemplate -> response.addDefaultTemplates(templateUuidQualifierBuilder.clear().setQualifier(Qualifiers.APP).setTemplateId(viewDefaultTemplate)));
    resolvedDefaultTemplates.getPortfolio().ifPresent(viewDefaultTemplate -> response.addDefaultTemplates(templateUuidQualifierBuilder.clear().setQualifier(Qualifiers.VIEW).setTemplateId(viewDefaultTemplate)));
}
Also used : TemplateIdQualifier(org.sonarqube.ws.Permissions.SearchTemplatesWsResponse.TemplateIdQualifier) ResolvedDefaultTemplates(org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates)

Aggregations

ResolvedDefaultTemplates (org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates)4 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)2 ArrayList (java.util.ArrayList)1 CheckForNull (javax.annotation.CheckForNull)1 TemplateIdQualifier (org.sonarqube.ws.Permissions.SearchTemplatesWsResponse.TemplateIdQualifier)1