From 6858633cb9e254c4d4040d379a6acf4121b8d9df Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Sat, 18 May 2024 00:14:43 +0530 Subject: [PATCH] feat: identity: promote user to admin --- src/identity/application/services/errors.rs | 1 + .../services/set_user_admin/command.rs | 63 +++++++++++++++++++ .../services/set_user_admin/error.rs | 3 + .../services/set_user_admin/events.rs | 19 ++++++ .../services/set_user_admin/mod.rs | 18 ++++++ .../services/set_user_admin/service.rs | 46 ++++++++++++++ 6 files changed, 150 insertions(+) create mode 100644 src/identity/application/services/set_user_admin/command.rs create mode 100644 src/identity/application/services/set_user_admin/error.rs create mode 100644 src/identity/application/services/set_user_admin/events.rs create mode 100644 src/identity/application/services/set_user_admin/mod.rs create mode 100644 src/identity/application/services/set_user_admin/service.rs diff --git a/src/identity/application/services/errors.rs b/src/identity/application/services/errors.rs index 6ad3c12..e7ed714 100644 --- a/src/identity/application/services/errors.rs +++ b/src/identity/application/services/errors.rs @@ -23,6 +23,7 @@ pub enum IdentityCommandError { BadPassowrd(String), PasswordsDontMatch, WrongPassword, + PermissionDenied, } impl From for IdentityCommandError { diff --git a/src/identity/application/services/set_user_admin/command.rs b/src/identity/application/services/set_user_admin/command.rs new file mode 100644 index 0000000..587d6b2 --- /dev/null +++ b/src/identity/application/services/set_user_admin/command.rs @@ -0,0 +1,63 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; + +use super::*; +use crate::identity::domain::aggregate::User; + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] +pub struct SetAdminCommand { + promoted_by_user: User, +} + +impl SetAdminCommand { + pub fn new(promoted_by_user: User) -> IdentityCommandResult { + if !promoted_by_user.is_admin() { + return Err(IdentityCommandError::PermissionDenied); + } + + Ok(Self { promoted_by_user }) + } +} + +#[cfg(test)] +mod tests { + use crate::identity::domain::aggregate::UserBuilder; + + use super::*; + + #[actix_rt::test] + async fn test_cmd() { + let username = "realaravinth"; + + SetAdminCommand::new( + UserBuilder::default() + .username(username.into()) + .email(username.into()) + .hashed_password(username.into()) + .is_verified(true) + .is_admin(true) + .build() + .unwrap(), + ) + .unwrap(); + + assert_eq!( + SetAdminCommand::new( + UserBuilder::default() + .username(username.into()) + .email(username.into()) + .hashed_password(username.into()) + .is_verified(true) + .is_admin(false) + .build() + .unwrap(), + ) + .err(), + Some(IdentityCommandError::PermissionDenied) + ); + } +} diff --git a/src/identity/application/services/set_user_admin/error.rs b/src/identity/application/services/set_user_admin/error.rs new file mode 100644 index 0000000..56f60de --- /dev/null +++ b/src/identity/application/services/set_user_admin/error.rs @@ -0,0 +1,3 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later diff --git a/src/identity/application/services/set_user_admin/events.rs b/src/identity/application/services/set_user_admin/events.rs new file mode 100644 index 0000000..ad246ee --- /dev/null +++ b/src/identity/application/services/set_user_admin/events.rs @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use derive_getters::Getters; +use serde::{Deserialize, Serialize}; + +use crate::identity::domain::aggregate::User; + +#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Ord, PartialOrd, Getters)] +pub struct UserPromotedToAdminEvent { + promoted_by_user: User, +} + +impl UserPromotedToAdminEvent { + pub fn new(promoted_by_user: User) -> Self { + Self { promoted_by_user } + } +} diff --git a/src/identity/application/services/set_user_admin/mod.rs b/src/identity/application/services/set_user_admin/mod.rs new file mode 100644 index 0000000..8aa32f7 --- /dev/null +++ b/src/identity/application/services/set_user_admin/mod.rs @@ -0,0 +1,18 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +pub mod command; +pub mod events; +pub mod service; + +use super::errors::*; + +#[async_trait::async_trait] +pub trait SetUserAdminUseCase { + async fn set_user_admin( + &self, + cmd: command::SetAdminCommand, + //) -> errors::ProcessAuthorizationServiceResult; + ) -> events::UserPromotedToAdminEvent; +} diff --git a/src/identity/application/services/set_user_admin/service.rs b/src/identity/application/services/set_user_admin/service.rs new file mode 100644 index 0000000..c3ac6aa --- /dev/null +++ b/src/identity/application/services/set_user_admin/service.rs @@ -0,0 +1,46 @@ +// SPDX-FileCopyrightText: 2024 Aravinth Manivannan +// +// SPDX-License-Identifier: AGPL-3.0-or-later + +use super::*; + +struct SetUserAdminService; + +#[async_trait::async_trait] +impl SetUserAdminUseCase for SetUserAdminService { + async fn set_user_admin( + &self, + cmd: command::SetAdminCommand, + //) -> errors::ProcessAuthorizationServiceResult; + ) -> events::UserPromotedToAdminEvent { + events::UserPromotedToAdminEvent::new(cmd.promoted_by_user().to_owned()) + } +} + +#[cfg(test)] +mod tests { + use crate::identity::domain::aggregate::UserBuilder; + + use super::*; + + #[actix_rt::test] + async fn test_service() { + let username = "realaravinth"; + + let s = SetUserAdminService; + let u = UserBuilder::default() + .username(username.into()) + .email(username.into()) + .hashed_password(username.into()) + .is_verified(true) + .is_admin(true) + .build() + .unwrap(); + + let cmd = command::SetAdminCommand::new(u).unwrap(); + assert_eq!( + s.set_user_admin(cmd.clone()).await.promoted_by_user(), + cmd.promoted_by_user() + ) + } +}