Skip to content

Commit

Permalink
Merge pull request #180 from CrabeDeFrance/feature-hide-login-registe…
Browse files Browse the repository at this point in the history
…r-buttons

 Hide login and register buttons if they are disabled #179
  • Loading branch information
Hirevo authored Nov 13, 2023
2 parents a48bd26 + 3a417bf commit 5cc8e74
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 0 deletions.
14 changes: 14 additions & 0 deletions crates/alexandrie/src/config/frontend/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions crates/alexandrie/src/frontend/account/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions crates/alexandrie/src/frontend/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions crates/alexandrie/src/frontend/krate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
3 changes: 3 additions & 0 deletions crates/alexandrie/src/frontend/last_updated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions crates/alexandrie/src/frontend/most_downloaded.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 3 additions & 0 deletions crates/alexandrie/src/frontend/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions templates/partials/navbar.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -308,15 +308,19 @@
</div>
</form>
</div>
{{#unless auth_disabled}}
<div class="navbar-login-container">
{{#if user}}
<a href="/account/manage" class="navbar-tag">Manage account</a>
<a href="/account/logout" class="navbar-tag">Logout</a>
{{else}}
<a href="/account/login" class="navbar-tag">Login</a>
{{#unless registration_disabled}}
<a href="/account/register" class="navbar-tag">Register</a>
{{/unless}}
{{/if}}
</div>
{{/unless}}
<div class="navbar-burger-container">
<label class="navbar-burger-icon fas" for="burger-checkbox"></label>
</div>
Expand Down

0 comments on commit 5cc8e74

Please sign in to comment.