From d489eac12119ed62f899569db121364b20b1c9dc Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 27 Dec 2017 16:38:13 +0500 Subject: Implement device disable --- src/org/traccar/BaseProtocolDecoder.java | 36 ++++++++++++++---------- src/org/traccar/api/resource/DeviceResource.java | 6 +++- src/org/traccar/database/DeviceManager.java | 26 +++++++++++++++-- src/org/traccar/database/PermissionsManager.java | 23 ++++++++++++--- src/org/traccar/model/Device.java | 10 +++++++ 5 files changed, 79 insertions(+), 22 deletions(-) (limited to 'src') diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 2d6286bf8..e412134d1 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -85,12 +85,13 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { private Map addressDeviceSessions = new HashMap<>(); // connectionless protocols private long findDeviceId(SocketAddress remoteAddress, String... uniqueIds) { - long deviceId = 0; if (uniqueIds.length > 0) { + long deviceId = 0; + Device device = null; try { for (String uniqueId : uniqueIds) { if (uniqueId != null) { - Device device = Context.getIdentityManager().getByUniqueId(uniqueId); + device = Context.getIdentityManager().getByUniqueId(uniqueId); if (device != null) { deviceId = device.getId(); break; @@ -100,22 +101,27 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { } catch (Exception e) { Log.warning(e); } + if (deviceId == 0 && Context.getConfig().getBoolean("database.registerUnknown")) { + return addUnknownDevice(uniqueIds[0]); + } + if (device != null && !device.getDisabled() || Context.getConfig().getBoolean("database.storeDisabled")) { + return deviceId; + } + StringBuilder message = new StringBuilder(); if (deviceId == 0) { - if (Context.getConfig().getBoolean("database.registerUnknown")) { - return addUnknownDevice(uniqueIds[0]); - } - - StringBuilder message = new StringBuilder("Unknown device -"); - for (String uniqueId : uniqueIds) { - message.append(" ").append(uniqueId); - } - if (remoteAddress != null) { - message.append(" (").append(((InetSocketAddress) remoteAddress).getHostString()).append(")"); - } - Log.warning(message.toString()); + message.append("Unknown device -"); + } else { + message.append("Disabled device -"); + } + for (String uniqueId : uniqueIds) { + message.append(" ").append(uniqueId); + } + if (remoteAddress != null) { + message.append(" (").append(((InetSocketAddress) remoteAddress).getHostString()).append(")"); } + Log.warning(message.toString()); } - return deviceId; + return 0; } public DeviceSession getDeviceSession(Channel channel, SocketAddress remoteAddress, String... uniqueIds) { diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index fda96a09d..0ea532567 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -65,7 +65,11 @@ public class DeviceResource extends BaseObjectResource { userId = getUserId(); } Context.getPermissionsManager().checkUser(getUserId(), userId); - result = deviceManager.getUserItems(userId); + if (Context.getPermissionsManager().getUserAdmin(getUserId())) { + result = deviceManager.getAllUserItems(userId); + } else { + result = deviceManager.getUserItems(userId); + } } else { result = new HashSet(); for (String uniqueId : uniqueIds) { diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 1031c5da9..85ee11c40 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -104,15 +104,35 @@ public class DeviceManager extends BaseObjectManager implements Identity return getItems(getAllItems()); } + public Set getAllUserItems(long userId) { + return Context.getPermissionsManager().getDevicePermissions(userId); + } + @Override public Set getUserItems(long userId) { if (Context.getPermissionsManager() != null) { - return Context.getPermissionsManager().getDevicePermissions(userId); + Set result = new HashSet<>(); + for (long deviceId : Context.getPermissionsManager().getDevicePermissions(userId)) { + Device device = Context.getIdentityManager().getById(deviceId); + if (device != null && !device.getDisabled()) { + result.add(deviceId); + } + } + return result; } else { return new HashSet<>(); } } + public Set getAllManagedItems(long userId) { + Set result = new HashSet<>(); + result.addAll(getAllUserItems(userId)); + for (long managedUserId : Context.getUsersManager().getUserItems(userId)) { + result.addAll(getAllUserItems(managedUserId)); + } + return result; + } + @Override public Set getManagedItems(long userId) { Set result = new HashSet<>(); @@ -160,6 +180,7 @@ public class DeviceManager extends BaseObjectManager implements Identity cachedDevice.setCategory(device.getCategory()); cachedDevice.setContact(device.getContact()); cachedDevice.setModel(device.getModel()); + cachedDevice.setDisabled(device.getDisabled()); cachedDevice.setAttributes(device.getAttributes()); if (!device.getUniqueId().equals(cachedDevice.getUniqueId())) { devicesByUniqueId.remove(cachedDevice.getUniqueId()); @@ -246,7 +267,8 @@ public class DeviceManager extends BaseObjectManager implements Identity List result = new LinkedList<>(); if (Context.getPermissionsManager() != null) { - for (long deviceId : getUserItems(userId)) { + for (long deviceId : Context.getPermissionsManager().getUserAdmin(userId) + ? getAllUserItems(userId) : getUserItems(userId)) { if (positions.containsKey(deviceId)) { result.add(positions.get(deviceId)); } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 0e9097d25..60bda99ce 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -74,13 +74,28 @@ public class PermissionsManager { return devicePermissions.get(userId); } - public Set getDeviceUsers(long deviceId) { + private Set getAllDeviceUsers(long deviceId) { if (!deviceUsers.containsKey(deviceId)) { deviceUsers.put(deviceId, new HashSet()); } return deviceUsers.get(deviceId); } + public Set getDeviceUsers(long deviceId) { + Device device = Context.getIdentityManager().getById(deviceId); + if (device != null && !device.getDisabled()) { + return getAllDeviceUsers(deviceId); + } else { + Set result = new HashSet<>(); + for (long userId : getAllDeviceUsers(deviceId)) { + if (getUserAdmin(userId)) { + result.add(userId); + } + } + return result; + } + } + public Set getGroupDevices(long groupId) { if (!groupDevices.containsKey(groupId)) { groupDevices.put(groupId, new HashSet()); @@ -133,7 +148,7 @@ public class PermissionsManager { deviceUsers.clear(); for (Map.Entry> entry : devicePermissions.entrySet()) { for (long deviceId : entry.getValue()) { - getDeviceUsers(deviceId).add(entry.getKey()); + getAllDeviceUsers(deviceId).add(entry.getKey()); } } } @@ -179,9 +194,9 @@ public class PermissionsManager { if (deviceLimit != -1) { int deviceCount = 0; if (getUserManager(userId)) { - deviceCount = Context.getDeviceManager().getManagedItems(userId).size(); + deviceCount = Context.getDeviceManager().getAllManagedItems(userId).size(); } else { - deviceCount = Context.getDeviceManager().getUserItems(userId).size(); + deviceCount = Context.getDeviceManager().getAllUserItems(userId).size(); } if (deviceCount >= deviceLimit) { throw new SecurityException("User device limit reached"); diff --git a/src/org/traccar/model/Device.java b/src/org/traccar/model/Device.java index c8a28404c..51b479b09 100644 --- a/src/org/traccar/model/Device.java +++ b/src/org/traccar/model/Device.java @@ -149,4 +149,14 @@ public class Device extends ExtendedModel { this.category = category; } + private boolean disabled; + + public boolean getDisabled() { + return disabled; + } + + public void setDisabled(boolean disabled) { + this.disabled = disabled; + } + } -- cgit v1.2.3 From 71ef701357da0b4b5c8214418b6b149e87c16ac2 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 28 Dec 2017 08:49:15 +0500 Subject: Remove unnecessary Context use --- src/org/traccar/database/DeviceManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index 85ee11c40..cb410b7c0 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -113,7 +113,7 @@ public class DeviceManager extends BaseObjectManager implements Identity if (Context.getPermissionsManager() != null) { Set result = new HashSet<>(); for (long deviceId : Context.getPermissionsManager().getDevicePermissions(userId)) { - Device device = Context.getIdentityManager().getById(deviceId); + Device device = getById(deviceId); if (device != null && !device.getDisabled()) { result.add(deviceId); } -- cgit v1.2.3