Search in sources :

Example 1 with PermissionTemplateUserDto

use of org.sonar.db.permission.template.PermissionTemplateUserDto 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 PermissionTemplateUserDto

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

the class TemplateUsersAction method handle.

@Override
public void handle(Request wsRequest, Response wsResponse) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        WsTemplateRef templateRef = WsTemplateRef.fromRequest(wsRequest);
        PermissionTemplateDto template = support.findTemplate(dbSession, templateRef);
        checkGlobalAdmin(userSession, template.getOrganizationUuid());
        PermissionQuery query = buildQuery(wsRequest, template);
        int total = dbClient.permissionTemplateDao().countUserLoginsByQueryAndTemplate(dbSession, query, template.getId());
        Paging paging = Paging.forPageIndex(wsRequest.mandatoryParamAsInt(PAGE)).withPageSize(wsRequest.mandatoryParamAsInt(PAGE_SIZE)).andTotal(total);
        List<UserDto> users = findUsers(dbSession, query, template);
        List<PermissionTemplateUserDto> permissionTemplateUsers = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateIdAndUserLogins(dbSession, template.getId(), users.stream().map(UserDto::getLogin).collect(Collectors.toList()));
        WsPermissions.UsersWsResponse templateUsersResponse = buildResponse(users, permissionTemplateUsers, paging);
        writeProtobuf(templateUsersResponse, wsRequest, wsResponse);
    }
}
Also used : DbSession(org.sonar.db.DbSession) PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) UsersWsResponse(org.sonarqube.ws.WsPermissions.UsersWsResponse) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) Paging(org.sonar.api.utils.Paging) UserDto(org.sonar.db.user.UserDto) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) WsPermissions(org.sonarqube.ws.WsPermissions) PermissionQuery(org.sonar.db.permission.PermissionQuery)

Aggregations

DbSession (org.sonar.db.DbSession)2 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)2 PermissionTemplateUserDto (org.sonar.db.permission.template.PermissionTemplateUserDto)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 Qualifiers (org.sonar.api.resources.Qualifiers)1 DefaultGroups.isAnyone (org.sonar.api.security.DefaultGroups.isAnyone)1 ServerSide (org.sonar.api.server.ServerSide)1 Paging (org.sonar.api.utils.Paging)1