default user

This commit is contained in:
linkong
2026-05-12 09:20:13 +08:00
parent 8955c58d19
commit b15d097b9c
5 changed files with 176 additions and 63 deletions

View File

@@ -72,22 +72,40 @@ async def seed_default_datasources(session: AsyncSession):
await session.commit() await session.commit()
DEFAULT_LOGIN_USERS = (
{
"username": "admin",
"email": "admin@planet.local",
"password": "admin123",
"role": "super_admin",
},
{
"username": "linkong",
"email": "linkong@planet.local",
"password": "12345678",
"role": "super_admin",
},
)
async def ensure_default_admin_user(session: AsyncSession): async def ensure_default_admin_user(session: AsyncSession):
from app.core.security import get_password_hash from app.core.security import get_password_hash
from app.models.user import User from app.models.user import User
for default_user in DEFAULT_LOGIN_USERS:
result = await session.execute( result = await session.execute(
text("SELECT id FROM users WHERE username = 'admin'") text("SELECT id FROM users WHERE username = :username"),
{"username": default_user["username"]},
) )
if result.fetchone(): if result.fetchone():
return continue
session.add( session.add(
User( User(
username="admin", username=default_user["username"],
email="admin@planet.local", email=default_user["email"],
password_hash=get_password_hash("admin123"), password_hash=get_password_hash(default_user["password"]),
role="super_admin", role=default_user["role"],
is_active=True, is_active=True,
) )
) )

View File

@@ -17,15 +17,16 @@ async def create_admin():
existing_user = result.scalar_one_or_none() existing_user = result.scalar_one_or_none()
if existing_user: if existing_user:
print(f"用户 linkong 已存在,更新密码...") print("用户 linkong 已存在,更新密码...")
existing_user.set_password("LK12345678") existing_user.set_password("12345678")
existing_user.role = "super_admin" existing_user.role = "super_admin"
existing_user.email = "linkong@planet.local"
else: else:
print("创建管理员用户...") print("创建管理员用户...")
user = User( user = User(
username="linkong", username="linkong",
email="linkong@example.com", email="linkong@planet.local",
password_hash=get_password_hash("LK12345678"), password_hash=get_password_hash("12345678"),
role="super_admin", role="super_admin",
is_active=True, is_active=True,
) )

View File

@@ -6,29 +6,58 @@ import sys
sys.path.insert(0, ".") sys.path.insert(0, ".")
from app.core.security import get_password_hash from app.core.security import get_password_hash
from app.db.session import engine, async_session_factory from app.db.session import async_session_factory
from app.models.user import User from app.models.user import User
DEFAULT_LOGIN_USERS = (
{
"username": "admin",
"email": "admin@planet.local",
"password": "admin123",
"role": "super_admin",
},
{
"username": "linkong",
"email": "linkong@planet.local",
"password": "12345678",
"role": "super_admin",
},
)
async def create_admin(): async def create_admin():
from sqlalchemy import text from sqlalchemy import text
async with async_session_factory() as session: async with async_session_factory() as session:
result = await session.execute(text("SELECT id FROM users WHERE username = 'admin'")) created = []
for default_user in DEFAULT_LOGIN_USERS:
result = await session.execute(
text("SELECT id FROM users WHERE username = :username"),
{"username": default_user["username"]},
)
if result.fetchone(): if result.fetchone():
print("Admin user already exists") continue
return
admin = User( session.add(
username="admin", User(
email="admin@planet.local", username=default_user["username"],
password_hash=get_password_hash("admin123"), email=default_user["email"],
role="super_admin", password_hash=get_password_hash(default_user["password"]),
role=default_user["role"],
is_active=True, is_active=True,
) )
session.add(admin) )
created.append(default_user)
await session.commit() await session.commit()
print("Admin user created: admin / admin123") if not created:
print("Default login users already exist")
return
for default_user in created:
print(
f"Default login user created: {default_user['username']} / {default_user['password']}"
)
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -12,8 +12,20 @@ from sqlalchemy.orm import sessionmaker
import bcrypt import bcrypt
# Generate proper bcrypt hash DEFAULT_LOGIN_USERS = (
ADMIN_PASSWORD_HASH = bcrypt.hashpw("admin123".encode(), bcrypt.gensalt()).decode() {
"username": "admin",
"email": "admin@planet.local",
"password": "admin123",
"role": "super_admin",
},
{
"username": "linkong",
"email": "linkong@planet.local",
"password": "12345678",
"role": "super_admin",
},
)
async def create_admin(): async def create_admin():
@@ -22,23 +34,42 @@ async def create_admin():
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as session: async with async_session() as session:
created = []
for default_user in DEFAULT_LOGIN_USERS:
result = await session.execute( result = await session.execute(
text("SELECT id FROM users WHERE username = 'admin'") text("SELECT id FROM users WHERE username = :username"),
{"username": default_user["username"]},
) )
if result.fetchone(): if result.fetchone():
print("Admin user already exists") continue
return
password_hash = bcrypt.hashpw(
default_user["password"].encode(), bcrypt.gensalt()
).decode()
await session.execute( await session.execute(
text(""" text("""
INSERT INTO users (username, email, password_hash, role, is_active, created_at, updated_at) INSERT INTO users (username, email, password_hash, role, is_active, created_at, updated_at)
VALUES ('admin', 'admin@planet.local', :password, 'super_admin', true, NOW(), NOW()) VALUES (:username, :email, :password, :role, true, NOW(), NOW())
"""), """),
{"password": ADMIN_PASSWORD_HASH}, {
"username": default_user["username"],
"email": default_user["email"],
"password": password_hash,
"role": default_user["role"],
},
) )
created.append((default_user, password_hash))
await session.commit() await session.commit()
print(f"Admin user created: admin / admin123") if not created:
print(f"Hash: {ADMIN_PASSWORD_HASH}") print("Default login users already exist")
return
for default_user, password_hash in created:
print(
f"Default login user created: {default_user['username']} / {default_user['password']}"
)
print(f"Hash: {password_hash}")
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -13,7 +13,20 @@ from sqlalchemy.orm import sessionmaker
import bcrypt import bcrypt
ADMIN_PASSWORD_HASH = bcrypt.hashpw("admin123".encode(), bcrypt.gensalt()).decode() DEFAULT_LOGIN_USERS = (
{
"username": "admin",
"email": "admin@planet.local",
"password": "admin123",
"role": "super_admin",
},
{
"username": "linkong",
"email": "linkong@planet.local",
"password": "12345678",
"role": "super_admin",
},
)
async def create_admin(): async def create_admin():
@@ -24,21 +37,42 @@ async def create_admin():
async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False) async_session = sessionmaker(engine, class_=AsyncSession, expire_on_commit=False)
async with async_session() as session: async with async_session() as session:
result = await session.execute(text("SELECT id FROM users WHERE username = 'admin'")) created = []
for default_user in DEFAULT_LOGIN_USERS:
result = await session.execute(
text("SELECT id FROM users WHERE username = :username"),
{"username": default_user["username"]},
)
if result.fetchone(): if result.fetchone():
print("Admin user already exists") continue
return
password_hash = bcrypt.hashpw(
default_user["password"].encode(), bcrypt.gensalt()
).decode()
await session.execute( await session.execute(
text(""" text("""
INSERT INTO users (username, email, password_hash, role, is_active, created_at, updated_at) INSERT INTO users (username, email, password_hash, role, is_active, created_at, updated_at)
VALUES ('admin', 'admin@planet.local', :password, 'super_admin', true, NOW(), NOW()) VALUES (:username, :email, :password, :role, true, NOW(), NOW())
"""), """),
{"password": ADMIN_PASSWORD_HASH}, {
"username": default_user["username"],
"email": default_user["email"],
"password": password_hash,
"role": default_user["role"],
},
) )
created.append((default_user, password_hash))
await session.commit() await session.commit()
print(f"Admin user created: admin / admin123") if not created:
print(f"Hash: {ADMIN_PASSWORD_HASH}") print("Default login users already exist")
return
for default_user, password_hash in created:
print(
f"Default login user created: {default_user['username']} / {default_user['password']}"
)
print(f"Hash: {password_hash}")
if __name__ == "__main__": if __name__ == "__main__":