feat: identity: promote user to admin
This commit is contained in:
parent
a65bbdaa8c
commit
6858633cb9
6 changed files with 150 additions and 0 deletions
|
@ -23,6 +23,7 @@ pub enum IdentityCommandError {
|
|||
BadPassowrd(String),
|
||||
PasswordsDontMatch,
|
||||
WrongPassword,
|
||||
PermissionDenied,
|
||||
}
|
||||
|
||||
impl From<CredsError> for IdentityCommandError {
|
||||
|
|
63
src/identity/application/services/set_user_admin/command.rs
Normal file
63
src/identity/application/services/set_user_admin/command.rs
Normal file
|
@ -0,0 +1,63 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// 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<Self> {
|
||||
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)
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,3 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
19
src/identity/application/services/set_user_admin/events.rs
Normal file
19
src/identity/application/services/set_user_admin/events.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// 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 }
|
||||
}
|
||||
}
|
18
src/identity/application/services/set_user_admin/mod.rs
Normal file
18
src/identity/application/services/set_user_admin/mod.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// 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<String>;
|
||||
) -> events::UserPromotedToAdminEvent;
|
||||
}
|
46
src/identity/application/services/set_user_admin/service.rs
Normal file
46
src/identity/application/services/set_user_admin/service.rs
Normal file
|
@ -0,0 +1,46 @@
|
|||
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
|
||||
//
|
||||
// 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<String>;
|
||||
) -> 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()
|
||||
)
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue