From 8cfa427d581d657b36bf06d6727f2343a5cf47d9 Mon Sep 17 00:00:00 2001 From: Francia Csaba Date: Mon, 22 Apr 2024 14:41:54 +0200 Subject: [PATCH] Use verify_permission --- amarillo/plugins/grfs_export/gtfs_generator.py | 4 ++-- amarillo/plugins/grfs_export/router.py | 18 ++++++++++++------ pyproject.toml | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/amarillo/plugins/grfs_export/gtfs_generator.py b/amarillo/plugins/grfs_export/gtfs_generator.py index a63bf0d..afdf3cf 100644 --- a/amarillo/plugins/grfs_export/gtfs_generator.py +++ b/amarillo/plugins/grfs_export/gtfs_generator.py @@ -18,7 +18,7 @@ import logging logger = logging.getLogger(__name__) regions = {} -for region_file_name in glob('conf/region/*.json'): +for region_file_name in glob('data/region/*.json'): with open(region_file_name) as region_file: dict = json.load(region_file) region = Region(**dict) @@ -26,7 +26,7 @@ for region_file_name in glob('conf/region/*.json'): regions[region_id] = region agencies = [] -for agency_file_name in glob('conf/agency/*.json'): +for agency_file_name in glob('data/agency/*.json'): with open(agency_file_name) as agency_file: dict = json.load(agency_file) agency = GtfsAgency(dict["id"], dict["name"], dict["url"], dict["timezone"], dict["lang"], dict["email"]) diff --git a/amarillo/plugins/grfs_export/router.py b/amarillo/plugins/grfs_export/router.py index 5d44cd7..4e1d50b 100644 --- a/amarillo/plugins/grfs_export/router.py +++ b/amarillo/plugins/grfs_export/router.py @@ -3,8 +3,9 @@ import logging from fastapi import APIRouter, HTTPException, status, Depends from amarillo.models.Carpool import Region -from amarillo.services.oauth2 import get_current_agency from amarillo.services.regions import RegionService +from amarillo.services.oauth2 import get_current_user, verify_permission +from amarillo.models.User import User from amarillo.utils.container import container from fastapi.responses import FileResponse @@ -12,8 +13,10 @@ logger = logging.getLogger(__name__) router = APIRouter() -@router.post("/export-grfs") -async def post_agency_conf(admin_api_key: str = Depends(get_current_agency)): + +@router.post("/export") +async def trigger_export(requesting_user: User = Depends(get_current_user)): + verify_permission("gtfs-generate", requesting_user) #import is here to avoid circular import from amarillo.plugins.grfs_export.gtfs_generator import generate_gtfs generate_gtfs() @@ -40,7 +43,8 @@ def _assert_region_exists(region_id: str) -> Region: status.HTTP_404_NOT_FOUND: {"description": "Region not found"}, } ) -async def get_file(region_id: str, user: str = Depends(get_current_agency)): +async def get_file(region_id: str, requesting_user: User = Depends(get_current_user)): + verify_permission("gtfs", requesting_user) _assert_region_exists(region_id) try: from amarillo.plugins.metrics import increment_grfs_download_counter @@ -57,7 +61,8 @@ async def get_file(region_id: str, user: str = Depends(get_current_agency)): status.HTTP_404_NOT_FOUND: {"description": "Region not found"}, } ) -async def get_file(region_id: str, user: str = Depends(get_current_agency)): +async def get_file(region_id: str, requesting_user: User = Depends(get_current_user)): + verify_permission("gtfs", requesting_user) _assert_region_exists(region_id) try: from amarillo.plugins.metrics import increment_grfs_download_counter @@ -76,7 +81,8 @@ async def get_file(region_id: str, user: str = Depends(get_current_agency)): status.HTTP_400_BAD_REQUEST: {"description": "Bad request, e.g. because format is not supported, i.e. neither protobuf nor json."} } ) -async def get_file(region_id: str, format: str = 'protobuf', user: str = Depends(get_current_agency)): +async def get_file(region_id: str, format: str = 'protobuf', requesting_user: User = Depends(get_current_user)): + verify_permission("gtfs", requesting_user) _assert_region_exists(region_id) if format == 'json': return FileResponse(f'data/grfs/amarillo.{region_id}.gtfsrt.json') diff --git a/pyproject.toml b/pyproject.toml index fbaefe6..a87fc85 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "amarillo-grfs-export" -version = "0.0.6" +version = "0.0.7" dependencies = [ "amarillo", "amarillo-enhancer",