u

user-management

此 Python 程式使用多個 Mixin,提供日誌紀錄、時間標記、數據儲存與權限管理功能,適用於構建可擴展的用戶管理系統,提升代碼的模組化與可重用性。

0 0 0 1
1 import datetime
2
3 class LoggingMixin:
4 """ 提供日誌功能的 Mixin 類別 """
5
6 def log(self, message):
7 """ 簡單的日誌紀錄方法 """
8 timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
9 print(f"[{timestamp}] {self.__class__.__name__}: {message}")
Last active 5 hours ago logging mixin oop python timestamp user-management

此 Python 程式透過 LoggingMixin 提供日誌功能,TimestampMixin 管理時間標記,並應用於 User 類別,使其具備自動記錄變更歷史的能力,適用於用戶管理、數據追蹤等應用。

0 0 0 1
1 import datetime
2
3 class LoggingMixin:
4 """ 提供日誌功能的 Mixin 類別 """
5
6 def log(self, message):
7 """ 簡單的日誌紀錄方法 """
8 timestamp = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
9 print(f"[{timestamp}] {self.__class__.__name__}: {message}")

這個 Bash 腳本用於根據輸入的使用者名稱,建立一個新的使用者帳號,並提示輸入密碼後設定該密碼,然後將該使用者加入 sudo 群組,同時更新 sudoers 檔案以允許該使用者無密碼執行 sudo 指令,方便日後進行系統管理。

0 0 0 1
1 #!/bin/bash
2
3 # Check if a username was provided as an argument
4 if [ $# -ne 1 ]; then
5 echo "Error: Please provide a username as an argument."
6 exit 1
7 fi
8
9 # Get the username from the argument
10 username="$1"

這段程式碼使用 Streamlit 和 streamlit-authenticator 來 管理使用者登入,透過 YAML 設定檔 儲存使用者憑證與 Cookie 配置,並提供 登入、登出 和 使用者驗證 功能。

0 0 0 2
1 cookie:
2 expiry_days: 30
3 key: some_signature_key
4 name: my_auth_cookie
5 credentials:
6 usernames:
7 jsmith:
8 email: jsmith@gmail.com
9 failed_login_attempts: 0 # Will be managed automatically
10 logged_in: False # Will be managed automatically

這段程式碼使用 streamlit_authenticator 建立一個簡單的登入系統,支持使用者驗證(帳號、密碼)和登出功能,並在側邊欄提供登出按鈕。密碼經過雜湊處理以增加安全性。

0 0 0 1
1 import streamlit as st # 匯入 streamlit 模組
2 import streamlit_authenticator as stauth # 匯入 streamlit_authenticator 模組
3
4 names = ["John", "Mary"] # 定義名字清單
5 usernames = ["john", "mary"] # 定義使用者名清單
6 passwords = ["1234", "5678"] # 定義密碼清單
7
8 hashed_passwords = stauth.Hasher(passwords).generate() # 使用 Hasher 類別對密碼進行雜湊處理
9
10 authenticator = stauth.Authenticate( # 建立一個 stauth.Authenticate 的實例 authenticator

這段 MySQL 指令 用於 建立 root 帳戶並賦予全域權限,適用於 開放遠端連線的 MySQL 伺服器。

0 0 0 1
1 CREATE USER 'root'@'%' IDENTIFIED BY 'my_root_pwd';
2 GRANT ALL PRIVILEGES ON *.* TO 'root'@'%';
3 FLUSH PRIVILEGES; -- 重新整理權限表
4
5 ALTER USER 'root'@'%' IDENTIFIED BY 'my_root_pwd';
6 FLUSH PRIVILEGES; -- 重新整理權限表