Skip to content

Commit 5cc8e74

Browse files
authored
Merge pull request #180 from CrabeDeFrance/feature-hide-login-register-buttons
Hide login and register buttons if they are disabled #179
2 parents a48bd26 + 3a417bf commit 5cc8e74

File tree

8 files changed

+36
-0
lines changed

8 files changed

+36
-0
lines changed

crates/alexandrie/src/config/frontend/auth/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ pub struct AuthConfig {
2828
pub gitlab: GitlabAuthConfig,
2929
}
3030

31+
impl AuthConfig {
32+
/// return true if at least one configuration is enabled
33+
pub fn enabled(&self) -> bool {
34+
self.local.enabled || self.github.enabled || self.gitlab.enabled
35+
}
36+
37+
/// return true if at least one registration is allowed
38+
pub fn allow_registration(&self) -> bool {
39+
(self.local.enabled && self.local.allow_registration)
40+
|| (self.github.enabled && self.github.allow_registration)
41+
|| (self.gitlab.enabled && self.gitlab.allow_registration)
42+
}
43+
}
44+
3145
/// The authentication state, having things like OAuth clients for external authentication.
3246
pub struct AuthState {
3347
/// The authentication state for the "local" strategy, if enabled.

crates/alexandrie/src/frontend/account/login.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ pub(crate) async fn get(
5757
let local_registration_enabled = auth.local.allow_registration;
5858
let has_separator = local_enabled && (github_enabled || gitlab_enabled);
5959

60+
let auth = &state.frontend.config.auth;
6061
let context = json!({
62+
"auth_disabled": !auth.enabled(),
63+
"registration_disabled": !auth.allow_registration(),
6164
"instance": &state.frontend.config,
6265
"flash": flash_message,
6366
"local_enabled": local_enabled,

crates/alexandrie/src/frontend/index.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,11 @@ pub(crate) async fn get(
5555
.limit(10)
5656
.load(conn)?;
5757

58+
let auth = &state.frontend.config.auth;
5859
let engine = &state.frontend.handlebars;
5960
let context = json!({
61+
"auth_disabled": !auth.enabled(),
62+
"registration_disabled": !auth.allow_registration(),
6063
"user": user.map(|it| it.into_inner()),
6164
"instance": &state.frontend.config,
6265
"total_downloads": helpers::humanize_number(total_downloads),

crates/alexandrie/src/frontend/krate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,11 @@ pub(crate) async fn get(
334334
chrono::NaiveDateTime::parse_from_str(crate_desc.updated_at.as_str(), DATETIME_FORMAT)
335335
.unwrap();
336336

337+
let auth = &state.frontend.config.auth;
337338
let engine = &state.frontend.handlebars;
338339
let context = json!({
340+
"auth_disabled": !auth.enabled(),
341+
"registration_disabled": !auth.allow_registration(),
339342
"user": user.map(|it| it.into_inner()),
340343
"instance": &state.frontend.config,
341344
"crate": {

crates/alexandrie/src/frontend/last_updated.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ pub(crate) async fn get(
8383
None
8484
};
8585

86+
let auth = &state.frontend.config.auth;
8687
let engine = &state.frontend.handlebars;
8788
let context = json!({
89+
"auth_disabled": !auth.enabled(),
90+
"registration_disabled": !auth.allow_registration(),
8891
"user": user.map(|it| it.into_inner()),
8992
"instance": &state.frontend.config,
9093
"total_results": total_results,

crates/alexandrie/src/frontend/most_downloaded.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,11 @@ pub(crate) async fn get(
8383
None
8484
};
8585

86+
let auth = &state.frontend.config.auth;
8687
let engine = &state.frontend.handlebars;
8788
let context = json!({
89+
"auth_disabled": !auth.enabled(),
90+
"registration_disabled": !auth.allow_registration(),
8891
"user": user.map(|it| it.into_inner()),
8992
"instance": &state.frontend.config,
9093
"total_results": total_results,

crates/alexandrie/src/frontend/search.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,11 @@ pub(crate) async fn get(
9696
None
9797
};
9898

99+
let auth = &state.frontend.config.auth;
99100
let engine = &state.frontend.handlebars;
100101
let context = json!({
102+
"auth_disabled": !auth.enabled(),
103+
"registration_disabled": !auth.allow_registration(),
101104
"user": user.map(|it| it.into_inner()),
102105
"instance": &state.frontend.config,
103106
"searched_text": searched_text,

templates/partials/navbar.hbs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,19 @@
308308
</div>
309309
</form>
310310
</div>
311+
{{#unless auth_disabled}}
311312
<div class="navbar-login-container">
312313
{{#if user}}
313314
<a href="/account/manage" class="navbar-tag">Manage account</a>
314315
<a href="/account/logout" class="navbar-tag">Logout</a>
315316
{{else}}
316317
<a href="/account/login" class="navbar-tag">Login</a>
318+
{{#unless registration_disabled}}
317319
<a href="/account/register" class="navbar-tag">Register</a>
320+
{{/unless}}
318321
{{/if}}
319322
</div>
323+
{{/unless}}
320324
<div class="navbar-burger-container">
321325
<label class="navbar-burger-icon fas" for="burger-checkbox"></label>
322326
</div>

0 commit comments

Comments
 (0)