mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 08:18:42 +00:00
* fix: require read access to the linked table for joins and link writes A token or restricted role limited to one table could read a linked table through joins, and read its primary values by linking rows. * fix: batch linked table checks and cover the remaining link write paths Checks now run once per linked table so queries stay flat, and token batch results no longer take the last table's outcome. Undo, skipped import columns and restricted-view self-links work again; upsert names and view default links are checked. * fix: authorize link row view defaults when they are saved An unreadable default on a shared view blocked everyone from adding rows there, so saving a new one now needs linked-table read. Personal views keep the check on row creation because their owner configures them. * fix: link the checked rows and explain linked table denials Names were resolved again after the check, ids of missing rows passed it, and the UI sends view defaults as values. Denials now name the linked table. * fix: resolve self-link names per field and recheck trusted link defaults Name lookups ran once per row in batch writes, and saved link defaults stayed trusted after the field's linked table or the view's ownership changed. * fix: check the linked table for link filters, assistant row tools and batch services A link row filter and the assistant's row tools returned names of rows from a table the user can't read, and batch services validated links before the check. A default kept from another field type could be re-saved without the check. * fix: restore removed link defaults on undo and refresh them in open views Removing defaults on a link target change lost them on undo and left open views sending the old value. Copying defaults into a view that trusts them skipped the linked table check.
200 lines
6.5 KiB
JavaScript
200 lines
6.5 KiB
JavaScript
import { getPrimaryOrFirstField } from '@baserow/modules/database/utils/field'
|
|
import BigNumber from 'bignumber.js'
|
|
import { DatabaseApplicationType } from '@baserow/modules/database/applicationTypes'
|
|
import { applyRowsUpdated } from '@baserow/modules/database/utils/row'
|
|
|
|
export default {
|
|
emits: ['update'],
|
|
|
|
data() {
|
|
return {
|
|
// If true, then the user must choose which values will be deleted. This only
|
|
// happens if field.link_row_multiple_relationships == false, meaning that only
|
|
// one relationship can persist.
|
|
removingRelationships: false,
|
|
removingRelationShipIds: [],
|
|
}
|
|
},
|
|
computed: {
|
|
// Return the reactive object that can be updated in runtime.
|
|
workspace() {
|
|
return this.$store.getters['workspace/get'](this.workspaceId)
|
|
},
|
|
allTables() {
|
|
const databaseType = DatabaseApplicationType.getType()
|
|
return this.$store.getters['application/getAll'].reduce(
|
|
(tables, application) => {
|
|
if (application.type === databaseType) {
|
|
return tables.concat(application.tables || [])
|
|
}
|
|
return tables
|
|
},
|
|
[]
|
|
)
|
|
},
|
|
canAccessLinkedTable() {
|
|
const linkedTable = this.allTables.find(
|
|
({ id }) => id === this.field.link_row_table_id
|
|
)
|
|
|
|
if (!linkedTable) {
|
|
return false
|
|
}
|
|
|
|
// Restricted view editors can read the table but not list its rows.
|
|
return (
|
|
this.$hasPermission(
|
|
'database.table.read',
|
|
linkedTable,
|
|
this.workspace.id
|
|
) &&
|
|
this.$hasPermission(
|
|
'database.table.list_rows',
|
|
linkedTable,
|
|
this.workspace.id
|
|
) &&
|
|
!this.readOnly
|
|
)
|
|
},
|
|
/**
|
|
* Returns the value of the field that can be used when creating a new row
|
|
* in the linked table starting from the current row.
|
|
*/
|
|
presetsForNewRowInLinkedTable() {
|
|
const presets = {}
|
|
const value = this.primaryFieldLinkRowValue
|
|
if (value && Boolean(value.id)) {
|
|
presets[`field_${this.field.link_row_related_field_id}`] = [value]
|
|
}
|
|
return presets
|
|
},
|
|
/**
|
|
* Returns the value of the field.
|
|
*/
|
|
primaryFieldLinkRowValue() {
|
|
// Set the starting row as preset so that can be used later if the user wants to create a new row
|
|
// starting from the selected row.
|
|
if (!this.allFieldsInTable) {
|
|
return
|
|
}
|
|
const primaryField = getPrimaryOrFirstField(this.allFieldsInTable)
|
|
const primaryFieldType = this.$registry.get(
|
|
'field',
|
|
primaryField._.type.type
|
|
)
|
|
return {
|
|
id: this.row.id,
|
|
value: primaryFieldType.toHumanReadableString(
|
|
primaryField,
|
|
this.row[`field_${primaryField.id}`]
|
|
),
|
|
}
|
|
},
|
|
visibleValues() {
|
|
if (this.removingRelationships) {
|
|
return this.value.filter(
|
|
(v) => !this.removingRelationShipIds.includes(v.id)
|
|
)
|
|
} else {
|
|
return this.value
|
|
}
|
|
},
|
|
canAddValue() {
|
|
return (
|
|
this.field.link_row_multiple_relationships ||
|
|
(Array.isArray(this.value) && this.value.length < 1)
|
|
)
|
|
},
|
|
},
|
|
methods: {
|
|
/**
|
|
* Removes an existing relation from the value.
|
|
*/
|
|
removeValue(event, value, id) {
|
|
// If the cell is in delete relationships mode, then the newly deleted
|
|
// relationship must be added to the list of to be deleted items.
|
|
if (this.removingRelationships) {
|
|
this.removingRelationShipIds.push(id)
|
|
// The backend only accepts one value in that case, so if one is left, then
|
|
// the value is actually updated.
|
|
if (this.removingRelationShipIds.length + 1 === value.length) {
|
|
const newValue = JSON.parse(JSON.stringify(value)).filter(
|
|
(v) => !this.removingRelationShipIds.includes(v.id)
|
|
)
|
|
this.removingRelationships = false
|
|
this.removingRelationShipIds = []
|
|
this.$emit('update', newValue, value)
|
|
}
|
|
return
|
|
}
|
|
|
|
// If it's not allowed to have multiple relationships, then the backend will
|
|
// accept only one value during update. Because it will otherwise fail, the cell
|
|
// is put in a mode where the user must delete all relationships except one.
|
|
if (!this.field.link_row_multiple_relationships && value.length > 2) {
|
|
this.removingRelationships = true
|
|
this.removingRelationShipIds = [id]
|
|
return
|
|
}
|
|
|
|
const newValue = JSON.parse(JSON.stringify(value))
|
|
const index = newValue.findIndex((item) => item.id === id)
|
|
if (index === -1) {
|
|
return
|
|
}
|
|
|
|
newValue.splice(index, 1)
|
|
this.$emit('update', newValue, value)
|
|
},
|
|
/**
|
|
* Adds a new relation to the value. This typically happens via the modal.
|
|
* A created row that links back skips the PATCH: the backend holds the link.
|
|
*/
|
|
addValue(value, { row, primary, created = false }) {
|
|
// Check if the relation already exists.
|
|
for (let i = 0; i < value.length; i++) {
|
|
if (value[i].id === row.id) {
|
|
return
|
|
}
|
|
}
|
|
|
|
// Prepare the new value with all the relations.
|
|
const rowValue = this.$registry
|
|
.get('field', primary.type)
|
|
.toHumanReadableString(primary, row[`field_${primary.id}`])
|
|
|
|
const newValue = JSON.parse(JSON.stringify(value))
|
|
newValue.push({ id: row.id, value: rowValue, order: row.order })
|
|
// Match the backend order sorting
|
|
newValue.sort((a, b) => {
|
|
const orderA = new BigNumber(a.order)
|
|
const orderB = new BigNumber(b.order)
|
|
return orderA.isLessThan(orderB)
|
|
? -1
|
|
: orderA.isEqualTo(orderB)
|
|
? a.id - b.id
|
|
: 1
|
|
})
|
|
|
|
const relatedValue =
|
|
row[`field_${this.field.link_row_related_field_id}`] || []
|
|
if (
|
|
// A listed row's values may predate an unlink made in this modal.
|
|
created &&
|
|
// Other tables get no realtime, only the PATCH response refreshes them.
|
|
this.field.table_id === this.$store.getters['table/getSelectedId'] &&
|
|
relatedValue.some(({ id }) => id === this.row.id)
|
|
) {
|
|
return applyRowsUpdated(this.$registry, this.$store, {
|
|
table_id: this.field.table_id,
|
|
rows: [{ id: this.row.id, [`field_${this.field.id}`]: newValue }],
|
|
// The grid rebuilds the row's metadata from the event.
|
|
metadata: { [this.row.id]: this.row._?.metadata },
|
|
updated_field_ids: [this.field.id],
|
|
})
|
|
}
|
|
this.$emit('update', newValue, value)
|
|
},
|
|
},
|
|
}
|