Search in sources :

Example 56 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class BackendCleanup method resetData.

/**
   * Reset data in order to to be in same state as a fresh installation (but without having to drop db and restart the server).
   *
   * Please be careful when updating this method as it's called by Orchestrator.
   */
public void resetData() {
    try (DbSession dbSession = dbClient.openSession(false);
        Connection connection = dbSession.getConnection()) {
        truncateAnalysisTables(connection);
        deleteManualRules(connection);
        truncateInternalProperties(null, null, connection);
        truncateUsers(null, null, connection);
        truncateOrganizations(null, null, connection);
    } catch (SQLException e) {
        throw new IllegalStateException("Fail to reset data", e);
    }
    clearIndex(IssueIndexDefinition.INDEX_TYPE_ISSUE.getIndex());
    clearIndex(ViewIndexDefinition.INDEX_TYPE_VIEW.getIndex());
    clearIndex(ProjectMeasuresIndexDefinition.INDEX_TYPE_PROJECT_MEASURES.getIndex());
    clearIndex(ComponentIndexDefinition.INDEX_TYPE_COMPONENT.getIndex());
}
Also used : DbSession(org.sonar.db.DbSession) SQLException(java.sql.SQLException) Connection(java.sql.Connection)

Example 57 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class RemoveUserAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        UserId user = support.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
        Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
        OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION));
        checkProjectAdmin(userSession, org.getUuid(), projectId);
        PermissionChange change = new UserPermissionChange(PermissionChange.Operation.REMOVE, org.getUuid(), request.mandatoryParam(PARAM_PERMISSION), projectId.orElse(null), user);
        permissionUpdater.apply(dbSession, asList(change));
        response.noContent();
    }
}
Also used : DbSession(org.sonar.db.DbSession) UserId(org.sonar.server.permission.UserId) PermissionChange(org.sonar.server.permission.PermissionChange) UserPermissionChange(org.sonar.server.permission.UserPermissionChange) ProjectId(org.sonar.server.permission.ProjectId) UserPermissionChange(org.sonar.server.permission.UserPermissionChange) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 58 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class UsersAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        OrganizationDto org = support.findOrganization(dbSession, request.param(PARAM_ORGANIZATION));
        Optional<ProjectId> projectId = support.findProjectId(dbSession, request);
        checkProjectAdmin(userSession, org.getUuid(), projectId);
        PermissionQuery query = buildPermissionQuery(request, projectId);
        List<UserDto> users = findUsers(dbSession, org, query);
        int total = dbClient.userPermissionDao().countUsers(dbSession, org.getUuid(), query);
        List<UserPermissionDto> userPermissions = findUserPermissions(dbSession, org, users, projectId);
        Paging paging = Paging.forPageIndex(request.mandatoryParamAsInt(Param.PAGE)).withPageSize(query.getPageSize()).andTotal(total);
        UsersWsResponse usersWsResponse = buildResponse(users, userPermissions, paging);
        writeProtobuf(usersWsResponse, request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) UserDto(org.sonar.db.user.UserDto) Paging(org.sonar.api.utils.Paging) ProjectId(org.sonar.server.permission.ProjectId) UserPermissionDto(org.sonar.db.permission.UserPermissionDto) OrganizationDto(org.sonar.db.organization.OrganizationDto) PermissionQuery(org.sonar.db.permission.PermissionQuery) UsersWsResponse(org.sonarqube.ws.WsPermissions.UsersWsResponse)

Example 59 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class AddProjectCreatorToTemplateAction method doHandle.

private void doHandle(AddProjectCreatorToTemplateWsRequest request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
        checkGlobalAdmin(userSession, template.getOrganizationUuid());
        Optional<PermissionTemplateCharacteristicDto> templatePermission = dbClient.permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(dbSession, request.getPermission(), template.getId());
        if (templatePermission.isPresent()) {
            updateTemplatePermission(dbSession, templatePermission.get());
        } else {
            addTemplatePermission(dbSession, request, template);
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) PermissionTemplateCharacteristicDto(org.sonar.db.permission.template.PermissionTemplateCharacteristicDto)

Example 60 with DbSession

use of org.sonar.db.DbSession in project sonarqube by SonarSource.

the class ApplyTemplateAction method doHandle.

private void doHandle(ApplyTemplateWsRequest request) {
    try (DbSession dbSession = dbClient.openSession(false)) {
        PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
        ComponentDto project = wsSupport.getRootComponentOrModule(dbSession, newWsProjectRef(request.getProjectId(), request.getProjectKey()));
        checkGlobalAdmin(userSession, template.getOrganizationUuid());
        permissionTemplateService.apply(dbSession, template, Collections.singletonList(project));
    }
}
Also used : DbSession(org.sonar.db.DbSession) PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) ComponentDto(org.sonar.db.component.ComponentDto)

Aggregations

DbSession (org.sonar.db.DbSession)254 ComponentDto (org.sonar.db.component.ComponentDto)63 OrganizationDto (org.sonar.db.organization.OrganizationDto)30 JsonWriter (org.sonar.api.utils.text.JsonWriter)24 UserDto (org.sonar.db.user.UserDto)20 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)16 Test (org.junit.Test)13 MetricDto (org.sonar.db.metric.MetricDto)13 Paging (org.sonar.api.utils.Paging)12 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)10 CeQueueDto (org.sonar.db.ce.CeQueueDto)8 DepthTraversalTypeAwareCrawler (org.sonar.server.computation.task.projectanalysis.component.DepthTraversalTypeAwareCrawler)8 SearchOptions (org.sonar.server.es.SearchOptions)8 NotFoundException (org.sonar.server.exceptions.NotFoundException)8 List (java.util.List)7 SnapshotDto (org.sonar.db.component.SnapshotDto)7 GroupDto (org.sonar.db.user.GroupDto)7 DbClient (org.sonar.db.DbClient)6 ProjectId (org.sonar.server.permission.ProjectId)6 RuleKey (org.sonar.api.rule.RuleKey)5