default user
This commit is contained in:
1 parent
8955c58d19
commit
b15d097b9c
5 files changed
+175
-62
No files matched your search
@@ -6,29 +6,58 @@ import sys
|
||||
sys.path.insert(0, ".")
|
||||
|
||||
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
|
||||
|
||||
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():
|
||||
from sqlalchemy import text
|
||||
|
||||
async with async_session_factory() as session:
|
||||
result = await session.execute(text("SELECT id FROM users WHERE username = 'admin'"))
|
||||
if result.fetchone():
|
||||
print("Admin user already exists")
|
||||
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():
|
||||
continue
|
||||
|
||||
session.add(
|
||||
User(
|
||||
username=default_user["username"],
|
||||
email=default_user["email"],
|
||||
password_hash=get_password_hash(default_user["password"]),
|
||||
role=default_user["role"],
|
||||
is_active=True,
|
||||
)
|
||||
)
|
||||
created.append(default_user)
|
||||
|
||||
await session.commit()
|
||||
if not created:
|
||||
print("Default login users already exist")
|
||||
return
|
||||
|
||||
admin = User(
|
||||
username="admin",
|
||||
email="admin@planet.local",
|
||||
password_hash=get_password_hash("admin123"),
|
||||
role="super_admin",
|
||||
is_active=True,
|
||||
)
|
||||
session.add(admin)
|
||||
await session.commit()
|
||||
print("Admin user created: admin / admin123")
|
||||
for default_user in created:
|
||||
print(
|
||||
f"Default login user created: {default_user['username']} / {default_user['password']}"
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user