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()));
}
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));
}
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));
}
}
Aggregations