feat: identity: promote user to admin

This commit is contained in:
Aravinth Manivannan 2024-05-18 00:14:43 +05:30
parent a65bbdaa8c
commit 6858633cb9
Signed by: realaravinth
GPG key ID: F8F50389936984FF
6 changed files with 150 additions and 0 deletions

View file

@ -23,6 +23,7 @@ pub enum IdentityCommandError {
BadPassowrd(String),
PasswordsDontMatch,
WrongPassword,
PermissionDenied,
}
impl From<CredsError> for IdentityCommandError {

View 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)
);
}
}

View file

@ -0,0 +1,3 @@
// SPDX-FileCopyrightText: 2024 Aravinth Manivannan <realaravinth@batsense.net>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

View 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 }
}
}

View 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;
}

View 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()
)
}
}