diff --git a/js/authModals.js b/js/authModals.js index b28c408..350d249 100644 --- a/js/authModals.js +++ b/js/authModals.js @@ -55,12 +55,23 @@ export function openTOTPLoginModal() { }); // Toggle between TOTP and Recovery - document.getElementById("toggleRecovery").addEventListener("click", e => { + document.getElementById("toggleRecovery").addEventListener("click", function (e) { e.preventDefault(); - document.getElementById("totpSection").style.display = - document.getElementById("recoverySection").style.display === "none" ? "none" : "block"; - document.getElementById("recoverySection").style.display = - document.getElementById("recoverySection").style.display === "none" ? "block" : "none"; + const totpSection = document.getElementById("totpSection"); + const recoverySection = document.getElementById("recoverySection"); + const toggleLink = this; + + if (recoverySection.style.display === "none") { + // Switch to recovery + totpSection.style.display = "none"; + recoverySection.style.display = "block"; + toggleLink.textContent = "Use TOTP Code instead"; + } else { + // Switch back to TOTP + recoverySection.style.display = "none"; + totpSection.style.display = "block"; + toggleLink.textContent = "Use Recovery Code instead"; + } }); // Recovery submission @@ -79,18 +90,18 @@ export function openTOTPLoginModal() { }, body: JSON.stringify({ recovery_code: recoveryCode }) }) - .then(res => res.json()) - .then(json => { - if (json.status === "ok") { - // recovery succeeded → finalize login - window.location.href = "index.html"; - } else { - showToast(json.message || "Recovery code verification failed"); - } - }) - .catch(() => { - showToast("Error verifying recovery code."); - }); + .then(res => res.json()) + .then(json => { + if (json.status === "ok") { + // recovery succeeded → finalize login + window.location.href = "index.html"; + } else { + showToast(json.message || "Recovery code verification failed"); + } + }) + .catch(() => { + showToast("Error verifying recovery code."); + }); }); // TOTP submission @@ -108,23 +119,23 @@ export function openTOTPLoginModal() { }, body: JSON.stringify({ totp_code: code }) }) - .then(res => res.json()) - .then(json => { - if (json.status === "ok") { - window.location.href = "index.html"; - } else { - showToast(json.message || "TOTP verification failed"); + .then(res => res.json()) + .then(json => { + if (json.status === "ok") { + window.location.href = "index.html"; + } else { + showToast(json.message || "TOTP verification failed"); + this.value = ""; + totpLoginModal.style.display = "flex"; + totpInput.focus(); + } + }) + .catch(() => { + showToast("TOTP verification failed"); this.value = ""; totpLoginModal.style.display = "flex"; totpInput.focus(); - } - }) - .catch(() => { - showToast("TOTP verification failed"); - this.value = ""; - totpLoginModal.style.display = "flex"; - totpInput.focus(); - }); + }); } }); } else { @@ -158,9 +169,9 @@ export function openUserPanel() { transition: none; `; if (!userPanelModal) { - userPanelModal = document.createElement("div"); - userPanelModal.id = "userPanelModal"; - userPanelModal.style.cssText = ` + userPanelModal = document.createElement("div"); + userPanelModal.id = "userPanelModal"; + userPanelModal.style.cssText = ` position: fixed; top: 0; left: 0; @@ -172,7 +183,7 @@ export function openUserPanel() { align-items: center; z-index: 3000; `; - userPanelModal.innerHTML = ` + userPanelModal.innerHTML = `
`; - document.body.appendChild(userPanelModal); - document.getElementById("closeUserPanel").addEventListener("click", () => { - userPanelModal.style.display = "none"; - }); - document.getElementById("openChangePasswordModalBtn").addEventListener("click", () => { - document.getElementById("changePasswordModal").style.display = "block"; - }); - const totpCheckbox = document.getElementById("userTOTPEnabled"); - totpCheckbox.checked = localStorage.getItem("userTOTPEnabled") === "true"; - totpCheckbox.addEventListener("change", function () { - localStorage.setItem("userTOTPEnabled", this.checked ? "true" : "false"); - const enabled = this.checked; - fetch("updateUserPanel.php", { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json", - "X-CSRF-Token": window.csrfToken - }, - body: JSON.stringify({ totp_enabled: enabled }) - }) - .then(r => r.json()) - .then(result => { - if (!result.success) { - showToast("Error updating TOTP setting: " + result.error); - } else if (enabled) { - openTOTPModal(); - } - }) - .catch(() => { showToast("Error updating TOTP setting."); }); - }); + document.body.appendChild(userPanelModal); + document.getElementById("closeUserPanel").addEventListener("click", () => { + userPanelModal.style.display = "none"; + }); + document.getElementById("openChangePasswordModalBtn").addEventListener("click", () => { + document.getElementById("changePasswordModal").style.display = "block"; + }); + const totpCheckbox = document.getElementById("userTOTPEnabled"); + totpCheckbox.checked = localStorage.getItem("userTOTPEnabled") === "true"; + totpCheckbox.addEventListener("change", function () { + localStorage.setItem("userTOTPEnabled", this.checked ? "true" : "false"); + const enabled = this.checked; + fetch("updateUserPanel.php", { + method: "POST", + credentials: "include", + headers: { + "Content-Type": "application/json", + "X-CSRF-Token": window.csrfToken + }, + body: JSON.stringify({ totp_enabled: enabled }) + }) + .then(r => r.json()) + .then(result => { + if (!result.success) { + showToast("Error updating TOTP setting: " + result.error); + } else if (enabled) { + openTOTPModal(); + } + }) + .catch(() => { showToast("Error updating TOTP setting."); }); + }); } else { - userPanelModal.style.backgroundColor = overlayBackground; - const modalContent = userPanelModal.querySelector(".modal-content"); - modalContent.style.background = isDarkMode ? "#2c2c2c" : "#fff"; - modalContent.style.color = isDarkMode ? "#e0e0e0" : "#000"; - modalContent.style.border = isDarkMode ? "1px solid #444" : "1px solid #ccc"; + userPanelModal.style.backgroundColor = overlayBackground; + const modalContent = userPanelModal.querySelector(".modal-content"); + modalContent.style.background = isDarkMode ? "#2c2c2c" : "#fff"; + modalContent.style.color = isDarkMode ? "#e0e0e0" : "#000"; + modalContent.style.border = isDarkMode ? "1px solid #444" : "1px solid #ccc"; } userPanelModal.style.display = "flex"; } @@ -251,7 +262,7 @@ function showRecoveryCodeModal(recoveryCode) { `; document.body.appendChild(recoveryModal); - + document.getElementById("closeRecoveryModal").addEventListener("click", () => { recoveryModal.remove(); }); @@ -299,11 +310,11 @@ export function openTOTPModal() { `; document.body.appendChild(totpModal); - + document.getElementById("closeTOTPModal").addEventListener("click", () => { closeTOTPModal(true); }); - + document.getElementById("confirmTOTPBtn").addEventListener("click", function () { const code = document.getElementById("totpConfirmInput").value.trim(); if (code.length !== 6) { @@ -313,41 +324,41 @@ export function openTOTPModal() { fetch("totp_verify.php", { method: "POST", credentials: "include", - headers: { + headers: { "Content-Type": "application/json", "X-CSRF-Token": window.csrfToken }, body: JSON.stringify({ totp_code: code }) }) - .then(r => r.json()) - .then(result => { - if (result.status === 'ok') { - showToast("TOTP successfully enabled."); - // After successful TOTP verification, fetch the recovery code - fetch("totp_saveCode.php", { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json", - "X-CSRF-Token": window.csrfToken - } - }) - .then(r => r.json()) - .then(data => { - if (data.status === 'ok' && data.recoveryCode) { - // Show the recovery code in a secure modal - showRecoveryCodeModal(data.recoveryCode); - } else { - showToast("Error generating recovery code: " + (data.message || "Unknown error.")); - } - }) - .catch(() => { showToast("Error generating recovery code."); }); - closeTOTPModal(false); - } else { - showToast("TOTP verification failed: " + (result.message || "Invalid code.")); - } - }) - .catch(() => { showToast("Error verifying TOTP code."); }); + .then(r => r.json()) + .then(result => { + if (result.status === 'ok') { + showToast("TOTP successfully enabled."); + // After successful TOTP verification, fetch the recovery code + fetch("totp_saveCode.php", { + method: "POST", + credentials: "include", + headers: { + "Content-Type": "application/json", + "X-CSRF-Token": window.csrfToken + } + }) + .then(r => r.json()) + .then(data => { + if (data.status === 'ok' && data.recoveryCode) { + // Show the recovery code in a secure modal + showRecoveryCodeModal(data.recoveryCode); + } else { + showToast("Error generating recovery code: " + (data.message || "Unknown error.")); + } + }) + .catch(() => { showToast("Error generating recovery code."); }); + closeTOTPModal(false); + } else { + showToast("TOTP verification failed: " + (result.message || "Invalid code.")); + } + }) + .catch(() => { showToast("Error verifying TOTP code."); }); }); // Focus the input and attach enter key listener @@ -366,7 +377,7 @@ export function openTOTPModal() { const modalContent = totpModal.querySelector(".modal-content"); modalContent.style.background = isDarkMode ? "#2c2c2c" : "#fff"; modalContent.style.color = isDarkMode ? "#e0e0e0" : "#000"; - + // Focus the input and attach enter key listener const totpConfirmInput = document.getElementById("totpConfirmInput"); if (totpConfirmInput) { @@ -382,44 +393,44 @@ export function openTOTPModal() { // Updated closeTOTPModal function with a disable parameter export function closeTOTPModal(disable = true) { - const totpModal = document.getElementById("totpModal"); - if (totpModal) totpModal.style.display = "none"; - - if (disable) { - // Uncheck the Enable TOTP checkbox - const totpCheckbox = document.getElementById("userTOTPEnabled"); - if (totpCheckbox) { - totpCheckbox.checked = false; - localStorage.setItem("userTOTPEnabled", "false"); + const totpModal = document.getElementById("totpModal"); + if (totpModal) totpModal.style.display = "none"; + + if (disable) { + // Uncheck the Enable TOTP checkbox + const totpCheckbox = document.getElementById("userTOTPEnabled"); + if (totpCheckbox) { + totpCheckbox.checked = false; + localStorage.setItem("userTOTPEnabled", "false"); + } + // Call endpoint to remove the TOTP secret from the user's record + fetch("totp_disable.php", { + method: "POST", + credentials: "include", + headers: { + "Content-Type": "application/json", + "X-CSRF-Token": window.csrfToken } - // Call endpoint to remove the TOTP secret from the user's record - fetch("totp_disable.php", { - method: "POST", - credentials: "include", - headers: { - "Content-Type": "application/json", - "X-CSRF-Token": window.csrfToken + }) + .then(r => r.json()) + .then(result => { + if (!result.success) { + showToast("Error disabling TOTP setting: " + result.error); } }) - .then(r => r.json()) - .then(result => { - if (!result.success) { - showToast("Error disabling TOTP setting: " + result.error); - } - }) - .catch(() => { showToast("Error disabling TOTP setting."); }); - } + .catch(() => { showToast("Error disabling TOTP setting."); }); } +} export function openAdminPanel() { - fetch("getConfig.php", { credentials: "include" }) - .then(response => response.json()) - .then(config => { - if (config.oidc) Object.assign(window.currentOIDCConfig, config.oidc); - if (config.globalOtpauthUrl) window.currentOIDCConfig.globalOtpauthUrl = config.globalOtpauthUrl; - const isDarkMode = document.body.classList.contains("dark-mode"); - const overlayBackground = isDarkMode ? "rgba(0,0,0,0.7)" : "rgba(0,0,0,0.3)"; - const modalContentStyles = ` + fetch("getConfig.php", { credentials: "include" }) + .then(response => response.json()) + .then(config => { + if (config.oidc) Object.assign(window.currentOIDCConfig, config.oidc); + if (config.globalOtpauthUrl) window.currentOIDCConfig.globalOtpauthUrl = config.globalOtpauthUrl; + const isDarkMode = document.body.classList.contains("dark-mode"); + const overlayBackground = isDarkMode ? "rgba(0,0,0,0.7)" : "rgba(0,0,0,0.3)"; + const modalContentStyles = ` background: ${isDarkMode ? "#2c2c2c" : "#fff"}; color: ${isDarkMode ? "#e0e0e0" : "#000"}; padding: 20px; @@ -431,12 +442,12 @@ export function openAdminPanel() { max-height: 90vh; border: ${isDarkMode ? "1px solid #444" : "1px solid #ccc"}; `; - let adminModal = document.getElementById("adminPanelModal"); + let adminModal = document.getElementById("adminPanelModal"); - if (!adminModal) { - adminModal = document.createElement("div"); - adminModal.id = "adminPanelModal"; - adminModal.style.cssText = ` + if (!adminModal) { + adminModal = document.createElement("div"); + adminModal.id = "adminPanelModal"; + adminModal.style.cssText = ` position: fixed; top: 0; left: 0; @@ -448,8 +459,8 @@ export function openAdminPanel() { align-items: center; z-index: 3000; `; - // Added a version number next to "Admin Panel" - adminModal.innerHTML = ` + // Added a version number next to "Admin Panel" + adminModal.innerHTML = ` `; - document.body.appendChild(adminModal); + document.body.appendChild(adminModal); - document.getElementById("closeAdminPanel").addEventListener("click", closeAdminPanel); - adminModal.addEventListener("click", (e) => { - if (e.target === adminModal) closeAdminPanel(); - }); - document.getElementById("cancelAdminSettings").addEventListener("click", closeAdminPanel); - document.getElementById("adminOpenAddUser").addEventListener("click", () => { - toggleVisibility("addUserModal", true); - document.getElementById("newUsername").focus(); - }); - document.getElementById("adminOpenRemoveUser").addEventListener("click", () => { - if (typeof window.loadUserList === "function") { - window.loadUserList(); - } - toggleVisibility("removeUserModal", true); - }); - // New event binding for the User Permissions button: - document.getElementById("adminOpenUserPermissions").addEventListener("click", () => { - openUserPermissionsModal(); - }); - document.getElementById("saveAdminSettings").addEventListener("click", () => { - const disableFormLoginCheckbox = document.getElementById("disableFormLogin"); - const disableBasicAuthCheckbox = document.getElementById("disableBasicAuth"); - const disableOIDCLoginCheckbox = document.getElementById("disableOIDCLogin"); - const totalDisabled = [disableFormLoginCheckbox, disableBasicAuthCheckbox, disableOIDCLoginCheckbox].filter(cb => cb.checked).length; - if (totalDisabled === 3) { - showToast("At least one login method must remain enabled."); - disableOIDCLoginCheckbox.checked = false; - localStorage.setItem("disableOIDCLogin", "false"); - if (typeof window.updateLoginOptionsUI === "function") { - window.updateLoginOptionsUI({ - disableFormLogin: disableFormLoginCheckbox.checked, - disableBasicAuth: disableBasicAuthCheckbox.checked, - disableOIDCLogin: disableOIDCLoginCheckbox.checked - }); - } - return; - } - const newOIDCConfig = { - providerUrl: document.getElementById("oidcProviderUrl").value.trim(), - clientId: document.getElementById("oidcClientId").value.trim(), - clientSecret: document.getElementById("oidcClientSecret").value.trim(), - redirectUri: document.getElementById("oidcRedirectUri").value.trim() - }; - const disableFormLogin = disableFormLoginCheckbox.checked; - const disableBasicAuth = disableBasicAuthCheckbox.checked; - const disableOIDCLogin = disableOIDCLoginCheckbox.checked; - const globalOtpauthUrl = document.getElementById("globalOtpauthUrl").value.trim(); - sendRequest("updateConfig.php", "POST", { - oidc: newOIDCConfig, - disableFormLogin, - disableBasicAuth, - disableOIDCLogin, - globalOtpauthUrl - }, { "X-CSRF-Token": window.csrfToken }) - .then(response => { - if (response.success) { - showToast("Settings updated successfully."); - localStorage.setItem("disableFormLogin", disableFormLogin); - localStorage.setItem("disableBasicAuth", disableBasicAuth); - localStorage.setItem("disableOIDCLogin", disableOIDCLogin); - if (typeof window.updateLoginOptionsUI === "function") { - window.updateLoginOptionsUI({ disableFormLogin, disableBasicAuth, disableOIDCLogin }); - } - closeAdminPanel(); - } else { - showToast("Error updating settings: " + (response.error || "Unknown error")); - } - }) - .catch(() => { }); - }); - const disableFormLoginCheckbox = document.getElementById("disableFormLogin"); - const disableBasicAuthCheckbox = document.getElementById("disableBasicAuth"); - const disableOIDCLoginCheckbox = document.getElementById("disableOIDCLogin"); - function enforceLoginOptionConstraint(changedCheckbox) { - const totalDisabled = [disableFormLoginCheckbox, disableBasicAuthCheckbox, disableOIDCLoginCheckbox].filter(cb => cb.checked).length; - if (changedCheckbox.checked && totalDisabled === 3) { - showToast("At least one login method must remain enabled."); - changedCheckbox.checked = false; - } - } - disableFormLoginCheckbox.addEventListener("change", function () { enforceLoginOptionConstraint(this); }); - disableBasicAuthCheckbox.addEventListener("change", function () { enforceLoginOptionConstraint(this); }); - disableOIDCLoginCheckbox.addEventListener("change", function () { enforceLoginOptionConstraint(this); }); - - document.getElementById("disableFormLogin").checked = config.loginOptions.disableFormLogin === true; - document.getElementById("disableBasicAuth").checked = config.loginOptions.disableBasicAuth === true; - document.getElementById("disableOIDCLogin").checked = config.loginOptions.disableOIDCLogin === true; - } else { - adminModal.style.backgroundColor = overlayBackground; - const modalContent = adminModal.querySelector(".modal-content"); - if (modalContent) { - modalContent.style.background = isDarkMode ? "#2c2c2c" : "#fff"; - modalContent.style.color = isDarkMode ? "#e0e0e0" : "#000"; - modalContent.style.border = isDarkMode ? "1px solid #444" : "1px solid #ccc"; - } - document.getElementById("oidcProviderUrl").value = window.currentOIDCConfig.providerUrl; - document.getElementById("oidcClientId").value = window.currentOIDCConfig.clientId; - document.getElementById("oidcClientSecret").value = window.currentOIDCConfig.clientSecret; - document.getElementById("oidcRedirectUri").value = window.currentOIDCConfig.redirectUri; - document.getElementById("globalOtpauthUrl").value = window.currentOIDCConfig.globalOtpauthUrl || 'otpauth://totp/{label}?secret={secret}&issuer=FileRise'; - document.getElementById("disableFormLogin").checked = config.loginOptions.disableFormLogin === true; - document.getElementById("disableBasicAuth").checked = config.loginOptions.disableBasicAuth === true; - document.getElementById("disableOIDCLogin").checked = config.loginOptions.disableOIDCLogin === true; - adminModal.style.display = "flex"; - } - }) - .catch(() => { - let adminModal = document.getElementById("adminPanelModal"); - if (adminModal) { - adminModal.style.backgroundColor = "rgba(0,0,0,0.5)"; - const modalContent = adminModal.querySelector(".modal-content"); - if (modalContent) { - modalContent.style.background = "#fff"; - modalContent.style.color = "#000"; - modalContent.style.border = "1px solid #ccc"; - } - document.getElementById("oidcProviderUrl").value = window.currentOIDCConfig.providerUrl; - document.getElementById("oidcClientId").value = window.currentOIDCConfig.clientId; - document.getElementById("oidcClientSecret").value = window.currentOIDCConfig.clientSecret; - document.getElementById("oidcRedirectUri").value = window.currentOIDCConfig.redirectUri; - document.getElementById("globalOtpauthUrl").value = window.currentOIDCConfig.globalOtpauthUrl || 'otpauth://totp/{label}?secret={secret}&issuer=FileRise'; - document.getElementById("disableFormLogin").checked = localStorage.getItem("disableFormLogin") === "true"; - document.getElementById("disableBasicAuth").checked = localStorage.getItem("disableBasicAuth") === "true"; - document.getElementById("disableOIDCLogin").checked = localStorage.getItem("disableOIDCLogin") === "true"; - adminModal.style.display = "flex"; - } else { - openAdminPanel(); - } + document.getElementById("closeAdminPanel").addEventListener("click", closeAdminPanel); + adminModal.addEventListener("click", (e) => { + if (e.target === adminModal) closeAdminPanel(); }); + document.getElementById("cancelAdminSettings").addEventListener("click", closeAdminPanel); + document.getElementById("adminOpenAddUser").addEventListener("click", () => { + toggleVisibility("addUserModal", true); + document.getElementById("newUsername").focus(); + }); + document.getElementById("adminOpenRemoveUser").addEventListener("click", () => { + if (typeof window.loadUserList === "function") { + window.loadUserList(); + } + toggleVisibility("removeUserModal", true); + }); + // New event binding for the User Permissions button: + document.getElementById("adminOpenUserPermissions").addEventListener("click", () => { + openUserPermissionsModal(); + }); + document.getElementById("saveAdminSettings").addEventListener("click", () => { + const disableFormLoginCheckbox = document.getElementById("disableFormLogin"); + const disableBasicAuthCheckbox = document.getElementById("disableBasicAuth"); + const disableOIDCLoginCheckbox = document.getElementById("disableOIDCLogin"); + const totalDisabled = [disableFormLoginCheckbox, disableBasicAuthCheckbox, disableOIDCLoginCheckbox].filter(cb => cb.checked).length; + if (totalDisabled === 3) { + showToast("At least one login method must remain enabled."); + disableOIDCLoginCheckbox.checked = false; + localStorage.setItem("disableOIDCLogin", "false"); + if (typeof window.updateLoginOptionsUI === "function") { + window.updateLoginOptionsUI({ + disableFormLogin: disableFormLoginCheckbox.checked, + disableBasicAuth: disableBasicAuthCheckbox.checked, + disableOIDCLogin: disableOIDCLoginCheckbox.checked + }); + } + return; + } + const newOIDCConfig = { + providerUrl: document.getElementById("oidcProviderUrl").value.trim(), + clientId: document.getElementById("oidcClientId").value.trim(), + clientSecret: document.getElementById("oidcClientSecret").value.trim(), + redirectUri: document.getElementById("oidcRedirectUri").value.trim() + }; + const disableFormLogin = disableFormLoginCheckbox.checked; + const disableBasicAuth = disableBasicAuthCheckbox.checked; + const disableOIDCLogin = disableOIDCLoginCheckbox.checked; + const globalOtpauthUrl = document.getElementById("globalOtpauthUrl").value.trim(); + sendRequest("updateConfig.php", "POST", { + oidc: newOIDCConfig, + disableFormLogin, + disableBasicAuth, + disableOIDCLogin, + globalOtpauthUrl + }, { "X-CSRF-Token": window.csrfToken }) + .then(response => { + if (response.success) { + showToast("Settings updated successfully."); + localStorage.setItem("disableFormLogin", disableFormLogin); + localStorage.setItem("disableBasicAuth", disableBasicAuth); + localStorage.setItem("disableOIDCLogin", disableOIDCLogin); + if (typeof window.updateLoginOptionsUI === "function") { + window.updateLoginOptionsUI({ disableFormLogin, disableBasicAuth, disableOIDCLogin }); + } + closeAdminPanel(); + } else { + showToast("Error updating settings: " + (response.error || "Unknown error")); + } + }) + .catch(() => { }); + }); + const disableFormLoginCheckbox = document.getElementById("disableFormLogin"); + const disableBasicAuthCheckbox = document.getElementById("disableBasicAuth"); + const disableOIDCLoginCheckbox = document.getElementById("disableOIDCLogin"); + function enforceLoginOptionConstraint(changedCheckbox) { + const totalDisabled = [disableFormLoginCheckbox, disableBasicAuthCheckbox, disableOIDCLoginCheckbox].filter(cb => cb.checked).length; + if (changedCheckbox.checked && totalDisabled === 3) { + showToast("At least one login method must remain enabled."); + changedCheckbox.checked = false; + } + } + disableFormLoginCheckbox.addEventListener("change", function () { enforceLoginOptionConstraint(this); }); + disableBasicAuthCheckbox.addEventListener("change", function () { enforceLoginOptionConstraint(this); }); + disableOIDCLoginCheckbox.addEventListener("change", function () { enforceLoginOptionConstraint(this); }); + + document.getElementById("disableFormLogin").checked = config.loginOptions.disableFormLogin === true; + document.getElementById("disableBasicAuth").checked = config.loginOptions.disableBasicAuth === true; + document.getElementById("disableOIDCLogin").checked = config.loginOptions.disableOIDCLogin === true; + } else { + adminModal.style.backgroundColor = overlayBackground; + const modalContent = adminModal.querySelector(".modal-content"); + if (modalContent) { + modalContent.style.background = isDarkMode ? "#2c2c2c" : "#fff"; + modalContent.style.color = isDarkMode ? "#e0e0e0" : "#000"; + modalContent.style.border = isDarkMode ? "1px solid #444" : "1px solid #ccc"; + } + document.getElementById("oidcProviderUrl").value = window.currentOIDCConfig.providerUrl; + document.getElementById("oidcClientId").value = window.currentOIDCConfig.clientId; + document.getElementById("oidcClientSecret").value = window.currentOIDCConfig.clientSecret; + document.getElementById("oidcRedirectUri").value = window.currentOIDCConfig.redirectUri; + document.getElementById("globalOtpauthUrl").value = window.currentOIDCConfig.globalOtpauthUrl || 'otpauth://totp/{label}?secret={secret}&issuer=FileRise'; + document.getElementById("disableFormLogin").checked = config.loginOptions.disableFormLogin === true; + document.getElementById("disableBasicAuth").checked = config.loginOptions.disableBasicAuth === true; + document.getElementById("disableOIDCLogin").checked = config.loginOptions.disableOIDCLogin === true; + adminModal.style.display = "flex"; + } + }) + .catch(() => { + let adminModal = document.getElementById("adminPanelModal"); + if (adminModal) { + adminModal.style.backgroundColor = "rgba(0,0,0,0.5)"; + const modalContent = adminModal.querySelector(".modal-content"); + if (modalContent) { + modalContent.style.background = "#fff"; + modalContent.style.color = "#000"; + modalContent.style.border = "1px solid #ccc"; + } + document.getElementById("oidcProviderUrl").value = window.currentOIDCConfig.providerUrl; + document.getElementById("oidcClientId").value = window.currentOIDCConfig.clientId; + document.getElementById("oidcClientSecret").value = window.currentOIDCConfig.clientSecret; + document.getElementById("oidcRedirectUri").value = window.currentOIDCConfig.redirectUri; + document.getElementById("globalOtpauthUrl").value = window.currentOIDCConfig.globalOtpauthUrl || 'otpauth://totp/{label}?secret={secret}&issuer=FileRise'; + document.getElementById("disableFormLogin").checked = localStorage.getItem("disableFormLogin") === "true"; + document.getElementById("disableBasicAuth").checked = localStorage.getItem("disableBasicAuth") === "true"; + document.getElementById("disableOIDCLogin").checked = localStorage.getItem("disableOIDCLogin") === "true"; + adminModal.style.display = "flex"; + } else { + openAdminPanel(); + } + }); } export function closeAdminPanel() { - const adminModal = document.getElementById("adminPanelModal"); - if (adminModal) adminModal.style.display = "none"; + const adminModal = document.getElementById("adminPanelModal"); + if (adminModal) adminModal.style.display = "none"; } // --- New: User Permissions Modal --- export function openUserPermissionsModal() { - let userPermissionsModal = document.getElementById("userPermissionsModal"); - const isDarkMode = document.body.classList.contains("dark-mode"); - const overlayBackground = isDarkMode ? "rgba(0,0,0,0.7)" : "rgba(0,0,0,0.3)"; - const modalContentStyles = ` + let userPermissionsModal = document.getElementById("userPermissionsModal"); + const isDarkMode = document.body.classList.contains("dark-mode"); + const overlayBackground = isDarkMode ? "rgba(0,0,0,0.7)" : "rgba(0,0,0,0.3)"; + const modalContentStyles = ` background: ${isDarkMode ? "#2c2c2c" : "#fff"}; color: ${isDarkMode ? "#e0e0e0" : "#000"}; padding: 20px; @@ -666,10 +677,10 @@ export function openUserPermissionsModal() { position: relative; `; - if (!userPermissionsModal) { - userPermissionsModal = document.createElement("div"); - userPermissionsModal.id = "userPermissionsModal"; - userPermissionsModal.style.cssText = ` + if (!userPermissionsModal) { + userPermissionsModal = document.createElement("div"); + userPermissionsModal.id = "userPermissionsModal"; + userPermissionsModal.style.cssText = ` position: fixed; top: 0; left: 0; @@ -681,7 +692,7 @@ export function openUserPermissionsModal() { align-items: center; z-index: 3500; `; - userPermissionsModal.innerHTML = ` + userPermissionsModal.innerHTML = ` `; - document.body.appendChild(userPermissionsModal); - document.getElementById("closeUserPermissionsModal").addEventListener("click", () => { + document.body.appendChild(userPermissionsModal); + document.getElementById("closeUserPermissionsModal").addEventListener("click", () => { + userPermissionsModal.style.display = "none"; + }); + document.getElementById("cancelUserPermissionsBtn").addEventListener("click", () => { + userPermissionsModal.style.display = "none"; + }); + document.getElementById("saveUserPermissionsBtn").addEventListener("click", () => { + // Collect permissions data from each user row. + const rows = userPermissionsModal.querySelectorAll(".user-permission-row"); + const permissionsData = []; + rows.forEach(row => { + const username = row.getAttribute("data-username"); + const folderOnlyCheckbox = row.querySelector("input[data-permission='folderOnly']"); + const readOnlyCheckbox = row.querySelector("input[data-permission='readOnly']"); + const disableUploadCheckbox = row.querySelector("input[data-permission='disableUpload']"); + permissionsData.push({ + username, + folderOnly: folderOnlyCheckbox.checked, + readOnly: readOnlyCheckbox.checked, + disableUpload: disableUploadCheckbox.checked + }); + }); + // Send the permissionsData to the server. + sendRequest("updateUserPermissions.php", "POST", { permissions: permissionsData }, { "X-CSRF-Token": window.csrfToken }) + .then(response => { + if (response.success) { + showToast("User permissions updated successfully."); userPermissionsModal.style.display = "none"; + } else { + showToast("Error updating permissions: " + (response.error || "Unknown error")); + } + }) + .catch(() => { + showToast("Error updating permissions."); }); - document.getElementById("cancelUserPermissionsBtn").addEventListener("click", () => { - userPermissionsModal.style.display = "none"; - }); - document.getElementById("saveUserPermissionsBtn").addEventListener("click", () => { - // Collect permissions data from each user row. - const rows = userPermissionsModal.querySelectorAll(".user-permission-row"); - const permissionsData = []; - rows.forEach(row => { - const username = row.getAttribute("data-username"); - const folderOnlyCheckbox = row.querySelector("input[data-permission='folderOnly']"); - const readOnlyCheckbox = row.querySelector("input[data-permission='readOnly']"); - const disableUploadCheckbox = row.querySelector("input[data-permission='disableUpload']"); - permissionsData.push({ - username, - folderOnly: folderOnlyCheckbox.checked, - readOnly: readOnlyCheckbox.checked, - disableUpload: disableUploadCheckbox.checked - }); - }); - // Send the permissionsData to the server. - sendRequest("updateUserPermissions.php", "POST", { permissions: permissionsData }, { "X-CSRF-Token": window.csrfToken }) - .then(response => { - if (response.success) { - showToast("User permissions updated successfully."); - userPermissionsModal.style.display = "none"; - } else { - showToast("Error updating permissions: " + (response.error || "Unknown error")); - } - }) - .catch(() => { - showToast("Error updating permissions."); - }); - }); - } else { - userPermissionsModal.style.display = "flex"; - } - // Load the list of users into the modal. - loadUserPermissionsList(); + }); + } else { + userPermissionsModal.style.display = "flex"; + } + // Load the list of users into the modal. + loadUserPermissionsList(); } function loadUserPermissionsList() { - const listContainer = document.getElementById("userPermissionsList"); - if (!listContainer) return; - listContainer.innerHTML = ""; + const listContainer = document.getElementById("userPermissionsList"); + if (!listContainer) return; + listContainer.innerHTML = ""; - // First, fetch the current permissions from the server. - fetch("getUserPermissions.php", { credentials: "include" }) + // First, fetch the current permissions from the server. + fetch("getUserPermissions.php", { credentials: "include" }) + .then(response => response.json()) + .then(permissionsData => { + // Then, fetch the list of users. + return fetch("getUsers.php", { credentials: "include" }) .then(response => response.json()) - .then(permissionsData => { - // Then, fetch the list of users. - return fetch("getUsers.php", { credentials: "include" }) - .then(response => response.json()) - .then(usersData => { - const users = Array.isArray(usersData) ? usersData : (usersData.users || []); - if (users.length === 0) { - listContainer.innerHTML = "No users found.
"; - return; - } - users.forEach(user => { - // Skip admin users. - if ((user.role && user.role === "1") || user.username.toLowerCase() === "admin") return; + .then(usersData => { + const users = Array.isArray(usersData) ? usersData : (usersData.users || []); + if (users.length === 0) { + listContainer.innerHTML = "No users found.
"; + return; + } + users.forEach(user => { + // Skip admin users. + if ((user.role && user.role === "1") || user.username.toLowerCase() === "admin") return; - // Use stored permissions if available; otherwise fall back to localStorage defaults. - const defaultPerm = { - folderOnly: localStorage.getItem("folderOnly") === "true", - readOnly: localStorage.getItem("readOnly") === "true", - disableUpload: localStorage.getItem("disableUpload") === "true" - }; - const userPerm = (permissionsData && typeof permissionsData === "object" && permissionsData[user.username]) || defaultPerm; + // Use stored permissions if available; otherwise fall back to localStorage defaults. + const defaultPerm = { + folderOnly: localStorage.getItem("folderOnly") === "true", + readOnly: localStorage.getItem("readOnly") === "true", + disableUpload: localStorage.getItem("disableUpload") === "true" + }; + const userPerm = (permissionsData && typeof permissionsData === "object" && permissionsData[user.username]) || defaultPerm; - // Create a row for the user. - const row = document.createElement("div"); - row.classList.add("user-permission-row"); - row.setAttribute("data-username", user.username); - row.style.padding = "10px 0"; - row.innerHTML = ` + // Create a row for the user. + const row = document.createElement("div"); + row.classList.add("user-permission-row"); + row.setAttribute("data-username", user.username); + row.style.padding = "10px 0"; + row.innerHTML = `Error loading users.
"; + listContainer.appendChild(row); + }); }); + }) + .catch(() => { + listContainer.innerHTML = "Error loading users.
"; + }); } \ No newline at end of file