71 lines
2.1 KiB
Python
71 lines
2.1 KiB
Python
|
|
"""在 Docker 容器中创建超级管理员。
|
|||
|
|
|
|||
|
|
使用方式:
|
|||
|
|
docker exec -it <api容器名> python /app/scripts/init_admin_docker.py
|
|||
|
|
|
|||
|
|
或者直接复制以下命令到 Docker 容器中执行:
|
|||
|
|
"""
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
import bcrypt
|
|||
|
|
import uuid
|
|||
|
|
|
|||
|
|
# 添加 app 路径
|
|||
|
|
sys.path.insert(0, '/app/api')
|
|||
|
|
|
|||
|
|
|
|||
|
|
def create_admin():
|
|||
|
|
"""创建超级管理员账号。"""
|
|||
|
|
try:
|
|||
|
|
from insurance.app import create_app
|
|||
|
|
from insurance.db.compat import db
|
|||
|
|
from insurance.models.wecom_user import WeComUserMapping
|
|||
|
|
|
|||
|
|
app = create_app()
|
|||
|
|
|
|||
|
|
with app.app_context():
|
|||
|
|
username = "admin"
|
|||
|
|
password = "Admin@123456"
|
|||
|
|
|
|||
|
|
# 检查是否已存在
|
|||
|
|
existing = db.session.query(WeComUserMapping).filter_by(username=username).first()
|
|||
|
|
if existing:
|
|||
|
|
print(f"User {username} already exists, role: {existing.role}")
|
|||
|
|
# 如果不是管理员,更新角色
|
|||
|
|
if existing.role not in ('super_admin', 'admin'):
|
|||
|
|
existing.role = 'super_admin'
|
|||
|
|
db.session.commit()
|
|||
|
|
print(f"Updated role to super_admin")
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 创建用户
|
|||
|
|
password_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
|||
|
|
mapping = WeComUserMapping(
|
|||
|
|
wecom_userid=f"local_{uuid.uuid4().hex[:8]}",
|
|||
|
|
internal_user_id=f"user_{uuid.uuid4().hex[:8]}",
|
|||
|
|
username=username,
|
|||
|
|
password_hash=password_hash,
|
|||
|
|
role="super_admin",
|
|||
|
|
department="管理组",
|
|||
|
|
status="active",
|
|||
|
|
)
|
|||
|
|
db.session.add(mapping)
|
|||
|
|
db.session.commit()
|
|||
|
|
|
|||
|
|
print("=" * 50)
|
|||
|
|
print("Admin user created successfully!")
|
|||
|
|
print("=" * 50)
|
|||
|
|
print(f" Username: {username}")
|
|||
|
|
print(f" Password: {password}")
|
|||
|
|
print(f" Role: super_admin")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"Error: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
create_admin()
|