torrust_index_backend/services/
settings.rs

1//! Settings service.
2use std::sync::Arc;
3
4use super::user::DbUserRepository;
5use crate::config::{Configuration, ConfigurationPublic, TorrustBackend};
6use crate::errors::ServiceError;
7use crate::models::user::UserId;
8
9pub struct Service {
10    configuration: Arc<Configuration>,
11    user_repository: Arc<DbUserRepository>,
12}
13
14impl Service {
15    #[must_use]
16    pub fn new(configuration: Arc<Configuration>, user_repository: Arc<DbUserRepository>) -> Service {
17        Service {
18            configuration,
19            user_repository,
20        }
21    }
22
23    /// It gets all the settings.
24    ///
25    /// # Errors
26    ///
27    /// It returns an error if the user does not have the required permissions.
28    pub async fn get_all(&self, user_id: &UserId) -> Result<TorrustBackend, ServiceError> {
29        let user = self.user_repository.get_compact(user_id).await?;
30
31        // Check if user is administrator
32        // todo: extract authorization service
33        if !user.administrator {
34            return Err(ServiceError::Unauthorized);
35        }
36
37        Ok(self.configuration.get_all().await)
38    }
39
40    /// It gets only the public settings.
41    ///
42    /// # Errors
43    ///
44    /// It returns an error if the user does not have the required permissions.
45    pub async fn get_public(&self) -> ConfigurationPublic {
46        self.configuration.get_public().await
47    }
48
49    /// It gets the site name from the settings.
50    ///
51    /// # Errors
52    ///
53    /// It returns an error if the user does not have the required permissions.
54    pub async fn get_site_name(&self) -> String {
55        self.configuration.get_site_name().await
56    }
57}