commit
9c5fef1a4d
412 files changed
+94582
No files matched your search
@@ -0,0 +1,59 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import getpass
|
||||
|
||||
from sqlalchemy import func, or_, select
|
||||
|
||||
from sakura_cloud.core.security import hash_password
|
||||
from sakura_cloud.db.session import SessionLocal
|
||||
from sakura_cloud.models.user import User, UserRole
|
||||
|
||||
|
||||
def main() -> None:
|
||||
parser = argparse.ArgumentParser(description="Create or promote the first Sakura Cloud super admin.")
|
||||
parser.add_argument("--username", required=True)
|
||||
parser.add_argument("--email", required=True)
|
||||
parser.add_argument("--password", default="")
|
||||
args = parser.parse_args()
|
||||
|
||||
password = args.password or getpass.getpass("Password: ")
|
||||
if len(password) < 8:
|
||||
raise SystemExit("Password must be at least 8 characters.")
|
||||
|
||||
db = SessionLocal()
|
||||
try:
|
||||
existing_super = db.scalar(select(User).where(User.role == UserRole.SUPER_ADMIN))
|
||||
existing = db.scalar(
|
||||
select(User).where(
|
||||
or_(
|
||||
func.lower(User.username) == args.username.lower(),
|
||||
func.lower(User.email) == args.email.lower(),
|
||||
)
|
||||
)
|
||||
)
|
||||
if existing is None:
|
||||
user = User(
|
||||
username=args.username.strip(),
|
||||
email=args.email.strip().lower(),
|
||||
password_hash=hash_password(password),
|
||||
role=UserRole.SUPER_ADMIN,
|
||||
is_active=True,
|
||||
)
|
||||
db.add(user)
|
||||
db.commit()
|
||||
print(f"Created super_admin: {user.username}")
|
||||
return
|
||||
if existing_super is not None and existing.role != UserRole.SUPER_ADMIN:
|
||||
raise SystemExit("A super_admin already exists; refusing to promote another user from this script.")
|
||||
existing.password_hash = hash_password(password)
|
||||
existing.role = UserRole.SUPER_ADMIN
|
||||
existing.is_active = True
|
||||
db.commit()
|
||||
print(f"Promoted super_admin: {existing.username}")
|
||||
finally:
|
||||
db.close()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user