Search in sources :

Example 1 with UserPermissionDto

use of org.sonar.db.permission.UserPermissionDto 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 2 with UserPermissionDto

use of org.sonar.db.permission.UserPermissionDto in project sonarqube by SonarSource.

the class UserPermissionChanger method addPermission.

private boolean addPermission(DbSession dbSession, UserPermissionChange change) {
    if (loadExistingPermissions(dbSession, change).contains(change.getPermission())) {
        return false;
    }
    UserPermissionDto dto = new UserPermissionDto(change.getOrganizationUuid(), change.getPermission(), change.getUserId().getId(), change.getNullableProjectId());
    dbClient.userPermissionDao().insert(dbSession, dto);
    return true;
}
Also used : UserPermissionDto(org.sonar.db.permission.UserPermissionDto)

Example 3 with UserPermissionDto

use of org.sonar.db.permission.UserPermissionDto 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 4 with UserPermissionDto

use of org.sonar.db.permission.UserPermissionDto in project sonarqube by SonarSource.

the class SearchActionTest method return_only_users_favorite.

@Test
public void return_only_users_favorite() {
    OrganizationDto organizationDto = db.organizations().insert();
    addComponent(newProjectDto(organizationDto).setKey("K1"));
    ComponentDto otherUserFavorite = newProjectDto(organizationDto).setKey("K42");
    db.components().insertComponent(otherUserFavorite);
    db.favorites().add(otherUserFavorite, 42);
    dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(organizationDto.getUuid(), UserRole.USER, 42, otherUserFavorite.getId()));
    db.commit();
    SearchResponse result = call();
    assertThat(result.getFavoritesList()).extracting(Favorite::getKey).containsExactly("K1");
}
Also used : ComponentDto(org.sonar.db.component.ComponentDto) UserPermissionDto(org.sonar.db.permission.UserPermissionDto) OrganizationDto(org.sonar.db.organization.OrganizationDto) SearchResponse(org.sonarqube.ws.Favorites.SearchResponse) Test(org.junit.Test)

Example 5 with UserPermissionDto

use of org.sonar.db.permission.UserPermissionDto in project sonarqube by SonarSource.

the class SearchActionTest method addComponent.

private void addComponent(ComponentDto component) {
    db.components().insertComponent(component);
    db.favorites().add(component, USER_ID);
    dbClient.userPermissionDao().insert(dbSession, new UserPermissionDto(component.getOrganizationUuid(), UserRole.USER, USER_ID, component.getId()));
    db.commit();
}
Also used : UserPermissionDto(org.sonar.db.permission.UserPermissionDto)

Aggregations

UserPermissionDto (org.sonar.db.permission.UserPermissionDto)8 DbSession (org.sonar.db.DbSession)2 ComponentDto (org.sonar.db.component.ComponentDto)2 OrganizationDto (org.sonar.db.organization.OrganizationDto)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 String.format (java.lang.String.format)1 MessageFormat (java.text.MessageFormat)1 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Set (java.util.Set)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 StringUtils (org.apache.commons.lang.StringUtils)1 Test (org.junit.Test)1 Qualifiers (org.sonar.api.resources.Qualifiers)1 DefaultGroups.isAnyone (org.sonar.api.security.DefaultGroups.isAnyone)1