|
| 1 | +""" |
| 2 | +Authentication component for detailed map access. |
| 3 | +""" |
| 4 | + |
| 5 | +import os |
| 6 | +import streamlit as st |
| 7 | +from config.settings import DETAILED_MAP_PASSWORD |
| 8 | + |
| 9 | + |
| 10 | +def get_detailed_map_password() -> str: |
| 11 | + """Get the detailed map password from environment variable or config.""" |
| 12 | + return os.environ.get("AUTH_PASSWORD", DETAILED_MAP_PASSWORD) |
| 13 | + |
| 14 | + |
| 15 | +def check_detailed_map_access() -> bool: |
| 16 | + """ |
| 17 | + Check if the user has access to the detailed map. |
| 18 | + Returns True if authorized, False otherwise. |
| 19 | + """ |
| 20 | + # Initialize session state for detailed map access |
| 21 | + if "detailed_map_authorized" not in st.session_state: |
| 22 | + st.session_state.detailed_map_authorized = False |
| 23 | + |
| 24 | + return st.session_state.detailed_map_authorized |
| 25 | + |
| 26 | + |
| 27 | +def render_detailed_map_auth() -> bool: |
| 28 | + """ |
| 29 | + Render the detailed map authentication interface. |
| 30 | + Returns True if user is authorized for detailed map access. |
| 31 | + """ |
| 32 | + # Check if already authorized |
| 33 | + if check_detailed_map_access(): |
| 34 | + return True |
| 35 | + |
| 36 | + # Show authentication interface |
| 37 | + with st.expander("🔐 Detailed Map Access", expanded=False): |
| 38 | + st.markdown(""" |
| 39 | + **Project Team Access**: Enter your dashboard password to access detailed device locations. |
| 40 | + |
| 41 | + ⚠️ **Note**: This will show exact device coordinates for project management purposes. |
| 42 | + """) |
| 43 | + |
| 44 | + password = st.text_input( |
| 45 | + "Password", |
| 46 | + type="password", |
| 47 | + help="Enter your dashboard password to unlock detailed map view", |
| 48 | + key="detailed_map_password" |
| 49 | + ) |
| 50 | + |
| 51 | + col1, col2 = st.columns([1, 3]) |
| 52 | + |
| 53 | + with col1: |
| 54 | + if st.button("🔓 Unlock Detailed Map", type="primary"): |
| 55 | + if password == get_detailed_map_password(): |
| 56 | + st.session_state.detailed_map_authorized = True |
| 57 | + st.success("✅ Access granted! Detailed map is now available.") |
| 58 | + st.rerun() |
| 59 | + else: |
| 60 | + st.error("❌ Invalid password") |
| 61 | + |
| 62 | + with col2: |
| 63 | + if st.session_state.detailed_map_authorized: |
| 64 | + if st.button("🔒 Lock Detailed Map"): |
| 65 | + st.session_state.detailed_map_authorized = False |
| 66 | + st.info("🔒 Detailed map access revoked") |
| 67 | + st.rerun() |
| 68 | + |
| 69 | + return check_detailed_map_access() |
| 70 | + |
| 71 | + |
| 72 | +def get_map_zoom_level() -> int: |
| 73 | + """ |
| 74 | + Get the appropriate zoom level based on user authorization. |
| 75 | + Returns detailed zoom level if authorized, otherwise privacy-protected level. |
| 76 | + """ |
| 77 | + from config.settings import MAX_ZOOM_LEVEL, DETAILED_MAP_MAX_ZOOM |
| 78 | + |
| 79 | + if check_detailed_map_access(): |
| 80 | + return DETAILED_MAP_MAX_ZOOM |
| 81 | + else: |
| 82 | + return MAX_ZOOM_LEVEL |
| 83 | + |
| 84 | + |
| 85 | +def get_map_access_status() -> dict: |
| 86 | + """ |
| 87 | + Get the current map access status and settings. |
| 88 | + Returns a dictionary with access information. |
| 89 | + """ |
| 90 | + from config.settings import MAX_ZOOM_LEVEL, DETAILED_MAP_MAX_ZOOM |
| 91 | + |
| 92 | + is_authorized = check_detailed_map_access() |
| 93 | + |
| 94 | + return { |
| 95 | + "is_authorized": is_authorized, |
| 96 | + "current_max_zoom": DETAILED_MAP_MAX_ZOOM if is_authorized else MAX_ZOOM_LEVEL, |
| 97 | + "access_level": "Detailed (Project Team)" if is_authorized else "Privacy Protected (Public)", |
| 98 | + "zoom_description": f"Up to level {DETAILED_MAP_MAX_ZOOM}" if is_authorized else f"Limited to level {MAX_ZOOM_LEVEL}" |
| 99 | + } |
0 commit comments