From 3a417bff0a03a1ebe691733956643520d05d2528 Mon Sep 17 00:00:00 2001 From: CrabeDeFrance Date: Mon, 6 Nov 2023 23:48:06 +0100 Subject: [PATCH] Do not show login or register button if they are not allowed --- crates/alexandrie/src/config/frontend/auth/mod.rs | 14 ++++++++++++++ crates/alexandrie/src/frontend/account/login.rs | 3 +++ crates/alexandrie/src/frontend/index.rs | 3 +++ crates/alexandrie/src/frontend/krate.rs | 3 +++ crates/alexandrie/src/frontend/last_updated.rs | 3 +++ crates/alexandrie/src/frontend/most_downloaded.rs | 3 +++ crates/alexandrie/src/frontend/search.rs | 3 +++ templates/partials/navbar.hbs | 4 ++++ 8 files changed, 36 insertions(+) diff --git a/crates/alexandrie/src/config/frontend/auth/mod.rs b/crates/alexandrie/src/config/frontend/auth/mod.rs index b2593d31..b5f5374a 100644 --- a/crates/alexandrie/src/config/frontend/auth/mod.rs +++ b/crates/alexandrie/src/config/frontend/auth/mod.rs @@ -28,6 +28,20 @@ pub struct AuthConfig { pub gitlab: GitlabAuthConfig, } +impl AuthConfig { + /// return true if at least one configuration is enabled + pub fn enabled(&self) -> bool { + self.local.enabled || self.github.enabled || self.gitlab.enabled + } + + /// return true if at least one registration is allowed + pub fn allow_registration(&self) -> bool { + (self.local.enabled && self.local.allow_registration) + || (self.github.enabled && self.github.allow_registration) + || (self.gitlab.enabled && self.gitlab.allow_registration) + } +} + /// The authentication state, having things like OAuth clients for external authentication. pub struct AuthState { /// The authentication state for the "local" strategy, if enabled. diff --git a/crates/alexandrie/src/frontend/account/login.rs b/crates/alexandrie/src/frontend/account/login.rs index 93fb698c..8726ff0e 100644 --- a/crates/alexandrie/src/frontend/account/login.rs +++ b/crates/alexandrie/src/frontend/account/login.rs @@ -57,7 +57,10 @@ pub(crate) async fn get( let local_registration_enabled = auth.local.allow_registration; let has_separator = local_enabled && (github_enabled || gitlab_enabled); + let auth = &state.frontend.config.auth; let context = json!({ + "auth_disabled": !auth.enabled(), + "registration_disabled": !auth.allow_registration(), "instance": &state.frontend.config, "flash": flash_message, "local_enabled": local_enabled, diff --git a/crates/alexandrie/src/frontend/index.rs b/crates/alexandrie/src/frontend/index.rs index b67349c8..fbb4f422 100644 --- a/crates/alexandrie/src/frontend/index.rs +++ b/crates/alexandrie/src/frontend/index.rs @@ -55,8 +55,11 @@ pub(crate) async fn get( .limit(10) .load(conn)?; + let auth = &state.frontend.config.auth; let engine = &state.frontend.handlebars; let context = json!({ + "auth_disabled": !auth.enabled(), + "registration_disabled": !auth.allow_registration(), "user": user.map(|it| it.into_inner()), "instance": &state.frontend.config, "total_downloads": helpers::humanize_number(total_downloads), diff --git a/crates/alexandrie/src/frontend/krate.rs b/crates/alexandrie/src/frontend/krate.rs index 61db7d54..72ece800 100644 --- a/crates/alexandrie/src/frontend/krate.rs +++ b/crates/alexandrie/src/frontend/krate.rs @@ -334,8 +334,11 @@ pub(crate) async fn get( chrono::NaiveDateTime::parse_from_str(crate_desc.updated_at.as_str(), DATETIME_FORMAT) .unwrap(); + let auth = &state.frontend.config.auth; let engine = &state.frontend.handlebars; let context = json!({ + "auth_disabled": !auth.enabled(), + "registration_disabled": !auth.allow_registration(), "user": user.map(|it| it.into_inner()), "instance": &state.frontend.config, "crate": { diff --git a/crates/alexandrie/src/frontend/last_updated.rs b/crates/alexandrie/src/frontend/last_updated.rs index bd340c3e..45787eca 100644 --- a/crates/alexandrie/src/frontend/last_updated.rs +++ b/crates/alexandrie/src/frontend/last_updated.rs @@ -83,8 +83,11 @@ pub(crate) async fn get( None }; + let auth = &state.frontend.config.auth; let engine = &state.frontend.handlebars; let context = json!({ + "auth_disabled": !auth.enabled(), + "registration_disabled": !auth.allow_registration(), "user": user.map(|it| it.into_inner()), "instance": &state.frontend.config, "total_results": total_results, diff --git a/crates/alexandrie/src/frontend/most_downloaded.rs b/crates/alexandrie/src/frontend/most_downloaded.rs index 2f0cfa79..5a21b0f0 100644 --- a/crates/alexandrie/src/frontend/most_downloaded.rs +++ b/crates/alexandrie/src/frontend/most_downloaded.rs @@ -83,8 +83,11 @@ pub(crate) async fn get( None }; + let auth = &state.frontend.config.auth; let engine = &state.frontend.handlebars; let context = json!({ + "auth_disabled": !auth.enabled(), + "registration_disabled": !auth.allow_registration(), "user": user.map(|it| it.into_inner()), "instance": &state.frontend.config, "total_results": total_results, diff --git a/crates/alexandrie/src/frontend/search.rs b/crates/alexandrie/src/frontend/search.rs index 338f1040..23ba2c0f 100644 --- a/crates/alexandrie/src/frontend/search.rs +++ b/crates/alexandrie/src/frontend/search.rs @@ -96,8 +96,11 @@ pub(crate) async fn get( None }; + let auth = &state.frontend.config.auth; let engine = &state.frontend.handlebars; let context = json!({ + "auth_disabled": !auth.enabled(), + "registration_disabled": !auth.allow_registration(), "user": user.map(|it| it.into_inner()), "instance": &state.frontend.config, "searched_text": searched_text, diff --git a/templates/partials/navbar.hbs b/templates/partials/navbar.hbs index 7b30b3dd..1cee87b2 100644 --- a/templates/partials/navbar.hbs +++ b/templates/partials/navbar.hbs @@ -308,15 +308,19 @@ + {{#unless auth_disabled}} + {{/unless}}