Skip to content

Commit

Permalink
Implement device disable
Browse files Browse the repository at this point in the history
  • Loading branch information
Abyss777 committed Dec 27, 2017
1 parent f128ed1 commit d489eac
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 22 deletions.
16 changes: 16 additions & 0 deletions schema/changelog-3.16.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.4.xsd"
logicalFilePath="changelog-3.16">

<changeSet author="author" id="changelog-3.16">

<addColumn tableName="devices">
<column name="disabled" type="BOOLEAN" defaultValueBoolean="false" />
</addColumn>

</changeSet>
</databaseChangeLog>
1 change: 1 addition & 0 deletions schema/changelog-master.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<include file="changelog-3.12.xml" relativeToChangelogFile="true" />
<include file="changelog-3.14.xml" relativeToChangelogFile="true" />
<include file="changelog-3.15.xml" relativeToChangelogFile="true" />
<include file="changelog-3.16.xml" relativeToChangelogFile="true" />
</databaseChangeLog>
36 changes: 21 additions & 15 deletions src/org/traccar/BaseProtocolDecoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,13 @@ protected double convertSpeed(double value, String defaultUnits) {
private Map<SocketAddress, DeviceSession> 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;
Expand All @@ -100,22 +101,27 @@ private long findDeviceId(SocketAddress remoteAddress, String... uniqueIds) {
} 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) {
Expand Down
6 changes: 5 additions & 1 deletion src/org/traccar/api/resource/DeviceResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,11 @@ public Collection<Device> get(
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<Long>();
for (String uniqueId : uniqueIds) {
Expand Down
26 changes: 24 additions & 2 deletions src/org/traccar/database/DeviceManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,35 @@ public Collection<Device> getAllDevices() {
return getItems(getAllItems());
}

public Set<Long> getAllUserItems(long userId) {
return Context.getPermissionsManager().getDevicePermissions(userId);
}

@Override
public Set<Long> getUserItems(long userId) {
if (Context.getPermissionsManager() != null) {
return Context.getPermissionsManager().getDevicePermissions(userId);
Set<Long> 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<Long> getAllManagedItems(long userId) {
Set<Long> result = new HashSet<>();
result.addAll(getAllUserItems(userId));
for (long managedUserId : Context.getUsersManager().getUserItems(userId)) {
result.addAll(getAllUserItems(managedUserId));
}
return result;
}

@Override
public Set<Long> getManagedItems(long userId) {
Set<Long> result = new HashSet<>();
Expand Down Expand Up @@ -160,6 +180,7 @@ protected void updateCachedItem(Device device) {
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());
Expand Down Expand Up @@ -246,7 +267,8 @@ public Collection<Position> getInitialState(long userId) {
List<Position> 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));
}
Expand Down
23 changes: 19 additions & 4 deletions src/org/traccar/database/PermissionsManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,28 @@ public Set<Long> getDevicePermissions(long userId) {
return devicePermissions.get(userId);
}

public Set<Long> getDeviceUsers(long deviceId) {
private Set<Long> getAllDeviceUsers(long deviceId) {
if (!deviceUsers.containsKey(deviceId)) {
deviceUsers.put(deviceId, new HashSet<Long>());
}
return deviceUsers.get(deviceId);
}

public Set<Long> getDeviceUsers(long deviceId) {
Device device = Context.getIdentityManager().getById(deviceId);
if (device != null && !device.getDisabled()) {
return getAllDeviceUsers(deviceId);
} else {
Set<Long> result = new HashSet<>();
for (long userId : getAllDeviceUsers(deviceId)) {
if (getUserAdmin(userId)) {
result.add(userId);
}
}
return result;
}
}

public Set<Long> getGroupDevices(long groupId) {
if (!groupDevices.containsKey(groupId)) {
groupDevices.put(groupId, new HashSet<Long>());
Expand Down Expand Up @@ -133,7 +148,7 @@ public final void refreshDeviceAndGroupPermissions() {
deviceUsers.clear();
for (Map.Entry<Long, Set<Long>> entry : devicePermissions.entrySet()) {
for (long deviceId : entry.getValue()) {
getDeviceUsers(deviceId).add(entry.getKey());
getAllDeviceUsers(deviceId).add(entry.getKey());
}
}
}
Expand Down Expand Up @@ -179,9 +194,9 @@ public void checkDeviceLimit(long userId) throws SecurityException, SQLException
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");
Expand Down
10 changes: 10 additions & 0 deletions src/org/traccar/model/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,14 @@ public void setCategory(String category) {
this.category = category;
}

private boolean disabled;

public boolean getDisabled() {
return disabled;
}

public void setDisabled(boolean disabled) {
this.disabled = disabled;
}

}

0 comments on commit d489eac

Please sign in to comment.