use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.
the class OrganizationCreationImpl method insertPersonalOrgDefaultTemplate.
private void insertPersonalOrgDefaultTemplate(DbSession dbSession, OrganizationDto organizationDto) {
long now = system2.now();
Date dateNow = new Date(now);
PermissionTemplateDto permissionTemplateDto = dbClient.permissionTemplateDao().insert(dbSession, new PermissionTemplateDto().setOrganizationUuid(organizationDto.getUuid()).setUuid(uuidFactory.create()).setName("Default template").setDescription(format(PERM_TEMPLATE_DESCRIPTION_PATTERN, organizationDto.getName())).setCreatedAt(dateNow).setUpdatedAt(dateNow));
insertProjectCreatorPermission(dbSession, permissionTemplateDto, UserRole.ADMIN, now);
insertProjectCreatorPermission(dbSession, permissionTemplateDto, UserRole.ISSUE_ADMIN, now);
insertProjectCreatorPermission(dbSession, permissionTemplateDto, OrganizationPermission.SCAN.getKey(), now);
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.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.
the class PermissionTemplateService method wouldUserHaveScanPermissionWithDefaultTemplate.
public boolean wouldUserHaveScanPermissionWithDefaultTemplate(DbSession dbSession, String organizationUuid, @Nullable Integer userId, @Nullable String branch, String projectKey, String qualifier) {
if (userSession.hasPermission(OrganizationPermission.SCAN, organizationUuid)) {
return true;
}
String effectiveKey = ComponentKeys.createKey(projectKey, branch);
ComponentDto dto = new ComponentDto().setOrganizationUuid(organizationUuid).setKey(effectiveKey).setQualifier(qualifier);
PermissionTemplateDto template = findTemplate(dbSession, organizationUuid, dto);
if (template == null) {
return false;
}
List<String> potentialPermissions = dbClient.permissionTemplateDao().selectPotentialPermissionsByUserIdAndTemplateId(dbSession, userId, template.getId());
return potentialPermissions.contains(OrganizationPermission.SCAN.getKey());
}
use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.
the class PermissionTemplateService method copyPermissions.
private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, ComponentDto project, @Nullable Integer projectCreatorUserId) {
dbClient.resourceDao().updateAuthorizationDate(project.getId(), dbSession);
dbClient.groupPermissionDao().deleteByRootComponentId(dbSession, project.getId());
dbClient.userPermissionDao().deleteProjectPermissions(dbSession, project.getId());
List<PermissionTemplateUserDto> usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, template.getId());
String organizationUuid = template.getOrganizationUuid();
usersPermissions.forEach(up -> {
UserPermissionDto dto = new UserPermissionDto(organizationUuid, up.getPermission(), up.getUserId(), project.getId());
dbClient.userPermissionDao().insert(dbSession, dto);
});
List<PermissionTemplateGroupDto> groupsPermissions = dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateId(dbSession, template.getId());
groupsPermissions.forEach(gp -> {
GroupPermissionDto dto = new GroupPermissionDto().setOrganizationUuid(organizationUuid).setGroupId(isAnyone(gp.getGroupName()) ? null : gp.getGroupId()).setRole(gp.getPermission()).setResourceId(project.getId());
dbClient.groupPermissionDao().insert(dbSession, dto);
});
List<PermissionTemplateCharacteristicDto> characteristics = dbClient.permissionTemplateCharacteristicDao().selectByTemplateIds(dbSession, asList(template.getId()));
if (projectCreatorUserId != null) {
Set<String> permissionsForCurrentUserAlreadyInDb = usersPermissions.stream().filter(userPermission -> projectCreatorUserId.equals(userPermission.getUserId())).map(PermissionTemplateUserDto::getPermission).collect(java.util.stream.Collectors.toSet());
characteristics.stream().filter(PermissionTemplateCharacteristicDto::getWithProjectCreator).filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission())).forEach(c -> {
UserPermissionDto dto = new UserPermissionDto(organizationUuid, c.getPermission(), projectCreatorUserId, project.getId());
dbClient.userPermissionDao().insert(dbSession, dto);
});
}
}
use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.
the class RemoveGroupFromTemplateAction method handle.
@Override
public void handle(Request request, Response response) throws Exception {
try (DbSession dbSession = dbClient.openSession(false)) {
String permission = request.mandatoryParam(PARAM_PERMISSION);
PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.fromRequest(request));
checkGlobalAdmin(userSession, template.getOrganizationUuid());
GroupIdOrAnyone groupId = wsSupport.findGroup(dbSession, request);
checkArgument(groupId.getOrganizationUuid().equals(template.getOrganizationUuid()), "Group and template are on different organizations");
dbClient.permissionTemplateDao().deleteGroupPermission(dbSession, template.getId(), groupId.getId(), permission);
dbSession.commit();
}
response.noContent();
}
use of org.sonar.db.permission.template.PermissionTemplateDto in project sonarqube by SonarSource.
the class SearchTemplatesAction method buildTemplatesResponse.
private static void buildTemplatesResponse(WsPermissions.SearchTemplatesWsResponse.Builder response, SearchTemplatesData data) {
Permission.Builder permissionResponse = Permission.newBuilder();
PermissionTemplate.Builder templateBuilder = PermissionTemplate.newBuilder();
for (PermissionTemplateDto templateDto : data.templates()) {
templateBuilder.clear().setId(templateDto.getUuid()).setName(templateDto.getName()).setCreatedAt(formatDateTime(templateDto.getCreatedAt())).setUpdatedAt(formatDateTime(templateDto.getUpdatedAt()));
setNullable(templateDto.getKeyPattern(), templateBuilder::setProjectKeyPattern);
setNullable(templateDto.getDescription(), templateBuilder::setDescription);
for (String permission : ProjectPermissions.ALL) {
templateBuilder.addPermissions(permissionResponse.clear().setKey(permission).setUsersCount(data.userCount(templateDto.getId(), permission)).setGroupsCount(data.groupCount(templateDto.getId(), permission)).setWithProjectCreator(data.withProjectCreator(templateDto.getId(), permission)));
}
response.addPermissionTemplates(templateBuilder);
}
}
Aggregations