#/nas2/kuang/MyPrograms/streamlit/login.py
importstreamlitasstfromldap3importServer,Connection,ALL,MODIFY_REPLACE,MODIFY_ADD,MODIFY_DELETEdefldap_login(username,password):BASE_DN="dc=sinotech-eng,dc=com"user=f"uid={username},cn=users,cn=accounts,dc=sinotech-eng,dc=com"server=Server('ldap://node03.sinotech-eng.com',get_info=ALL)try:conn=Connection(server,user,password,auto_bind=True)except:st.error('invalidCredentials')returnresult=conn.search(BASE_DN,'(objectClass=*)',attributes=['*'])returnconndefget_ldap_credentials():username=st.session_state['username']password=st.session_state['password']returnusername,passworddefauthenticate_user(username,password):ldap_credentials=get_ldap_credentials()conn=ldap_login(username,password)ifnotconn:st.error('not connected')returnifconn.response_to_json()==None:st.error('Failed to authenticate user.')returnconn.unbind()returnTruest.header('LDAP Login')st.session_state.__init__()if'username'notinst.session_state:st.session_state['username']=''# or any default value you prefer
# Similar initialization for 'password' if needed
if'password'notinst.session_state:st.session_state['password']=''placeholder=st.empty()# 使用佔位符顯示一些內容
withplaceholder.container():st.write("這是第一個頁面的內容。")withst.form('login_form'):username=st.text_input('Username')password=st.text_input('Password',type='password',key='password')submit=st.form_submit_button(label='Login')ifsubmit:ifauthenticate_user(username,password):st.success('Login successful!')st.success(f'<a href="https://node03.sinotech-eng.com/Sup.calendars/zh/" > 轉到新網址</a>')# Redirect to dashboard
ifusernameisNone:username=st.session_state['username']=st.text_input("Enter your username")ifst.button("清空並重新開始"):placeholder.empty()st.write("這是新的頁面的內容。")