diff --git a/server/domain/src/form/models.rs b/server/domain/src/form/models.rs index 9925d818..6a4190b2 100644 --- a/server/domain/src/form/models.rs +++ b/server/domain/src/form/models.rs @@ -14,7 +14,7 @@ use types::Resolver; use crate::{ repository::form_repository::FormRepository, - types::authorization_guard::{AuthorizationGuard, AuthorizationGuardDefinitions, Create}, + types::authorization_guard::AuthorizationGuardDefinitions, user::models::{Role::Administrator, User}, }; @@ -513,12 +513,6 @@ impl AuthorizationGuardDefinitions for Message { } } -impl From for AuthorizationGuard { - fn from(value: Message) -> Self { - AuthorizationGuard::new(value) - } -} - impl Message { /// [`Message`] の生成を試みます。 /// diff --git a/server/domain/src/notification/models.rs b/server/domain/src/notification/models.rs index 021ff215..6f6d909c 100644 --- a/server/domain/src/notification/models.rs +++ b/server/domain/src/notification/models.rs @@ -2,8 +2,7 @@ use derive_getters::Getters; use serde::Deserialize; use crate::{ - form::models::MessageId, - types::authorization_guard::{AuthorizationGuard, AuthorizationGuardDefinitions, Create}, + form::models::MessageId, types::authorization_guard::AuthorizationGuardDefinitions, user::models::User, }; @@ -111,9 +110,3 @@ impl AuthorizationGuardDefinitions for Notification { self.recipient().id == actor.id } } - -impl From for AuthorizationGuard { - fn from(value: Notification) -> Self { - AuthorizationGuard::new(value) - } -} diff --git a/server/domain/src/types/authorization_guard.rs b/server/domain/src/types/authorization_guard.rs index dafba7f4..e0947168 100644 --- a/server/domain/src/types/authorization_guard.rs +++ b/server/domain/src/types/authorization_guard.rs @@ -112,7 +112,7 @@ impl> AuthorizationGuard { } impl> AuthorizationGuard { - /// `actor` が `guard_target` を取得することを試みます。 + /// `actor` が `guard_target` の参照を取得することを試みます。 pub fn try_read(&self, actor: &User) -> Result<&T, DomainError> { if self.guard_target.can_read(actor) { Ok(&self.guard_target) @@ -121,6 +121,15 @@ impl> AuthorizationGuard { } } + /// `actor` が `guard_target` を取得することを試みます。 + pub fn try_into_read(self, actor: &User) -> Result { + if self.guard_target.can_read(actor) { + Ok(self.guard_target) + } else { + Err(DomainError::Forbidden) + } + } + /// [`AuthorizationGuard`] の Action を [`Update`] に変換します。 pub fn into_update(self) -> AuthorizationGuard { AuthorizationGuard { @@ -191,6 +200,12 @@ pub trait AuthorizationGuardDefinitions { fn can_delete(&self, actor: &User) -> bool; } +impl> From for AuthorizationGuard { + fn from(guard_target: T) -> Self { + AuthorizationGuard::new(guard_target) + } +} + #[cfg(test)] mod test { use uuid::Uuid; diff --git a/server/presentation/src/notification_handler.rs b/server/presentation/src/notification_handler.rs index fc8d6b86..c0d5c398 100644 --- a/server/presentation/src/notification_handler.rs +++ b/server/presentation/src/notification_handler.rs @@ -29,8 +29,8 @@ pub async fn fetch_by_request_user( .into_iter() .map(|notification| { notification - .try_read(&user) - .map(NotificationResponse::from_notification_ref) + .try_into_read(&user) + .map(Into::::into) }) .collect::, _>>() .map_err(Into::into) @@ -66,8 +66,8 @@ pub async fn update_read_state( .into_iter() .map(|notification| { notification - .try_read(&user) - .map(NotificationResponse::from_notification_ref) + .try_into_read(&user) + .map(Into::::into) }) .collect::, _>>() .map_err(Into::into) diff --git a/server/presentation/src/schemas/notification/notification_response_schemas.rs b/server/presentation/src/schemas/notification/notification_response_schemas.rs index fd5e81fc..87c8c0f5 100644 --- a/server/presentation/src/schemas/notification/notification_response_schemas.rs +++ b/server/presentation/src/schemas/notification/notification_response_schemas.rs @@ -9,8 +9,8 @@ pub struct NotificationResponse { pub is_read: bool, } -impl NotificationResponse { - pub fn from_notification_ref(notification: &Notification) -> Self { +impl From for NotificationResponse { + fn from(notification: Notification) -> Self { let (source_type, source_id) = match notification.source() { NotificationSource::Message(message_id) => { ("MESSAGE".to_string(), message_id.to_string())