Search in sources :

Example 1 with UserId

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

the class PermissionTemplateService method copyPermissions.

private void copyPermissions(DbSession dbSession, PermissionTemplateDto template, ComponentDto project, @Nullable String projectCreatorUserUuid) {
    List<PermissionTemplateUserDto> usersPermissions = dbClient.permissionTemplateDao().selectUserPermissionsByTemplateId(dbSession, template.getUuid());
    Set<String> permissionTemplateUserUuids = usersPermissions.stream().map(PermissionTemplateUserDto::getUserUuid).collect(Collectors.toSet());
    Map<String, UserId> userIdByUuid = dbClient.userDao().selectByUuids(dbSession, permissionTemplateUserUuids).stream().collect(Collectors.toMap(UserDto::getUuid, u -> u));
    usersPermissions.stream().filter(up -> permissionValidForProject(project, up.getPermission())).forEach(up -> {
        UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), up.getPermission(), up.getUserUuid(), project.uuid());
        dbClient.userPermissionDao().insert(dbSession, dto, project, userIdByUuid.get(up.getUserUuid()), template);
    });
    List<PermissionTemplateGroupDto> groupsPermissions = dbClient.permissionTemplateDao().selectGroupPermissionsByTemplateUuid(dbSession, template.getUuid());
    groupsPermissions.stream().filter(gp -> groupNameValidForProject(project, gp.getGroupName())).filter(gp -> permissionValidForProject(project, gp.getPermission())).forEach(gp -> {
        String groupUuid = isAnyone(gp.getGroupName()) ? null : gp.getGroupUuid();
        String groupName = groupUuid == null ? null : dbClient.groupDao().selectByUuid(dbSession, groupUuid).getName();
        GroupPermissionDto dto = new GroupPermissionDto().setUuid(uuidFactory.create()).setGroupUuid(groupUuid).setGroupName(groupName).setRole(gp.getPermission()).setComponentUuid(project.uuid()).setComponentName(project.name());
        dbClient.groupPermissionDao().insert(dbSession, dto, project, template);
    });
    List<PermissionTemplateCharacteristicDto> characteristics = dbClient.permissionTemplateCharacteristicDao().selectByTemplateUuids(dbSession, singletonList(template.getUuid()));
    if (projectCreatorUserUuid != null) {
        Set<String> permissionsForCurrentUserAlreadyInDb = usersPermissions.stream().filter(userPermission -> projectCreatorUserUuid.equals(userPermission.getUserUuid())).map(PermissionTemplateUserDto::getPermission).collect(java.util.stream.Collectors.toSet());
        UserDto userDto = dbClient.userDao().selectByUuid(dbSession, projectCreatorUserUuid);
        characteristics.stream().filter(PermissionTemplateCharacteristicDto::getWithProjectCreator).filter(up -> permissionValidForProject(project, up.getPermission())).filter(characteristic -> !permissionsForCurrentUserAlreadyInDb.contains(characteristic.getPermission())).forEach(c -> {
            UserPermissionDto dto = new UserPermissionDto(uuidFactory.create(), c.getPermission(), userDto.getUuid(), project.uuid());
            dbClient.userPermissionDao().insert(dbSession, dto, project, userDto, template);
        });
    }
}
Also used : PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) ProjectIndexers(org.sonar.server.es.ProjectIndexers) StringUtils(org.apache.commons.lang.StringUtils) UserDto(org.sonar.db.user.UserDto) 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) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) SCAN(org.sonar.db.permission.GlobalPermission.SCAN) Map(java.util.Map) ProjectIndexer(org.sonar.server.es.ProjectIndexer) PUBLIC_PERMISSIONS(org.sonar.api.web.UserRole.PUBLIC_PERMISSIONS) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) Collection(java.util.Collection) UuidFactory(org.sonar.core.util.UuidFactory) Set(java.util.Set) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) TemplateMatchingKeyException(org.sonar.server.exceptions.TemplateMatchingKeyException) UserId(org.sonar.db.user.UserId) 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) ResolvedDefaultTemplates(org.sonar.server.permission.DefaultTemplatesResolver.ResolvedDefaultTemplates) UserSession(org.sonar.server.user.UserSession) CheckForNull(javax.annotation.CheckForNull) UserPermissionDto(org.sonar.db.permission.UserPermissionDto) UserDto(org.sonar.db.user.UserDto) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) UserPermissionDto(org.sonar.db.permission.UserPermissionDto) PermissionTemplateGroupDto(org.sonar.db.permission.template.PermissionTemplateGroupDto) PermissionTemplateUserDto(org.sonar.db.permission.template.PermissionTemplateUserDto) UserId(org.sonar.db.user.UserId) PermissionTemplateCharacteristicDto(org.sonar.db.permission.template.PermissionTemplateCharacteristicDto) GroupPermissionDto(org.sonar.db.permission.GroupPermissionDto)

Example 2 with UserId

use of org.sonar.db.user.UserId 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 = wsSupport.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
        String permission = request.mandatoryParam(PARAM_PERMISSION);
        if (ADMINISTER.getKey().equals(permission) && user.getLogin().equals(userSession.getLogin())) {
            throw BadRequestException.create("As an admin, you can't remove your own admin right");
        }
        Optional<ComponentDto> project = wsSupport.findProject(dbSession, request);
        wsSupport.checkPermissionManagementAccess(userSession, project.orElse(null));
        PermissionChange change = new UserPermissionChange(PermissionChange.Operation.REMOVE, permission, project.orElse(null), user, permissionService);
        permissionUpdater.apply(dbSession, singletonList(change));
        response.noContent();
    }
}
Also used : DbSession(org.sonar.db.DbSession) UserId(org.sonar.db.user.UserId) PermissionChange(org.sonar.server.permission.PermissionChange) UserPermissionChange(org.sonar.server.permission.UserPermissionChange) ComponentDto(org.sonar.db.component.ComponentDto) UserPermissionChange(org.sonar.server.permission.UserPermissionChange)

Example 3 with UserId

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

the class RemoveUserFromTemplateAction method doHandle.

private void doHandle(RemoveUserFromTemplateRequest request) {
    String permission = request.getPermission();
    String userLogin = request.getLogin();
    try (DbSession dbSession = dbClient.openSession(false)) {
        requestValidator.validateProjectPermission(permission);
        PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getTemplateName()));
        checkGlobalAdmin(userSession);
        UserId user = wsSupport.findUser(dbSession, userLogin);
        dbClient.permissionTemplateDao().deleteUserPermission(dbSession, template.getUuid(), user.getUuid(), permission, template.getName(), user.getLogin());
        dbSession.commit();
    }
}
Also used : DbSession(org.sonar.db.DbSession) PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) UserId(org.sonar.db.user.UserId)

Example 4 with UserId

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

the class AddUserAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        UserId user = wsSupport.findUser(dbSession, request.mandatoryParam(PARAM_USER_LOGIN));
        Optional<ComponentDto> project = wsSupport.findProject(dbSession, request);
        checkProjectAdmin(userSession, configuration, project.orElse(null));
        PermissionChange change = new UserPermissionChange(PermissionChange.Operation.ADD, request.mandatoryParam(PARAM_PERMISSION), project.orElse(null), user, permissionService);
        permissionUpdater.apply(dbSession, singletonList(change));
    }
    response.noContent();
}
Also used : DbSession(org.sonar.db.DbSession) UserId(org.sonar.db.user.UserId) PermissionChange(org.sonar.server.permission.PermissionChange) UserPermissionChange(org.sonar.server.permission.UserPermissionChange) ComponentDto(org.sonar.db.component.ComponentDto) UserPermissionChange(org.sonar.server.permission.UserPermissionChange)

Example 5 with UserId

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

the class AddUserToTemplateAction method doHandle.

private void doHandle(AddUserToTemplateRequest request) {
    String permission = request.getPermission();
    String userLogin = request.getLogin();
    try (DbSession dbSession = dbClient.openSession(false)) {
        PermissionTemplateDto template = wsSupport.findTemplate(dbSession, newTemplateRef(request.getTemplateId(), request.getTemplateName()));
        checkGlobalAdmin(userSession);
        UserId user = wsSupport.findUser(dbSession, userLogin);
        if (!isUserAlreadyAdded(dbSession, template.getUuid(), userLogin, permission)) {
            dbClient.permissionTemplateDao().insertUserPermission(dbSession, template.getUuid(), user.getUuid(), permission, template.getName(), user.getLogin());
            dbSession.commit();
        }
    }
}
Also used : DbSession(org.sonar.db.DbSession) PermissionTemplateDto(org.sonar.db.permission.template.PermissionTemplateDto) UserId(org.sonar.db.user.UserId)

Aggregations

DbSession (org.sonar.db.DbSession)5 UserId (org.sonar.db.user.UserId)5 ComponentDto (org.sonar.db.component.ComponentDto)3 PermissionTemplateDto (org.sonar.db.permission.template.PermissionTemplateDto)3 PermissionChange (org.sonar.server.permission.PermissionChange)2 UserPermissionChange (org.sonar.server.permission.UserPermissionChange)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 Collection (java.util.Collection)1 Collections.singletonList (java.util.Collections.singletonList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Collectors (java.util.stream.Collectors)1 CheckForNull (javax.annotation.CheckForNull)1 Nullable (javax.annotation.Nullable)1 StringUtils (org.apache.commons.lang.StringUtils)1