diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2016-08-24 14:55:06 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2016-08-24 14:55:06 +1200 |
commit | ea0bdea88003f46fd6f6164d5c45e33d53f47c81 (patch) | |
tree | d8e51a10e56f7e916cc2bf7486d75c90feb5696e /src/org/traccar/database | |
parent | a645eb8e2338f11e2c807045b1787a47cd1f1464 (diff) | |
parent | f6484e9808f50aa997f028f9421b05aad6aed8bd (diff) | |
download | trackermap-server-ea0bdea88003f46fd6f6164d5c45e33d53f47c81.tar.gz trackermap-server-ea0bdea88003f46fd6f6164d5c45e33d53f47c81.tar.bz2 trackermap-server-ea0bdea88003f46fd6f6164d5c45e33d53f47c81.zip |
Merge pull request #2242 from Abyss777/users_cache
Implement users cache
Diffstat (limited to 'src/org/traccar/database')
-rw-r--r-- | src/org/traccar/database/PermissionsManager.java | 54 |
1 files changed, 49 insertions, 5 deletions
diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 3c2cc252b..f5fed978a 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -25,10 +25,12 @@ import org.traccar.model.Server; import org.traccar.model.User; import java.sql.SQLException; +import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; public class PermissionsManager { @@ -36,7 +38,7 @@ public class PermissionsManager { private volatile Server server; - private final Map<Long, User> users = new HashMap<>(); + private final Map<Long, User> users = new ConcurrentHashMap<>(); private final Map<Long, Set<Long>> groupPermissions = new HashMap<>(); private final Map<Long, Set<Long>> devicePermissions = new HashMap<>(); @@ -73,19 +75,26 @@ public class PermissionsManager { public PermissionsManager(DataManager dataManager) { this.dataManager = dataManager; - refresh(); + refreshUsers(); + refreshPermissions(); } - public final void refresh() { + public final void refreshUsers() { users.clear(); - groupPermissions.clear(); - devicePermissions.clear(); try { server = dataManager.getServer(); for (User user : dataManager.getUsers()) { users.put(user.getId(), user); } + } catch (SQLException error) { + Log.warning(error); + } + } + public final void refreshPermissions() { + groupPermissions.clear(); + devicePermissions.clear(); + try { GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(), Context.getDeviceManager().getAllDevices()); for (GroupPermission permission : dataManager.getGroupPermissions()) { @@ -176,4 +185,39 @@ public class PermissionsManager { this.server = server; } + public Collection<User> getUsers() { + return users.values(); + } + + public User getUser(long userId) { + return users.get(userId); + } + + public void addUser(User user) throws SQLException { + dataManager.addUser(user); + users.put(user.getId(), user); + refreshPermissions(); + } + + public void updateUser(User user) throws SQLException { + dataManager.updateUser(user); + users.put(user.getId(), user); + refreshPermissions(); + } + + public void removeUser(long userId) throws SQLException { + dataManager.removeUser(userId); + users.remove(userId); + refreshPermissions(); + } + + public User login(String email, String password) throws SQLException { + User user = dataManager.login(email, password); + if (user != null && users.get(user.getId()) != null) { + return users.get(user.getId()); + } else { + return null; + } + } + } |