Skip to content

Commit

Permalink
refactor: [torrust#448] fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Mar 20, 2024
1 parent 37e3eb9 commit 51c9757
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::common::AppData;
use crate::config::Configuration;
use crate::databases::database;
use crate::services::authentication::{DbUserAuthenticationRepository, JsonWebToken, Service};
use crate::services::authorization::{AuthorizationService, DbUserAuthorizationRepository};
use crate::services::authorization::{AuthorizeService, DbUserAuthorizationRepository};
use crate::services::category::{self, DbCategoryRepository};
use crate::services::tag::{self, DbTagRepository};
use crate::services::torrent::{
Expand Down Expand Up @@ -85,7 +85,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
let banned_user_list = Arc::new(DbBannedUserList::new(database.clone()));

// Services
let authorization_service = Arc::new(AuthorizationService::new(user_authorization_repository.clone()));
let authorization_service = Arc::new(AuthorizeService::new(user_authorization_repository.clone()));
let tracker_service = Arc::new(tracker::service::Service::new(configuration.clone(), database.clone()).await);
let tracker_statistics_importer =
Arc::new(StatisticsImporter::new(configuration.clone(), tracker_service.clone(), database.clone()).await);
Expand Down
6 changes: 3 additions & 3 deletions src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::cache::image::manager::ImageCacheService;
use crate::config::Configuration;
use crate::databases::database::Database;
use crate::services::authentication::{DbUserAuthenticationRepository, JsonWebToken, Service};
use crate::services::authorization::{AuthorizationService, DbUserAuthorizationRepository};
use crate::services::authorization::{AuthorizeService, DbUserAuthorizationRepository};
use crate::services::category::{self, DbCategoryRepository};
use crate::services::tag::{self, DbTagRepository};
use crate::services::torrent::{
Expand All @@ -24,7 +24,7 @@ pub struct AppData {
pub json_web_token: Arc<JsonWebToken>,
pub auth: Arc<Authentication>,
pub authentication_service: Arc<Service>,
pub authorization_service: Arc<AuthorizationService>,
pub authorization_service: Arc<AuthorizeService>,
pub tracker_service: Arc<tracker::service::Service>,
pub tracker_statistics_importer: Arc<StatisticsImporter>,
pub mailer: Arc<mailer::Service>,
Expand Down Expand Up @@ -62,7 +62,7 @@ impl AppData {
json_web_token: Arc<JsonWebToken>,
auth: Arc<Authentication>,
authentication_service: Arc<Service>,
authorization_service: Arc<AuthorizationService>,
authorization_service: Arc<AuthorizeService>,
tracker_service: Arc<tracker::service::Service>,
tracker_statistics_importer: Arc<StatisticsImporter>,
mailer: Arc<mailer::Service>,
Expand Down
16 changes: 12 additions & 4 deletions src/services/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,25 @@ use crate::databases::database::{Database, Error};
use crate::errors::ServiceError;
use crate::models::user::{UserAuthorization, UserId};

pub struct AuthorizationService {
pub struct AuthorizeService {
user_authorization_repository: Arc<DbUserAuthorizationRepository>,
}

impl AuthorizationService {
impl AuthorizeService {
#[must_use]
pub fn new(user_authorization_repository: Arc<DbUserAuthorizationRepository>) -> Self {
Self {
user_authorization_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
Expand All @@ -24,13 +32,13 @@ impl AuthorizationService {

//If admin privilages are required, it checks if the user is an admin
if admin_required {
return self.authorize_admin_user(authorization_info).await;
self.authorize_admin_user(authorization_info)
} else {
Ok(())
}
}

async fn authorize_admin_user(&self, user_authorization_info: UserAuthorization) -> Result<(), ServiceError> {
fn authorize_admin_user(&self, user_authorization_info: UserAuthorization) -> Result<(), ServiceError> {
if user_authorization_info.administrator {
Ok(())
} else {
Expand Down
6 changes: 3 additions & 3 deletions src/services/category.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! Category service.
use std::sync::Arc;

use super::authorization::AuthorizationService;
use super::authorization::AuthorizeService;
use crate::databases::database::{Category, Database, Error as DatabaseError};
use crate::errors::ServiceError;
use crate::models::category::CategoryId;
use crate::models::user::UserId;

pub struct Service {
category_repository: Arc<DbCategoryRepository>,
authorization_service: Arc<AuthorizationService>,
authorization_service: Arc<AuthorizeService>,
}

impl Service {
#[must_use]
pub fn new(category_repository: Arc<DbCategoryRepository>, authorization_service: Arc<AuthorizationService>) -> Service {
pub fn new(category_repository: Arc<DbCategoryRepository>, authorization_service: Arc<AuthorizeService>) -> Service {
Service {
category_repository,
authorization_service,
Expand Down
6 changes: 3 additions & 3 deletions src/services/settings.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
//! Settings service.
use std::sync::Arc;

use super::authorization::AuthorizationService;
use super::authorization::AuthorizeService;
use crate::config::{Configuration, ConfigurationPublic, TorrustIndex};
use crate::errors::ServiceError;
use crate::models::user::UserId;

pub struct Service {
configuration: Arc<Configuration>,
authorization_service: Arc<AuthorizationService>,
authorization_service: Arc<AuthorizeService>,
}

impl Service {
#[must_use]
pub fn new(configuration: Arc<Configuration>, authorization_service: Arc<AuthorizationService>) -> Service {
pub fn new(configuration: Arc<Configuration>, authorization_service: Arc<AuthorizeService>) -> Service {
Service {
configuration,
authorization_service,
Expand Down
6 changes: 3 additions & 3 deletions src/services/tag.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! Tag service.
use std::sync::Arc;

use super::authorization::AuthorizationService;
use super::authorization::AuthorizeService;
use crate::databases::database::{Database, Error as DatabaseError, Error};
use crate::errors::ServiceError;
use crate::models::torrent_tag::{TagId, TorrentTag};
use crate::models::user::UserId;

pub struct Service {
tag_repository: Arc<DbTagRepository>,
authorization_service: Arc<AuthorizationService>,
authorization_service: Arc<AuthorizeService>,
}

impl Service {
#[must_use]
pub fn new(tag_repository: Arc<DbTagRepository>, authorization_service: Arc<AuthorizationService>) -> Service {
pub fn new(tag_repository: Arc<DbTagRepository>, authorization_service: Arc<AuthorizeService>) -> Service {
Service {
tag_repository,
authorization_service,
Expand Down

0 comments on commit 51c9757

Please sign in to comment.