Search in sources :

Example 56 with GroupDto

use of org.sonar.db.user.GroupDto in project sonarqube by SonarSource.

the class SearchAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    int page = request.mandatoryParamAsInt(Param.PAGE);
    int pageSize = request.mandatoryParamAsInt(Param.PAGE_SIZE);
    SearchOptions options = new SearchOptions().setPage(page, pageSize);
    String query = defaultIfBlank(request.param(Param.TEXT_QUERY), "");
    Set<String> fields = neededFields(request);
    try (DbSession dbSession = dbClient.openSession(false)) {
        OrganizationDto organization = groupWsSupport.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY));
        userSession.checkLoggedIn().checkPermission(ADMINISTER, organization);
        int limit = dbClient.groupDao().countByQuery(dbSession, organization.getUuid(), query);
        List<GroupDto> groups = dbClient.groupDao().selectByQuery(dbSession, organization.getUuid(), query, options.getOffset(), pageSize);
        List<Integer> groupIds = groups.stream().map(GroupDto::getId).collect(Collectors.toList(groups.size()));
        Map<String, Integer> userCountByGroup = dbClient.groupMembershipDao().countUsersByGroups(dbSession, groupIds);
        JsonWriter json = response.newJsonWriter().beginObject();
        options.writeJson(json, limit);
        writeGroups(json, groups, userCountByGroup, fields);
        json.endObject().close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) GroupDto(org.sonar.db.user.GroupDto) SearchOptions(org.sonar.server.es.SearchOptions) JsonWriter(org.sonar.api.utils.text.JsonWriter) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 57 with GroupDto

use of org.sonar.db.user.GroupDto in project sonarqube by SonarSource.

the class UpdateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        int groupId = request.mandatoryParamAsInt(PARAM_GROUP_ID);
        GroupDto group = dbClient.groupDao().selectById(dbSession, groupId);
        checkFound(group, "Could not find a user group with id '%s'.", groupId);
        Optional<OrganizationDto> org = dbClient.organizationDao().selectByUuid(dbSession, group.getOrganizationUuid());
        checkFoundWithOptional(org, "Could not find organization with id '%s'.", group.getOrganizationUuid());
        userSession.checkPermission(ADMINISTER, org.get());
        boolean changed = false;
        String newName = request.param(PARAM_GROUP_NAME);
        if (newName != null) {
            changed = true;
            UserGroupValidation.validateGroupName(newName);
            support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), newName);
            String oldName = group.getName();
            group.setName(newName);
            updateDefaultGroupIfNeeded(dbSession, org.get(), oldName, newName);
        }
        String description = request.param(PARAM_GROUP_DESCRIPTION);
        if (description != null) {
            changed = true;
            group.setDescription(support.validateDescription(description));
        }
        if (changed) {
            dbClient.groupDao().update(dbSession, group);
            dbSession.commit();
        }
        writeResponse(dbSession, request, response, org.get(), group);
    }
}
Also used : DbSession(org.sonar.db.DbSession) GroupDto(org.sonar.db.user.GroupDto) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 58 with GroupDto

use of org.sonar.db.user.GroupDto in project sonarqube by SonarSource.

the class CreateAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        OrganizationDto organization = support.findOrganizationByKey(dbSession, request.param(PARAM_ORGANIZATION_KEY));
        userSession.checkPermission(ADMINISTER, organization);
        GroupDto group = new GroupDto().setOrganizationUuid(organization.getUuid()).setName(request.mandatoryParam(PARAM_GROUP_NAME)).setDescription(request.param(PARAM_GROUP_DESCRIPTION));
        // validations
        UserGroupValidation.validateGroupName(group.getName());
        support.validateDescription(group.getDescription());
        support.checkNameDoesNotExist(dbSession, group.getOrganizationUuid(), group.getName());
        dbClient.groupDao().insert(dbSession, group);
        dbSession.commit();
        writeResponse(request, response, organization, group);
    }
}
Also used : DbSession(org.sonar.db.DbSession) GroupDto(org.sonar.db.user.GroupDto) OrganizationDto(org.sonar.db.organization.OrganizationDto)

Example 59 with GroupDto

use of org.sonar.db.user.GroupDto in project sonarqube by SonarSource.

the class UserIdentityAuthenticatorTest method authenticate_new_user_with_groups.

@Test
public void authenticate_new_user_with_groups() throws Exception {
    GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group1");
    GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group2");
    authenticate(USER_LOGIN, "group1", "group2", "group3");
    Optional<UserDto> user = db.users().selectUserByLogin(USER_LOGIN);
    assertThat(user).isPresent();
    assertThat(user.get().isRoot()).isFalse();
    assertThat(db.users().selectGroupIdsOfUser(user.get())).containsOnly(group1.getId(), group2.getId());
}
Also used : UserDto(org.sonar.db.user.UserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) GroupDto(org.sonar.db.user.GroupDto) Test(org.junit.Test)

Example 60 with GroupDto

use of org.sonar.db.user.GroupDto in project sonarqube by SonarSource.

the class UserIdentityAuthenticatorTest method authenticate_existing_user_and_remove_groups.

@Test
public void authenticate_existing_user_and_remove_groups() throws Exception {
    UserDto user = db.users().insertUser(newUserDto().setLogin(USER_LOGIN).setActive(true).setName("John"));
    GroupDto group1 = db.users().insertGroup(db.getDefaultOrganization(), "group1");
    GroupDto group2 = db.users().insertGroup(db.getDefaultOrganization(), "group2");
    db.users().insertMember(group1, user);
    db.users().insertMember(group2, user);
    authenticate(USER_LOGIN, "group1");
    assertThat(db.users().selectGroupIdsOfUser(user)).containsOnly(group1.getId());
}
Also used : UserDto(org.sonar.db.user.UserDto) UserTesting.newUserDto(org.sonar.db.user.UserTesting.newUserDto) GroupDto(org.sonar.db.user.GroupDto) Test(org.junit.Test)

Aggregations

GroupDto (org.sonar.db.user.GroupDto)177 Test (org.junit.Test)158 OrganizationDto (org.sonar.db.organization.OrganizationDto)74 UserDto (org.sonar.db.user.UserDto)67 ComponentDto (org.sonar.db.component.ComponentDto)38 GroupTesting.newGroupDto (org.sonar.db.user.GroupTesting.newGroupDto)31 UserTesting.newUserDto (org.sonar.db.user.UserTesting.newUserDto)18 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)17 PermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateGroupDto)14 UserGroupDto (org.sonar.db.user.UserGroupDto)12 DbSession (org.sonar.db.DbSession)11 BasePermissionWsTest (org.sonar.server.permission.ws.BasePermissionWsTest)9 PermissionTemplateTesting.newPermissionTemplateGroupDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateGroupDto)8 WsGroupsResponse (org.sonarqube.ws.WsPermissions.WsGroupsResponse)7 InputStream (java.io.InputStream)6 ArrayList (java.util.ArrayList)3 System2 (org.sonar.api.utils.System2)3 DbClient (org.sonar.db.DbClient)3 PermissionTemplateTesting.newPermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateTesting.newPermissionTemplateDto)3 Strings (com.google.common.base.Strings)2