仓库模块完了
This commit is contained in:
79
backend/check_admin.py
Normal file
79
backend/check_admin.py
Normal file
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
检查并重置管理员账号
|
||||
"""
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
from app.models.database import engine, Base, SessionLocal
|
||||
from app.models.models import User, Role
|
||||
from app.services.user import UserService
|
||||
|
||||
|
||||
def check_admin():
|
||||
"""检查并重置管理员账号"""
|
||||
db = SessionLocal()
|
||||
|
||||
try:
|
||||
# 检查是否存在管理员账号
|
||||
print("检查管理员账号...")
|
||||
admin_user = db.query(User).filter(User.username == "admin").first()
|
||||
|
||||
if not admin_user:
|
||||
print("⚠️ 管理员账号不存在,创建新的管理员账号...")
|
||||
|
||||
# 初始化默认角色
|
||||
UserService.init_default_roles(db)
|
||||
|
||||
# 获取默认管理员角色
|
||||
admin_role = UserService.get_role_by_name(db, "admin")
|
||||
if not admin_role:
|
||||
print("❌ 管理员角色不存在,创建失败")
|
||||
return
|
||||
|
||||
# 创建默认管理员账号
|
||||
admin_user = User(
|
||||
id="user-admin",
|
||||
username="admin",
|
||||
email="admin@example.com",
|
||||
password_hash=UserService.get_password_hash("admin123"),
|
||||
role_id=admin_role.id,
|
||||
status="active"
|
||||
)
|
||||
db.add(admin_user)
|
||||
db.commit()
|
||||
db.refresh(admin_user)
|
||||
print("✅ 管理员账号创建成功")
|
||||
else:
|
||||
print("✅ 管理员账号存在,重置密码...")
|
||||
# 重置管理员密码
|
||||
admin_user.password_hash = UserService.get_password_hash("admin123")
|
||||
db.commit()
|
||||
print("✅ 管理员密码重置成功")
|
||||
|
||||
# 显示管理员账号信息
|
||||
print("\n管理员账号信息:")
|
||||
print(f"用户名: {admin_user.username}")
|
||||
print(f"密码: admin123")
|
||||
print(f"邮箱: {admin_user.email}")
|
||||
print(f"状态: {admin_user.status}")
|
||||
|
||||
# 检查角色信息
|
||||
if admin_user.role:
|
||||
print(f"角色: {admin_user.role.name}")
|
||||
else:
|
||||
print("⚠️ 管理员角色信息缺失")
|
||||
# 尝试修复角色关联
|
||||
admin_role = UserService.get_role_by_name(db, "admin")
|
||||
if admin_role:
|
||||
admin_user.role_id = admin_role.id
|
||||
db.commit()
|
||||
print("✅ 管理员角色关联修复成功")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 操作失败: {e}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
check_admin()
|
||||
Reference in New Issue
Block a user