60 lines
1.6 KiB
Python
60 lines
1.6 KiB
Python
from app.database.database import SessionLocal
|
|
from app.models import User
|
|
import bcrypt
|
|
|
|
|
|
def create_initial_users():
|
|
"""创建初始用户"""
|
|
db = SessionLocal()
|
|
|
|
try:
|
|
# 检查是否已有用户
|
|
existing_users = db.query(User).count()
|
|
if existing_users > 0:
|
|
print(f"Database already has {existing_users} users. Skipping initialization.")
|
|
return
|
|
|
|
# 创建初始用户
|
|
password = "123456"
|
|
hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
|
|
|
users = [
|
|
User(
|
|
username="admin",
|
|
password=hashed_password,
|
|
department="IT",
|
|
role="admin"
|
|
),
|
|
User(
|
|
username="marketing",
|
|
password=hashed_password,
|
|
department="Marketing",
|
|
role="marketing"
|
|
),
|
|
User(
|
|
username="other",
|
|
password=hashed_password,
|
|
department="Other",
|
|
role="other"
|
|
)
|
|
]
|
|
|
|
for user in users:
|
|
db.add(user)
|
|
|
|
db.commit()
|
|
print("Initial users created successfully!")
|
|
print("Username: admin, Password: 123456")
|
|
print("Username: marketing, Password: 123456")
|
|
print("Username: other, Password: 123456")
|
|
|
|
except Exception as e:
|
|
db.rollback()
|
|
print(f"Error creating initial users: {e}")
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
create_initial_users()
|