Skip to content

Commit

Permalink
Merge pull request #625 from GiganticMinecraft/refactor/authorization…
Browse files Browse the repository at this point in the history
…-guard

AuthorizationGuard 周りのリファクタリング
  • Loading branch information
rito528 authored Dec 14, 2024
2 parents 897686c + 0c9b8b4 commit 17bed6c
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
8 changes: 1 addition & 7 deletions server/domain/src/form/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -513,12 +513,6 @@ impl AuthorizationGuardDefinitions<Message> for Message {
}
}

impl From<Message> for AuthorizationGuard<Message, Create> {
fn from(value: Message) -> Self {
AuthorizationGuard::new(value)
}
}

impl Message {
/// [`Message`] の生成を試みます。
///
Expand Down
9 changes: 1 addition & 8 deletions server/domain/src/notification/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down Expand Up @@ -111,9 +110,3 @@ impl AuthorizationGuardDefinitions<Notification> for Notification {
self.recipient().id == actor.id
}
}

impl From<Notification> for AuthorizationGuard<Notification, Create> {
fn from(value: Notification) -> Self {
AuthorizationGuard::new(value)
}
}
17 changes: 16 additions & 1 deletion server/domain/src/types/authorization_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl<T: AuthorizationGuardDefinitions<T>> AuthorizationGuard<T, Update> {
}

impl<T: AuthorizationGuardDefinitions<T>> AuthorizationGuard<T, Read> {
/// `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)
Expand All @@ -121,6 +121,15 @@ impl<T: AuthorizationGuardDefinitions<T>> AuthorizationGuard<T, Read> {
}
}

/// `actor` が `guard_target` を取得することを試みます。
pub fn try_into_read(self, actor: &User) -> Result<T, DomainError> {
if self.guard_target.can_read(actor) {
Ok(self.guard_target)
} else {
Err(DomainError::Forbidden)
}
}

/// [`AuthorizationGuard`] の Action を [`Update`] に変換します。
pub fn into_update(self) -> AuthorizationGuard<T, Update> {
AuthorizationGuard {
Expand Down Expand Up @@ -191,6 +200,12 @@ pub trait AuthorizationGuardDefinitions<T> {
fn can_delete(&self, actor: &User) -> bool;
}

impl<T: AuthorizationGuardDefinitions<T>> From<T> for AuthorizationGuard<T, Create> {
fn from(guard_target: T) -> Self {
AuthorizationGuard::new(guard_target)
}
}

#[cfg(test)]
mod test {
use uuid::Uuid;
Expand Down
8 changes: 4 additions & 4 deletions server/presentation/src/notification_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<NotificationResponse>::into)
})
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
Expand Down Expand Up @@ -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::<NotificationResponse>::into)
})
.collect::<Result<Vec<_>, _>>()
.map_err(Into::into)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ pub struct NotificationResponse {
pub is_read: bool,
}

impl NotificationResponse {
pub fn from_notification_ref(notification: &Notification) -> Self {
impl From<Notification> 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())
Expand Down

0 comments on commit 17bed6c

Please sign in to comment.