Skip to content

Commit 2880f7c

Browse files
committed
feat: [#615] added authorization layer for get public settings method of the settings service
1 parent 530f37a commit 2880f7c

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

src/services/authorization.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ pub enum ACTION {
4444
GetAboutPage,
4545
GetLicensePage,
4646
GetImageByUrl,
47+
GetPublicSettings,
4748
}
4849

4950
pub struct Service {
@@ -174,18 +175,20 @@ impl CasbinConfiguration {
174175
"
175176
admin, AddCategory
176177
admin, DeleteCategory
177-
admin, GetSettings
178+
admin, GetPublicSettings
178179
admin, GetSettingsSecret
179180
admin, AddTag
180181
admin, DeleteTag
181182
admin, DeleteTorrent
182183
admin, BanUser
183184
admin, GetImageByUrl
184185
registered, GetImageByUrl
186+
registered, GetPublicSettings
185187
guest, GetCategories
186188
guest, GetTags
187189
guest, GetAboutPage
188190
guest, GetLicensePage
191+
guest, GetPublicSettings
189192
",
190193
),
191194
}

src/services/settings.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,13 @@ impl Service {
6262
/// # Errors
6363
///
6464
/// It returns an error if the user does not have the required permissions.
65-
pub async fn get_public(&self) -> ConfigurationPublic {
65+
pub async fn get_public(&self, opt_user_id: Option<UserId>) -> Result<ConfigurationPublic, ServiceError> {
66+
self.authorization_service
67+
.authorize(ACTION::GetPublicSettings, opt_user_id)
68+
.await?;
69+
6670
let settings_lock = self.configuration.get_all().await;
67-
extract_public_settings(&settings_lock)
71+
Ok(extract_public_settings(&settings_lock))
6872
}
6973

7074
/// It gets the site name from the settings.

src/web/api/server/v1/contexts/settings/handlers.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use axum::extract::State;
66
use axum::response::{IntoResponse, Json, Response};
77

88
use crate::common::AppData;
9+
use crate::web::api::server::v1::extractors::optional_user_id::ExtractOptionalLoggedInUser;
910
use crate::web::api::server::v1::extractors::user_id::ExtractLoggedInUser;
1011
use crate::web::api::server::v1::responses;
1112

@@ -30,10 +31,14 @@ pub async fn get_all_handler(
3031

3132
/// Get public Settings.
3233
#[allow(clippy::unused_async)]
33-
pub async fn get_public_handler(State(app_data): State<Arc<AppData>>) -> Response {
34-
let public_settings = app_data.settings_service.get_public().await;
35-
36-
Json(responses::OkResponseData { data: public_settings }).into_response()
34+
pub async fn get_public_handler(
35+
State(app_data): State<Arc<AppData>>,
36+
ExtractOptionalLoggedInUser(opt_user_id): ExtractOptionalLoggedInUser,
37+
) -> Response {
38+
match app_data.settings_service.get_public(opt_user_id).await {
39+
Ok(public_settings) => Json(responses::OkResponseData { data: public_settings }).into_response(),
40+
Err(error) => error.into_response(),
41+
}
3742
}
3843

3944
/// Get website name.

0 commit comments

Comments
 (0)