Enables user selection for permissions
Replaces the user ID input field with a user selection dropdown, allowing administrators to search and select users for permission management. This simplifies the process of assigning permissions and improves user experience. Fetches Castle users via a new API endpoint and filters them based on search input. Only users with Castle accounts (receivables, payables, equity, or permissions) are listed.
This commit is contained in:
parent
fc12dae435
commit
d6a1c6e5b3
3 changed files with 142 additions and 7 deletions
65
views_api.py
65
views_api.py
|
|
@ -1339,6 +1339,71 @@ async def api_get_all_users(
|
|||
return users
|
||||
|
||||
|
||||
@castle_api_router.get("/api/v1/admin/castle-users")
|
||||
async def api_get_castle_users(
|
||||
wallet: WalletTypeInfo = Depends(require_admin_key),
|
||||
) -> list[dict]:
|
||||
"""
|
||||
Get all users who have Castle accounts (receivables, payables, equity, or permissions).
|
||||
This returns only users who are actively using Castle, not all LNbits users.
|
||||
Admin only.
|
||||
"""
|
||||
from lnbits.core.crud.users import get_user
|
||||
from .crud import get_all_equity_eligible_users
|
||||
|
||||
# Get all user-specific accounts (Receivable/Payable/Equity)
|
||||
all_accounts = await get_all_accounts()
|
||||
user_accounts = [acc for acc in all_accounts if acc.user_id is not None]
|
||||
|
||||
# Get all users who have permissions
|
||||
all_permissions = []
|
||||
for account in all_accounts:
|
||||
account_perms = await get_account_permissions(account.id)
|
||||
all_permissions.extend(account_perms)
|
||||
|
||||
# Get all equity-eligible users
|
||||
equity_users = await get_all_equity_eligible_users()
|
||||
|
||||
# Collect unique user IDs
|
||||
user_ids = set()
|
||||
|
||||
# Add users with accounts
|
||||
for acc in user_accounts:
|
||||
user_ids.add(acc.user_id)
|
||||
|
||||
# Add users with permissions
|
||||
for perm in all_permissions:
|
||||
user_ids.add(perm.user_id)
|
||||
|
||||
# Add equity-eligible users
|
||||
for equity in equity_users:
|
||||
user_ids.add(equity.user_id)
|
||||
|
||||
# Build user list with enriched data
|
||||
users = []
|
||||
for user_id in user_ids:
|
||||
# Get user details from core
|
||||
user = await get_user(user_id)
|
||||
|
||||
# Use username if available, otherwise use user_id
|
||||
username = user.username if user and user.username else None
|
||||
|
||||
# Get user's wallet setting if exists
|
||||
user_wallet = await get_user_wallet(user_id)
|
||||
|
||||
users.append({
|
||||
"id": user_id,
|
||||
"user_id": user_id, # Compatibility with existing code
|
||||
"username": username,
|
||||
"user_wallet_id": user_wallet.user_wallet_id if user_wallet else None,
|
||||
})
|
||||
|
||||
# Sort by username (None values last)
|
||||
users.sort(key=lambda x: (x["username"] is None, x["username"] or "", x["user_id"]))
|
||||
|
||||
return users
|
||||
|
||||
|
||||
@castle_api_router.get("/api/v1/user/wallet")
|
||||
async def api_get_user_wallet(
|
||||
user: User = Depends(check_user_exists),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue