60 lines
1.8 KiB
Python
60 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
插入测试用户,供测试工程师使用。统一密码:123456
|
|
用法:cd backend && PYTHONPATH=src python3 scripts/seed_test_users.py
|
|
"""
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
_backend = Path(__file__).resolve().parent.parent
|
|
if str(_backend) not in sys.path:
|
|
sys.path.insert(0, str(_backend))
|
|
|
|
import bcrypt
|
|
|
|
from src.database import get_connection, new_id
|
|
|
|
|
|
def _hash(pwd: str) -> str:
|
|
"""bcrypt 哈希,与 auth 中 passlib 校验兼容"""
|
|
return bcrypt.hashpw(pwd.encode("utf-8"), bcrypt.gensalt()).decode("utf-8")
|
|
|
|
# 角色枚举与测试账号: (username, role, display_name)
|
|
TEST_USERS = [
|
|
("admin", "管理员", "管理员"),
|
|
("market", "市场部", "市场部"),
|
|
("engineer", "工程部", "工程部"),
|
|
("tech", "技经部", "技经部"),
|
|
("finance", "财务部", "财务部"),
|
|
("material", "物贸部", "物贸部"),
|
|
]
|
|
|
|
TEST_PASSWORD = "123456"
|
|
|
|
|
|
def main():
|
|
pwd_hash = _hash(TEST_PASSWORD)
|
|
with get_connection() as conn:
|
|
cur = conn.cursor()
|
|
for username, role, display_name in TEST_USERS:
|
|
uid = new_id()
|
|
try:
|
|
cur.execute(
|
|
"INSERT INTO users (id, username, password_hash, role, display_name) VALUES (%s, %s, %s, %s, %s)",
|
|
(uid, username, pwd_hash, role, display_name),
|
|
)
|
|
conn.commit()
|
|
print(f"已插入: {username} ({role})")
|
|
except Exception as e:
|
|
if "Duplicate entry" in str(e) or "1062" in str(e):
|
|
print(f"已存在,跳过: {username}")
|
|
else:
|
|
raise
|
|
cur.close()
|
|
print(f"\n测试账号统一密码: {TEST_PASSWORD}")
|
|
print("账号列表: admin, market, engineer, tech, finance, material")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|