aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r--src/org/traccar/database/DataManager.java52
-rw-r--r--src/org/traccar/database/PermissionsManager.java14
2 files changed, 52 insertions, 14 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index 9a8a14615..d2da4e701 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -21,6 +21,7 @@ import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.SQLException;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
@@ -36,6 +37,7 @@ import liquibase.exception.LiquibaseException;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.ResourceAccessor;
import org.traccar.Config;
+import org.traccar.Context;
import org.traccar.helper.Log;
import org.traccar.model.Device;
import org.traccar.model.Group;
@@ -55,10 +57,14 @@ public class DataManager implements IdentityManager {
private DataSource dataSource;
+ private final long dataRefreshDelay;
+
private final Map<Long, Device> devicesById = new HashMap<>();
private final Map<String, Device> devicesByUniqueId = new HashMap<>();
private long devicesLastUpdate;
- private final long devicesRefreshDelay;
+
+ private final Map<Long, Group> groupsById = new HashMap<>();
+ private long groupsLastUpdate;
public DataManager(Config config) throws Exception {
this.config = config;
@@ -66,7 +72,7 @@ public class DataManager implements IdentityManager {
initDatabase();
initDatabaseSchema();
- devicesRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
+ dataRefreshDelay = config.getLong("database.refreshDelay", DEFAULT_REFRESH_DELAY) * 1000;
}
public DataSource getDataSource() {
@@ -114,7 +120,7 @@ public class DataManager implements IdentityManager {
}
private void updateDeviceCache(boolean force) throws SQLException {
- if (System.currentTimeMillis() - devicesLastUpdate > devicesRefreshDelay || force) {
+ if (System.currentTimeMillis() - devicesLastUpdate > dataRefreshDelay || force) {
devicesById.clear();
devicesByUniqueId.clear();
for (Device device : getAllDevices()) {
@@ -142,6 +148,25 @@ public class DataManager implements IdentityManager {
return devicesByUniqueId.get(uniqueId);
}
+ private void updateGroupCache(boolean force) throws SQLException {
+ if (System.currentTimeMillis() - groupsLastUpdate > dataRefreshDelay || force) {
+ groupsById.clear();
+ for (Group group : getAllGroups()) {
+ groupsById.put(group.getId(), group);
+ }
+ groupsLastUpdate = System.currentTimeMillis();
+ }
+ }
+
+ public Group getGroupById(long id) {
+ try {
+ updateGroupCache(!groupsById.containsKey(id));
+ } catch (SQLException e) {
+ Log.warning(e);
+ }
+ return groupsById.get(id);
+ }
+
private String getQuery(String key) {
String query = config.getString(key);
if (query == null) {
@@ -239,9 +264,11 @@ public class DataManager implements IdentityManager {
}
public Collection<Device> getDevices(long userId) throws SQLException {
- return QueryBuilder.create(dataSource, getQuery("database.selectDevices"))
- .setLong("userId", userId)
- .executeQuery(Device.class);
+ Collection<Device> devices = new ArrayList<>();
+ for (long id : Context.getPermissionsManager().getDevicePermissions(userId)) {
+ devices.add(getDeviceById(id));
+ }
+ return devices;
}
public void addDevice(Device device) throws SQLException {
@@ -293,6 +320,19 @@ public class DataManager implements IdentityManager {
AsyncServlet.sessionRefreshUser(userId);
}
+ public Collection<Group> getAllGroups() throws SQLException {
+ return QueryBuilder.create(dataSource, getQuery("database.selectGroupsAll"))
+ .executeQuery(Group.class);
+ }
+
+ public Collection<Group> getGroups(long userId) throws SQLException {
+ Collection<Group> groups = new ArrayList<>();
+ for (long id : Context.getPermissionsManager().getGroupPermissions(userId)) {
+ groups.add(getGroupById(id));
+ }
+ return groups;
+ }
+
public void addGroup(Group group) throws SQLException {
group.setId(QueryBuilder.create(dataSource, getQuery("database.insertGroup"), true)
.setObject(group)
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java
index 0ed8227b4..5d6430764 100644
--- a/src/org/traccar/database/PermissionsManager.java
+++ b/src/org/traccar/database/PermissionsManager.java
@@ -38,14 +38,14 @@ public class PermissionsManager {
private final Map<Long, Set<Long>> groupPermissions = new HashMap<>();
private final Map<Long, Set<Long>> devicePermissions = new HashMap<>();
- private Set<Long> getGroupPermissions(long userId) {
+ public Set<Long> getGroupPermissions(long userId) {
if (!groupPermissions.containsKey(userId)) {
groupPermissions.put(userId, new HashSet<Long>());
}
return groupPermissions.get(userId);
}
- private Set<Long> getDevicePermissions(long userId) {
+ public Set<Long> getDevicePermissions(long userId) {
if (!devicePermissions.containsKey(userId)) {
devicePermissions.put(userId, new HashSet<Long>());
}
@@ -92,12 +92,10 @@ public class PermissionsManager {
}
}
- public Collection<Long> allowedGroups(long userId) {
- return getGroupPermissions(userId);
- }
-
- public Collection<Long> allowedDevices(long userId) {
- return getDevicePermissions(userId);
+ public void checkGroup(long userId, long groupId) throws SecurityException {
+ if (!getGroupPermissions(userId).contains(groupId)) {
+ throw new SecurityException("Group access denied");
+ }
}
public void checkDevice(long userId, long deviceId) throws SecurityException {