diff --git a/service/src/main/inse/dashboard/service.py b/service/src/main/inse/dashboard/service.py index 4f1fe83d..ec065b45 100644 --- a/service/src/main/inse/dashboard/service.py +++ b/service/src/main/inse/dashboard/service.py @@ -622,7 +622,7 @@ async def get_dashboard_dfg_projects( ) -> DashboardStatisticValue: normalized_id = _normalize_institution_id(institution_id) period = _dashboard_period(from_year, to_year) - projects = await projects_service.get_dfg_projects_by_author_id( + projects = await projects_service.get_dfg_projects( institutions_search_client=InstitutionsSearchClient(), raw_oa_institution_id=_raw_institution_id(normalized_id), author_id=None, @@ -871,7 +871,7 @@ async def _get_dfg_project_ids_from_projects_service( project_ids: set[str] = set() while True: - projects = await projects_service.get_dfg_projects_by_author_id( + projects = await projects_service.get_dfg_projects( institutions_search_client=InstitutionsSearchClient(), raw_oa_institution_id=_raw_institution_id(institution_id), author_id=None, diff --git a/service/src/main/inse/dashboard/test_service.py b/service/src/main/inse/dashboard/test_service.py index 630f8121..2de2ca6e 100644 --- a/service/src/main/inse/dashboard/test_service.py +++ b/service/src/main/inse/dashboard/test_service.py @@ -68,7 +68,7 @@ async def test_dfg_topic_project_ids_use_paginated_projects_service(): ] with patch( - "inse.dashboard.service.projects_service.get_dfg_projects_by_author_id", + "inse.dashboard.service.projects_service.get_dfg_projects", new=AsyncMock(side_effect=pages), ) as mocked_get_projects: project_ids = await _get_dfg_project_ids_from_projects_service( @@ -144,7 +144,7 @@ async def test_dashboard_statistics_counts_organization_data_and_all_projects(): ) ), ), patch( - "inse.dashboard.service.projects_service.get_dfg_projects_by_author_id", + "inse.dashboard.service.projects_service.get_dfg_projects", new=AsyncMock( return_value=DfgProjectPage(elements=[], total=3, page=0, size=1) ), @@ -254,7 +254,7 @@ async def test_dashboard_dfg_projects_uses_projects_service_total(): project_page = DfgProjectPage(elements=[], total=42, page=0, size=1) with patch( - "inse.dashboard.service.projects_service.get_dfg_projects_by_author_id", + "inse.dashboard.service.projects_service.get_dfg_projects", new=AsyncMock(return_value=project_page), ) as mocked_get_projects: result = await get_dashboard_dfg_projects( diff --git a/service/src/main/inse/projects/router.py b/service/src/main/inse/projects/router.py index 71e44778..6382f575 100644 --- a/service/src/main/inse/projects/router.py +++ b/service/src/main/inse/projects/router.py @@ -7,6 +7,9 @@ from inse.auth.service import CurrentUserAccessTokenPayloadDep from inse.institutions.institutions_search_client import ( InstitutionsSearchClient, ) +from inse.projects.logic import ( + is_projects_access_forbidden +) from inse.institutions.router import get_institutions_search_client from inse.models.bund_project_details import BundProjectDetails from inse.models.dfg_project_details import DfgProjectDetails @@ -30,6 +33,7 @@ from inse.projects.model import ( SortDirection, ) from inse.tags import Tags +from fastapi import HTTPException as HttpException logger = logging.getLogger(__name__) @@ -89,10 +93,8 @@ async def get_dfg_projects( str | None, Query(description="The openalex institution ID to filter by.") ] = None, author_id: Annotated[str | None, Query(description="The openalex author ID to filter by.")] = None, - author_filter_ids: Annotated[list[int] | None, Query(description="The dfg author IDs to filter by.")] = None, - institution_ids: Annotated[ - list[int] | None, Query(description="The institution IDs to filter by.") - ] = None, + author_filter_ids: Annotated[list[str] | None, Query(description="The dfg author IDs to filter by.")] = None, + institution_ids: Annotated[list[str] | None, Query(description="The institution IDs to filter by.")] = None, title: Annotated[ str | None, Query(description="The title to filter by.") ] = None, @@ -144,8 +146,13 @@ async def get_dfg_projects( sort_direction, fuzzy, ) - return await projects_service.get_dfg_projects_by_author_id( - access_token_metadata=access_token_metadata, + is_forbidden = await is_projects_access_forbidden(access_token_metadata, raw_oa_institution_id, author_id) + if is_forbidden: + raise HttpException( + status_code=403, + detail="Access denied for the provided author ID or institution ID", + ) + return await projects_service.get_dfg_projects( institutions_search_client=institutions_search_client, raw_oa_institution_id=raw_oa_institution_id, author_id=author_id, @@ -259,6 +266,12 @@ async def search_bund_projects( Query(description="Use OpenAlex topic-id resolution if true, otherwise raw keyword search."), ] = False, ) -> BundProjectPage: + is_forbidden = await is_projects_access_forbidden(access_token_metadata, raw_oa_institution_id, None) + if is_forbidden: + raise HttpException( + status_code=403, + detail="Access denied for the provided author ID or institution ID", + ) return await projects_service.search_bund_projects_with_filters( access_token_metadata=access_token_metadata, institutions_search_client=institutions_search_client, @@ -438,9 +451,13 @@ async def search_eu_cordis_projects_endpoint( sort_direction, fuzzy, ) - + is_forbidden = await is_projects_access_forbidden(access_token_metadata, raw_oa_institution_id, author_id) + if is_forbidden: + raise HttpException( + status_code=403, + detail="Access denied for the provided author ID or institution ID", + ) return await search_eu_cordis_projects( - access_token_metadata=access_token_metadata, institutions_search_client=institutions_search_client, raw_oa_institution_id=raw_oa_institution_id, author_id=author_id, diff --git a/service/src/main/inse/projects/service.py b/service/src/main/inse/projects/service.py index dc5b92ca..f1abccf8 100644 --- a/service/src/main/inse/projects/service.py +++ b/service/src/main/inse/projects/service.py @@ -27,9 +27,7 @@ from inse.projects.eu_cordis_projects_client import ( from inse.projects.eu_cordis_projects_client import ( search_eu_cordis_projects as _search_eu_cordis_projects_es, ) -from inse.projects.logic import ( - get_dfg_author_names_by_ids, is_projects_access_forbidden -) + from inse.projects.model import ( BundProjectPage, DfgProjectPage, @@ -42,6 +40,7 @@ from inse.projects.model import ( ProjectType, SortDirection, ) +from inse.projects.logic import get_dfg_author_names_by_ids from inse.works.logic import get_publication_dois_of_author from fastapi import HTTPException as HttpException logger = logging.getLogger(__name__) @@ -158,13 +157,25 @@ async def _get_dfg_project_ids_for_openalex_authors( return list(set.intersection(*project_sets)) -async def get_dfg_projects_by_author_id( - access_token_metadata: AccessTokenMetadata, +def _is_number_list(values: list[str] | None) -> bool: + if not values: + return True # nothing to validate, treat as valid/empty + for value in values: + if value is None or value == "": + return False + try: + float(value) + except (TypeError, ValueError): + return False + return True + + +async def get_dfg_projects( institutions_search_client: InstitutionsSearchClient, raw_oa_institution_id: str | None, author_id: str | None, - author_filter_ids: list[int] | None, - institution_ids: list[int] | None, + author_filter_ids: list[str] | None, + institution_ids: list[str] | None, keywords: list[str] | None, topic_ids: list[str] | None, filter_topic_ids: list[str] | None, @@ -198,14 +209,25 @@ async def get_dfg_projects_by_author_id( sort_direction, fuzzy, ) + + # Validate author_filter_ids / institution_ids. Return an empty page + if not _is_number_list(author_filter_ids) or not _is_number_list(institution_ids): + logger.debug( + "Invalid author_filter_ids=%s or institution_ids=%s, returning empty page", + author_filter_ids, + institution_ids, + ) + return DfgProjectPage(elements=[], page=page, size=size, total=0) + + author_filter_ids_int = ( + [int(float(v)) for v in author_filter_ids] if author_filter_ids else None + ) + institution_ids_int = ( + [int(float(v)) for v in institution_ids] if institution_ids else None + ) + filter_names = [] author_project_ids = [] - is_forbidden = await is_projects_access_forbidden(access_token_metadata, raw_oa_institution_id, author_id) - if is_forbidden: - raise HttpException( - status_code=403, - detail="Access denied for the provided author ID or institution ID", - ) if author_id: author_project_ids = await _get_dfg_project_ids_for_openalex_authors([author_id]) logger.info("Author project IDs for author_id:%s: %s", author_id, author_project_ids) @@ -216,16 +238,16 @@ async def get_dfg_projects_by_author_id( size=size, total=0) - if author_filter_ids: + if author_filter_ids_int: filter_names = ( - await get_dfg_author_names_by_ids(author_ids=author_filter_ids) - if author_filter_ids + await get_dfg_author_names_by_ids(author_ids=author_filter_ids_int) + if author_filter_ids_int else [] ) - if author_filter_ids and not filter_names: + if author_filter_ids_int and not filter_names: logger.debug( "No author names found for author_filter_ids=%s", - author_filter_ids, + author_filter_ids_int, ) institution_aliases = [] if raw_oa_institution_id: @@ -251,7 +273,7 @@ async def get_dfg_projects_by_author_id( institution_aliases=institution_aliases, project_ids=author_project_ids, names=filter_names or [], - institution_ids=institution_ids, + institution_ids=institution_ids_int, keywords=keywords, topic_ids=topic_ids, filter_topic_ids=filter_topic_ids, @@ -421,7 +443,6 @@ async def get_institution_bund_projects_funding_over_time( #Bund async def search_bund_projects_with_filters( - access_token_metadata: AccessTokenMetadata, institutions_search_client: InstitutionsSearchClient, executing_agency: str | None, raw_oa_institution_id: str | None, @@ -439,12 +460,6 @@ async def search_bund_projects_with_filters( sort_direction: str = "asc", fuzzy: bool = False, ) -> BundProjectPage: - is_forbidden = await is_projects_access_forbidden(access_token_metadata, raw_oa_institution_id, None) - if is_forbidden: - raise HttpException( - status_code=403, - detail="Access denied for the provided author ID or institution ID", - ) institution_aliases = [] if raw_oa_institution_id: if institutions_search_client is None: @@ -626,7 +641,6 @@ async def _get_eu_project_ids_for_openalex_authors( async def search_eu_cordis_projects( - access_token_metadata: AccessTokenMetadata, page: int, size: int, project_types: list[EuCordisProjectType] | None = None, @@ -665,12 +679,6 @@ async def search_eu_cordis_projects( funding scheme, status, funding years, country) are ANDed together as in eu_cordis_projects_client.search_eu_cordis_projects. """ - is_forbidden = await is_projects_access_forbidden(access_token_metadata, raw_oa_institution_id, author_id) - if is_forbidden: - raise HttpException( - status_code=403, - detail="Access denied for the provided author ID or institution ID", - ) resolved_organization_names: list[str] = list(organization_names or []) if raw_oa_institution_id: if institutions_search_client is None: diff --git a/webapp/src/app/api/api/projects.service.ts b/webapp/src/app/api/api/projects.service.ts index 0db7818b..938af152 100644 --- a/webapp/src/app/api/api/projects.service.ts +++ b/webapp/src/app/api/api/projects.service.ts @@ -86,10 +86,10 @@ export class ProjectsService extends BaseService { * @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body. * @param reportProgress flag to report request and response progress. */ - public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable; - public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; - public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; - public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable { + public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable; + public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable>; + public getDfgProjects(rawOaInstitutionId?: string, authorId?: string, authorFilterIds?: Array, institutionIds?: Array, title?: string, keywords?: Array, topicIds?: Array, filterTopicIds?: Array, fundingStartYears?: Array, fundingEndYears?: Array, page?: number, size?: number, sortBy?: string, sortDirection?: string, fuzzy?: boolean, secureFgpAcc?: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable { let localVarQueryParameters = new HttpParams({encoder: this.encoder}); localVarQueryParameters = this.addToHttpParams(localVarQueryParameters, diff --git a/webapp/src/app/resolvers/dfg-projects.resolver.ts b/webapp/src/app/resolvers/dfg-projects.resolver.ts index 365a4214..102976f9 100644 --- a/webapp/src/app/resolvers/dfg-projects.resolver.ts +++ b/webapp/src/app/resolvers/dfg-projects.resolver.ts @@ -1,6 +1,5 @@ import { inject } from '@angular/core'; import { ResolveFn } from '@angular/router'; -import { of } from 'rxjs'; import { DfgProjectPage, ProjectsService } from '../api'; export const dfgProjectQueryParamKeys = { @@ -25,34 +24,6 @@ export const RESOLVING_PROJECT_PAGE_FOR = { KEYWORDS_SEARCH: 'KEYWORDS_SEARCH', } as const; -// Empty page returned whenever a required numeric param fails to parse. -const EMPTY_DFG_PROJECT_PAGE: DfgProjectPage = { - page: 0, - size: 0, - total: 0, - elements: [], -} as DfgProjectPage; - -function toNumberOrNull(value: unknown): number | null { - if (value === undefined || value === null || value === '') { - return null; - } - const parsed = Number(value); - return Number.isFinite(parsed) ? parsed : null; -} - -function toNumberArrayOrNull(values: string[]): number[] | null { - const result: number[] = []; - for (const value of values) { - const parsed = toNumberOrNull(value); - if (parsed === null) { - return null; - } - result.push(parsed); - } - return result; -} - export const dfgProjectsResolver: ResolveFn = ( route, state, @@ -69,11 +40,10 @@ export const dfgProjectsResolver: ResolveFn = ( ); const page = - toNumberOrNull(route.queryParamMap.get(dfgProjectQueryParamKeys.page)) ?? 0; + Number(route.queryParamMap.get(dfgProjectQueryParamKeys.page)) || 0; const size = - toNumberOrNull(route.queryParamMap.get(dfgProjectQueryParamKeys.size)) ?? - 10; + Number(route.queryParamMap.get(dfgProjectQueryParamKeys.size)) || 10; const searchTerm = route.queryParamMap.get(dfgProjectQueryParamKeys.title) ?? undefined; @@ -92,15 +62,12 @@ export const dfgProjectsResolver: ResolveFn = ( dfgProjectQueryParamKeys.topicId, ); - const institutionIds = toNumberArrayOrNull( - route.queryParamMap.getAll(dfgProjectQueryParamKeys.institutionId), + const institutionIds = route.queryParamMap.getAll( + dfgProjectQueryParamKeys.institutionId, ); - const authorIds = toNumberArrayOrNull( - route.queryParamMap.getAll(dfgProjectQueryParamKeys.authorId), + const authorIds = route.queryParamMap.getAll( + dfgProjectQueryParamKeys.authorId, ); - if (institutionIds === null || authorIds === null) { - return of(EMPTY_DFG_PROJECT_PAGE); - } if (resolvingFor === RESOLVING_PROJECT_PAGE_FOR.PERSON) { const rawAuthorId = route.parent?.parent?.params['id'];