forked from torrust/torrust-index
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: [torrust#448] refactor services
- Loading branch information
Showing
12 changed files
with
289 additions
and
192 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,52 @@ | ||
//! Authorization service. | ||
use std::sync::Arc; | ||
|
||
use crate::databases::database::{Database, Error}; | ||
use super::user::Repository; | ||
use crate::errors::ServiceError; | ||
use crate::models::user::{UserAuthorization, UserId}; | ||
use crate::models::user::{UserCompact, UserId}; | ||
|
||
pub struct AuthorizeService { | ||
user_authorization_repository: Arc<DbUserAuthorizationRepository>, | ||
pub enum ACTION { | ||
AddCategory, | ||
DeleteCategory, | ||
} | ||
|
||
impl AuthorizeService { | ||
pub struct Service { | ||
user_repository: Arc<Box<dyn Repository>>, | ||
} | ||
|
||
impl Service { | ||
#[must_use] | ||
pub fn new(user_authorization_repository: Arc<DbUserAuthorizationRepository>) -> Self { | ||
Self { | ||
user_authorization_repository, | ||
} | ||
pub fn new(user_repository: Arc<Box<dyn Repository>>) -> Self { | ||
Self { user_repository } | ||
} | ||
|
||
/// Checks if the user has the right privileges to perform the requested action. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if unable to get the user | ||
/// authorization data from the database or if the user | ||
/// does not have the right privileges to perform the action. | ||
pub async fn authorize_user(&self, user_id: UserId, admin_required: bool) -> Result<(), ServiceError> { | ||
// Checks if the user exists in the database | ||
let authorization_info = self | ||
.user_authorization_repository | ||
.get_user_authorization_from_id(&user_id) | ||
.await?; | ||
|
||
//If admin privilages are required, it checks if the user is an admin | ||
if admin_required { | ||
Self::authorize_admin_user(&authorization_info) | ||
} else { | ||
Ok(()) | ||
/// Will return an error if: | ||
/// | ||
/// - There is not any user with the provided `UserId` (when the user id is some). | ||
/// - The user is not authorized to perform the action. | ||
pub async fn authorize(&self, action: ACTION, maybe_user_id: Option<UserId>) -> Result<(), ServiceError> { | ||
match action { | ||
ACTION::AddCategory | ACTION::DeleteCategory => match maybe_user_id { | ||
Some(user_id) => { | ||
let user = self.get_user(user_id).await?; | ||
|
||
if !user.administrator { | ||
return Err(ServiceError::Unauthorized); | ||
} | ||
|
||
Ok(()) | ||
} | ||
None => Err(ServiceError::Unauthorized), | ||
}, | ||
} | ||
} | ||
|
||
fn authorize_admin_user(user_authorization_info: &UserAuthorization) -> Result<(), ServiceError> { | ||
if user_authorization_info.administrator { | ||
Ok(()) | ||
} else { | ||
Err(ServiceError::Unauthorized) | ||
} | ||
async fn get_user(&self, user_id: UserId) -> Result<UserCompact, ServiceError> { | ||
self.user_repository.get_compact(&user_id).await | ||
} | ||
} | ||
|
||
pub struct DbUserAuthorizationRepository { | ||
database: Arc<Box<dyn Database>>, | ||
} | ||
|
||
impl DbUserAuthorizationRepository { | ||
#[must_use] | ||
pub fn new(database: Arc<Box<dyn Database>>) -> Self { | ||
Self { database } | ||
} | ||
|
||
/// Get user authorization data from user id. | ||
/// | ||
/// # Errors | ||
/// | ||
/// This function will return an error if unable to get the user | ||
/// authorization data from the database. | ||
pub async fn get_user_authorization_from_id(&self, user_id: &UserId) -> Result<UserAuthorization, Error> { | ||
self.database.get_user_authorization_from_id(*user_id).await | ||
} | ||
} | ||
#[cfg(test)] | ||
mod tests {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.