Search in sources :

Example 1 with PermissionTemplateDto

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()));
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) Date(java.util.Date) DefaultTemplates(org.sonar.db.organization.DefaultTemplates)

Example 2 with PermissionTemplateDto

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());
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) ComponentDto(org.sonar.db.component.ComponentDto)

Example 3 with PermissionTemplateDto

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);
        });
    }
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) StringUtils(org.apache.commons.lang.StringUtils) OrganizationPermission(org.sonar.db.permission.OrganizationPermission) DbSession(org.sonar.db.DbSession) GroupPermissionDto(org.sonar.db.permission.GroupPermissionDto) PermissionTemplateGroupDto(org.sonar.db.permission.template.PermissionTemplateGroupDto) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) Collections.singletonList(java.util.Collections.singletonList) DefaultTemplatesResolverImpl(org.sonar.server.permission.ws.template.DefaultTemplatesResolverImpl) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Arrays.asList(java.util.Arrays.asList) DefaultTemplatesResolver(org.sonar.server.permission.ws.template.DefaultTemplatesResolver) Nullable(javax.annotation.Nullable) PermissionIndexer(org.sonar.server.permission.index.PermissionIndexer) Iterator(java.util.Iterator) Collection(java.util.Collection) DefaultTemplates(org.sonar.db.organization.DefaultTemplates) Set(java.util.Set) Collectors(org.sonar.core.util.stream.Collectors) String.format(java.lang.String.format) DbClient(org.sonar.db.DbClient) List(java.util.List) ComponentDto(org.sonar.db.component.ComponentDto) Qualifiers(org.sonar.api.resources.Qualifiers) PermissionTemplateCharacteristicDto(org.sonar.db.permission.template.PermissionTemplateCharacteristicDto) DefaultGroups.isAnyone(org.sonar.api.security.DefaultGroups.isAnyone) ServerSide(org.sonar.api.server.ServerSide) UserSession(org.sonar.server.user.UserSession) CheckForNull(javax.annotation.CheckForNull) UserPermissionDto(org.sonar.db.permission.UserPermissionDto) ComponentKeys(org.sonar.core.component.ComponentKeys) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) PermissionTemplateCharacteristicDto(org.sonar.db.permission.template.PermissionTemplateCharacteristicDto) UserPermissionDto(org.sonar.db.permission.UserPermissionDto) PermissionTemplateGroupDto(org.sonar.db.permission.template.PermissionTemplateGroupDto) GroupPermissionDto(org.sonar.db.permission.GroupPermissionDto)

Example 4 with PermissionTemplateDto

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();
}
Also used : DbSession(org.sonar.db.DbSession) PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) GroupIdOrAnyone(org.sonar.server.usergroups.ws.GroupIdOrAnyone)

Example 5 with PermissionTemplateDto

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);
    }
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) PermissionTemplate(org.sonarqube.ws.WsPermissions.PermissionTemplate) Permission(org.sonarqube.ws.WsPermissions.Permission)

Aggregations

PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)97 Test (org.junit.Test)63 BasePermissionWsTest (org.sonar.server.permission.ws.BasePermissionWsTest)42 OrganizationDto (org.sonar.db.organization.OrganizationDto)33 GroupDto (org.sonar.db.user.GroupDto)17 DbSession (org.sonar.db.DbSession)16 UserDto (org.sonar.db.user.UserDto)15 PermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateGroupDto)14 InputStream (java.io.InputStream)13 PermissionTemplateTesting.newPermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto)12 DefaultTemplates (org.sonar.db.organization.DefaultTemplates)10 PermissionTemplateUserDto (org.sonar.db.permission.template.PermissionTemplateUserDto)9 WsPermissions (org.sonarqube.ws.WsPermissions)9 PermissionTemplateTesting.newPermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateGroupDto)8 GroupTesting.newGroupDto (org.sonar.db.user.GroupTesting.newGroupDto)8 PermissionTemplateTesting.newPermissionTemplateUserDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateUserDto)7 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)7 WsGroupsResponse (org.sonarqube.ws.WsPermissions.WsGroupsResponse)7 ComponentDto (org.sonar.db.component.ComponentDto)6 Date (java.util.Date)5