From f6dc74368f5651ea9e79860dad8b370c855e7207 Mon Sep 17 00:00:00 2001 From: Stan Soldatov <118521851+iwatkot@users.noreply.github.com> Date: Sun, 29 Dec 2024 17:01:50 +0100 Subject: [PATCH] Locked mode --- webui/config.py | 22 ++++++++++++++++++++++ webui/generator.py | 17 +++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/webui/config.py b/webui/config.py index 08e4db14..c0d98292 100644 --- a/webui/config.py +++ b/webui/config.py @@ -30,6 +30,7 @@ STREAMLIT_COMMUNITY_VALUE = "streamlit" PUBLIC_HOSTNAME_KEY = "PUBLIC_HOSTNAME" PUBLIC_HOSTNAME_VALUE = "maps4fs" +PUBLIC_PASSWORD_KEY = "PUBLIC_PASSWORD" DOCS_DIRECTORY = os.path.join(WORKING_DIRECTORY, "docs") MD_FILES = { @@ -77,6 +78,27 @@ def is_public() -> bool: return os.environ.get(PUBLIC_HOSTNAME_KEY) == PUBLIC_HOSTNAME_VALUE +def public_password_is_correct(password: str) -> bool: + """Check if the public password is correct. + + Arguments: + password (str): The password to check. + + Returns: + bool: True if the password is correct, False otherwise. + """ + return password == os.environ.get(PUBLIC_PASSWORD_KEY) + + +def public_password_is_set() -> bool: + """Check if the public password is set. + + Returns: + bool: True if the public password is set, False otherwise. + """ + return PUBLIC_PASSWORD_KEY in os.environ + + def remove_with_delay_without_blocking( file_path: str, logger: mfs.Logger, diff --git a/webui/generator.py b/webui/generator.py index 989b3618..e4be460f 100644 --- a/webui/generator.py +++ b/webui/generator.py @@ -48,6 +48,23 @@ def __init__(self): self.logger = mfs.Logger(level="INFO", to_file=False) self.public = config.is_public() + self.locked = False + if self.public and config.public_password_is_set(): + # Maintainance mode is enabled. + self.locked = True + st.warning("The app is in maintenance mode, enter the password or get back later.") + + password = st.text_input("Password", type="password", key="password") + + if st.button("Enter", key="enter_btn"): + if not config.public_password_is_correct(password): + return + else: + self.locked = False + + if self.locked: + return + self.logger.debug("The application launched on a public server: %s", self.public) self.left_column, self.right_column = st.columns(2, gap="large")