From 670c8c7041163a2232d3edb4ef413af70182ea0a Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 18 Nov 2016 18:08:52 +0500 Subject: - Flip user-token map - Fix null check - Combine validation to setter --- src/org/traccar/database/PermissionsManager.java | 26 +++++++++--------------- 1 file changed, 10 insertions(+), 16 deletions(-) (limited to 'src/org/traccar/database') diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 269dfc7bf..71633f6ef 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -29,7 +29,6 @@ import java.util.Collection; import java.util.HashMap; import java.util.HashSet; import java.util.Map; -import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; @@ -40,7 +39,7 @@ public class PermissionsManager { private volatile Server server; private final Map users = new ConcurrentHashMap<>(); - private final Map usersTokens = new HashMap<>(); + private final Map usersTokens = new HashMap<>(); private final Map> groupPermissions = new HashMap<>(); private final Map> devicePermissions = new HashMap<>(); @@ -89,7 +88,7 @@ public class PermissionsManager { for (User user : dataManager.getUsers()) { users.put(user.getId(), user); if (user.getToken() != null) { - usersTokens.put(user.getId(), user.getToken()); + usersTokens.put(user.getToken(), user.getId()); } } } catch (SQLException error) { @@ -217,26 +216,28 @@ public class PermissionsManager { dataManager.addUser(user); users.put(user.getId(), user); if (user.getToken() != null) { - usersTokens.put(user.getId(), user.getToken()); + usersTokens.put(user.getToken(), user.getId()); } refreshPermissions(); } public void updateUser(User user) throws SQLException { dataManager.updateUser(user); + User old = users.get(user.getId()); users.put(user.getId(), user); if (user.getToken() != null) { - usersTokens.put(user.getId(), user.getToken()); - } else if (usersTokens.containsKey(user.getId())) { - usersTokens.remove(user.getId()); + usersTokens.put(user.getToken(), user.getId()); + } + if (old.getToken() != null && !old.getToken().equals(user.getToken())) { + usersTokens.remove(old.getToken()); } refreshPermissions(); } public void removeUser(long userId) throws SQLException { dataManager.removeUser(userId); + usersTokens.remove(users.get(userId).getToken()); users.remove(userId); - usersTokens.remove(userId); refreshPermissions(); } @@ -250,14 +251,7 @@ public class PermissionsManager { } public User getUserByToken(String token) { - if (usersTokens.containsValue(token)) { - for (Entry entry : usersTokens.entrySet()) { - if (entry.getValue().equals(token)) { - return users.get(entry.getKey()); - } - } - } - return null; + return users.get(usersTokens.get(token)); } } -- cgit v1.2.3