Files
aiagent/backend/scripts/set_admin.py
renjianbo 41ffd31923 feat: P0 commercial readiness — password security, legal, account management
Backend changes for Phase A of v1.0 commercial launch:

P0-3: Password Security
- Remove hardcoded admin/123456 defaults from 34+ bootstrap scripts
- Add PLATFORM_USERNAME/PLATFORM_PASSWORD env vars to config
- Add PUT /api/v1/auth/change-password endpoint (requires old password)

P0-1: User Agreement & Privacy Policy
- Add agreed_terms/agreed_terms_version/agreed_terms_at to User model
- New GET /api/v1/legal/privacy and GET /api/v1/legal/terms endpoints
- New POST /api/v1/legal/agree endpoint to record consent
- Register endpoint now validates agreed_terms must be True

P0-6: Account Management
- Add phone/status/is_email_verified to User model
- Add DELETE /api/v1/auth/account for account deletion (with password confirmation)
- Add PUT /api/v1/auth/phone for phone binding
- Reject authentication for deleted accounts (status=deleted)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-07-01 22:01:54 +08:00

77 lines
2.4 KiB
Python
Executable File
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
设置admin用户为管理员
"""
import sys
import os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from app.core.database import SessionLocal
from app.models.user import User
from app.models.permission import Role
def set_admin():
"""设置admin用户为管理员"""
db = SessionLocal()
try:
print("=" * 60)
print("设置admin用户为管理员")
print("=" * 60)
print()
target_username = os.getenv("PLATFORM_USERNAME")
if not target_username:
print("❌ 请设置 PLATFORM_USERNAME 环境变量指定要提升为管理员的用户名")
return
# 查找目标用户
admin_user = db.query(User).filter(User.username == target_username).first()
if not admin_user:
print("❌ 未找到admin用户请先创建admin用户")
return
print(f"找到用户: {admin_user.username} (ID: {admin_user.id})")
print(f"当前角色: {admin_user.role}")
print()
# 设置role字段为admin
admin_user.role = "admin"
print("✅ 已将role字段设置为admin")
# 如果存在admin角色也分配给用户
admin_role = db.query(Role).filter(Role.name == "admin").first()
if admin_role:
# 检查用户是否已经有admin角色
if admin_role not in admin_user.roles:
admin_user.roles.append(admin_role)
print("✅ 已分配admin角色给用户")
else:
print(" 用户已有admin角色")
else:
print(" admin角色不存在可能需要先运行init_rbac_data.py")
db.commit()
print()
print("=" * 60)
print("✅ admin用户已设置为管理员")
print("=" * 60)
print()
print("用户信息:")
print(f" 用户名: {admin_user.username}")
print(f" 邮箱: {admin_user.email}")
print(f" 角色: {admin_user.role}")
if admin_user.roles:
print(f" RBAC角色: {', '.join([r.name for r in admin_user.roles])}")
except Exception as e:
db.rollback()
print(f"❌ 设置失败: {e}")
import traceback
traceback.print_exc()
finally:
db.close()
if __name__ == "__main__":
set_admin()