feat: group the Authenticate as picker into members and identities (#6577)

This commit is contained in:
Peter Evans authored and GitHub committed 2026-11-05 11:43:42 +00:00
1 parent b95ccd0f8d
commit 73aa028b7b
6 files changed
+187 -78

No files matched your search

@@ -9,6 +9,12 @@
}
}
// The subjects have a description, so they're taller than the four one-line
// items the list fits by default.
.local-baserow-form__subject .select__items {
max-height: 320px;
}
.field-mapping-form__placeholder {
width: 100%;
height: 36px;
@@ -32,6 +32,14 @@ export default (client) =>
data: { ...response.data, results, next: null },
}
},
/** Fetch one page of the Agents of the workspace. */
listPage(workspaceId, { page = 1, search = '', size = 100 } = {}) {
const params = { page, size }
if (search) {
params.search = search
}
return client.get(`/agents/workspace/${workspaceId}/`, { params })
},
update(agentId, values) {
return client.patch(`/agents/${agentId}/`, values)
},
@@ -2,26 +2,39 @@
<div>
<FormGroup :label="$t('localBaserowForm.subject')" required small-label>
<PaginatedDropdown
class="local-baserow-form__subject"
:value="authenticationSubject"
:fetch-page="fetchAgentPage"
:fetch-page="fetchIdentityPage"
:add-empty-item="false"
:initial-display-name="initialSubjectName"
:include-display-name-in-selected-event="true"
@input="setAuthenticationSubject"
>
<template #items="{ results }">
<DropdownItem
:name="$t('localBaserowForm.currentUser')"
value="user"
icon="iconoir-user"
/>
<DropdownItem
v-for="subject in results"
:key="subject.id"
:name="subject.name"
:value="subject.id"
icon="iconoir-lock"
/>
<DropdownSection :title="userSubjectType.getTabDisplayName()">
<DropdownItem
:name="$t('localBaserowForm.currentUser')"
:value="currentUserKey"
:icon="userSubjectType.iconClass"
:description="$t('localBaserowForm.currentUserDescription')"
/>
</DropdownSection>
<DropdownSection :title="agentSubjectType.getTabDisplayName()">
<DropdownItem
v-for="identity in results"
:key="identity.id"
:name="identity.name"
:value="identity.id"
:icon="agentSubjectType.iconClass"
:description="identity.description"
/>
<DropdownItem
v-if="hasIdentities === false"
:name="$t('localBaserowForm.noIdentities')"
value="no-identities"
disabled
/>
</DropdownSection>
</template>
</PaginatedDropdown>
<template #helper>
@@ -34,10 +47,15 @@
<script>
import form from '@baserow/modules/core/mixins/form'
import PaginatedDropdown from '@baserow/modules/core/components/PaginatedDropdown'
import SubjectService from '@baserow/modules/core/services/subject'
import DropdownSection from '@baserow/modules/core/components/DropdownSection'
import AgentService from '@baserow/modules/core/services/agent'
import {
AgentSubjectType,
UserSubjectType,
} from '@baserow/modules/core/subjectTypes.jsx'
export default {
components: { PaginatedDropdown },
components: { PaginatedDropdown, DropdownSection },
mixins: [form],
props: {
application: {
@@ -51,15 +69,27 @@ export default {
authorized_subject_id:
this.defaultValues.authorized_subject?.id || null,
authorized_subject_type:
this.defaultValues.authorized_subject?.type || 'auth.User',
this.defaultValues.authorized_subject?.type ||
UserSubjectType.getType(),
},
allowedValues: ['authorized_subject_id', 'authorized_subject_type'],
// Whether the workspace has identities, unknown until they're fetched.
hasIdentities: null,
}
},
computed: {
currentUserId() {
return this.$store.getters['auth/getUserId']
},
userSubjectType() {
return this.$registry.get('subject', UserSubjectType.getType())
},
agentSubjectType() {
return this.$registry.get('subject', AgentSubjectType.getType())
},
currentUserKey() {
return this.userSubjectType.getKey({ id: this.currentUserId })
},
initialSubjectName() {
const subject = this.defaultValues.authorized_subject
// A trashed agent isn't selectable, so show nothing selected and let the
@@ -76,36 +106,63 @@ export default {
}
return subjectType.getDisplayName(subject)
},
authenticationSubject: {
get() {
return this.values.authorized_subject_type === 'auth.User' &&
(this.values.authorized_subject_id === null ||
this.values.authorized_subject_id === this.currentUserId)
? 'user'
: `${this.values.authorized_subject_type}:${this.values.authorized_subject_id}`
},
set(value) {
if (value === 'user') {
this.values.authorized_subject_id = this.currentUserId
this.values.authorized_subject_type = 'auth.User'
}
},
authenticationSubject() {
const type = this.values.authorized_subject_type
const id =
type === UserSubjectType.getType() &&
this.values.authorized_subject_id === null
? this.currentUserId
: this.values.authorized_subject_id
return this.$registry.get('subject', type).getKey({ id })
},
},
methods: {
fetchAgentPage(page, search) {
return SubjectService(this.$client).list(this.application.workspace.id, {
page,
search,
subjectTypes: 'core.Agent',
/**
* Fetches a page of the identities of the workspace, with their role as
* description. The identities endpoint, unlike the subjects one, has it.
*/
async fetchIdentityPage(page, search) {
const workspaceId = this.application.workspace.id
const [response, roles] = await Promise.all([
AgentService(this.$client).listPage(workspaceId, { page, search }),
this.fetchRoles(workspaceId),
])
if (!search) {
this.hasIdentities = response.data.count > 0
}
const results = response.data.results.map((agent) => {
const role = roles.find(({ uid }) => uid === agent.role_uid)
return {
id: this.agentSubjectType.getKey(agent),
subject_id: this.agentSubjectType.getId(agent),
name: agent.name,
description: role
? this.$t('localBaserowForm.identityRole', { role: role.name })
: null,
}
})
return { ...response, data: { ...response.data, results } }
},
/**
* The roles of the workspace, to name the role of each identity. The
* identities are listed without them if they can't be fetched.
*/
async fetchRoles(workspaceId) {
const workspace = this.$store.getters['workspace/get'](workspaceId)
try {
await this.$store.dispatch('workspace/fetchRoles', workspace)
} catch {
return []
}
return workspace._.roles || []
},
setAuthenticationSubject({ value, item }) {
if (value === 'user') {
this.authenticationSubject = value
if (value === this.currentUserKey) {
this.values.authorized_subject_id = this.currentUserId
this.values.authorized_subject_type = UserSubjectType.getType()
} else {
this.values.authorized_subject_id = item.subject_id
this.values.authorized_subject_type = item.subject_type
this.values.authorized_subject_type = AgentSubjectType.getType()
}
},
},
@@ -45,10 +45,6 @@ export class LocalBaserowIntegrationType extends IntegrationType {
return LocalBaserowForm
}
get warning() {
return this.app.$i18n.t('localBaserowIntegrationType.localBaserowWarning')
}
getDefaultValues() {
const user = this.app.$store.getters['auth/getUserObject']
return {
@@ -14,8 +14,7 @@
"localBaserowSubjectSummary": "Local Baserow - {type}: {name}",
"localBaserowNoUser": "Local Baserow - Not configured",
"errorTrashedIdentity": "The authorized identity has been trashed",
"errorNoUser": "No user or identity has authorized this integration.",
"localBaserowWarning": "Authorizing your account gives everyone who has edit permissions to the application full access to the data you have access to. It's possible to create a second user, give the right permissions and use that one."
"errorNoUser": "No user or identity has authorized this integration."
},
"aiIntegrationType": {
"inheritingWorkspace": "Inheriting workspace AI settings",
@@ -124,6 +123,9 @@
"localBaserowForm": {
"subject": "Authenticate as",
"currentUser": "My user account",
"currentUserDescription": "Warning: using your account gives everyone who has edit permissions to the application full access to the data you have access to.",
"identityRole": "Workspace role: {role}",
"noIdentities": "You do not have any identities yet",
"subjectMessage": "Actions use the permissions of the selected user or identity. Identity actions are identified as such in the audit log."
},
"aiForm": {
@@ -3,29 +3,32 @@ import flushPromises from 'flush-promises'
import LocalBaserowForm from '@baserow/modules/integrations/localBaserow/components/integrations/LocalBaserowForm'
import { TestApp } from '@baserow/test/helpers/testApp'
const sections = (wrapper) =>
wrapper.findAllComponents({ name: 'DropdownSection' }).map((section) => ({
title: section.props('title'),
items: section
.findAllComponents({ name: 'DropdownItem' })
.map((item) => [item.props('name'), item.props('description')]),
}))
describe('LocalBaserowForm', () => {
let testApp = null
beforeEach(() => {
beforeEach(async () => {
testApp = new TestApp()
testApp.store.state.auth.user = { id: 1 }
testApp.mock.onGet('/subjects/').reply(200, {
await testApp.store.dispatch('workspace/forceCreate', {
id: 1,
name: 'Acme',
users: [],
})
testApp.mock.onGet('/agents/workspace/1/').reply(200, {
count: 2,
next: null,
previous: null,
results: [
{
id: 'core.Agent:10',
subject_id: 10,
subject_type: 'core.Agent',
name: 'Writer agent',
},
{
id: 'core.Agent:11',
subject_id: 11,
subject_type: 'core.Agent',
name: 'Reader agent',
},
{ id: 10, name: 'Writer agent', role_uid: 'MEMBER' },
{ id: 11, name: 'Reader agent', role_uid: 'UNKNOWN' },
],
})
})
@@ -43,29 +46,71 @@ describe('LocalBaserowForm', () => {
})
}
test('lists agents and emits the selected typed subject', async () => {
const wrapper = await mountComponent()
async function openDropdown(wrapper) {
await wrapper.find('.dropdown__selected').trigger('click')
await flushPromises()
}
async function selectItem(wrapper, name) {
await wrapper
.findAllComponents({ name: 'DropdownItem' })
.find((item) => item.props('name') === name)
.find('.select__item-link')
.trigger('click')
await flushPromises()
}
test('lists the current user and the identities in their sections', async () => {
const wrapper = await mountComponent()
await openDropdown(wrapper)
expect(testApp.mock.history.get.at(-1).params).toEqual({
workspace_id: 1,
page: 1,
search: '',
subject_types: 'core.Agent',
size: 100,
})
const items = wrapper.findAllComponents({ name: 'DropdownItem' })
expect(items.map((item) => item.props('name'))).toEqual([
'localBaserowForm.currentUser',
'Writer agent',
'Reader agent',
// An identity whose role isn't one of the workspace roles has no
// description.
expect(sections(wrapper)).toEqual([
{
title: 'subjectType.members',
items: [
[
'localBaserowForm.currentUser',
'localBaserowForm.currentUserDescription',
],
],
},
{
title: 'subjectType.identities',
items: [
['Writer agent', 'localBaserowForm.identityRole'],
['Reader agent', null],
],
},
])
})
await items.at(1).find('.select__item-link').trigger('click')
await flushPromises()
test('tells when the workspace has no identities yet', async () => {
testApp.mock
.onGet('/agents/workspace/1/')
.reply(200, { count: 0, next: null, previous: null, results: [] })
const wrapper = await mountComponent()
await openDropdown(wrapper)
const identities = sections(wrapper)[1]
expect(identities.items).toEqual([['localBaserowForm.noIdentities', null]])
await selectItem(wrapper, 'localBaserowForm.noIdentities')
expect(wrapper.emitted('values-changed')).toBeUndefined()
})
test('emits the selected identity as typed subject', async () => {
const wrapper = await mountComponent()
await openDropdown(wrapper)
await selectItem(wrapper, 'Writer agent')
expect(wrapper.find('.dropdown__selected-text').text()).toBe('Writer agent')
expect(wrapper.emitted('values-changed').at(-1)[0]).toEqual({
authorized_subject_id: 10,
authorized_subject_type: 'core.Agent',
@@ -128,13 +173,8 @@ describe('LocalBaserowForm', () => {
const wrapper = await mountComponent({
authorized_subject: { id: 11, type: 'core.Agent', name: 'Reader agent' },
})
await flushPromises()
await wrapper
.findComponent({ name: 'DropdownItem', props: { value: 'user' } })
.find('.select__item-link')
.trigger('click')
await flushPromises()
await openDropdown(wrapper)
await selectItem(wrapper, 'localBaserowForm.currentUser')
expect(wrapper.emitted('values-changed').at(-1)[0]).toEqual({
authorized_subject_id: 1,