Adds super user and config setup

Adds super user role to restrict settings changes.

Improves the settings screen to only allow super users to make modifications.

Adds a warning banner if the Castle wallet is not configured.

Changes admin key to inkey for fetching settings.
This fixes an issue where settings weren't accessible.

Adds a validation to require the Castle wallet ID when updating settings.
This commit is contained in:
padreug 2025-10-22 14:45:18 +02:00
parent 29983cedb7
commit 31344607c6
3 changed files with 106 additions and 16 deletions

View file

@ -1,8 +1,13 @@
from http import HTTPStatus
from fastapi import APIRouter, Depends, HTTPException
from lnbits.core.models import WalletTypeInfo
from lnbits.decorators import require_admin_key, require_invoice_key
from lnbits.core.models import User, WalletTypeInfo
from lnbits.decorators import (
check_super_user,
check_user_exists,
require_admin_key,
require_invoice_key,
)
from lnbits.utils.exchange_rates import allowed_currencies, fiat_amount_as_satoshis
from .crud import (
@ -37,6 +42,20 @@ from .services import get_settings, update_settings
castle_api_router = APIRouter()
# ===== HELPER FUNCTIONS =====
async def check_castle_wallet_configured() -> str:
"""Ensure castle wallet is configured, return wallet_id"""
settings = await get_settings("admin")
if not settings or not settings.castle_wallet_id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Castle wallet not configured. Please contact the super user to configure the Castle wallet in settings.",
)
return settings.castle_wallet_id
# ===== UTILITY ENDPOINTS =====
@ -459,18 +478,27 @@ async def api_pay_user(
@castle_api_router.get("/api/v1/settings")
async def api_get_settings(
wallet: WalletTypeInfo = Depends(require_admin_key),
user: User = Depends(check_user_exists),
) -> CastleSettings:
"""Get Castle settings (admin only)"""
"""Get Castle settings"""
user_id = "admin"
return await get_settings(user_id)
settings = await get_settings(user_id)
# Return empty settings if not configured (so UI can show setup screen)
if not settings:
return CastleSettings()
return settings
@castle_api_router.put("/api/v1/settings")
async def api_update_settings(
data: CastleSettings,
wallet: WalletTypeInfo = Depends(require_admin_key),
user: User = Depends(check_super_user),
) -> CastleSettings:
"""Update Castle settings (admin only)"""
"""Update Castle settings (super user only)"""
if not data.castle_wallet_id:
raise HTTPException(
status_code=HTTPStatus.BAD_REQUEST,
detail="Castle wallet ID is required",
)
user_id = "admin"
return await update_settings(user_id, data)