Skip to content

Commit

Permalink
feat: [#615] authorization service implemented for the about service
Browse files Browse the repository at this point in the history
  • Loading branch information
mario-nt committed Aug 3, 2024
1 parent a757c5c commit 6f8de80
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ pub async fn run(configuration: Configuration, api_version: &Version) -> Running
user_authentication_repository.clone(),
));

let about_service = Arc::new(about::Service::new());
let about_service = Arc::new(about::Service::new(authorization_service.clone()));

// Build app container

Expand Down
34 changes: 24 additions & 10 deletions src/services/about.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
//! Templates for "about" static pages.

pub struct Service {}
use std::sync::Arc;

use super::authorization::{self, ACTION};
use crate::errors::ServiceError;

pub struct Service {
authorization_service: Arc<authorization::Service>,
}

impl Service {
#[must_use]
pub fn new() -> Service {
Service {}
pub fn new(authorization_service: Arc<authorization::Service>) -> Service {
Service { authorization_service }
}

pub fn get_about_page(&self) -> String {
r#"
pub async fn get_about_page(&self) -> Result<String, ServiceError> {
self.authorization_service.authorize(ACTION::GetAboutPage, None).await?;

let html = r#"
<html>
<head>
<title>About</title>
Expand All @@ -25,12 +34,15 @@ impl Service {
<a href="./about/license">license</a>
</footer>
</html>
"#
.to_string()
"#;

Ok(html.to_string())
}

pub fn get_license_page(&self) -> String {
r#"
pub async fn get_license_page(&self) -> Result<String, ServiceError> {
self.authorization_service.authorize(ACTION::GetLicensePage, None).await?;

let html = r#"
<html>
<head>
<title>Licensing</title>
Expand All @@ -56,6 +68,8 @@ impl Service {
<a href="../about">about</a>
</footer>
</html>
"#.to_string()
"#;

Ok(html.to_string())
}
}
5 changes: 4 additions & 1 deletion src/services/authorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub enum ACTION {
DeleteTag,
DeleteTorrent,
BanUser,
GetAboutPage,
GetLicensePage,
}

pub struct Service {
Expand Down Expand Up @@ -175,7 +177,8 @@ impl CasbinConfiguration {
admin, DeleteTag
admin, DeleteTorrent
admin, BanUser
guest, GetAboutPage
guest, GetLicensePage
",
),
}
Expand Down
16 changes: 10 additions & 6 deletions src/web/api/server/v1/contexts/about/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ use crate::common::AppData;

#[allow(clippy::unused_async)]
pub async fn about_page_handler(State(app_data): State<Arc<AppData>>) -> Response {
let html = app_data.about_service.get_about_page();

(StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response()
match app_data.about_service.get_about_page().await {
Ok(html) => (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response(),
Err(error) => error.into_response(),
}
}

#[allow(clippy::unused_async)]
pub async fn license_page_handler(State(app_data): State<Arc<AppData>>) -> Response {
let html = app_data.about_service.get_license_page();

(StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html).into_response()
match app_data.about_service.get_license_page().await {
Ok(html) => (StatusCode::OK, [(header::CONTENT_TYPE, "text/html; charset=utf-8")], html)
.into_response()
.into_response(),
Err(error) => error.into_response(),
}
}

0 comments on commit 6f8de80

Please sign in to comment.