mirror of
https://github.com/zadam/trilium.git
synced 2025-11-06 21:36:05 +01:00
WIP: 1st step at verifying shareRoot is set
This commit is contained in:
@@ -2,44 +2,91 @@ import OptionsWidget from "../options_widget.js";
|
|||||||
import options from "../../../../services/options.js";
|
import options from "../../../../services/options.js";
|
||||||
import { t } from "../../../../services/i18n.js";
|
import { t } from "../../../../services/i18n.js";
|
||||||
import type { OptionMap, OptionNames } from "../../../../../../services/options_interface.js";
|
import type { OptionMap, OptionNames } from "../../../../../../services/options_interface.js";
|
||||||
|
import searchService from "../../../../services/search.js";
|
||||||
|
|
||||||
const TPL = `
|
const TPL = `
|
||||||
<div class="options-section">
|
<div class="options-section">
|
||||||
<h4>${t("share.title")}</h4>
|
|
||||||
|
|
||||||
<label class="tn-checkbox">
|
<label class="tn-checkbox">
|
||||||
<input class="form-check-input" type="checkbox" name="redirectBareDomain" value="true">
|
<input type="checkbox" name="redirectBareDomain">
|
||||||
${t("share.redirect_bare_domain")}
|
<span>${t("share.redirect_bare_domain")}</span>
|
||||||
</label>
|
</label>
|
||||||
<p class="form-text">${t("share.redirect_bare_domain_description")}</p>
|
<p class="form-text">${t("share.redirect_bare_domain_description")}</p>
|
||||||
|
|
||||||
|
<div class="share-root-check mt-2 mb-2" style="display: none;">
|
||||||
|
<button class="btn btn-sm btn-secondary check-share-root">${t("share.check_share_root")}</button>
|
||||||
|
<div class="share-root-status form-text mt-2"></div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<label class="tn-checkbox">
|
<label class="tn-checkbox">
|
||||||
<input class="form-check-input" type="checkbox" name="showLoginInShareTheme" value="true">
|
<input type="checkbox" name="shareSubtree">
|
||||||
${t("share.show_login_link")}
|
<span>${t("share.share_subtree")}</span>
|
||||||
</label>
|
</label>
|
||||||
<p class="form-text">${t("share.show_login_link_description")}</p>
|
|
||||||
</div>`;
|
</div>`;
|
||||||
|
|
||||||
export default class ShareSettingsOptions extends OptionsWidget {
|
export default class ShareSettingsOptions extends OptionsWidget {
|
||||||
|
private $shareRootCheck!: JQuery<HTMLElement>;
|
||||||
|
private $shareRootStatus!: JQuery<HTMLElement>;
|
||||||
|
|
||||||
doRender() {
|
doRender() {
|
||||||
this.$widget = $(TPL);
|
this.$widget = $(TPL);
|
||||||
this.contentSized();
|
this.contentSized();
|
||||||
|
|
||||||
|
this.$shareRootCheck = this.$widget.find('.share-root-check');
|
||||||
|
this.$shareRootStatus = this.$widget.find('.share-root-status');
|
||||||
|
|
||||||
// Add change handlers for both checkboxes
|
// Add change handlers for both checkboxes
|
||||||
this.$widget.find('input[type="checkbox"]').on("change", () => this.save());
|
this.$widget.find('input[type="checkbox"]').on("change", (e: JQuery.ChangeEvent) => {
|
||||||
|
this.save();
|
||||||
|
|
||||||
|
// Show/hide share root status section based on redirectBareDomain checkbox
|
||||||
|
const target = e.target as HTMLInputElement;
|
||||||
|
if (target.name === 'redirectBareDomain') {
|
||||||
|
this.$shareRootCheck.toggle(target.checked);
|
||||||
|
if (target.checked) {
|
||||||
|
this.checkShareRoot();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Add click handler for check share root button
|
||||||
|
this.$widget.find('.check-share-root').on("click", () => this.checkShareRoot());
|
||||||
}
|
}
|
||||||
|
|
||||||
async optionsLoaded(options: OptionMap) {
|
async optionsLoaded(options: OptionMap) {
|
||||||
this.$widget.find('input[name="redirectBareDomain"]').prop("checked", options.redirectBareDomain === "true");
|
const redirectBareDomain = options.redirectBareDomain === "true";
|
||||||
|
this.$widget.find('input[name="redirectBareDomain"]').prop("checked", redirectBareDomain);
|
||||||
|
this.$shareRootCheck.toggle(redirectBareDomain);
|
||||||
|
if (redirectBareDomain) {
|
||||||
|
await this.checkShareRoot();
|
||||||
|
}
|
||||||
|
|
||||||
this.$widget.find('input[name="showLoginInShareTheme"]').prop("checked", options.showLoginInShareTheme === "true");
|
this.$widget.find('input[name="shareSubtree"]').prop("checked", options.shareSubtree === "true");
|
||||||
|
}
|
||||||
|
|
||||||
|
async checkShareRoot() {
|
||||||
|
const shareRootNotes = await searchService.searchNotes("#shareRoot", {
|
||||||
|
includeArchivedNotes: true,
|
||||||
|
ignoreHoistedNote: true
|
||||||
|
});
|
||||||
|
|
||||||
|
if (shareRootNotes.length > 0) {
|
||||||
|
this.$shareRootStatus
|
||||||
|
.removeClass('text-danger')
|
||||||
|
.addClass('text-success')
|
||||||
|
.text(t("share.share_root_found", {noteTitle: shareRootNotes[0].title}));
|
||||||
|
} else {
|
||||||
|
this.$shareRootStatus
|
||||||
|
.removeClass('text-success')
|
||||||
|
.addClass('text-danger')
|
||||||
|
.text(t("share.share_root_not_found"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async save() {
|
async save() {
|
||||||
const redirectBareDomain = this.$widget.find('input[name="redirectBareDomain"]').prop("checked");
|
const redirectBareDomain = this.$widget.find('input[name="redirectBareDomain"]').prop("checked");
|
||||||
await this.updateOption<"redirectBareDomain">("redirectBareDomain", redirectBareDomain.toString());
|
await this.updateOption<"redirectBareDomain">("redirectBareDomain", redirectBareDomain.toString());
|
||||||
|
|
||||||
const showLoginInShareTheme = this.$widget.find('input[name="showLoginInShareTheme"]').prop("checked");
|
const showLoginInShareTheme = this.$widget.find('input[name="shareSubtree"]').prop("checked");
|
||||||
await this.updateOption<"showLoginInShareTheme">("showLoginInShareTheme", showLoginInShareTheme.toString());
|
await this.updateOption<"shareSubtree">("shareSubtree", showLoginInShareTheme.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1660,7 +1660,10 @@
|
|||||||
"redirect_bare_domain": "Redirect bare domain to Share page",
|
"redirect_bare_domain": "Redirect bare domain to Share page",
|
||||||
"redirect_bare_domain_description": "When enabled, accessing the root URL will redirect to the Share page instead of Login",
|
"redirect_bare_domain_description": "When enabled, accessing the root URL will redirect to the Share page instead of Login",
|
||||||
"show_login_link": "Show Login link in Share theme",
|
"show_login_link": "Show Login link in Share theme",
|
||||||
"show_login_link_description": "Add a login link to the Share page footer"
|
"show_login_link_description": "Add a login link to the Share page footer",
|
||||||
|
"check_share_root": "Check Share Root Status",
|
||||||
|
"share_root_found": "Share root found: {{noteTitle}}",
|
||||||
|
"share_root_not_found": "No note with #shareRoot label found. Set up a note with #shareRoot label first."
|
||||||
},
|
},
|
||||||
"time_selector": {
|
"time_selector": {
|
||||||
"invalid_input": "The entered time value is not a valid number."
|
"invalid_input": "The entered time value is not a valid number."
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import passwordEncryptionService from "./encryption/password_encryption.js";
|
|||||||
import config from "./config.js";
|
import config from "./config.js";
|
||||||
import passwordService from "./encryption/password.js";
|
import passwordService from "./encryption/password.js";
|
||||||
import options from "./options.js";
|
import options from "./options.js";
|
||||||
|
import attributes from "./attributes.js";
|
||||||
import type { NextFunction, Request, Response } from "express";
|
import type { NextFunction, Request, Response } from "express";
|
||||||
|
|
||||||
const noAuthentication = config.General && config.General.noAuthentication === true;
|
const noAuthentication = config.General && config.General.noAuthentication === true;
|
||||||
@@ -16,7 +17,15 @@ function checkAuth(req: Request, res: Response, next: NextFunction) {
|
|||||||
if (!sqlInit.isDbInitialized()) {
|
if (!sqlInit.isDbInitialized()) {
|
||||||
res.redirect("setup");
|
res.redirect("setup");
|
||||||
} else if (!req.session.loggedIn && !isElectron && !noAuthentication) {
|
} else if (!req.session.loggedIn && !isElectron && !noAuthentication) {
|
||||||
const redirectToShare = options.getOption("redirectBareDomain") === "true";
|
const redirectToShare = options.getOptionBool("redirectBareDomain");
|
||||||
|
if (redirectToShare) {
|
||||||
|
// Check if any note has the #shareRoot label
|
||||||
|
const shareRootNotes = attributes.getNotesWithLabel("shareRoot");
|
||||||
|
if (shareRootNotes.length === 0) {
|
||||||
|
res.status(404).json({ message: "Share root not found. Please set up a note with #shareRoot label first." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
res.redirect(redirectToShare ? "share" : "login");
|
res.redirect(redirectToShare ? "share" : "login");
|
||||||
} else {
|
} else {
|
||||||
next();
|
next();
|
||||||
|
|||||||
Reference in New Issue
Block a user