100% open-source IoT Platform - Integrate your devices, add intelligence, build custom apps
Java asset-managementbuildingdashboarddataplatformdevice-managementedge-computingenergyenergy-managementenergy-monitorfleet-managementfleet-telematicsiotiot-applicationiot-platformmodbusmqttmqtt-brokerota-updaterules-enginesmartcity
Fix user query scoping and filtering in rules facades (#3475)
Eric Bariaux74894fde3cf6
diff --git a/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.java b/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.javaindex a5b217cbc..ff8bf536c 100644--- a/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.java+++ b/manager/src/main/java/org/openremote/manager/rules/facade/UsersFacade.java@@ -60,6 +60,13 @@ public class UsersFacade<T extends Ruleset> extends Users {@Overridepublic Stream<String> getResults(UserQuery userQuery) {+ // Rule targets can reuse the caller's query. Isolate nested predicates as well, since the+ // facade and identity provider both modify filters during execution.+ if (userQuery == null) {+ throw new IllegalArgumentException("Unable to copy user query");+ }+ userQuery = new UserQuery(userQuery);+// Do security checks to ensure correct scoping// No restriction for global rulesetsif (RealmRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {@@ -71,7 +78,13 @@ public class UsersFacade<T extends Ruleset> extends Users {.orElseThrow(() -> new IllegalArgumentException("Realm ID missing: " + rulesEngineId)));} else if (AssetRuleset.class.isAssignableFrom(rulesEngineId.getScope())) {- userQuery.realmPredicate = null;+ // Asset links must not expose users from outside the rule's realm.+ userQuery.realmPredicate =+ new RealmPredicate(+ rulesEngineId+ .getRealm()+ .orElseThrow(+ () -> new IllegalArgumentException("Realm ID missing: " + rulesEngineId)));String assetId =rulesEngineId.getAssetId()@@ -80,17 +93,35 @@ public class UsersFacade<T extends Ruleset> extends Users {if (userQuery.pathPredicate == null || userQuery.pathPredicate.path == null) {userQuery.pathPredicate = new PathPredicate(assetId);- } else {+ } else if (!Arrays.asList(userQuery.pathPredicate.path).contains(assetId)) {+ // Retain caller restrictions without duplicating an already supplied scope asset.List<String> path = new ArrayList<>(Arrays.asList(userQuery.pathPredicate.path));path.add(assetId);userQuery.pathPredicate.path = path.toArray(new String[0]);}}- // Prevent system users being retrieved- userQuery.attributes(+ // Preserve caller filters and avoid duplicating a full exclusion already supplied by the+ // caller.+ UserQuery.AttributeValuePredicate systemAccountExclusion =new UserQuery.AttributeValuePredicate(- true, new StringPredicate(User.SYSTEM_ACCOUNT_ATTRIBUTE), null));+ true, new StringPredicate(User.SYSTEM_ACCOUNT_ATTRIBUTE), null);+ boolean excludesSystemAccounts =+ userQuery.attributes != null+ && Arrays.stream(userQuery.attributes)+ .anyMatch(+ attribute ->+ attribute.negated+ && attribute.value == null+ && systemAccountExclusion.name.equals(attribute.name));+ if (!excludesSystemAccounts) {+ UserQuery.AttributeValuePredicate[] attributes =+ userQuery.attributes == null+ ? new UserQuery.AttributeValuePredicate[1]+ : Arrays.copyOf(userQuery.attributes, userQuery.attributes.length + 1);+ attributes[attributes.length - 1] = systemAccountExclusion;+ userQuery.attributes(attributes);+ }// Prevent service users being retrieveduserQuery.serviceUsers(false);diff --git a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.javaindex 6cfa228fd..49705385b 100644--- a/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java+++ b/manager/src/main/java/org/openremote/manager/security/ManagerIdentityProvider.java@@ -113,22 +113,6 @@ public interface ManagerIdentityProvider extends IdentityProvider {List<Object> parameters = new ArrayList<>();final UserQuery userQuery = query != null ? query : new UserQuery();- // Limit to regular/service users- if (userQuery.serviceUsers != null) {- // Not ideal mutating the query but seems excessive cloning it- if (userQuery.usernames == null) {- userQuery.usernames = new StringPredicate[1];- userQuery.usernames(- new StringPredicate(AssetQuery.Match.BEGIN, User.SERVICE_ACCOUNT_PREFIX)- .negate(!userQuery.serviceUsers));- } else {- userQuery.usernames = Arrays.copyOf(userQuery.usernames, userQuery.usernames.length + 1);- userQuery.usernames[userQuery.usernames.length - 1] =- new StringPredicate(AssetQuery.Match.BEGIN, User.SERVICE_ACCOUNT_PREFIX)- .negate(!userQuery.serviceUsers);- }- }-// BUILD SELECTsb.append("SELECT u.*, (SELECT C.SECRET FROM PUBLIC.CLIENT C WHERE C.ID = SERVICE_ACCOUNT_CLIENT_LINK) as secret, r.name as realm");@@ -163,6 +147,15 @@ public interface ManagerIdentityProvider extends IdentityProvider {sb.append(" AND u.id IN (?").append(parameters.size() + 1).append(")");parameters.add(Arrays.asList(userQuery.ids));}+ if (userQuery.serviceUsers != null) {+ // Account type must constrain the username alternatives, not add another OR match.+ StringPredicate serviceUserPredicate =+ new StringPredicate(AssetQuery.Match.BEGIN, false, User.SERVICE_ACCOUNT_PREFIX)+ .negate(!userQuery.serviceUsers);+ sb.append(" AND upper(u.username)");+ sb.append(toSQLParameter(serviceUserPredicate, parameters.size() + 1, false));+ parameters.add(serviceUserPredicate.prepareValue());+ }if (userQuery.usernames != null && userQuery.usernames.length > 0) {sb.append(" and (FALSE");diff --git a/manager/src/test/groovy/org/openremote/manager/rules/facade/UsersFacadeQueryTest.groovy b/manager/src/test/groovy/org/openremote/manager/rules/facade/UsersFacadeQueryTest.groovynew file mode 100644index 000000000..b05bc24aa--- /dev/null+++ b/manager/src/test/groovy/org/openremote/manager/rules/facade/UsersFacadeQueryTest.groovy@@ -0,0 +1,308 @@+/*+ * Copyright 2026, OpenRemote Inc.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * SPDX-License-Identifier: AGPL-3.0-or-later+ */+package org.openremote.manager.rules.facade++import org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.notification.NotificationService+import org.openremote.manager.rules.RulesEngineId+import org.openremote.manager.security.ManagerIdentityProvider+import org.openremote.manager.security.ManagerIdentityService+import org.openremote.model.query.AssetQuery+import org.openremote.model.query.UserQuery+import org.openremote.model.query.filter.PathPredicate+import org.openremote.model.query.filter.RealmPredicate+import org.openremote.model.query.filter.StringPredicate+import org.openremote.model.security.User+import spock.lang.Specification++class UsersFacadeQueryTest extends Specification {++ def "Reusing a #scope query with #filterCase keeps system-account exclusion bounded"() {+ given: "the query object retained across rule firings"+ def callerFilters = createFilters() as UserQuery.AttributeValuePredicate[]+ def query = new UserQuery().attributes(callerFilters)+ def expectedFilters = snapshot(callerFilters)+ if (!alreadyExcluded) {+ expectedFilters.add(exclusion().toString())+ }+ List<List<String>> submittedFilters = []+ def provider = Stub(ManagerIdentityProvider) {+ queryUsers(_ as UserQuery) >> { UserQuery submitted ->+ // Snapshot at execution time; retaining the mutable query would hide earlier growth.+ submittedFilters.add(snapshot(submitted.attributes))+ new User[0]+ }+ }+ def identityService = Stub(ManagerIdentityService) {+ getIdentityProvider() >> provider+ }+ def engineId = scope == "global" ? new RulesEngineId() :+ scope == "realm" ? new RulesEngineId("building") : new RulesEngineId("building", "scope-asset")+ def facade = new UsersFacade(engineId, Stub(AssetStorageService), Stub(NotificationService), identityService)++ when: "the same query is executed repeatedly"+ 100.times { facade.getResults(query).toList() }+ def filterCounts = submittedFilters.collect { it.size() }.unique()++ then: "every execution preserves the caller filters and has just one full system-account exclusion"+ submittedFilters.size() == 100+ filterCounts == [expectedFilters.size()]+ submittedFilters.each { assert it == expectedFilters }++ where:+ [scope, filterCase, createFilters, alreadyExcluded] << ["global", "realm", "asset"].collectMany { scope ->+ [+ [+ "no attributes",+ { -> null },+ false+ ],+ [+ "empty attributes",+ { -> [] },+ false+ ],+ [+ "a caller filter",+ { -> [department()] },+ false+ ],+ [+ "an existing full exclusion",+ { -> [department(), exclusion()] },+ true+ ],+ [+ "a value-specific exclusion",+ { -> [department(), exclusion("true")] },+ false+ ],+ [+ "a positive system filter",+ {+ -> [+ department(),+ new UserQuery.AttributeValuePredicate(false, new StringPredicate(User.SYSTEM_ACCOUNT_ATTRIBUTE))+ ]+ },+ false+ ]+ ].collect { row -> [scope] + row }+ }+ }++ def "Reusing an asset query with #pathCase keeps the scope path bounded"() {+ given: "an asset-scoped rule retaining its user query across firings"+ def query = new UserQuery().assetPath(callerPath)+ List<List<String>> submittedPaths = []+ def provider = Stub(ManagerIdentityProvider) {+ queryUsers(_ as UserQuery) >> { UserQuery submitted ->+ submittedPaths.add(submitted.pathPredicate.path.toList())+ new User[0]+ }+ }+ def identityService = Stub(ManagerIdentityService) {+ getIdentityProvider() >> provider+ }+ def facade = new UsersFacade(new RulesEngineId("building", "scope-asset"),+ Stub(AssetStorageService), Stub(NotificationService), identityService)++ when: "the same query is executed repeatedly"+ 100.times { facade.getResults(query).toList() }+ def pathLengths = submittedPaths.collect { it.size() }.unique()++ then: "every execution retains the caller's path and includes the scope asset only once"+ submittedPaths.size() == 100+ pathLengths == [expectedPath.size()]+ submittedPaths.each { assert it == expectedPath }++ where:+ pathCase | callerPath | expectedPath+ "no predicate" | null | ["scope-asset"]+ "null path values" | new PathPredicate() | ["scope-asset"]+ "empty path values" | new PathPredicate([] as String[]) | ["scope-asset"]+ "a descendant filter" | new PathPredicate("child") | ["child", "scope-asset"]+ "an outside filter" | new PathPredicate("outside") | ["outside", "scope-asset"]+ "the scope already present" | new PathPredicate("scope-asset") | ["scope-asset"]+ "the scope at the start" | new PathPredicate("scope-asset", "child") | ["scope-asset", "child"]+ "the scope at the end" | new PathPredicate("child", "scope-asset") | ["child", "scope-asset"]+ }++ def "#scope scope leaves the caller query unchanged with populated filters: #populated"() {+ given: "a query owned by the caller"+ def query = populated ? populatedQuery() : new UserQuery()+ def original = queryState(query)+ def engineId = engineFor(scope)+ List<Map> submittedStates = []+ def provider = Stub(ManagerIdentityProvider) {+ queryUsers(_ as UserQuery) >> { UserQuery submitted ->+ submittedStates.add(queryState(submitted))+ // The real identity query normalizes username matching in place.+ submitted.usernames?.each { it.caseSensitive = false }+ new User[0]+ }+ }++ when:+ facadeFor(engineId, provider).getResults(query).toList()++ then: "scope checks apply to the execution without changing caller filters or nested predicates"+ verifyAll {+ queryState(query) == original+ submittedStates == [scopedState(original, engineId)]+ }++ where:+ [scope, populated] << ["global", "realm", "asset"].collectMany { scope ->+ [false, true].collect { populated -> [scope, populated] }+ }+ }++ def "Reusing a caller query across engines does not carry scope restrictions forward with populated filters: #populated"() {+ given: "one query used by two asset engines, a realm engine and a global engine"+ def query = populated ? populatedQuery() : new UserQuery()+ def original = queryState(query)+ def engines = [+ new RulesEngineId("realm-a", "asset-a"),+ new RulesEngineId("realm-b", "asset-b"),+ new RulesEngineId("realm-c"),+ new RulesEngineId()+ ]+ List<Map> submittedStates = []+ def provider = Stub(ManagerIdentityProvider) {+ queryUsers(_ as UserQuery) >> { UserQuery submitted ->+ submittedStates.add(queryState(submitted))+ new User[0]+ }+ }++ when:+ engines.each { facadeFor(it, provider).getResults(query).toList() }++ then: "each engine applies only its own scope to the original caller query"+ verifyAll {+ queryState(query) == original+ submittedStates == engines.collect { scopedState(original, it) }+ }++ where:+ populated << [false, true]+ }++ def "Later caller edits are isolated from earlier executions in #scope scope"() {+ given: "a query with nested filters and an identity provider retaining submitted queries"+ def query = populatedQuery()+ def engineId = engineFor(scope)+ List<UserQuery> submittedQueries = []+ def provider = Stub(ManagerIdentityProvider) {+ queryUsers(_ as UserQuery) >> { UserQuery submitted ->+ submittedQueries.add(submitted)+ new User[0]+ }+ }+ def facade = facadeFor(engineId, provider)+ facade.getResults(query).toList()+ def firstExecution = queryState(submittedQueries[0])++ when: "the caller updates nested filters and executes the same query again"+ query.realmPredicate.name = "next-realm"+ query.assets[0] = "next-asset"+ query.pathPredicate.path[0] = "next-path"+ query.ids[0] = "next-user"+ query.usernames[0].value = "NextOperator"+ query.attributes[0].name.value = "team"+ query.attributes[0].value.value = "operations"+ query.attributes[1].negated = false+ query.clientRoles[0].value = "write:assets"+ query.realmRoles[0].value = "next-role"+ query.select.basic = false+ query.orderBy.descending = false+ query.limit = 10+ query.offset = 3+ def updatedInput = queryState(query)+ facade.getResults(query).toList()++ then: "the first execution retains its filters, and the next execution uses the edited input"+ verifyAll {+ submittedQueries.size() == 2+ queryState(submittedQueries[0]) == firstExecution+ queryState(submittedQueries[1]) == scopedState(updatedInput, engineId)+ queryState(query) == updatedInput+ }++ where:+ scope << ["global", "realm", "asset"]+ }++ private UsersFacade facadeFor(RulesEngineId engineId, ManagerIdentityProvider provider) {+ def identityService = Stub(ManagerIdentityService) {+ getIdentityProvider() >> provider+ }+ new UsersFacade(engineId, Stub(AssetStorageService), Stub(NotificationService), identityService)+ }++ private static RulesEngineId engineFor(String scope) {+ scope == "global" ? new RulesEngineId() :+ scope == "realm" ? new RulesEngineId("building") : new RulesEngineId("building", "scope-asset")+ }++ private static UserQuery populatedQuery() {+ new UserQuery().realm(new RealmPredicate("caller-realm"))+ .assets("caller-asset").assetPath(new PathPredicate("caller-path")).ids("caller-user")+ .usernames(new StringPredicate(AssetQuery.Match.BEGIN, true, "Operator").negate(true))+ .attributes(department(), new UserQuery.AttributeValuePredicate(true, new StringPredicate("blocked")))+ .clientRoles(new StringPredicate("read:assets").negate(true))+ .realmRoles(new StringPredicate("restricted-user"))+ .serviceUsers(true).limit(7).offset(2)+ .select(new UserQuery.Select().basic(true))+ .orderBy(new UserQuery.OrderBy(UserQuery.OrderBy.Property.USERNAME, true))+ }++ private static Map queryState(UserQuery query) {+ [realm: query.realmPredicate?.name, assets: query.assets?.toList(), path: query.pathPredicate?.path?.toList(),+ ids: query.ids?.toList(), usernames: query.usernames?.collect { it.toString() },+ attributes: query.attributes == null ? null : snapshot(query.attributes),+ clientRoles: query.clientRoles?.collect {+ it.toString()+ }, realmRoles: query.realmRoles?.collect {+ it.toString()+ },+ serviceUsers: query.serviceUsers, limit: query.limit, offset: query.offset,+ select: query.select?.toString(), orderBy: query.orderBy?.toString()]+ }++ private static Map scopedState(Map original, RulesEngineId engineId) {+ original + [realm: engineId.realm.orElse(original.realm),+ path: engineId.assetId.present ? (original.path ?: []) + [engineId.assetId.get()] : original.path,+ attributes: (original.attributes ?: []) + [exclusion().toString()], serviceUsers: false]+ }++ private static UserQuery.AttributeValuePredicate department() {+ new UserQuery.AttributeValuePredicate(false, new StringPredicate("department"), new StringPredicate("maintenance"))+ }++ private static UserQuery.AttributeValuePredicate exclusion(String value = null) {+ new UserQuery.AttributeValuePredicate(true, new StringPredicate(User.SYSTEM_ACCOUNT_ATTRIBUTE),+ value == null ? null : new StringPredicate(value))+ }++ private static List<String> snapshot(UserQuery.AttributeValuePredicate[] attributes) {+ attributes == null ? [] : attributes.collect { it.toString() }+ }+}diff --git a/model/src/main/java/org/openremote/model/query/UserQuery.java b/model/src/main/java/org/openremote/model/query/UserQuery.javaindex b96d94495..055b274b0 100644--- a/model/src/main/java/org/openremote/model/query/UserQuery.java+++ b/model/src/main/java/org/openremote/model/query/UserQuery.java@@ -205,6 +205,59 @@ public class UserQuery {public UserQuery() {}+ /**+ * Copies all query options, including nested mutable predicates. Empty arrays are preserved: an+ * empty asset selection matches no users, whereas a null selection imposes no asset filter.+ */+ public UserQuery(UserQuery source) {+ realmPredicate =+ source.realmPredicate == null ? null : new RealmPredicate(source.realmPredicate.name);+ assets = source.assets == null ? null : source.assets.clone();+ pathPredicate =+ source.pathPredicate == null+ ? null+ : new PathPredicate(+ source.pathPredicate.path == null ? null : source.pathPredicate.path.clone());+ ids = source.ids == null ? null : source.ids.clone();+ select = source.select == null ? null : new Select().basic(source.select.basic);+ usernames = copyPredicates(source.usernames);+ attributes =+ source.attributes == null+ ? null+ : Arrays.stream(source.attributes)+ .map(+ attribute ->+ attribute == null+ ? null+ : new AttributeValuePredicate(+ attribute.negated,+ copyPredicate(attribute.name),+ copyPredicate(attribute.value)))+ .toArray(AttributeValuePredicate[]::new);+ clientRoles = copyPredicates(source.clientRoles);+ realmRoles = copyPredicates(source.realmRoles);+ serviceUsers = source.serviceUsers;+ limit = source.limit;+ offset = source.offset;+ orderBy =+ source.orderBy == null+ ? null+ : new OrderBy(source.orderBy.property, source.orderBy.descending);+ }++ private static StringPredicate[] copyPredicates(StringPredicate[] predicates) {+ return predicates == null+ ? null+ : Arrays.stream(predicates).map(UserQuery::copyPredicate).toArray(StringPredicate[]::new);+ }++ private static StringPredicate copyPredicate(StringPredicate predicate) {+ return predicate == null+ ? null+ : new StringPredicate(predicate.match, predicate.caseSensitive, predicate.value)+ .negate(predicate.negate);+ }+public UserQuery realm(RealmPredicate realmPredicate) {this.realmPredicate = realmPredicate;return this;diff --git a/test/src/test/groovy/org/openremote/test/rules/UsersFacadeTest.groovy b/test/src/test/groovy/org/openremote/test/rules/UsersFacadeTest.groovynew file mode 100644index 000000000..d5dd86c68--- /dev/null+++ b/test/src/test/groovy/org/openremote/test/rules/UsersFacadeTest.groovy@@ -0,0 +1,377 @@+/*+ * Copyright 2026, OpenRemote Inc.+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as+ * published by the Free Software Foundation, either version 3 of the+ * License, or (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * SPDX-License-Identifier: AGPL-3.0-or-later+ */+package org.openremote.test.rules++import org.openremote.manager.asset.AssetStorageService+import org.openremote.manager.notification.NotificationService+import org.openremote.manager.rules.RulesEngineId+import org.openremote.manager.rules.facade.UsersFacade+import org.openremote.manager.security.ManagerIdentityService+import org.openremote.manager.setup.SetupService+import org.openremote.model.asset.UserAssetLink+import org.openremote.model.asset.impl.ThingAsset+import org.openremote.model.query.UserQuery+import org.openremote.model.query.filter.PathPredicate+import org.openremote.model.query.filter.RealmPredicate+import org.openremote.model.query.filter.StringPredicate+import org.openremote.model.security.User+import org.openremote.setup.integration.KeycloakTestSetup+import org.openremote.test.ManagerContainerTrait+import spock.lang.Specification++class UsersFacadeTest extends Specification implements ManagerContainerTrait {++ def "Asset scope querying users linked to #targetName with #linkedUserNames returns #expectedUserNames"() {+ given: "real identity and asset storage services"+ def container = startContainer(defaultConfig(), defaultServices())+ def assetStorageService = container.getService(AssetStorageService.class)+ def identityService = container.getService(ManagerIdentityService.class)+ def identityProvider = identityService.getIdentityProvider()+ def keycloakSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def realm = keycloakSetup.realmBuilding.name+ def foreignRealm = keycloakSetup.realmCity.name+ List<String> createdAssetIds = []+ List<User> createdUsers = []++ and: "a scope root, its descendant and an unrelated asset in the same realm"+ def createAsset = { String name, String parentId ->+ def asset = assetStorageService.merge(new ThingAsset("Users facade ${name}")+ .setRealm(realm).setParentId(parentId))+ createdAssetIds.add(asset.id)+ asset+ }+ def scopedAsset = createAsset("scope", null)+ def childAsset = createAsset("child", scopedAsset.id)+ def unrelatedAsset = createAsset("unrelated", null)+ def targetAsset = targetName == "root" ? scopedAsset : childAsset++ and: "regular users in the rule's realm and another realm"+ def users = ["sameRealm", "foreignRealm", "outsideSubtree"].collectEntries { name ->+ def userRealm = name == "foreignRealm" ? foreignRealm : realm+ def user = identityProvider.createUpdateUser(userRealm,+ new User().setUsername("users-facade-${UUID.randomUUID()}").setEnabled(true), null, false)+ createdUsers.add(user)+ def storedUser = identityProvider.getUser(user.id)+ assert storedUser.realm == userRealm+ assert !storedUser.isServiceAccount()+ assert !storedUser.isSystemAccount()+ [(name): storedUser]+ }++ and: "stored links include any foreign user selected by this case"+ // Storage permits cross-realm links independently of REST link-creation checks.+ linkedUserNames.each { name ->+ def user = users[name]+ assetStorageService.storeUserAssetLinks([new UserAssetLink(user.realm, user.id, targetAsset.id)])+ }+ def outsideUser = users.outsideSubtree+ assetStorageService.storeUserAssetLinks([new UserAssetLink(outsideUser.realm, outsideUser.id, unrelatedAsset.id)])+ assert identityProvider.queryUsers(new UserQuery().assets(targetAsset.id))*.id.toSet() ==+ linkedUserNames.collect { users[it].id }.toSet()+ assert identityProvider.queryUsers(new UserQuery().assets(unrelatedAsset.id))*.id == [outsideUser.id]++ def facade = new UsersFacade(new RulesEngineId(realm, scopedAsset.id), assetStorageService,+ container.getService(NotificationService.class), identityService)+ def query = new UserQuery().ids(users.values().collect { it.id } as String[])++ when: "the asset-scoped facade queries all three users"+ def result = facade.getResults(query).toList()++ then: "only users in the rule's realm linked within its subtree are returned"+ result.toSet() == expectedUserNames.collect { users[it].id }.toSet()++ cleanup:+ if (!createdAssetIds.isEmpty()) {+ assert assetStorageService.delete(createdAssetIds)+ }+ createdUsers.each { identityProvider.deleteUser(it.realm, it.id) }++ where:+ targetName | linkedUserNames | expectedUserNames+ "root" | ["sameRealm"] | ["sameRealm"]+ "root" | ["foreignRealm"] | []+ "root" | ["sameRealm", "foreignRealm"] | ["sameRealm"]+ "child" | ["sameRealm"] | ["sameRealm"]+ "child" | ["foreignRealm"] | []+ "child" | ["sameRealm", "foreignRealm"] | ["sameRealm"]+ }++ def "#scope scope querying usernames #queryUserNames returns only regular users #expectedUserNames"() {+ given: "real identity and asset storage services"+ def container = startContainer(defaultConfig(), defaultServices())+ def assetStorageService = container.getService(AssetStorageService.class)+ def identityService = container.getService(ManagerIdentityService.class)+ def identityProvider = identityService.getIdentityProvider()+ def keycloakSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def realm = keycloakSetup.realmBuilding.name+ List<User> createdUsers = []+ def scopedAsset = assetStorageService.merge(new ThingAsset("Users facade username filtering").setRealm(realm))++ and: "two regular users and a real service account, all within the rule's scope"+ def users = ["regular", "unmatched", "service"].collectEntries { name ->+ def user = identityProvider.createUpdateUser(realm,+ new User().setUsername("users-facade-${UUID.randomUUID()}").setEnabled(true)+ .setServiceAccount(name == "service"), null, false)+ createdUsers.add(user)+ def storedUser = identityProvider.getUser(user.id)+ assert storedUser.realm == realm+ assert storedUser.isServiceAccount() == (name == "service")+ assert !storedUser.isSystemAccount()+ assetStorageService.storeUserAssetLinks([new UserAssetLink(realm, user.id, scopedAsset.id)])+ [(name): storedUser]+ }+ assert identityProvider.queryUsers(new UserQuery().assets(scopedAsset.id))*.id.toSet() ==+ users.values().collect { it.id }.toSet()++ and: "a username query which independently matches the intended accounts"+ def createQuery = {+ def query = new UserQuery().ids(users.values().collect { it.id } as String[])+ if (!queryUserNames.isEmpty()) {+ query.usernames(queryUserNames.collect { name ->+ def user = users[name]+ // User.getUsername() omits Keycloak's prefix; username predicates match the stored name.+ def storedUsername = user.isServiceAccount() ? User.SERVICE_ACCOUNT_PREFIX + user.username : user.username+ new StringPredicate(storedUsername)+ } as StringPredicate[])+ }+ query+ }+ def matchingNames = queryUserNames.isEmpty() ? users.keySet() : queryUserNames+ assert identityProvider.queryUsers(createQuery())*.id.toSet() ==+ matchingNames.collect { users[it].id }.toSet()++ def engineId = scope == "global" ? new RulesEngineId() :+ scope == "realm" ? new RulesEngineId(realm) : new RulesEngineId(realm, scopedAsset.id)+ def facade = new UsersFacade(engineId, assetStorageService,+ container.getService(NotificationService.class), identityService)++ when: "the facade applies its account exclusions to the query"+ def result = facade.getResults(createQuery()).toList()++ then: "service accounts stay excluded and the username filter is respected"+ result.toSet() == expectedUserNames.collect { users[it].id }.toSet()++ cleanup:+ if (scopedAsset != null) {+ assert assetStorageService.delete([scopedAsset.id])+ }+ createdUsers.each { identityProvider.deleteUser(it.realm, it.id) }++ where:+ [scope, queryUserNames, expectedUserNames] << ["global", "realm", "asset"].collectMany { scope ->+ [+ [[], ["regular", "unmatched"]],+ [["regular"], ["regular"]],+ [["service"], []],+ [["regular", "service"], ["regular"]]+ ].collect { row -> [scope] + row }+ }+ }++ def "#scope scope querying attributes #queryAttributes returns only regular users #expectedUserNames"() {+ given: "real identity and asset storage services"+ def container = startContainer(defaultConfig(), defaultServices())+ def assetStorageService = container.getService(AssetStorageService.class)+ def identityService = container.getService(ManagerIdentityService.class)+ def identityProvider = identityService.getIdentityProvider()+ def keycloakSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def realm = keycloakSetup.realmBuilding.name+ List<User> createdUsers = []+ def scopedAsset = assetStorageService.merge(new ThingAsset("Users facade attribute filtering").setRealm(realm))++ and: "regular users with different attributes and a system account matching both filters"+ def userAttributes = [+ matching: [department: "maintenance", location: "north"],+ otherDepartment: [department: "operations", location: "north"],+ otherLocation: [department: "maintenance", location: "south"],+ system: [department: "maintenance", location: "north"]+ ]+ def users = userAttributes.collectEntries { name, attributes ->+ def newUser = new User().setUsername("users-facade-${UUID.randomUUID()}").setEnabled(true)+ .setSystemAccount(name == "system")+ attributes.each { key, value -> newUser.setAttribute(key, value) }+ def user = identityProvider.createUpdateUser(realm, newUser, null, false)+ createdUsers.add(user)+ def storedUser = identityProvider.getUser(user.id)+ assert storedUser.realm == realm+ assert !storedUser.isServiceAccount()+ assert storedUser.isSystemAccount() == (name == "system")+ attributes.each { key, value -> assert storedUser.attributeMap[key] == [value] }+ assetStorageService.storeUserAssetLinks([new UserAssetLink(realm, user.id, scopedAsset.id)])+ [(name): storedUser]+ }+ assert identityProvider.queryUsers(new UserQuery().assets(scopedAsset.id))*.id.toSet() ==+ users.values().collect { it.id }.toSet()++ and: "an attribute query which independently matches the expected regular and system accounts"+ def createQuery = {+ def query = new UserQuery().ids(users.values().collect { it.id } as String[])+ if (!queryAttributes.isEmpty()) {+ query.attributes(queryAttributes.collect { key, value ->+ new UserQuery.AttributeValuePredicate(false, new StringPredicate(key), new StringPredicate(value))+ } as UserQuery.AttributeValuePredicate[])+ }+ query+ }+ assert identityProvider.queryUsers(createQuery())*.id.toSet() ==+ rawUserNames.collect { users[it].id }.toSet()++ def engineId = scope == "global" ? new RulesEngineId() :+ scope == "realm" ? new RulesEngineId(realm) : new RulesEngineId(realm, scopedAsset.id)+ def facade = new UsersFacade(engineId, assetStorageService,+ container.getService(NotificationService.class), identityService)++ when: "the facade applies its system-account exclusion to the attribute query"+ def result = facade.getResults(createQuery()).toList()++ then: "caller filters and system-account exclusion both apply"+ verifyAll {+ !result.contains(users.system.id)+ result.toSet() == expectedUserNames.collect { users[it].id }.toSet()+ }++ cleanup:+ if (scopedAsset != null) {+ assert assetStorageService.delete([scopedAsset.id])+ }+ createdUsers.each { identityProvider.deleteUser(it.realm, it.id) }++ where:+ [scope, queryAttributes, rawUserNames, expectedUserNames] << ["global", "realm", "asset"].collectMany { scope ->+ [+ [+ [:],+ ["matching", "otherDepartment", "otherLocation", "system"],+ ["matching", "otherDepartment", "otherLocation"]+ ],+ [[department: "maintenance"], ["matching", "otherLocation", "system"], ["matching", "otherLocation"]],+ [[department: "maintenance", location: "north"], ["matching", "system"], ["matching"]],+ [[department: "nonexistent"], [], []]+ ].collect { row -> [scope] + row }+ }+ }++ def "#scope scope with realm #realmFilter, assets #assetNames and path #pathNames returns #expectedUserNames"() {+ given: "real identity and asset storage services"+ def container = startContainer(defaultConfig(), defaultServices())+ def assetStorageService = container.getService(AssetStorageService.class)+ def identityService = container.getService(ManagerIdentityService.class)+ def identityProvider = identityService.getIdentityProvider()+ def keycloakSetup = container.getService(SetupService.class).getTaskOfType(KeycloakTestSetup.class)+ def realm = keycloakSetup.realmBuilding.name+ def foreignRealm = keycloakSetup.realmCity.name+ List<String> createdAssetIds = []+ List<User> createdUsers = []++ and: "a scoped subtree, a sibling outside it, and an asset in another realm"+ def createAsset = { String name, String assetRealm, String parentId ->+ def asset = assetStorageService.merge(new ThingAsset("Users facade boundaries ${name}")+ .setRealm(assetRealm).setParentId(parentId))+ createdAssetIds.add(asset.id)+ asset+ }+ def assets = [:]+ assets.ancestor = createAsset("ancestor", realm, null)+ assets.scoped = createAsset("scoped", realm, assets.ancestor.id)+ assets.child = createAsset("child", realm, assets.scoped.id)+ assets.grandchild = createAsset("grandchild", realm, assets.child.id)+ assets.outside = createAsset("outside", realm, assets.ancestor.id)+ assets.foreign = createAsset("foreign", foreignRealm, null)+ assert assetStorageService.find(assets.grandchild.id, true).path.contains(assets.child.id)+ assert !assetStorageService.find(assets.outside.id, true).path.contains(assets.scoped.id)++ and: "one regular user linked to each target asset"+ def users = ["scoped", "child", "grandchild", "outside", "foreign"].collectEntries { name ->+ def asset = assets[name]+ def user = identityProvider.createUpdateUser(asset.realm,+ new User().setUsername("users-facade-${UUID.randomUUID()}").setEnabled(true), null, false)+ createdUsers.add(user)+ def storedUser = identityProvider.getUser(user.id)+ assert storedUser.realm == asset.realm+ assert !storedUser.isServiceAccount()+ assert !storedUser.isSystemAccount()+ assetStorageService.storeUserAssetLinks([new UserAssetLink(asset.realm, user.id, asset.id)])+ assert identityProvider.queryUsers(new UserQuery().assets(asset.id))*.id == [user.id]+ [(name): storedUser]+ }++ and: "a caller query with optional realm, asset and path restrictions"+ def createQuery = {+ def query = new UserQuery().ids(users.values().collect { it.id } as String[])+ if (realmFilter != null) {+ query.realm(new RealmPredicate(foreignRealm))+ }+ if (assetNames != null) {+ query.assets(assetNames.collect { assets[it].id } as String[])+ }+ if (pathNames != null) {+ query.assetPath(new PathPredicate(pathNames.collect { assets[it].id } as String[]))+ }+ query+ }+ // An explicit empty asset selection must match nobody, unlike an absent asset filter.+ // All other cases match seeded users before the facade enforces its scope.+ def directResults = identityProvider.queryUsers(createQuery())+ if (assetNames != null && assetNames.isEmpty()) {+ assert directResults.length == 0+ } else {+ assert directResults.length> 0+ }+ def engineId = scope == "global" ? new RulesEngineId() :+ scope == "realm" ? new RulesEngineId(realm) : new RulesEngineId(realm, assets.scoped.id)+ def facade = new UsersFacade(engineId, assetStorageService,+ container.getService(NotificationService.class), identityService)++ when: "the rule queries users through its facade"+ def result = facade.getResults(createQuery()).toList()++ then: "the engine scope is enforced while valid narrower filters are retained"+ result.toSet() == expectedUserNames.collect { users[it].id }.toSet()++ cleanup:+ if (!createdAssetIds.isEmpty()) {+ assert assetStorageService.delete(createdAssetIds)+ }+ createdUsers.each { identityProvider.deleteUser(it.realm, it.id) }++ where:+ scope | realmFilter | assetNames | pathNames | expectedUserNames+ "global" | null | null | null | ["scoped", "child", "grandchild", "outside", "foreign"]+ "global" | null | [] | null | []+ "global" | "foreign" | null | null | ["foreign"]+ "realm" | null | null | null | ["scoped", "child", "grandchild", "outside"]+ "realm" | null | [] | null | []+ "realm" | "foreign" | null | null | ["scoped", "child", "grandchild", "outside"]+ "realm" | null | ["foreign"] | null | []+ "asset" | null | null | null | ["scoped", "child", "grandchild"]+ "asset" | null | [] | null | []+ "asset" | "foreign" | null | null | ["scoped", "child", "grandchild"]+ "asset" | null | ["scoped"] | null | ["scoped"]+ "asset" | null | ["child"] | null | ["child"]+ "asset" | null | ["outside"] | null | []+ "asset" | null | ["foreign"] | null | []+ "asset" | null | ["child", "outside", "foreign"] | null | ["child"]+ "asset" | null | null | ["ancestor"] | ["scoped", "child", "grandchild"]+ "asset" | null | null | ["outside"] | []+ "asset" | null | null | ["foreign"] | []+ "asset" | null | null | ["child"] | ["child", "grandchild"]+ "asset" | null | null | ["grandchild"] | ["grandchild"]+ "asset" | null | ["grandchild", "outside"] | ["child"] | ["grandchild"]+ }+}