Skip to content
Open
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
2 changes: 2 additions & 0 deletions editoast/authz/src/regulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ pub trait StorageDriver: Clone {
user_id: i64,
) -> impl Future<Output = Result<Option<UserInfo>, Self::Error>> + Send;

#[deprecated(note = "use editoast_models::Group::retrieve directly")]
fn get_group_info(
&self,
group_id: i64,
Expand Down Expand Up @@ -103,6 +104,7 @@ impl<S: StorageDriver> Regulator<S> {
/// Returns whether a group with some id exists
#[tracing::instrument(skip_all, fields(group_id = %group_id), ret(level = Level::DEBUG), err)]
pub async fn group_exists(&self, group_id: i64) -> Result<bool, Error<S::Error>> {
#[expect(deprecated)] // to be removed soon
self.driver
.get_group_info(group_id)
.await
Expand Down
19 changes: 8 additions & 11 deletions editoast/src/client/group.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use anyhow::anyhow;
use anyhow::bail;
use authz;
use authz::Group;
use authz::StorageDriver;
use authz::identity::UserInfo;
use authz::v2::Authorizer;
use clap::Args;
use clap::Subcommand;

use authz;
use authz::StorageDriver;
use authz::identity::GroupInfo;
use authz::identity::UserInfo;

use database::DbConnectionPoolV2;
use editoast_models::prelude::*;

use std::collections::HashSet;
use std::sync::Arc;

use editoast_models::prelude::*;

use crate::authorizers::Rejection;
use crate::authorizers::SystemAuthorizer;
use crate::authorizers::impossible;
Expand Down Expand Up @@ -100,20 +97,20 @@ pub async fn group_info(
openfga_config: OpenfgaConfig,
pool: Arc<DbConnectionPoolV2>,
) -> anyhow::Result<()> {
let regulator = openfga_config.into_regulator(pool).await?;
let regulator = openfga_config.into_regulator(pool.clone()).await?;
let driver = regulator.driver();
let Some(group_id) = driver.get_group_id(&name).await? else {
tracing::error!(name, "No such group");
return Ok(());
};
let Some(GroupInfo { name }) = driver.get_group_info(group_id).await? else {
let Some(group) = editoast_models::Group::retrieve(pool.get().await?, group_id).await? else {
tracing::error!(group.id = group_id, "No such group");
return Ok(());
};
let user_ids = regulator.group_members(&authz::Group(group_id)).await?;

println!("id : {group_id}");
println!("name : {name}");
println!("name : {}", group.name);
println!("members:");
for authz::User(user_id) in user_ids {
let Some(UserInfo { identities, name }) = driver.get_user_info(user_id).await? else {
Expand Down
17 changes: 12 additions & 5 deletions editoast/src/client/roles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use authz::identity::UserInfo;
use authz::v2::Authorizer;
use clap::Args;
use clap::Subcommand;
use database::DbConnection;
use database::DbConnectionPoolV2;
use editoast_models::Group;
use editoast_models::prelude::*;
use itertools::Itertools as _;
use strum::IntoEnumIterator;
use tracing::info;
Expand Down Expand Up @@ -114,6 +117,7 @@ impl Display for Subject {
async fn parse_and_fetch_subject(
subject: &String,
driver: &PgAuthDriver,
conn: DbConnection,
) -> anyhow::Result<Subject> {
let id = if let Ok(id) = subject.parse::<i64>() {
id
Expand All @@ -123,8 +127,8 @@ async fn parse_and_fetch_subject(
};
let subject = if let Some(info) = driver.get_user_info(id).await? {
Subject::new_user(id, info)
} else if let Some(info) = driver.get_group_info(id).await? {
Subject::new_group(id, info)
} else if let Some(group) = Group::retrieve(conn, id).await? {
Subject::new_group(id, GroupInfo { name: group.name })
} else {
bail!("No subject found with ID {id}");
};
Expand All @@ -137,8 +141,9 @@ pub async fn list_subject_roles(
pool: Arc<DbConnectionPoolV2>,
openfga_config: OpenfgaConfig,
) -> anyhow::Result<()> {
let conn = pool.get().await?;
let regulator = openfga_config.into_regulator(pool).await?;
let roles = match parse_and_fetch_subject(&subject, regulator.driver()).await? {
let roles = match parse_and_fetch_subject(&subject, regulator.driver(), conn).await? {
Subject {
id,
info: SubjectInfo::User(_),
Expand Down Expand Up @@ -192,7 +197,8 @@ pub async fn add_roles(
.collect_vec()
.join(", "),
);
let subject = parse_and_fetch_subject(&subject, &PgAuthDriver::new(pool)).await?;
let conn = pool.get().await?;
let subject = parse_and_fetch_subject(&subject, &PgAuthDriver::new(pool), conn).await?;
let add_roles = authz::v2::add_roles(subject.into_authz(), roles);
match system.authorize(add_roles).await?.access().await? {
Ok(()) => Ok(()),
Expand Down Expand Up @@ -227,7 +233,8 @@ pub async fn remove_roles(
.collect_vec()
.join(", "),
);
let subject = parse_and_fetch_subject(&subject, &PgAuthDriver::new(pool)).await?;
let conn = pool.get().await?;
let subject = parse_and_fetch_subject(&subject, &PgAuthDriver::new(pool), conn).await?;
let remove_roles = authz::v2::remove_roles(subject.into_authz(), roles);
match system.authorize(remove_roles).await?.access().await? {
Ok(()) => Ok(()),
Expand Down
15 changes: 6 additions & 9 deletions editoast/src/client/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ use anyhow::anyhow;
use anyhow::bail;
use authz;
use authz::StorageDriver;
use authz::identity::GroupInfo;
use authz::identity::UserInfo;
use clap::Args;
use clap::Subcommand;
use database::DbConnectionPoolV2;
use editoast_models::Group;
use editoast_models::PgAuthDriver;
use editoast_models::User;
use editoast_models::authn::user::AddIdentitiesError;
Expand Down Expand Up @@ -150,7 +150,7 @@ pub async fn user_info(
openfga_config: OpenfgaConfig,
pool: Arc<DbConnectionPoolV2>,
) -> anyhow::Result<()> {
let regulator = openfga_config.into_regulator(pool).await?;
let regulator = openfga_config.into_regulator(pool.clone()).await?;
let driver = regulator.driver();
let uid = if let Ok(id) = user.parse::<i64>() {
id
Expand All @@ -163,21 +163,18 @@ pub async fn user_info(
return Ok(());
};
let groups = regulator.user_groups(&authz::User(uid)).await?;
let conn = pool.get().await?;

println!("id : {uid}");
println!("identities: {}", identities.join(", "));
println!("name : {name}");
println!("groups :");
for authz::Group(group_id) in groups {
let Some(GroupInfo { name }) = driver.get_group_info(group_id).await? else {
tracing::warn!(
group.id = group_id,
group.name = name,
"group not found, skipping it!"
);
let Some(group) = Group::retrieve(conn.clone(), group_id).await? else {
tracing::warn!(group.id = group_id, "group not found, skipping it!");
continue;
};
println!("- [{group_id}] {name}");
println!("- [{group_id}] {}", group.name);
}
Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion editoast/src/views/timetable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,11 @@ use editoast_models::timetable::Timetable;
use editoast_models::timetable::TimetableWithTrains;
use itertools::Itertools;
use itertools::izip;
use schemas::rolling_stock::EtcsBrakeParams;
use schemas::rolling_stock::LoadingGaugeType;
use schemas::rolling_stock::RollingResistance;
use schemas::rolling_stock::RollingStock;
use schemas::rolling_stock::TowedRollingStock;
use schemas::rolling_stock::{EtcsBrakeParams, LoadingGaugeType};
use schemas::train_schedule::TrainScheduleLike;
use serde::Deserialize;
use serde::Serialize;
Expand Down
Loading