Search in sources :

Example 11 with DefaultTemplates

use of org.sonar.db.organization.DefaultTemplates in project sonarqube by SonarSource.

the class OrganizationCreationImpl method insertDefaultTemplate.

private void insertDefaultTemplate(DbSession dbSession, OrganizationDto organizationDto, GroupDto group) {
    Date now = new Date(system2.now());
    PermissionTemplateDto permissionTemplateDto = dbClient.permissionTemplateDao().insert(dbSession, new PermissionTemplateDto().setOrganizationUuid(organizationDto.getUuid()).setUuid(uuidFactory.create()).setName(PERM_TEMPLATE_NAME).setDescription(format(PERM_TEMPLATE_DESCRIPTION_PATTERN, organizationDto.getName())).setCreatedAt(now).setUpdatedAt(now));
    insertGroupPermission(dbSession, permissionTemplateDto, UserRole.ADMIN, group);
    insertGroupPermission(dbSession, permissionTemplateDto, UserRole.ISSUE_ADMIN, group);
    insertGroupPermission(dbSession, permissionTemplateDto, GlobalPermissions.SCAN_EXECUTION, group);
    insertGroupPermission(dbSession, permissionTemplateDto, UserRole.USER, null);
    insertGroupPermission(dbSession, permissionTemplateDto, UserRole.CODEVIEWER, null);
    dbClient.organizationDao().setDefaultTemplates(dbSession, organizationDto.getUuid(), new DefaultTemplates().setProjectUuid(permissionTemplateDto.getUuid()));
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) Date(java.util.Date) DefaultTemplates(org.sonar.db.organization.DefaultTemplates)

Example 12 with DefaultTemplates

use of org.sonar.db.organization.DefaultTemplates in project sonarqube by SonarSource.

the class OrganizationCreationImplTest method createForUser_creates_default_template_for_new_organization.

@Test
public void createForUser_creates_default_template_for_new_organization() throws OrganizationCreation.KeyConflictException {
    UserDto user = dbTester.users().insertUser(dto -> dto.setLogin(A_LOGIN).setName(A_NAME));
    when(organizationValidation.generateKeyFrom(A_LOGIN)).thenReturn(SLUG_OF_A_LOGIN);
    mockForSuccessfulInsert(SOME_UUID, SOME_DATE);
    enableCreatePersonalOrg(true);
    underTest.createForUser(dbSession, user);
    OrganizationDto organization = dbClient.organizationDao().selectByKey(dbSession, SLUG_OF_A_LOGIN).get();
    PermissionTemplateDto defaultTemplate = dbClient.permissionTemplateDao().selectByName(dbSession, organization.getUuid(), "default template");
    assertThat(defaultTemplate.getName()).isEqualTo("Default template");
    assertThat(defaultTemplate.getDescription()).isEqualTo("Default permission template of organization " + A_NAME);
    DefaultTemplates defaultTemplates = dbClient.organizationDao().getDefaultTemplates(dbSession, organization.getUuid()).get();
    assertThat(defaultTemplates.getProjectUuid()).isEqualTo(defaultTemplate.getUuid());
    assertThat(defaultTemplates.getViewUuid()).isNull();
    assertThat(dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateId(dbSession, defaultTemplate.getId())).extracting(PermissionTemplateGroupDto::getGroupId, PermissionTemplateGroupDto::getPermission).containsOnly(tuple(ANYONE_GROUP_ID, UserRole.USER), tuple(ANYONE_GROUP_ID, UserRole.CODEVIEWER));
    assertThat(dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, Collections.singletonList(defaultTemplate.getId()))).extracting(PermissionTemplateCharacteristicDto::getWithProjectCreator, PermissionTemplateCharacteristicDto::getPermission).containsOnly(tuple(true, UserRole.ADMIN), tuple(true, UserRole.ISSUE_ADMIN), tuple(true, GlobalPermissions.SCAN_EXECUTION));
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) UserDto(org.sonar.db.user.UserDto) OrganizationDto(org.sonar.db.organization.OrganizationDto) DefaultTemplates(org.sonar.db.organization.DefaultTemplates) Test(org.junit.Test)

Example 13 with DefaultTemplates

use of org.sonar.db.organization.DefaultTemplates 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, String organizationUuid, ComponentDto component) {
    List<PermissionTemplateDto> allPermissionTemplates = dbClient.permissionTemplateDao().selectAll(dbSession, organizationUuid, null);
    List<PermissionTemplateDto> matchingTemplates = new ArrayList<>();
    for (PermissionTemplateDto permissionTemplateDto : allPermissionTemplates) {
        String keyPattern = permissionTemplateDto.getKeyPattern();
        if (StringUtils.isNotBlank(keyPattern) && component.getKey().matches(keyPattern)) {
            matchingTemplates.add(permissionTemplateDto);
        }
    }
    checkAtMostOneMatchForComponentKey(component.getKey(), matchingTemplates);
    if (matchingTemplates.size() == 1) {
        return matchingTemplates.get(0);
    }
    DefaultTemplates defaultTemplates = dbClient.organizationDao().getDefaultTemplates(dbSession, organizationUuid).orElseThrow(() -> new IllegalStateException(format("No Default templates defined for organization with uuid '%s'", organizationUuid)));
    String qualifier = component.qualifier();
    DefaultTemplatesResolverImpl.ResolvedDefaultTemplates resolvedDefaultTemplates = defaultTemplatesResolver.resolve(defaultTemplates);
    if (Qualifiers.PROJECT.equals(qualifier)) {
        return dbClient.permissionTemplateDao().selectByUuid(dbSession, resolvedDefaultTemplates.getProject());
    } else if (Qualifiers.VIEW.equals(qualifier)) {
        String viewDefaultTemplateUuid = resolvedDefaultTemplates.getView().orElseThrow(() -> new IllegalStateException("Attempt to create a view when Governance plugin is not installed"));
        return dbClient.permissionTemplateDao().selectByUuid(dbSession, viewDefaultTemplateUuid);
    } else {
        throw new IllegalArgumentException(format("Qualifier '%s' is not supported", qualifier));
    }
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) DefaultTemplatesResolverImpl(org.sonar.server.permission.ws.template.DefaultTemplatesResolverImpl) ArrayList(java.util.ArrayList) DefaultTemplates(org.sonar.db.organization.DefaultTemplates) CheckForNull(javax.annotation.CheckForNull)

Aggregations

DefaultTemplates (org.sonar.db.organization.DefaultTemplates)13 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)8 Test (org.junit.Test)6 OrganizationDto (org.sonar.db.organization.OrganizationDto)3 Date (java.util.Date)2 DbSession (org.sonar.db.DbSession)2 PermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateGroupDto)2 GroupDto (org.sonar.db.user.GroupDto)2 UserDto (org.sonar.db.user.UserDto)2 ArrayList (java.util.ArrayList)1 CheckForNull (javax.annotation.CheckForNull)1 OrganizationDao (org.sonar.db.organization.OrganizationDao)1 ResolvedDefaultTemplates (org.sonar.server.permission.ws.template.DefaultTemplatesResolver.ResolvedDefaultTemplates)1 DefaultTemplatesResolverImpl (org.sonar.server.permission.ws.template.DefaultTemplatesResolverImpl)1