Skip to content

Commit

Permalink
Fix restricted user AssetEvent bug and realm API bug (openremote#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
richturner authored Mar 31, 2023
1 parent e0064bd commit 4cc82cd
Show file tree
Hide file tree
Showing 6 changed files with 232 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,8 @@ public <T extends SharedEvent> List<Message> splitForSubscribers(Exchange exchan
T filteredEvent = sessionSub.subscription.getFilter() == null ? event : sessionSub.subscription.getFilter().apply(event);

if (filteredEvent != null) {
LOG.finest("Creating message for subscribed session '" + sessionKey + "': " + event);
List<T> events = Collections.singletonList(event);
LOG.finest("Creating message for subscribed session '" + sessionKey + "': " + filteredEvent);
List<T> events = Collections.singletonList(filteredEvent);
TriggeredEventSubscription<T> triggeredEventSubscription = new TriggeredEventSubscription<>(events, sessionSub.subscriptionId);

if (sessionSub.subscription.getInternalConsumer() == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,9 @@ static Realm[] getRealmsFromDb(PersistenceService persistenceService) {
return o1.getName().compareTo(o2.getName());
});

// TODO: Remove this once migrated to hibernate 6.2.x+
realms.forEach(r -> r.getRealmRoles().size());

return realms.toArray(new Realm[realms.size()]);
});
}
Expand All @@ -307,6 +310,10 @@ static Realm getRealmFromDb(PersistenceService persistenceService, String realm)
return persistenceService.doReturningTransaction(em -> {
List<Realm> realms = em.createQuery("select r from Realm r where r.name = :realm", Realm.class)
.setParameter("realm", realm).getResultList();

// TODO: Remove this once migrated to hibernate 6.2.x+
realms.forEach(r -> r.getRealmRoles().size());

return realms.size() == 1 ? realms.get(0) : null;
}
);
Expand Down
21 changes: 11 additions & 10 deletions model/src/main/java/org/openremote/model/asset/AssetFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,17 +177,18 @@ public T apply(T event) {
}
}

if (attributeNames != null && attributeNames.length > 0) {
if (filterAttributesBy != null) {
// Filter attributes before doing name match
AssetEvent assetEvent = (AssetEvent) event;
if (assetEvent.getAsset() != null) {
Asset<?> asset = ValueUtil.clone(assetEvent.getAsset());
MetaItemDescriptor<?> finalFilterAttributesBy = filterAttributesBy;
asset.setAttributes(asset.getAttributes().values().stream().filter(attribute -> attribute.hasMeta(finalFilterAttributesBy)).collect(Collectors.toList()));
event = (T) new AssetEvent(assetEvent.getCause(), asset, assetEvent.getUpdatedProperties());
}
if (filterAttributesBy != null) {
// Filter attributes before doing name match
AssetEvent assetEvent = (AssetEvent) event;
if (assetEvent.getAsset() != null) {
Asset<?> asset = ValueUtil.clone(assetEvent.getAsset());
MetaItemDescriptor<?> finalFilterAttributesBy = filterAttributesBy;
asset.setAttributes(asset.getAttributes().values().stream().filter(attribute -> attribute.hasMeta(finalFilterAttributesBy)).collect(Collectors.toList()));
event = (T) new AssetEvent(assetEvent.getCause(), asset, assetEvent.getUpdatedProperties());
}
}

if (attributeNames != null && attributeNames.length > 0) {
List<String> eventAttributeNames = Arrays.asList(event.getAttributeNames());
if (Arrays.stream(attributeNames).noneMatch(eventAttributeNames::contains)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class ManagerTestSetup extends ManagerSetup {

// Update these numbers whenever you change a RULE_STATE flag in test data
public static final int DEMO_RULE_STATES_APARTMENT_1 = 44;
public static final int DEMO_RULE_STATES_APARTMENT_2 = 13;
public static final int DEMO_RULE_STATES_APARTMENT_2 = 14;
public static final int DEMO_RULE_STATES_APARTMENT_3 = 0;
public static final int DEMO_RULE_STATES_SMART_OFFICE = 5;
public static final int DEMO_RULE_STATES_SMART_BUILDING = DEMO_RULE_STATES_APARTMENT_1 + DEMO_RULE_STATES_APARTMENT_2 + DEMO_RULE_STATES_APARTMENT_3;
Expand Down Expand Up @@ -515,6 +515,7 @@ public void onStart() throws Exception {
new MetaItem<>(RULE_STATE, true)
)
);
addDemoApartmentTemperatureControl(apartment2Livingroom, false, null);
apartment2Livingroom = assetStorageService.merge(apartment2Livingroom);
apartment2LivingroomId = apartment2Livingroom.getId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,11 @@ class AssetQueryTest extends Specification implements ManagerContainerTrait {
then: "only one asset should be retrieved"
assets.size() == 1
assets.get(0).id == managerTestSetup.apartment2LivingroomId
assets.get(0).getAttributes().size() == 1
assets.get(0).getAttributes().size() == 2
assets.get(0).getAttribute("windowOpen").isPresent()
!assets.get(0).getAttribute("windowOpen").flatMap{it.value}.orElse(false)
!assets.get(0).getAttribute("windowOpen").get().getMeta().isEmpty()
assets.get(0).getAttribute("targetTemperature").isPresent()

when: "a recursive query is executed for apartment 1 assets"
assets = assetStorageService.findAll(
Expand Down
Loading

0 comments on commit 4cc82cd

Please sign in to comment.