Skip to content

Commit 65b3396

Browse files
committed
Current tenant is now retrieved from injected 'currentTenant' bean. If it's null then tenant parameter supplied in the method call is used
1 parent 7fa5b89 commit 65b3396

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

grails-app/services/grails/plugins/selection/repo/SelectionRepositoryService.groovy

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class SelectionRepositoryService {
2424

2525
static transactional = true
2626

27+
def currentTenant
28+
2729
/**
2830
* List selections stored in the repository.
2931
*
@@ -38,15 +40,16 @@ class SelectionRepositoryService {
3840
* @return a list of selections matching the parameters. Each list entry is a Map with selection properties
3941
*/
4042
List<Map<String, Object>> list(String location, String username = null, Long tenant = null) {
43+
Long tenantId = currentTenant?.get() ?: tenant
4144
SelectionRepository.createCriteria().list([sort: 'name', order: 'asc']) {
4245
eq('location', location)
4346
if (username) {
4447
eq('username', username)
4548
} else {
4649
isNull('username')
4750
}
48-
if (tenant != null) {
49-
eq('tenantId', tenant)
51+
if (tenantId != null) {
52+
eq('tenantId', tenantId)
5053
}
5154
}.collect {[id: it.id, name: it.name, description: it.description, uri:it.uri]}
5255
}
@@ -62,6 +65,10 @@ class SelectionRepositoryService {
6265
if (!s) {
6366
throw new IllegalArgumentException("No selection found with id [$id]")
6467
}
68+
def tenant = currentTenant?.get()
69+
if(tenant != null && tenant != s.tenantId) {
70+
throw new IllegalArgumentException("No selection found with id [$id]")
71+
}
6572
s.uri
6673
}
6774

@@ -77,6 +84,7 @@ class SelectionRepositoryService {
7784
* @return a selection URI suitable for retrieving/invoking the persisted selection
7885
*/
7986
URI put(URI selection, String location, String username, String name, String description = null, Long tenant = null) {
87+
Long tenantId = currentTenant?.get() ?: tenant
8088
// Try to find persistent selection.
8189
def s = SelectionRepository.createCriteria().get() {
8290
eq('location', location)
@@ -86,14 +94,14 @@ class SelectionRepositoryService {
8694
} else {
8795
isNull('username')
8896
}
89-
if (tenant != null) {
90-
eq('tenantId', tenant)
97+
if (tenantId != null) {
98+
eq('tenantId', tenantId)
9199
}
92100
}
93101

94102
if (!s) {
95103
// Create new persistent selection
96-
s = new SelectionRepository(tenantId: tenant, location: location, username: username, name: name)
104+
s = new SelectionRepository(tenantId: tenantId, location: location, username: username, name: name)
97105
}
98106

99107
// Update URI and description.
@@ -114,6 +122,10 @@ class SelectionRepositoryService {
114122
if (!s) {
115123
throw new IllegalArgumentException("No selection found with id [$id]")
116124
}
125+
def tenant = currentTenant?.get()
126+
if(tenant != null && tenant != s.tenantId) {
127+
throw new IllegalArgumentException("No selection found with id [$id]")
128+
}
117129
s.delete()
118130
}
119131
}

test/integration/grails/plugins/selection/repo/SelectionRepositoryServiceTests.groovy

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package grails.plugins.selection.repo
22

33
import test.TestEntity
44

5+
import java.util.concurrent.atomic.AtomicLong
6+
57
/**
6-
* Tests for SelectionStorageService
8+
* Tests for SelectionRepositoryService
79
*/
810
class SelectionRepositoryServiceTests extends GroovyTestCase {
911

@@ -38,15 +40,26 @@ class SelectionRepositoryServiceTests extends GroovyTestCase {
3840

3941
void testTenants() {
4042
// Store same selection under 5 different tenants.
41-
5.times {tenant ->
42-
selectionRepositoryService.put(new URI("gorm://testEntity/list?name=A*"), "testEntity", "joe.user", "Joe's private A people", null, tenant)
43+
(1..5).each { tenant ->
44+
assert selectionRepositoryService.put(new URI("gorm://testEntity/list?name=A*"), "testEntity", "joe.user", "Joe's private A people", null, tenant)
4345
}
4446

4547
// Each tenant should only se it's own selection.
46-
5.times {tenant ->
48+
(1..5).each { tenant ->
4749
assert selectionRepositoryService.list("testEntity", "joe.user", tenant).size() == 1
4850
}
4951

52+
selectionRepositoryService.currentTenant = new AtomicLong()
53+
54+
try {
55+
(1..5).each { tenant ->
56+
selectionRepositoryService.currentTenant.set(tenant)
57+
assert selectionRepositoryService.list("testEntity", "joe.user").size() == 1
58+
}
59+
} finally {
60+
selectionRepositoryService.currentTenant = null
61+
}
62+
5063
assert selectionRepositoryService.list("testEntity", "joe.user").size() == 5
5164
}
5265
}

0 commit comments

Comments
 (0)