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:
padreug 2025-11-07 23:06:24 +01:00
parent fc12dae435
commit d6a1c6e5b3
3 changed files with 142 additions and 7 deletions

View file

@ -5,6 +5,8 @@ window.app = Vue.createApp({
return {
permissions: [],
accounts: [],
users: [],
filteredUsers: [],
loading: false,
granting: false,
revoking: false,
@ -48,6 +50,15 @@ window.app = Vue.createApp({
}))
},
userOptions() {
const users = this.filteredUsers.length > 0 ? this.filteredUsers : this.users
return users.map(user => ({
id: user.id,
username: user.username || user.id,
label: user.username ? `${user.username} (${user.id.substring(0, 8)}...)` : user.id
}))
},
isGrantFormValid() {
return !!(
this.grantForm.user_id &&
@ -130,6 +141,48 @@ window.app = Vue.createApp({
}
},
async loadUsers() {
if (!this.isSuperUser) {
return
}
try {
const response = await LNbits.api.request(
'GET',
'/castle/api/v1/admin/castle-users',
this.g.user.wallets[0].adminkey
)
this.users = response.data || []
this.filteredUsers = []
} catch (error) {
console.error('Failed to load users:', error)
this.$q.notify({
type: 'negative',
message: 'Failed to load users',
caption: error.message || 'Unknown error',
timeout: 5000
})
}
},
filterUsers(val, update) {
if (val === '') {
update(() => {
this.filteredUsers = []
})
return
}
update(() => {
const needle = val.toLowerCase()
this.filteredUsers = this.users.filter(user => {
const username = user.username || ''
const userId = user.id || ''
return username.toLowerCase().includes(needle) || userId.toLowerCase().includes(needle)
})
})
},
async grantPermission() {
if (!this.isGrantFormValid) {
this.$q.notify({
@ -283,7 +336,10 @@ window.app = Vue.createApp({
if (this.g.user.wallets && this.g.user.wallets.length > 0) {
await this.loadAccounts()
if (this.isSuperUser) {
await this.loadPermissions()
await Promise.all([
this.loadPermissions(),
this.loadUsers()
])
}
}
}