-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add index authz for coaching session resources
There is a lot of duplicated code here. The plan is to defer refactoring for now and take a look at a better error handling system.
- Loading branch information
1 parent
e39e506
commit 06c401f
Showing
5 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{extractors::authenticated_user::AuthenticatedUser, AppState}; | ||
use axum::{ | ||
extract::{Query, Request, State}, | ||
http::StatusCode, | ||
middleware::Next, | ||
response::IntoResponse, | ||
}; | ||
use entity::Id; | ||
use entity_api::coaching_session; | ||
use log::*; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub(crate) struct QueryParams { | ||
coaching_session_id: Id, | ||
} | ||
|
||
/// Checks that coaching relationship record associated with the coaching session | ||
/// referenced by `coaching_session_id exists and that the authenticated user is associated with it. | ||
/// Intended to be given to axum::middleware::from_fn_with_state in the router | ||
pub(crate) async fn index( | ||
State(app_state): State<AppState>, | ||
AuthenticatedUser(user): AuthenticatedUser, | ||
Query(params): Query<QueryParams>, | ||
request: Request, | ||
next: Next, | ||
) -> impl IntoResponse { | ||
match coaching_session::find_by_id_with_coaching_relationship( | ||
app_state.db_conn_ref(), | ||
params.coaching_session_id, | ||
) | ||
.await | ||
{ | ||
Ok((_coaching_session, coaching_relationship)) => { | ||
if coaching_relationship.coach_id == user.id | ||
|| coaching_relationship.coachee_id == user.id | ||
{ | ||
// User has access to coaching relationship | ||
next.run(request).await | ||
} else { | ||
// User does not have access to coaching relationship | ||
(StatusCode::UNAUTHORIZED, "UNAUTHORIZED").into_response() | ||
} | ||
} | ||
Err(e) => { | ||
error!("Error authorizing overarching goals index{:?}", e); | ||
|
||
(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{extractors::authenticated_user::AuthenticatedUser, AppState}; | ||
use axum::{ | ||
extract::{Query, Request, State}, | ||
http::StatusCode, | ||
middleware::Next, | ||
response::IntoResponse, | ||
}; | ||
use entity::Id; | ||
use entity_api::coaching_session; | ||
use log::*; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub(crate) struct QueryParams { | ||
coaching_session_id: Id, | ||
} | ||
|
||
/// Checks that coaching relationship record associated with the coaching session | ||
/// referenced by `coaching_session_id exists and that the authenticated user is associated with it. | ||
/// Intended to be given to axum::middleware::from_fn_with_state in the router | ||
pub(crate) async fn index( | ||
State(app_state): State<AppState>, | ||
AuthenticatedUser(user): AuthenticatedUser, | ||
Query(params): Query<QueryParams>, | ||
request: Request, | ||
next: Next, | ||
) -> impl IntoResponse { | ||
match coaching_session::find_by_id_with_coaching_relationship( | ||
app_state.db_conn_ref(), | ||
params.coaching_session_id, | ||
) | ||
.await | ||
{ | ||
Ok((_coaching_session, coaching_relationship)) => { | ||
if coaching_relationship.coach_id == user.id | ||
|| coaching_relationship.coachee_id == user.id | ||
{ | ||
// User has access to coaching relationship | ||
next.run(request).await | ||
} else { | ||
// User does not have access to coaching relationship | ||
(StatusCode::UNAUTHORIZED, "UNAUTHORIZED").into_response() | ||
} | ||
} | ||
Err(e) => { | ||
error!("Error authorizing overarching goals index{:?}", e); | ||
|
||
(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
pub(crate) mod actions; | ||
pub(crate) mod agreements; | ||
pub(crate) mod coaching_relationships; | ||
pub(crate) mod coaching_sessions; | ||
pub(crate) mod notes; | ||
pub(crate) mod overarching_goals; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::{extractors::authenticated_user::AuthenticatedUser, AppState}; | ||
use axum::{ | ||
extract::{Query, Request, State}, | ||
http::StatusCode, | ||
middleware::Next, | ||
response::IntoResponse, | ||
}; | ||
use entity::Id; | ||
use entity_api::coaching_session; | ||
use log::*; | ||
use serde::Deserialize; | ||
|
||
#[derive(Debug, Deserialize)] | ||
pub(crate) struct QueryParams { | ||
coaching_session_id: Id, | ||
} | ||
|
||
/// Checks that coaching relationship record associated with the coaching session | ||
/// referenced by `coaching_session_id exists and that the authenticated user is associated with it. | ||
/// Intended to be given to axum::middleware::from_fn_with_state in the router | ||
pub(crate) async fn index( | ||
State(app_state): State<AppState>, | ||
AuthenticatedUser(user): AuthenticatedUser, | ||
Query(params): Query<QueryParams>, | ||
request: Request, | ||
next: Next, | ||
) -> impl IntoResponse { | ||
match coaching_session::find_by_id_with_coaching_relationship( | ||
app_state.db_conn_ref(), | ||
params.coaching_session_id, | ||
) | ||
.await | ||
{ | ||
Ok((_coaching_session, coaching_relationship)) => { | ||
if coaching_relationship.coach_id == user.id | ||
|| coaching_relationship.coachee_id == user.id | ||
{ | ||
// User has access to coaching relationship | ||
next.run(request).await | ||
} else { | ||
// User does not have access to coaching relationship | ||
(StatusCode::UNAUTHORIZED, "UNAUTHORIZED").into_response() | ||
} | ||
} | ||
Err(e) => { | ||
error!("Error authorizing overarching goals index{:?}", e); | ||
|
||
(StatusCode::INTERNAL_SERVER_ERROR, "INTERNAL SERVER ERROR").into_response() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters