Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

editoast: add roles check to views::train_schedule #8436

Merged
merged 1 commit into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions editoast/editoast_authz/src/builtin_role.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ pub enum BuiltinRole {
#[strum(serialize = "rolling_stock_collection:read")]
RollingStockCollectionRead,

#[strum(serialize = "train_schedule:read")]
TrainScheduleRead,

#[strum(serialize = "work_schedule:write")]
WorkScheduleWrite,

Expand All @@ -42,7 +39,6 @@ impl BuiltinRoleSet for BuiltinRole {
InfraRead => vec![],
InfraWrite => vec![InfraRead],
RollingStockCollectionRead => vec![],
TrainScheduleRead => vec![InfraRead],
WorkScheduleWrite => vec![],
MapRead => vec![],
Stdcm => vec![MapRead],
Expand Down
67 changes: 67 additions & 0 deletions editoast/src/views/v2/train_schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use axum::extract::Path;
use axum::extract::Query;
use axum::extract::State;
use axum::response::IntoResponse;
use axum::Extension;
use editoast_authz::BuiltinRole;
use itertools::Itertools;
use serde::Deserialize;
use serde::Serialize;
Expand Down Expand Up @@ -44,6 +46,8 @@ use crate::modelsv2::train_schedule::TrainScheduleChangeset;
use crate::views::v2::path::pathfinding::pathfinding_from_train;
use crate::views::v2::path::pathfinding_from_train_batch;
use crate::views::v2::path::PathfindingError;
use crate::views::AuthorizationError;
use crate::views::AuthorizerExt;
use crate::AppState;
use crate::RedisClient;
use crate::RollingStockModel;
Expand Down Expand Up @@ -174,8 +178,17 @@ impl From<TrainScheduleForm> for TrainScheduleChangeset {
)]
async fn get(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
train_schedule_id: Path<TrainScheduleIdParam>,
) -> Result<Json<TrainScheduleResult>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let train_schedule_id = train_schedule_id.id;
let conn = &mut db_pool.get().await?;
Expand Down Expand Up @@ -203,8 +216,17 @@ struct BatchRequest {
)]
async fn get_batch(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<BatchRequest>,
) -> Result<Json<Vec<TrainScheduleResult>>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let conn = &mut db_pool.get().await?;
let train_ids = data.ids;
Expand All @@ -229,8 +251,17 @@ async fn get_batch(
)]
async fn delete(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<BatchRequest>,
) -> Result<impl IntoResponse> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableWrite].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();

use crate::modelsv2::DeleteBatch;
Expand All @@ -256,9 +287,18 @@ async fn delete(
)]
async fn put(
db_pool: State<DbConnectionPoolV2>,
Extension(authorizer): AuthorizerExt,
train_schedule_id: Path<TrainScheduleIdParam>,
Json(data): Json<TrainScheduleForm>,
) -> Result<Json<TrainScheduleResult>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableWrite].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}

let conn = &mut db_pool.get().await?;

let train_schedule_id = train_schedule_id.id;
Expand Down Expand Up @@ -296,10 +336,19 @@ pub struct ElectricalProfileSetIdQueryParam {
)]
async fn simulation(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Path(train_schedule_id): Path<TrainScheduleIdParam>,
Query(infra_id_query): Query<InfraIdQueryParam>,
Query(electrical_profile_set_id_query): Query<ElectricalProfileSetIdQueryParam>,
) -> Result<Json<SimulationResponse>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}

let redis_client = app_state.redis.clone();
let core_client = app_state.core_client.clone();
let db_pool = app_state.db_pool_v2.clone();
Expand Down Expand Up @@ -622,8 +671,17 @@ enum SimulationSummaryResult {
)]
async fn simulation_summary(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Json(data): Json<SimulationBatchForm>,
) -> Result<Json<HashMap<i64, SimulationSummaryResult>>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let redis_client = app_state.redis.clone();
let core = app_state.core_client.clone();
Expand Down Expand Up @@ -715,11 +773,20 @@ async fn simulation_summary(
)]
async fn get_path(
app_state: State<AppState>,
Extension(authorizer): AuthorizerExt,
Path(TrainScheduleIdParam {
id: train_schedule_id,
}): Path<TrainScheduleIdParam>,
Query(InfraIdQueryParam { infra_id }): Query<InfraIdQueryParam>,
) -> Result<Json<PathfindingResult>> {
let authorized = authorizer
.check_roles([BuiltinRole::InfraRead, BuiltinRole::TimetableRead].into())
.await
.map_err(AuthorizationError::AuthError)?;
if !authorized {
return Err(AuthorizationError::Unauthorized.into());
}

let db_pool = app_state.db_pool_v2.clone();
let redis_client = app_state.redis.clone();
let core = app_state.core_client.clone();
Expand Down
2 changes: 1 addition & 1 deletion editoast/src/views/v2/train_schedule/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async fn project_path(
.check_roles(
[
BuiltinRole::InfraRead,
BuiltinRole::TrainScheduleRead,
BuiltinRole::TimetableRead,
BuiltinRole::RollingStockCollectionRead,
]
.into(),
Expand Down
Loading