feat: implement user authentication and permission-based data filtering
Add user login system with role-based access control: - Admin users see all data and can filter by any manager - Regular users only see records where ManagerName matches their username New components: - BIPUsersDAO: user authentication and management - SessionManager: singleton session state management - LoginDialog: modal login UI for app startup - init_users.sql: initial user setup script Permission enforcement: - Material validation tab: hide manager filter for non-admin, force filter by current user - Material type management: hide filter UI for non-admin, filter at database level - Window title and status bar display current user info Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
162
gui/login_dialog.py
Normal file
162
gui/login_dialog.py
Normal file
@@ -0,0 +1,162 @@
|
||||
"""
|
||||
Login Dialog - Modal dialog for user authentication
|
||||
"""
|
||||
import tkinter as tk
|
||||
from tkinter import ttk, messagebox
|
||||
from typing import Optional, Tuple
|
||||
|
||||
|
||||
class LoginDialog:
|
||||
"""
|
||||
Modal login dialog that captures user credentials
|
||||
|
||||
Usage:
|
||||
login_dialog = LoginDialog(parent)
|
||||
credentials = login_dialog.get_credentials()
|
||||
if credentials:
|
||||
username, password = credentials
|
||||
# Proceed with authentication
|
||||
"""
|
||||
|
||||
def __init__(self, parent):
|
||||
"""
|
||||
Initialize the login dialog
|
||||
|
||||
Args:
|
||||
parent: The parent Tkinter window
|
||||
"""
|
||||
self.parent = parent
|
||||
self.result = None # Will hold (username, password) or None
|
||||
self.dialog = None
|
||||
|
||||
# Create dialog as modal
|
||||
self._create_dialog()
|
||||
self.parent.wait_window(self.dialog)
|
||||
|
||||
def _create_dialog(self):
|
||||
"""Create the login dialog UI"""
|
||||
self.dialog = tk.Toplevel(self.parent)
|
||||
self.dialog.title("ERP 自动化工具 - 登录")
|
||||
self.dialog.geometry("400x250")
|
||||
self.dialog.resizable(False, False)
|
||||
|
||||
# Center the dialog on parent
|
||||
self.dialog.transient(self.parent)
|
||||
self.dialog.grab_set()
|
||||
|
||||
# Calculate position to center on parent
|
||||
self.parent.update_idletasks()
|
||||
parent_x = self.parent.winfo_x()
|
||||
parent_y = self.parent.winfo_y()
|
||||
parent_width = self.parent.winfo_width()
|
||||
parent_height = self.parent.winfo_height()
|
||||
|
||||
dialog_width = 400
|
||||
dialog_height = 250
|
||||
x = parent_x + (parent_width - dialog_width) // 2
|
||||
y = parent_y + (parent_height - dialog_height) // 2
|
||||
self.dialog.geometry(f"{dialog_width}x{dialog_height}+{x}+{y}")
|
||||
|
||||
# Create UI elements
|
||||
self._create_widgets()
|
||||
|
||||
# Bind Enter key to login button
|
||||
self.dialog.bind('<Return>', lambda e: self._on_login())
|
||||
|
||||
# Focus on username entry
|
||||
self.username_entry.focus_set()
|
||||
|
||||
def _create_widgets(self):
|
||||
"""Create the dialog widgets"""
|
||||
# Main frame with padding
|
||||
main_frame = ttk.Frame(self.dialog, padding="20")
|
||||
main_frame.pack(fill=tk.BOTH, expand=True)
|
||||
|
||||
# Title
|
||||
title_label = ttk.Label(
|
||||
main_frame,
|
||||
text="请登录",
|
||||
font=('', 16, 'bold')
|
||||
)
|
||||
title_label.pack(pady=(0, 20))
|
||||
|
||||
# Username field
|
||||
username_frame = ttk.Frame(main_frame)
|
||||
username_frame.pack(fill=tk.X, pady=5)
|
||||
|
||||
ttk.Label(username_frame, text="用户名:", width=10).pack(side=tk.LEFT)
|
||||
self.username_entry = ttk.Entry(username_frame)
|
||||
self.username_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
|
||||
|
||||
# Password field
|
||||
password_frame = ttk.Frame(main_frame)
|
||||
password_frame.pack(fill=tk.X, pady=5)
|
||||
|
||||
ttk.Label(password_frame, text="密码:", width=10).pack(side=tk.LEFT)
|
||||
self.password_entry = ttk.Entry(password_frame, show="*")
|
||||
self.password_entry.pack(side=tk.LEFT, fill=tk.X, expand=True)
|
||||
|
||||
# Buttons frame
|
||||
button_frame = ttk.Frame(main_frame)
|
||||
button_frame.pack(pady=(20, 0))
|
||||
|
||||
# Login button
|
||||
login_btn = ttk.Button(
|
||||
button_frame,
|
||||
text="登录",
|
||||
command=self._on_login,
|
||||
width=10
|
||||
)
|
||||
login_btn.pack(side=tk.LEFT, padx=5)
|
||||
|
||||
# Cancel button
|
||||
cancel_btn = ttk.Button(
|
||||
button_frame,
|
||||
text="取消",
|
||||
command=self._on_cancel,
|
||||
width=10
|
||||
)
|
||||
cancel_btn.pack(side=tk.LEFT, padx=5)
|
||||
|
||||
# Version info
|
||||
version_label = ttk.Label(
|
||||
main_frame,
|
||||
text="v1.0",
|
||||
font=('', 8),
|
||||
foreground='gray'
|
||||
)
|
||||
version_label.pack(side=tk.BOTTOM, pady=10)
|
||||
|
||||
def _on_login(self):
|
||||
"""Handle login button click"""
|
||||
username = self.username_entry.get().strip()
|
||||
password = self.password_entry.get().strip()
|
||||
|
||||
if not username:
|
||||
messagebox.showwarning("输入错误", "请输入用户名")
|
||||
self.username_entry.focus_set()
|
||||
return
|
||||
|
||||
if not password:
|
||||
messagebox.showwarning("输入错误", "请输入密码")
|
||||
self.password_entry.focus_set()
|
||||
return
|
||||
|
||||
# Return credentials for validation
|
||||
self.result = (username, password)
|
||||
self.dialog.destroy()
|
||||
|
||||
def _on_cancel(self):
|
||||
"""Handle cancel button click"""
|
||||
self.result = None
|
||||
self.dialog.destroy()
|
||||
|
||||
def get_credentials(self) -> Optional[Tuple[str, str]]:
|
||||
"""
|
||||
Get the entered credentials
|
||||
|
||||
Returns:
|
||||
Tuple of (username, password) if user clicked Login,
|
||||
None if user clicked Cancel or closed the dialog
|
||||
"""
|
||||
return self.result
|
||||
Reference in New Issue
Block a user