From 21bf1014e55ab8049e18ee991c6ee80b23d1789c Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 23 Aug 2016 14:48:01 +0500 Subject: Implement cache for users objects --- .../api/resource/DevicePermissionResource.java | 4 +- src/org/traccar/api/resource/DeviceResource.java | 4 +- .../api/resource/GroupPermissionResource.java | 4 +- src/org/traccar/api/resource/GroupResource.java | 4 +- src/org/traccar/api/resource/SessionResource.java | 4 +- src/org/traccar/api/resource/UserResource.java | 11 ++--- src/org/traccar/database/PermissionsManager.java | 54 ++++++++++++++++++++-- src/org/traccar/notification/NotificationMail.java | 10 ++-- 8 files changed, 66 insertions(+), 29 deletions(-) diff --git a/src/org/traccar/api/resource/DevicePermissionResource.java b/src/org/traccar/api/resource/DevicePermissionResource.java index 7de050074..7faa1ab09 100644 --- a/src/org/traccar/api/resource/DevicePermissionResource.java +++ b/src/org/traccar/api/resource/DevicePermissionResource.java @@ -37,7 +37,7 @@ public class DevicePermissionResource extends BaseResource { public Response add(DevicePermission entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().linkDevice(entity.getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } @@ -48,7 +48,7 @@ public class DevicePermissionResource extends BaseResource { public Response remove(DevicePermission entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().unlinkDevice(entity.getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index f48df7553..b12ab8c36 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -59,7 +59,7 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDeviceManager().addDevice(entity); Context.getDataManager().linkDevice(getUserId(), entity.getId()); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } @@ -84,7 +84,7 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkDevice(getUserId(), id); Context.getDeviceManager().removeDevice(id); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } diff --git a/src/org/traccar/api/resource/GroupPermissionResource.java b/src/org/traccar/api/resource/GroupPermissionResource.java index 1fbf37a2b..94100362b 100644 --- a/src/org/traccar/api/resource/GroupPermissionResource.java +++ b/src/org/traccar/api/resource/GroupPermissionResource.java @@ -37,7 +37,7 @@ public class GroupPermissionResource extends BaseResource { public Response add(GroupPermission entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().linkGroup(entity.getUserId(), entity.getGroupId()); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } @@ -48,7 +48,7 @@ public class GroupPermissionResource extends BaseResource { public Response remove(GroupPermission entity) throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); Context.getDataManager().unlinkGroup(entity.getUserId(), entity.getGroupId()); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index eec28e325..6b722ef6d 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -58,7 +58,7 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDeviceManager().addGroup(entity); Context.getDataManager().linkGroup(getUserId(), entity.getId()); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } @@ -83,7 +83,7 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), id); Context.getDeviceManager().removeGroup(id); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().refreshPermissions(); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refresh(); } diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java index 49670c1f9..deed70b37 100644 --- a/src/org/traccar/api/resource/SessionResource.java +++ b/src/org/traccar/api/resource/SessionResource.java @@ -73,7 +73,7 @@ public class SessionResource extends BaseResource { } if (userId != null) { - return Context.getDataManager().getUser(userId); + return Context.getPermissionsManager().getUser(userId); } else { throw new WebApplicationException(Response.status(Response.Status.NOT_FOUND).build()); } @@ -83,7 +83,7 @@ public class SessionResource extends BaseResource { @POST public User add( @FormParam("email") String email, @FormParam("password") String password) throws SQLException { - User user = Context.getDataManager().login(email, password); + User user = Context.getPermissionsManager().login(email, password); if (user != null) { request.getSession().setAttribute(USER_ID_KEY, user.getId()); return user; diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index da72c7f47..2d187fe9d 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -41,7 +41,7 @@ public class UserResource extends BaseResource { @GET public Collection get() throws SQLException { Context.getPermissionsManager().checkAdmin(getUserId()); - return Context.getDataManager().getUsers(); + return Context.getPermissionsManager().getUsers(); } @PermitAll @@ -50,8 +50,7 @@ public class UserResource extends BaseResource { if (!Context.getPermissionsManager().isAdmin(getUserId())) { Context.getPermissionsManager().checkRegistration(getUserId()); } - Context.getDataManager().addUser(entity); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().addUser(entity); if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); } @@ -66,8 +65,7 @@ public class UserResource extends BaseResource { } else { Context.getPermissionsManager().checkUser(getUserId(), entity.getId()); } - Context.getDataManager().updateUser(entity); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().updateUser(entity); if (Context.getNotificationManager() != null) { Context.getNotificationManager().refresh(); } @@ -78,8 +76,7 @@ public class UserResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkUser(getUserId(), id); - Context.getDataManager().removeUser(id); - Context.getPermissionsManager().refresh(); + Context.getPermissionsManager().removeUser(id); if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refreshUserGeofences(); } 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 users = new HashMap<>(); + private final Map users = new ConcurrentHashMap<>(); private final Map> groupPermissions = new HashMap<>(); private final Map> 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 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; + } + } + } diff --git a/src/org/traccar/notification/NotificationMail.java b/src/org/traccar/notification/NotificationMail.java index f9d42968f..f8449e7d2 100644 --- a/src/org/traccar/notification/NotificationMail.java +++ b/src/org/traccar/notification/NotificationMail.java @@ -15,7 +15,6 @@ */ package org.traccar.notification; -import java.sql.SQLException; import java.util.Properties; import javax.mail.Message; @@ -27,7 +26,6 @@ import javax.mail.internet.MimeMessage; import org.traccar.Config; import org.traccar.Context; -import org.traccar.database.DataManager; import org.traccar.helper.Log; import org.traccar.model.Event; import org.traccar.model.Extensible; @@ -106,14 +104,13 @@ public final class NotificationMail { } public static void sendMailSync(long userId, Event event, Position position) { - DataManager dataManager = Context.getDataManager(); Properties mailServerProperties; Session mailSession; MimeMessage mailMessage; try { - User user = dataManager.getUser(userId); + User user = Context.getPermissionsManager().getUser(userId); mailServerProperties = getConfigProperies(); if (!mailServerProperties.containsKey("mail.smtp.host")) { @@ -130,8 +127,7 @@ public final class NotificationMail { mailMessage.setFrom(new InternetAddress(mailServerProperties.getProperty("mail.smtp.from"))); } - mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress( - Context.getDataManager().getUser(userId).getEmail())); + mailMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(user.getEmail())); mailMessage.setSubject(NotificationFormatter.formatTitle(userId, event, position)); mailMessage.setText(NotificationFormatter.formatMessage(userId, event, position)); @@ -142,7 +138,7 @@ public final class NotificationMail { transport.sendMessage(mailMessage, mailMessage.getAllRecipients()); transport.close(); - } catch (MessagingException | SQLException error) { + } catch (MessagingException error) { Log.warning(error); } } -- cgit v1.2.3 From f6484e9808f50aa997f028f9421b05aad6aed8bd Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Tue, 23 Aug 2016 15:15:37 +0500 Subject: - Hid some buttons and menu items from readonly user - Removed unused variable --- web/app/view/DevicesController.js | 5 ++--- web/app/view/SettingsMenu.js | 12 +++++++++--- web/app/view/SettingsMenuController.js | 10 +++++++++- 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/web/app/view/DevicesController.js b/web/app/view/DevicesController.js index 85aa5fcd4..8058db376 100644 --- a/web/app/view/DevicesController.js +++ b/web/app/view/DevicesController.js @@ -45,6 +45,7 @@ Ext.define('Traccar.view.DevicesController', { this.lookupReference('toolbarAddButton').setVisible(!readonly); this.lookupReference('toolbarEditButton').setVisible(!readonly); this.lookupReference('toolbarRemoveButton').setVisible(!readonly); + this.lookupReference('toolbarGeofencesButton').setVisible(!readonly); }, onAddClick: function () { @@ -86,9 +87,7 @@ Ext.define('Traccar.view.DevicesController', { }, onGeofencesClick: function () { - var admin, device; - admin = Traccar.app.getUser().get('admin'); - device = this.getView().getSelectionModel().getSelection()[0]; + var device = this.getView().getSelectionModel().getSelection()[0]; Ext.create('Traccar.view.BaseWindow', { title: Strings.sharedGeofences, items: { diff --git a/web/app/view/SettingsMenu.js b/web/app/view/SettingsMenu.js index 8c230f36f..bf184424c 100644 --- a/web/app/view/SettingsMenu.js +++ b/web/app/view/SettingsMenu.js @@ -32,11 +32,15 @@ Ext.define('Traccar.view.SettingsMenu', { text: Strings.settingsUser, handler: 'onUserClick' }, { + hidden: true, text: Strings.settingsGroups, - handler: 'onGroupsClick' + handler: 'onGroupsClick', + reference: 'settingsGroupsButton' }, { + hidden: true, text: Strings.sharedGeofences, - handler: 'onGeofencesClick' + handler: 'onGeofencesClick', + reference: 'settingsGeofencesButton' }, { text: Strings.settingsServer, hidden: true, @@ -48,8 +52,10 @@ Ext.define('Traccar.view.SettingsMenu', { handler: 'onUsersClick', reference: 'settingsUsersButton' }, { + hidden: true, text: Strings.sharedNotifications, - handler: 'onNotificationsClick' + handler: 'onNotificationsClick', + reference: 'settingsNotificationsButton' }, { text: Strings.loginLogout, handler: 'onLogoutClick' diff --git a/web/app/view/SettingsMenuController.js b/web/app/view/SettingsMenuController.js index 45b159ccb..ebaa7007d 100644 --- a/web/app/view/SettingsMenuController.js +++ b/web/app/view/SettingsMenuController.js @@ -30,10 +30,18 @@ Ext.define('Traccar.view.SettingsMenuController', { ], init: function () { - if (Traccar.app.getUser().get('admin')) { + var admin, readonly; + admin = Traccar.app.getUser().get('admin'); + readonly = Traccar.app.getServer().get('readonly'); + if (admin) { this.lookupReference('settingsServerButton').setHidden(false); this.lookupReference('settingsUsersButton').setHidden(false); } + if (admin || !readonly) { + this.lookupReference('settingsGroupsButton').setHidden(false); + this.lookupReference('settingsGeofencesButton').setHidden(false); + this.lookupReference('settingsNotificationsButton').setHidden(false); + } }, onUserClick: function () { -- cgit v1.2.3