Search in sources :

Example 91 with DbSession

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

the class UserUpdater method addDefaultGroup.

private void addDefaultGroup(DbSession dbSession, UserDto userDto) {
    String defaultGroupName = settings.getString(CoreProperties.CORE_DEFAULT_GROUP);
    if (defaultGroupName == null) {
        return;
    }
    String defOrgUuid = defaultOrganizationProvider.get().getUuid();
    List<GroupDto> userGroups = dbClient.groupDao().selectByUserLogin(dbSession, userDto.getLogin());
    if (!userGroups.stream().anyMatch(g -> defOrgUuid.equals(g.getOrganizationUuid()) && g.getName().equals(defaultGroupName))) {
        Optional<GroupDto> groupDto = dbClient.groupDao().selectByName(dbSession, defOrgUuid, defaultGroupName);
        if (!groupDto.isPresent()) {
            throw new ServerException(HttpURLConnection.HTTP_INTERNAL_ERROR, format("The default group '%s' for new users does not exist. Please update the general security settings to fix this issue.", defaultGroupName));
        }
        dbClient.userGroupDao().insert(dbSession, new UserGroupDto().setUserId(userDto.getId()).setGroupId(groupDto.get().getId()));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) UserGroupDto(org.sonar.db.user.UserGroupDto) UserDto(org.sonar.db.user.UserDto) Strings.isNullOrEmpty(com.google.common.base.Strings.isNullOrEmpty) Random(java.util.Random) ServerException(org.sonar.server.exceptions.ServerException) DbSession(org.sonar.db.DbSession) SecureRandom(java.security.SecureRandom) Strings(com.google.common.base.Strings) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) OrganizationCreation(org.sonar.server.organization.OrganizationCreation) WsUtils.checkFound(org.sonar.server.ws.WsUtils.checkFound) WsUtils.checkRequest(org.sonar.server.ws.WsUtils.checkRequest) Validation(org.sonar.server.util.Validation) NewUserHandler(org.sonar.api.platform.NewUserHandler) Nullable(javax.annotation.Nullable) GroupDto(org.sonar.db.user.GroupDto) DefaultOrganizationProvider(org.sonar.server.organization.DefaultOrganizationProvider) System2(org.sonar.api.utils.System2) CoreProperties(org.sonar.api.CoreProperties) Collectors(org.sonar.core.util.stream.Collectors) String.format(java.lang.String.format) Objects(java.util.Objects) UserIndexer(org.sonar.server.user.index.UserIndexer) DbClient(org.sonar.db.DbClient) List(java.util.List) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) UserDto.encryptPassword(org.sonar.db.user.UserDto.encryptPassword) Settings(org.sonar.api.config.Settings) ServerSide(org.sonar.api.server.ServerSide) Optional(java.util.Optional) DigestUtils(org.apache.commons.codec.digest.DigestUtils) Collections(java.util.Collections) Joiner(com.google.common.base.Joiner) ServerException(org.sonar.server.exceptions.ServerException) UserGroupDto(org.sonar.db.user.UserGroupDto) GroupDto(org.sonar.db.user.GroupDto) UserGroupDto(org.sonar.db.user.UserGroupDto)

Example 92 with DbSession

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

the class UserIndexer method doIndex.

private void doIndex(@Nullable String login, Size bulkSize) {
    final BulkIndexer bulk = new BulkIndexer(esClient, UserIndexDefinition.INDEX_TYPE_USER.getIndex());
    bulk.setSize(bulkSize);
    try (DbSession dbSession = dbClient.openSession(false)) {
        try (UserResultSetIterator rowIt = UserResultSetIterator.create(dbClient, dbSession, login)) {
            doIndex(bulk, rowIt);
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) BulkIndexer(org.sonar.server.es.BulkIndexer)

Example 93 with DbSession

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

the class ChangePasswordAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkLoggedIn();
    try (DbSession dbSession = dbClient.openSession(false)) {
        String login = request.mandatoryParam(PARAM_LOGIN);
        if (login.equals(userSession.getLogin())) {
            String previousPassword = request.mandatoryParam(PARAM_PREVIOUS_PASSWORD);
            checkCurrentPassword(dbSession, login, previousPassword);
        } else {
            userSession.checkIsSystemAdministrator();
        }
        String password = request.mandatoryParam(PARAM_PASSWORD);
        UpdateUser updateUser = UpdateUser.create(login).setPassword(password);
        userUpdater.update(dbSession, updateUser);
    }
    response.noContent();
}
Also used : DbSession(org.sonar.db.DbSession) UpdateUser(org.sonar.server.user.UpdateUser)

Example 94 with DbSession

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

the class CurrentAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        Optional<UserDto> user = Optional.empty();
        Collection<String> groups = emptyList();
        if (userSession.isLoggedIn()) {
            user = selectCurrentUser(dbSession);
            groups = selectGroups(dbSession);
        }
        writeResponse(response, user, groups);
    }
}
Also used : DbSession(org.sonar.db.DbSession) UserDto(org.sonar.db.user.UserDto)

Example 95 with DbSession

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

the class ShowAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    userSession.checkIsSystemAdministrator();
    try (DbSession dbSession = dbClient.openSession(true)) {
        Map<String, PropertyDto> properties = dbClient.propertiesDao().selectGlobalPropertiesByKeys(dbSession, SETTINGS_KEYS).stream().collect(Collectors.uniqueIndex(PropertyDto::getKey, Function.identity()));
        writeProtobuf(doHandle(properties), request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) PropertyDto(org.sonar.db.property.PropertyDto)

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