Skip to content

Commit f877cb5

Browse files
authored
Merge pull request #13 from SciCompMod/audit2
Add create-by audit for scenario creation (again)
2 parents 42b7fd1 + 33e0e96 commit f877cb5

File tree

6 files changed

+20
-5
lines changed

6 files changed

+20
-5
lines changed

api/src/api/app/apis/scenarios_api.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,18 @@
4141
response_model_by_alias=True,
4242
)
4343
async def create_scenario(
44+
request: Request,
4445
scenario: Scenario = Body(None, description="")
4546
) -> ID:
4647
"""Create a new scenario to be simulated."""
47-
return await controller.create_scenario(scenario)
48+
49+
# Tag creator info
50+
scenario.creator_user_id = request.state.user.userId if request.state.user else None
51+
scenario.creator_org_id = request.state.realm if request.state.realm else None
52+
53+
return await controller.create_scenario(
54+
scenario
55+
)
4856

4957

5058
@router.delete(

api/src/api/app/controller/scenario_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class ScenarioController:
6161

6262
async def create_scenario(
6363
self,
64-
scenario: Optional[Scenario],
64+
scenario: Optional[Scenario]
6565
) -> ID:
6666
"""Create a new scenario to be simulated."""
6767
if not scenario:

api/src/api/app/db/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ class Scenario(SQLModel, table=True):
2828
timestampSubmitted: Optional[datetime] = Field(default=None, nullable=True)
2929
timestampSimulated: Optional[datetime] = Field(default=None, nullable=True)
3030

31+
creatorUserId: Optional[uuid.UUID] = Field(default=None, nullable=True)
32+
creatorOrgId: Optional[str] = Field(default=None, nullable=True)
3133

3234
class ParameterDefinition(SQLModel, table=True):
3335
id: Optional[uuid.UUID] = Field(default_factory=uuid.uuid4, primary_key=True, nullable=False)

api/src/api/app/db/tasks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ def scenario_create(scenario: Scenario) -> ID:
423423
percentiles=','.join([str(perc) for perc in scenario.percentiles]) if scenario.percentiles else '50',
424424
timestampSubmitted=datetime.now(),
425425
timestampSimulated=None,
426+
creatorUserId=scenario.creator_user_id,
427+
creatorOrgId=scenario.creator_org_id
426428
)
427429
with next(get_session()) as session:
428430
nested_dict = lambda: defaultdict(nested_dict)
@@ -536,6 +538,8 @@ def scenario_get_by_id(id: StrictStr) -> Scenario:
536538
percentiles=[int(perc) for perc in scenario.percentiles.split(',')] if scenario.percentiles else [50],
537539
timestampSubmitted=scenario.timestampSubmitted,
538540
timestampSimulated=scenario.timestampSimulated,
541+
creator_user_id=str(scenario.creatorUserId),
542+
creator_org_id=scenario.creatorOrgId
539543
)
540544

541545
def scenario_get_all() -> List[ReducedScenario]:

api/src/api/app/middlewares/authentication_middleware.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ async def dispatch(self, request, call_next):
2222
# async def get_user(request: Request):
2323
# return request.state.user
2424
request.state.user = user
25-
25+
request.state.realm = realm
2626
# (Optional) role check can be added
2727
# if ['admin'] not in user.role:
2828
# raise HTTPException(

api/src/api/app/models/scenario.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ class Scenario(BaseModel):
4747
percentiles: Optional[List[StrictInt]] = Field(default=[50], description="List of available percentiles for this scenario", alias="percentiles")
4848
timestamp_submitted: Optional[datetime] = Field(default=None, alias="timestampSubmitted", description="Timestamp when the scenario was added/created")
4949
timestamp_simulated: Optional[datetime] = Field(default=None, alias="timestampSimulated", description="Timestamp when the scenario was finished simulating and data is available")
50-
__properties: ClassVar[List[str]] = ["id", "name", "description", "startDate", "endDate", "modelId", "modelParameters", "nodeListId", "linkedInterventions", "percentiles", "timestampSubmitted", "timestampSimulated"]
51-
50+
creator_user_id: Optional[str] = Field(default=None, alias="creatorUserId", description="ID of the user who submitted the scenario")
51+
creator_org_id: Optional[str] = Field(default=None, alias="creatorOrgId", description="ID of the organization the submitting user belongs to")
52+
__properties: ClassVar[List[str]] = ["id", "name", "description", "startDate", "endDate", "modelId", "modelParameters", "nodeListId", "linkedInterventions", "percentiles", "timestampSubmitted", "timestampSimulated", "creatorUserId", "creatorOrgId"]
5253
model_config = {
5354
"populate_by_name": True,
5455
"validate_assignment": True,

0 commit comments

Comments
 (0)