Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide login and register buttons if they are disabled #179 #180

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading