Skip to content

Commit

Permalink
refactor: rework how require_authentication! and similar macros work,…
Browse files Browse the repository at this point in the history
… begin refactoring user endpoints
  • Loading branch information
simongoricar committed Sep 23, 2024
1 parent ce2c834 commit 843314d
Show file tree
Hide file tree
Showing 19 changed files with 329 additions and 219 deletions.
3 changes: 3 additions & 0 deletions kolomoni/src/api/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ impl From<UserQueryError> for APIError {
UserQueryError::HasherError { error } => Self::InternalGenericError {
error: Box::new(error),
},
UserQueryError::DatabaseConsistencyError { reason } => {
Self::InternalErrorWithReason { reason }
}
}
}
}
Expand Down
87 changes: 85 additions & 2 deletions kolomoni/src/api/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ macro_rules! json_error_response_with_reason {
/// }
/// ```
#[macro_export]
macro_rules! require_authentication {
macro_rules! require_user_authentication {
($user_auth_extractor:expr) => {
$user_auth_extractor
.authenticated_user()
Expand Down Expand Up @@ -556,7 +556,8 @@ macro_rules! require_permission_with_optional_authentication {
/// ```
///
#[macro_export]
macro_rules! require_permission {
#[deprecated]
macro_rules! require_permission_OLD {
($permission_set:expr, $required_permission:expr) => {
if !$permission_set.has_permission($required_permission) {
return Err(
Expand All @@ -576,3 +577,85 @@ macro_rules! require_permission {
}
};
}


#[macro_export]
macro_rules! require_permission_in_set {
($permission_set:expr, $required_permission:expr) => {
if !$permission_set.has_permission($required_permission) {
return Err(
$crate::api::errors::APIError::missing_specific_permission($required_permission),
);
}
};
}


#[macro_export]
macro_rules! require_permission_on_user {
($database_connection:expr, $authenticated_user:expr, $required_permission:expr) => {{
if !$authenticated_user
.transitively_has_permission($database_connection, $required_permission)
.await?
{
return Err(
$crate::api::errors::APIError::missing_specific_permission($required_permission),
);
}

$authenticated_user
}};
}

#[macro_export]
macro_rules! require_user_authentication_and_permission {
($database_connection:expr, $authentication_extractor:expr, $required_permission:expr) => {{
let __authenticated_user = $crate::require_user_authentication!($authentication_extractor);

$crate::require_permission_on_user!(
$database_connection,
__authenticated_user,
$required_permission
)
}};
}

#[deprecated]
#[macro_export]
macro_rules! require_permissionOLD2 {
($database_connection:expr, on authentication extractor $authentication_extractor:expr, $required_permission:expr) => {
if let Some(authenticated_user) = $authentication_extractor.authenticated_user() {
require_permission!(
$database_connection,
authenticated_user,
$required_permission
)
} else {
if !$authentication_extractor.is_permission_granted_to_all($required_permission) {
return Err(
$crate::api::errors::APIError::missing_specific_permission($required_permission),
);
}
}

if !$authentication_extractor
.transitively_has_permission($database_connection, $required_permission)
.await?
{
return Err(
$crate::api::errors::APIError::missing_specific_permission($required_permission),
);
}
};

($database_connection:expr, on user $authenticated_user:expr, $required_permission:expr) => {
if !$authenticated_user
.transitively_has_permission($database_connection, $required_permission)
.await?
{
return Err(
$crate::api::errors::APIError::missing_specific_permission($required_permission),
);
}
};
}
5 changes: 3 additions & 2 deletions kolomoni/src/api/openapi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub trait RequiredPermission {
macro_rules! generate_standalone_requirement_struct {
($permission_variant:ident) => {
::paste::paste! {
// FIXME this first doc isn't emitted for some reason? not a problem, but it's a bit annoying
#[doc = concat!(
"Corresponds to the [`Permission::",
stringify!($permission_variant),
Expand Down Expand Up @@ -176,7 +177,7 @@ impl<P: RequiredPermission> utoipa::IntoResponses for FailedAuthenticationRespon
)
.build();

let missing_user_permission_decription = format!("Missing the `{}` permission.", P::name());
let missing_user_permission_description = format!("Missing the `{}` permission.", P::name());

let mut missing_user_permission_example = serde_json::Map::with_capacity(1);
missing_user_permission_example.insert(
Expand All @@ -185,7 +186,7 @@ impl<P: RequiredPermission> utoipa::IntoResponses for FailedAuthenticationRespon
);

let missing_user_permission_response = ResponseBuilder::new()
.description(missing_user_permission_decription)
.description(missing_user_permission_description)
.content(
mime::APPLICATION_JSON.to_string(),
ContentBuilder::new()
Expand Down
16 changes: 8 additions & 8 deletions kolomoni/src/api/v1/dictionary/categories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ use crate::{
impl_json_response_builder,
json_error_response_with_reason,
obtain_database_connection,
require_authentication,
require_permission,
require_permission_OLD,
require_permission_with_optional_authentication,
require_user_authentication,
state::ApplicationState,
};

Expand Down Expand Up @@ -121,8 +121,8 @@ pub async fn create_category(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::CategoryCreate
Expand Down Expand Up @@ -406,8 +406,8 @@ pub async fn update_specific_category(
let mut transaction = database_connection.begin().await?;


let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::CategoryUpdate
Expand Down Expand Up @@ -565,8 +565,8 @@ pub async fn delete_specific_category(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::CategoryDelete
Expand Down
8 changes: 4 additions & 4 deletions kolomoni/src/api/v1/dictionary/english/meaning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::{
authentication::UserAuthenticationExtractor,
impl_json_response_builder,
obtain_database_connection,
require_permission,
require_permission_OLD,
require_permission_with_optional_authentication,
state::ApplicationState,
};
Expand Down Expand Up @@ -165,7 +165,7 @@ pub async fn create_english_word_meaning(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

require_permission!(
require_permission_OLD!(
&mut transaction,
authentication,
Permission::WordUpdate
Expand Down Expand Up @@ -228,7 +228,7 @@ pub async fn update_english_word_meaning(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

require_permission!(
require_permission_OLD!(
&mut transaction,
authentication,
Permission::WordUpdate
Expand Down Expand Up @@ -306,7 +306,7 @@ pub async fn delete_english_word_meaning(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

require_permission!(
require_permission_OLD!(
&mut transaction,
authentication,
Permission::WordUpdate
Expand Down
16 changes: 8 additions & 8 deletions kolomoni/src/api/v1/dictionary/english/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use crate::{
impl_json_response_builder,
json_error_response_with_reason,
obtain_database_connection,
require_authentication,
require_permission,
require_user_authentication,
require_permission_OLD,
require_permission_with_optional_authentication,
state::ApplicationState,
};
Expand Down Expand Up @@ -323,8 +323,8 @@ pub async fn create_english_word(
) -> EndpointResult {
let mut database_connection = obtain_database_connection!(state);

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut database_connection,
authenticated_user,
Permission::WordCreate
Expand Down Expand Up @@ -600,8 +600,8 @@ pub async fn update_specific_english_word(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::WordUpdate
Expand Down Expand Up @@ -720,8 +720,8 @@ pub async fn delete_english_word(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::WordDelete
Expand Down
8 changes: 4 additions & 4 deletions kolomoni/src/api/v1/dictionary/slovene/meaning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::{
authentication::UserAuthenticationExtractor,
impl_json_response_builder,
obtain_database_connection,
require_permission,
require_permission_OLD,
require_permission_with_optional_authentication,
state::ApplicationState,
};
Expand Down Expand Up @@ -172,7 +172,7 @@ pub async fn create_slovene_word_meaning(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

require_permission!(
require_permission_OLD!(
&mut transaction,
authentication,
Permission::WordUpdate
Expand Down Expand Up @@ -238,7 +238,7 @@ pub async fn update_slovene_word_meaning(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

require_permission!(
require_permission_OLD!(
&mut transaction,
authentication,
Permission::WordUpdate
Expand Down Expand Up @@ -317,7 +317,7 @@ pub async fn delete_slovene_word_meaning(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

require_permission!(
require_permission_OLD!(
&mut transaction,
authentication,
Permission::WordUpdate
Expand Down
16 changes: 8 additions & 8 deletions kolomoni/src/api/v1/dictionary/slovene/word.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use crate::{
impl_json_response_builder,
json_error_response_with_reason,
obtain_database_connection,
require_authentication,
require_permission,
require_user_authentication,
require_permission_OLD,
require_permission_with_optional_authentication,
state::ApplicationState,
};
Expand Down Expand Up @@ -393,8 +393,8 @@ pub async fn create_slovene_word(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::WordCreate
Expand Down Expand Up @@ -671,8 +671,8 @@ pub async fn update_specific_slovene_word(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::WordUpdate
Expand Down Expand Up @@ -791,8 +791,8 @@ pub async fn delete_specific_slovene_word(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::WordDelete
Expand Down
12 changes: 6 additions & 6 deletions kolomoni/src/api/v1/dictionary/translations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use crate::{
authentication::UserAuthenticationExtractor,
json_error_response_with_reason,
obtain_database_connection,
require_authentication,
require_permission,
require_permission_OLD,
require_user_authentication,
state::ApplicationState,
};

Expand Down Expand Up @@ -78,8 +78,8 @@ pub async fn create_translation(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::TranslationCreate
Expand Down Expand Up @@ -216,8 +216,8 @@ pub async fn delete_translation(
let mut database_connection = obtain_database_connection!(state);
let mut transaction = database_connection.begin().await?;

let authenticated_user = require_authentication!(authentication);
require_permission!(
let authenticated_user = require_user_authentication!(authentication);
require_permission_OLD!(
&mut transaction,
authenticated_user,
Permission::TranslationDelete
Expand Down
Loading

0 comments on commit 843314d

Please sign in to comment.