biaoqing commited on
Commit
c659586
·
verified ·
1 Parent(s): a04cf9d

Update src/landppt/auth/auth_service.py

Browse files
Files changed (1) hide show
  1. src/landppt/auth/auth_service.py +32 -11
src/landppt/auth/auth_service.py CHANGED
@@ -628,25 +628,45 @@ def get_auth_service() -> AuthService:
628
 
629
 
630
  def init_default_admin(db: Session) -> None:
631
- """Optionally bootstrap an admin user when explicitly configured."""
632
- if not app_config.bootstrap_admin_enabled:
633
- return
634
-
635
- user_count = db.query(User).count()
636
- if user_count != 0:
637
- return
638
-
639
- bootstrap_username = (app_config.bootstrap_admin_username or "").strip()
640
- bootstrap_password = app_config.bootstrap_admin_password or ""
641
 
642
- if not bootstrap_username or not bootstrap_password:
643
  logger.warning(
644
  "Skipping admin bootstrap because LANDPPT_BOOTSTRAP_ADMIN_USERNAME or "
645
  "LANDPPT_BOOTSTRAP_ADMIN_PASSWORD is missing."
646
  )
647
  return
648
 
 
 
 
 
 
 
 
 
 
 
 
 
 
649
  try:
 
 
 
 
 
 
 
 
 
 
 
 
 
650
  auth_service.create_user(
651
  db=db,
652
  username=bootstrap_username,
@@ -655,6 +675,7 @@ def init_default_admin(db: Session) -> None:
655
  )
656
  logger.info("Bootstrapped initial admin user: %s", bootstrap_username)
657
  except Exception as e:
 
658
  logger.error("Failed to bootstrap initial admin user: %s", e)
659
 
660
 
 
628
 
629
 
630
  def init_default_admin(db: Session) -> None:
631
+ """Bootstrap an admin user when configured, without overwriting changed passwords."""
632
+ explicit_bootstrap = bool(app_config.bootstrap_admin_enabled)
633
+ configured_username = (app_config.bootstrap_admin_username or "").strip()
634
+ configured_password = app_config.bootstrap_admin_password or ""
 
 
 
 
 
 
635
 
636
+ if explicit_bootstrap and (not configured_username or not configured_password):
637
  logger.warning(
638
  "Skipping admin bootstrap because LANDPPT_BOOTSTRAP_ADMIN_USERNAME or "
639
  "LANDPPT_BOOTSTRAP_ADMIN_PASSWORD is missing."
640
  )
641
  return
642
 
643
+ bootstrap_username = configured_username or "admin"
644
+ bootstrap_password = configured_password or "admin123456"
645
+ using_builtin_bootstrap = (
646
+ not explicit_bootstrap
647
+ and not configured_username
648
+ and not configured_password
649
+ and bootstrap_username == "admin"
650
+ and bootstrap_password == "admin123456"
651
+ )
652
+
653
+ if not explicit_bootstrap and not using_builtin_bootstrap:
654
+ return
655
+
656
  try:
657
+ existing = db.query(User).filter(User.username == bootstrap_username).first()
658
+ if existing:
659
+ if not existing.is_admin or not existing.is_active:
660
+ existing.is_admin = True
661
+ existing.is_active = True
662
+ db.commit()
663
+ logger.info("Ensured bootstrap admin user is active: %s", bootstrap_username)
664
+ return
665
+
666
+ user_count = db.query(User).count()
667
+ if user_count != 0 and not explicit_bootstrap:
668
+ return
669
+
670
  auth_service.create_user(
671
  db=db,
672
  username=bootstrap_username,
 
675
  )
676
  logger.info("Bootstrapped initial admin user: %s", bootstrap_username)
677
  except Exception as e:
678
+ db.rollback()
679
  logger.error("Failed to bootstrap initial admin user: %s", e)
680
 
681