This is an automated email from the ASF dual-hosted git repository.
jdaugherty pushed a commit to branch fix/multi-tenant-get-by-id
in repository https://gitbox.apache.org/repos/asf/grails-core.git
The following commit(s) were added to refs/heads/fix/multi-tenant-get-by-id by
this push:
new 9d47b98435 Cover lookups by id of an entity mapped with a composite id
9d47b98435 is described below
commit 9d47b9843572f7b9f02980f2c8061271f4294efe
Author: James Daugherty <[email protected]>
AuthorDate: Thu Oct 8 21:51:55 2026 -0400
Cover lookups by id of an entity mapped with a composite id
GORM for MongoDB and the in-memory datastore keep the generated id of a
multi-tenant entity that declares a composite id, so its lookups by id
are restricted to the current tenant as well.
---
.../MongoLookupByIdMultiTenancySpec.groovy | 40 +++++++++++++++++++++-
.../partitioned/PartitionedLookupByIdSpec.groovy | 37 ++++++++++++++++++++
2 files changed, 76 insertions(+), 1 deletion(-)
diff --git
a/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/multitenancy/MongoLookupByIdMultiTenancySpec.groovy
b/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/multitenancy/MongoLookupByIdMultiTenancySpec.groovy
index 67cbddd4d4..6c132095c8 100644
---
a/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/multitenancy/MongoLookupByIdMultiTenancySpec.groovy
+++
b/grails-data-mongodb/core/src/test/groovy/org/grails/datastore/gorm/mongo/multitenancy/MongoLookupByIdMultiTenancySpec.groovy
@@ -56,7 +56,7 @@ class MongoLookupByIdMultiTenancySpec extends
AutoStartedMongoSpec {
'grails.gorm.multiTenancy.tenantResolverClass':
SystemPropertyTenantResolver,
(MongoSettings.SETTING_URL) :
"mongodb://${mongoHost}:${mongoPort}/lookupByIdDb" as String,
]
- this.datastore = new MongoDatastore(config, Memo, NumberedMemo)
+ this.datastore = new MongoDatastore(config, Memo, NumberedMemo,
KeyedMemo)
}
void setup() {
@@ -129,6 +129,32 @@ class MongoLookupByIdMultiTenancySpec extends
AutoStartedMongoSpec {
thrown(DataIntegrityViolationException)
}
+ void 'a lookup by id of an entity mapped with a composite id is restricted
to the current tenant'() {
+ given: 'GORM for MongoDB keeps the generated id of an entity mapped
with a composite id'
+ KeyedMemo.DB.drop()
+ Long ownKeyedId = KeyedMemo.withTenant('own') {
+ KeyedMemo.withNewSession { new KeyedMemo(code: 'A', region:
'north', title: 'Own').save(flush: true).id }
+ }
+ Long otherKeyedId = KeyedMemo.withTenant('other') {
+ KeyedMemo.withNewSession { new KeyedMemo(code: 'B', region:
'south', title: 'Other').save(flush: true).id }
+ }
+
+ expect:
+ KeyedMemo.withNewSession { KeyedMemo.get(ownKeyedId)?.title } == 'Own'
+
+ and: 'an instance of another tenant is not found'
+ KeyedMemo.withNewSession { KeyedMemo.get(otherKeyedId) } == null
+ KeyedMemo.withNewSession { KeyedMemo.read(otherKeyedId) } == null
+ !KeyedMemo.withNewSession { KeyedMemo.exists(otherKeyedId) }
+ KeyedMemo.withNewSession { KeyedMemo.getAll(otherKeyedId, ownKeyedId)
}*.title == [null, 'Own']
+
+ when: 'a proxy for an instance of another tenant is used'
+ KeyedMemo.withNewSession { KeyedMemo.load(otherKeyedId).title }
+
+ then:
+ thrown(DataIntegrityViolationException)
+ }
+
void 'a lookup by id inside withTenant is restricted to that tenant'() {
expect:
Memo.withTenant('other') { Memo.withNewSession {
Memo.get(otherId)?.title } } == 'Other'
@@ -182,3 +208,15 @@ class NumberedMemo implements MultiTenant<NumberedMemo>,
MongoEntity<NumberedMem
String tenantId
String title
}
+
+@Entity
+class KeyedMemo implements MultiTenant<KeyedMemo>, MongoEntity<KeyedMemo> {
+ String code
+ String region
+ String tenantId
+ String title
+
+ static mapping = {
+ id composite: ['code', 'region']
+ }
+}
diff --git
a/grails-datamapping-core-test/src/test/groovy/grails/gorm/multitenancy/partitioned/PartitionedLookupByIdSpec.groovy
b/grails-datamapping-core-test/src/test/groovy/grails/gorm/multitenancy/partitioned/PartitionedLookupByIdSpec.groovy
index 18d9ddb53c..8cae6e6cc1 100644
---
a/grails-datamapping-core-test/src/test/groovy/grails/gorm/multitenancy/partitioned/PartitionedLookupByIdSpec.groovy
+++
b/grails-datamapping-core-test/src/test/groovy/grails/gorm/multitenancy/partitioned/PartitionedLookupByIdSpec.groovy
@@ -140,6 +140,31 @@ class PartitionedLookupByIdSpec extends Specification {
datastore.mappingContext.proxyFactory = proxyFactory
}
+ void 'a lookup by id of an entity mapped with a composite id is restricted
to the current tenant'() {
+ given: 'the in-memory datastore keeps the generated id of an entity
mapped with a composite id'
+ Long ownKeyedId = KeyedNote.withTenant('own') {
+ KeyedNote.withNewSession { new KeyedNote(code: 'A', region:
'north', title: 'Own').save(flush: true).id }
+ }
+ Long otherKeyedId = KeyedNote.withTenant('other') {
+ KeyedNote.withNewSession { new KeyedNote(code: 'B', region:
'south', title: 'Other').save(flush: true).id }
+ }
+
+ expect:
+ KeyedNote.withNewSession { KeyedNote.get(ownKeyedId)?.title } == 'Own'
+
+ and: 'an instance of another tenant is not found'
+ KeyedNote.withNewSession { KeyedNote.get(otherKeyedId) } == null
+ KeyedNote.withNewSession { KeyedNote.read(otherKeyedId) } == null
+ !KeyedNote.withNewSession { KeyedNote.exists(otherKeyedId) }
+ KeyedNote.withNewSession { KeyedNote.getAll(otherKeyedId, ownKeyedId)
}*.title == [null, 'Own']
+
+ when: 'a proxy for an instance of another tenant is used'
+ KeyedNote.withNewSession { KeyedNote.load(otherKeyedId).title }
+
+ then:
+ thrown(DataIntegrityViolationException)
+ }
+
void 'a lookup by id inside withTenant is restricted to that tenant'() {
expect:
Note.withTenant('other') { Note.withNewSession {
Note.get(otherId)?.title } } == 'Other'
@@ -198,6 +223,18 @@ class Note implements MultiTenant<Note> {
String tenantId
}
+@Entity
+class KeyedNote implements MultiTenant<KeyedNote> {
+ String code
+ String region
+ String title
+ String tenantId
+
+ static mapping = {
+ id composite: ['code', 'region']
+ }
+}
+
@Entity
class Label {
String name