From a487c1efa5dff2a7644ee7f2874967f4bb867b6a Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Tue, 28 Jun 2022 08:13:03 -0700 Subject: Fix new device updates --- src/main/java/org/traccar/api/BaseObjectResource.java | 5 +++++ src/main/java/org/traccar/session/ConnectionManager.java | 14 +++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'src/main') diff --git a/src/main/java/org/traccar/api/BaseObjectResource.java b/src/main/java/org/traccar/api/BaseObjectResource.java index 489360619..d10843917 100644 --- a/src/main/java/org/traccar/api/BaseObjectResource.java +++ b/src/main/java/org/traccar/api/BaseObjectResource.java @@ -21,6 +21,7 @@ import org.traccar.model.BaseModel; import org.traccar.model.Group; import org.traccar.model.Permission; import org.traccar.model.User; +import org.traccar.session.ConnectionManager; import org.traccar.session.cache.CacheManager; import org.traccar.storage.StorageException; import org.traccar.storage.query.Columns; @@ -41,6 +42,9 @@ public abstract class BaseObjectResource extends BaseResour @Inject private CacheManager cacheManager; + @Inject + private ConnectionManager connectionManager; + protected final Class baseClass; public BaseObjectResource(Class baseClass) { @@ -68,6 +72,7 @@ public abstract class BaseObjectResource extends BaseResour LogAction.create(getUserId(), entity); storage.addPermission(new Permission(User.class, getUserId(), baseClass, entity.getId())); cacheManager.invalidatePermission(User.class, getUserId(), baseClass, entity.getId()); + connectionManager.invalidatePermission(User.class, getUserId(), baseClass, entity.getId()); LogAction.link(getUserId(), User.class, getUserId(), baseClass, entity.getId()); return Response.ok(entity).build(); diff --git a/src/main/java/org/traccar/session/ConnectionManager.java b/src/main/java/org/traccar/session/ConnectionManager.java index c2f602c11..27d6184c2 100644 --- a/src/main/java/org/traccar/session/ConnectionManager.java +++ b/src/main/java/org/traccar/session/ConnectionManager.java @@ -364,6 +364,18 @@ public class ConnectionManager implements BroadcastInterface { } } + @Override + public synchronized void invalidatePermission( + Class clazz1, long id1, + Class clazz2, long id2) { + if (clazz1.equals(User.class) && clazz2.equals(Device.class)) { + if (listeners.containsKey(id1)) { + userDevices.get(id1).add(id2); + deviceUsers.put(id2, Set.of(id1)); + } + } + } + public interface UpdateListener { void onKeepalive(); void onUpdateDevice(Device device); @@ -379,7 +391,7 @@ public class ConnectionManager implements BroadcastInterface { var devices = storage.getObjects(Device.class, new Request( new Columns.Include("id"), new Condition.Permission(User.class, userId, Device.class))); - userDevices.put(userId, devices.stream().map(BaseModel::getId).collect(Collectors.toUnmodifiableSet())); + userDevices.put(userId, devices.stream().map(BaseModel::getId).collect(Collectors.toSet())); devices.forEach(device -> deviceUsers.computeIfAbsent(device.getId(), id -> new HashSet<>()).add(userId)); } set.add(listener); -- cgit v1.2.3