use of org.sonar.db.permission.template.PermissionTemplateCharacteristicDto 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);
});
}
}
use of org.sonar.db.permission.template.PermissionTemplateCharacteristicDto in project sonarqube by SonarSource.
the class AddProjectCreatorToTemplateAction method doHandle.
private void doHandle(AddProjectCreatorToTemplateWsRequest request) {
try (DbSession dbSession = dbClient.openSession(false)) {
PermissionTemplateDto template = wsSupport.findTemplate(dbSession, WsTemplateRef.newTemplateRef(request.getTemplateId(), request.getOrganization(), request.getTemplateName()));
checkGlobalAdmin(userSession, template.getOrganizationUuid());
Optional<PermissionTemplateCharacteristicDto> templatePermission = dbClient.permissionTemplateCharacteristicDao().selectByPermissionAndTemplateId(dbSession, request.getPermission(), template.getId());
if (templatePermission.isPresent()) {
updateTemplatePermission(dbSession, templatePermission.get());
} else {
addTemplatePermission(dbSession, request, template);
}
}
}
use of org.sonar.db.permission.template.PermissionTemplateCharacteristicDto in project sonarqube by SonarSource.
the class AddProjectCreatorToTemplateAction method updateTemplatePermission.
private void updateTemplatePermission(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermission) {
PermissionTemplateCharacteristicDto targetTemplatePermission = templatePermission.setUpdatedAt(system.now()).setWithProjectCreator(true);
dbClient.permissionTemplateCharacteristicDao().update(dbSession, targetTemplatePermission);
dbSession.commit();
}
use of org.sonar.db.permission.template.PermissionTemplateCharacteristicDto 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);
});
}
}
use of org.sonar.db.permission.template.PermissionTemplateCharacteristicDto in project sonarqube by SonarSource.
the class RemoveProjectCreatorFromTemplateAction method updateTemplateCharacteristic.
private void updateTemplateCharacteristic(DbSession dbSession, PermissionTemplateCharacteristicDto templatePermission, String templateName) {
PermissionTemplateCharacteristicDto targetTemplatePermission = templatePermission.setUpdatedAt(system.now()).setWithProjectCreator(false);
dbClient.permissionTemplateCharacteristicDao().update(dbSession, targetTemplatePermission, templateName);
dbSession.commit();
}
Aggregations