From a9478ded48de140d47d17def1ee5329267fe6088 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Wed, 19 Jul 2017 15:24:55 +0500 Subject: Refactored four managers --- setup/default.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'setup') diff --git a/setup/default.xml b/setup/default.xml index 04530104a..d3651b9f0 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -207,7 +207,7 @@ SELECT * FROM events WHERE deviceId = :deviceId AND serverTime BETWEEN :from AND :to ORDER BY serverTime - + SELECT * FROM geofences @@ -328,7 +328,7 @@ VALUES (:captureTime, :activeUsers, :activeDevices, :requests, :messagesReceived, :messagesStored, :mailSent, :smsSent, :geocoderRequests, :geolocationRequests, :attributes) - + SELECT * FROM calendars -- cgit v1.2.3 From afb9a199f57824ec06c993b6028c35b616f64885 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Thu, 20 Jul 2017 08:56:29 +0500 Subject: Combine permission resources and reuse common database functions --- setup/default.xml | 34 +-- src/org/traccar/BaseProtocolDecoder.java | 4 +- .../api/resource/AttributePermissionResource.java | 58 ---- .../traccar/api/resource/AttributeResource.java | 6 +- .../api/resource/CalendarPermissionResource.java | 57 ---- src/org/traccar/api/resource/CalendarResource.java | 6 +- .../api/resource/DeviceAttributeResource.java | 58 ---- .../traccar/api/resource/DeviceDriverResource.java | 58 ---- .../api/resource/DeviceGeofenceResource.java | 57 ---- .../api/resource/DevicePermissionResource.java | 66 ----- src/org/traccar/api/resource/DeviceResource.java | 45 ++- .../api/resource/DriverPermissionResource.java | 59 ---- src/org/traccar/api/resource/DriverResource.java | 6 +- .../api/resource/GeofencePermissionResource.java | 56 ---- src/org/traccar/api/resource/GeofenceResource.java | 6 +- .../api/resource/GroupAttributeResource.java | 58 ---- .../traccar/api/resource/GroupDriverResource.java | 58 ---- .../api/resource/GroupGeofenceResource.java | 56 ---- .../api/resource/GroupPermissionResource.java | 62 ----- src/org/traccar/api/resource/GroupResource.java | 43 ++- .../traccar/api/resource/PermissionsResource.java | 62 +++++ .../api/resource/UserPermissionResource.java | 56 ---- src/org/traccar/api/resource/UserResource.java | 6 +- src/org/traccar/database/AttributesManager.java | 2 +- src/org/traccar/database/DataManager.java | 309 +-------------------- src/org/traccar/database/DeviceManager.java | 16 +- src/org/traccar/database/DriversManager.java | 4 +- .../traccar/database/ExtendedObjectManager.java | 4 +- src/org/traccar/database/GeofenceManager.java | 7 +- src/org/traccar/database/NotificationManager.java | 10 +- src/org/traccar/database/PermissionsManager.java | 62 ++++- src/org/traccar/database/SimpleObjectManager.java | 8 - 32 files changed, 260 insertions(+), 1139 deletions(-) delete mode 100644 src/org/traccar/api/resource/AttributePermissionResource.java delete mode 100644 src/org/traccar/api/resource/CalendarPermissionResource.java delete mode 100644 src/org/traccar/api/resource/DeviceAttributeResource.java delete mode 100644 src/org/traccar/api/resource/DeviceDriverResource.java delete mode 100644 src/org/traccar/api/resource/DeviceGeofenceResource.java delete mode 100644 src/org/traccar/api/resource/DevicePermissionResource.java delete mode 100644 src/org/traccar/api/resource/DriverPermissionResource.java delete mode 100644 src/org/traccar/api/resource/GeofencePermissionResource.java delete mode 100644 src/org/traccar/api/resource/GroupAttributeResource.java delete mode 100644 src/org/traccar/api/resource/GroupDriverResource.java delete mode 100644 src/org/traccar/api/resource/GroupGeofenceResource.java delete mode 100644 src/org/traccar/api/resource/GroupPermissionResource.java create mode 100644 src/org/traccar/api/resource/PermissionsResource.java delete mode 100644 src/org/traccar/api/resource/UserPermissionResource.java (limited to 'setup') diff --git a/setup/default.xml b/setup/default.xml index d3651b9f0..dcf995af0 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -60,7 +60,7 @@ WHERE email = :email - + SELECT * FROM users @@ -111,7 +111,7 @@ SELECT userId, groupId FROM user_group - + SELECT * FROM devices @@ -141,15 +141,15 @@ DELETE FROM devices WHERE id = :id - + INSERT INTO user_device (userId, deviceId) VALUES (:userId, :deviceId) - + DELETE FROM user_device WHERE userId = :userId AND deviceId = :deviceId - + SELECT * FROM groups @@ -165,11 +165,11 @@ DELETE FROM groups WHERE id = :id - + INSERT INTO user_group (userId, groupId) VALUES (:userId, :groupId) - + DELETE FROM user_group WHERE userId = :userId AND groupId = :groupId @@ -234,11 +234,11 @@ SELECT userId, geofenceId FROM user_geofence - + INSERT INTO user_geofence (userId, geofenceId) VALUES (:userId, :geofenceId) - + DELETE FROM user_geofence WHERE userId = :userId AND geofenceId = :geofenceId @@ -353,11 +353,11 @@ SELECT userId, calendarId FROM user_calendar - + INSERT INTO user_calendar (userId, calendarId) VALUES (:userId, :calendarId) - + DELETE FROM user_calendar WHERE userId = :userId AND calendarId = :calendarId @@ -365,11 +365,11 @@ SELECT userId, managedUserId FROM user_user - + INSERT INTO user_user (userId, managedUserId) VALUES (:userId, :managedUserId) - + DELETE FROM user_user WHERE userId = :userId AND managedUserId = :managedUserId @@ -399,11 +399,11 @@ SELECT userId, attributeId FROM user_attribute - + INSERT INTO user_attribute (userId, attributeId) VALUES (:userId, :attributeId) - + DELETE FROM user_attribute WHERE userId = :userId AND attributeId = :attributeId @@ -456,11 +456,11 @@ SELECT userId, driverId FROM user_driver - + INSERT INTO user_driver (userId, driverId) VALUES (:userId, :driverId) - + DELETE FROM user_driver WHERE userId = :userId AND driverId = :driverId diff --git a/src/org/traccar/BaseProtocolDecoder.java b/src/org/traccar/BaseProtocolDecoder.java index 54d2bf28f..b8d2b122c 100644 --- a/src/org/traccar/BaseProtocolDecoder.java +++ b/src/org/traccar/BaseProtocolDecoder.java @@ -51,9 +51,7 @@ public abstract class BaseProtocolDecoder extends ExtendedObjectDecoder { if (defaultGroupId != 0) { Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); } return device.getId(); diff --git a/src/org/traccar/api/resource/AttributePermissionResource.java b/src/org/traccar/api/resource/AttributePermissionResource.java deleted file mode 100644 index 37e50e371..000000000 --- a/src/org/traccar/api/resource/AttributePermissionResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.AttributePermission; - -@Path("permissions/attributes") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class AttributePermissionResource extends BaseResource { - - @POST - public Response add(AttributePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); - Context.getDataManager().linkAttribute(entity.getUserId(), entity.getAttributeId()); - Context.getAttributesManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(AttributePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); - Context.getDataManager().unlinkAttribute(entity.getUserId(), entity.getAttributeId()); - Context.getAttributesManager().refreshUserItems(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index c67d9cafe..2f731e3a7 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -19,6 +19,7 @@ package org.traccar.api.resource; import java.sql.SQLException; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.Set; import javax.ws.rs.Consumes; @@ -86,7 +87,10 @@ public class AttributeResource extends BaseResource { private Response add(Attribute entity) throws SQLException { Context.getAttributesManager().addItem(entity); - Context.getDataManager().linkAttribute(getUserId(), entity.getId()); + LinkedHashMap link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("attributeId", entity.getId()); + Context.getDataManager().linkObject(link, true); Context.getAttributesManager().refreshUserItems(); return Response.ok(entity).build(); } diff --git a/src/org/traccar/api/resource/CalendarPermissionResource.java b/src/org/traccar/api/resource/CalendarPermissionResource.java deleted file mode 100644 index 3936bcaf3..000000000 --- a/src/org/traccar/api/resource/CalendarPermissionResource.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * Copyright 2016 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.CalendarPermission; - -@Path("permissions/calendars") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class CalendarPermissionResource extends BaseResource { - - @POST - public Response add(CalendarPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("calendar", getUserId(), entity.getCalendarId()); - Context.getDataManager().linkCalendar(entity.getUserId(), entity.getCalendarId()); - Context.getCalendarManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(CalendarPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("calendar", getUserId(), entity.getCalendarId()); - Context.getDataManager().unlinkCalendar(entity.getUserId(), entity.getCalendarId()); - Context.getCalendarManager().refreshUserItems(); - return Response.noContent().build(); - } -} diff --git a/src/org/traccar/api/resource/CalendarResource.java b/src/org/traccar/api/resource/CalendarResource.java index be675fc79..f4ff9d4e9 100644 --- a/src/org/traccar/api/resource/CalendarResource.java +++ b/src/org/traccar/api/resource/CalendarResource.java @@ -18,6 +18,7 @@ package org.traccar.api.resource; import java.sql.SQLException; import java.util.Collection; +import java.util.LinkedHashMap; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -66,7 +67,10 @@ public class CalendarResource extends BaseResource { public Response add(Calendar entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getCalendarManager().addItem(entity); - Context.getDataManager().linkCalendar(getUserId(), entity.getId()); + LinkedHashMap link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("calendarId", entity.getId()); + Context.getDataManager().linkObject(link, true); Context.getCalendarManager().refreshUserItems(); return Response.ok(entity).build(); } diff --git a/src/org/traccar/api/resource/DeviceAttributeResource.java b/src/org/traccar/api/resource/DeviceAttributeResource.java deleted file mode 100644 index bb833d4db..000000000 --- a/src/org/traccar/api/resource/DeviceAttributeResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DeviceAttribute; - -@Path("devices/attributes") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DeviceAttributeResource extends BaseResource { - - @POST - public Response add(DeviceAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); - Context.getDataManager().linkDeviceAttribute(entity.getDeviceId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DeviceAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); - Context.getDataManager().unlinkDeviceAttribute(entity.getDeviceId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DeviceDriverResource.java b/src/org/traccar/api/resource/DeviceDriverResource.java deleted file mode 100644 index 286f2223f..000000000 --- a/src/org/traccar/api/resource/DeviceDriverResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DeviceDriver; - -@Path("devices/drivers") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DeviceDriverResource extends BaseResource { - - @POST - public Response add(DeviceDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); - Context.getDataManager().linkDeviceDriver(entity.getDeviceId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DeviceDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); - Context.getDataManager().unlinkDeviceDriver(entity.getDeviceId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DeviceGeofenceResource.java b/src/org/traccar/api/resource/DeviceGeofenceResource.java deleted file mode 100644 index 8034e2168..000000000 --- a/src/org/traccar/api/resource/DeviceGeofenceResource.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DeviceGeofence; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import java.sql.SQLException; - -@Path("devices/geofences") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DeviceGeofenceResource extends BaseResource { - - @POST - public Response add(DeviceGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); - Context.getDataManager().linkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DeviceGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); - Context.getDataManager().unlinkDeviceGeofence(entity.getDeviceId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DevicePermissionResource.java b/src/org/traccar/api/resource/DevicePermissionResource.java deleted file mode 100644 index 6e00dc47f..000000000 --- a/src/org/traccar/api/resource/DevicePermissionResource.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.DevicePermission; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("permissions/devices") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DevicePermissionResource extends BaseResource { - - @POST - public Response add(DevicePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getDataManager().linkDevice(entity.getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DevicePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - if (getUserId() != entity.getUserId()) { - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - } else { - Context.getPermissionsManager().checkAdmin(getUserId()); - } - Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId()); - Context.getDataManager().unlinkDevice(entity.getUserId(), entity.getDeviceId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index ce46b4e29..41a8970e2 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -35,7 +35,9 @@ import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; @Path("devices") @Produces(MediaType.APPLICATION_JSON) @@ -77,11 +79,12 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDeviceReadonly(getUserId()); Context.getPermissionsManager().checkDeviceLimit(getUserId()); Context.getDeviceManager().addDevice(entity); - Context.getDataManager().linkDevice(getUserId(), entity.getId()); + LinkedHashMap link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("deviceId", entity.getId()); + Context.getDataManager().linkObject(link, true); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } @@ -93,13 +96,11 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDevice(getUserId(), entity.getId()); Context.getDeviceManager().updateDevice(entity); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } - @Path("{id}") + @Path("{id : \\d+}") @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); @@ -107,9 +108,7 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDevice(getUserId(), id); Context.getDeviceManager().removeDevice(id); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); Context.getAliasesManager().removeDevice(id); return Response.noContent().build(); } @@ -122,4 +121,28 @@ public class DeviceResource extends BaseResource { return Response.noContent().build(); } + @Path("/{slave : (geofences|drivers|attributes)}") + @POST + public Response add(Map entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + for (String key : entity.keySet()) { + Context.getPermissionsManager().checkPermission(key.replace("Id", ""), getUserId(), entity.get(key)); + } + Context.getDataManager().linkObject(entity, true); + Context.getPermissionsManager().refreshPermissions(entity); + return Response.noContent().build(); + } + + @Path("/{slave : (geofences|drivers|attributes)}") + @DELETE + public Response remove(Map entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + for (String key : entity.keySet()) { + Context.getPermissionsManager().checkPermission(key.replace("Id", ""), getUserId(), entity.get(key)); + } + Context.getDataManager().linkObject(entity, false); + Context.getPermissionsManager().refreshPermissions(entity); + return Response.noContent().build(); + } + } diff --git a/src/org/traccar/api/resource/DriverPermissionResource.java b/src/org/traccar/api/resource/DriverPermissionResource.java deleted file mode 100644 index 4d890cae9..000000000 --- a/src/org/traccar/api/resource/DriverPermissionResource.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; - -import org.traccar.model.DriverPermission; - -@Path("permissions/drivers") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class DriverPermissionResource extends BaseResource { - - @POST - public Response add(DriverPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); - Context.getDataManager().linkDriver(entity.getUserId(), entity.getDriverId()); - Context.getDriversManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(DriverPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); - Context.getDataManager().unlinkDriver(entity.getUserId(), entity.getDriverId()); - Context.getDriversManager().refreshUserItems(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/DriverResource.java b/src/org/traccar/api/resource/DriverResource.java index 3d6a12795..91a502790 100644 --- a/src/org/traccar/api/resource/DriverResource.java +++ b/src/org/traccar/api/resource/DriverResource.java @@ -19,6 +19,7 @@ package org.traccar.api.resource; import java.sql.SQLException; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.Set; import javax.ws.rs.Consumes; @@ -86,7 +87,10 @@ public class DriverResource extends BaseResource { public Response add(Driver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDriversManager().addItem(entity); - Context.getDataManager().linkDriver(getUserId(), entity.getId()); + LinkedHashMap link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("driverId", entity.getId()); + Context.getDataManager().linkObject(link, true); Context.getDriversManager().refreshUserItems(); return Response.ok(entity).build(); } diff --git a/src/org/traccar/api/resource/GeofencePermissionResource.java b/src/org/traccar/api/resource/GeofencePermissionResource.java deleted file mode 100644 index a569e56fd..000000000 --- a/src/org/traccar/api/resource/GeofencePermissionResource.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GeofencePermission; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("permissions/geofences") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GeofencePermissionResource extends BaseResource { - - @POST - public Response add(GeofencePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); - Context.getDataManager().linkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GeofencePermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); - Context.getDataManager().unlinkGeofence(entity.getUserId(), entity.getGeofenceId()); - Context.getGeofenceManager().refreshUserItems(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index 4a52822cc..c39d882b6 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -35,6 +35,7 @@ import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; import java.util.HashSet; +import java.util.LinkedHashMap; import java.util.Set; @Path("geofences") @@ -85,7 +86,10 @@ public class GeofenceResource extends BaseResource { public Response add(Geofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getGeofenceManager().addItem(entity); - Context.getDataManager().linkGeofence(getUserId(), entity.getId()); + LinkedHashMap link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("geofenceId", entity.getId()); + Context.getDataManager().linkObject(link, true); Context.getGeofenceManager().refreshUserItems(); return Response.ok(entity).build(); } diff --git a/src/org/traccar/api/resource/GroupAttributeResource.java b/src/org/traccar/api/resource/GroupAttributeResource.java deleted file mode 100644 index 5df63194e..000000000 --- a/src/org/traccar/api/resource/GroupAttributeResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupAttribute; - -@Path("groups/attributes") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupAttributeResource extends BaseResource { - - @POST - public Response add(GroupAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); - Context.getDataManager().linkGroupAttribute(entity.getGroupId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupAttribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkPermission("attribute", getUserId(), entity.getAttributeId()); - Context.getDataManager().unlinkGroupAttribute(entity.getGroupId(), entity.getAttributeId()); - Context.getAttributesManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupDriverResource.java b/src/org/traccar/api/resource/GroupDriverResource.java deleted file mode 100644 index 71c1aca29..000000000 --- a/src/org/traccar/api/resource/GroupDriverResource.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupDriver; - -@Path("groups/drivers") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupDriverResource extends BaseResource { - - @POST - public Response add(GroupDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); - Context.getDataManager().linkGroupDriver(entity.getGroupId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupDriver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkPermission("driver", getUserId(), entity.getDriverId()); - Context.getDataManager().unlinkGroupDriver(entity.getGroupId(), entity.getDriverId()); - Context.getDriversManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupGeofenceResource.java b/src/org/traccar/api/resource/GroupGeofenceResource.java deleted file mode 100644 index 14000c4b5..000000000 --- a/src/org/traccar/api/resource/GroupGeofenceResource.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2016 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupGeofence; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("groups/geofences") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupGeofenceResource extends BaseResource { - - @POST - public Response add(GroupGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); - Context.getDataManager().linkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupGeofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getPermissionsManager().checkPermission("geofence", getUserId(), entity.getGeofenceId()); - Context.getDataManager().unlinkGroupGeofence(entity.getGroupId(), entity.getGeofenceId()); - Context.getGeofenceManager().refresh(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupPermissionResource.java b/src/org/traccar/api/resource/GroupPermissionResource.java deleted file mode 100644 index 61a725222..000000000 --- a/src/org/traccar/api/resource/GroupPermissionResource.java +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright 2016 - 2017 Anton Tananaev (anton@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.GroupPermission; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; -import java.sql.SQLException; - -@Path("permissions/groups") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class GroupPermissionResource extends BaseResource { - - @POST - public Response add(GroupPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getDataManager().linkGroup(entity.getUserId(), entity.getGroupId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(GroupPermission entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getGroupId()); - Context.getDataManager().unlinkGroup(entity.getUserId(), entity.getGroupId()); - Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index ceba69105..97b6d671d 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -32,6 +32,8 @@ import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; +import java.util.LinkedHashMap; +import java.util.Map; @Path("groups") @Produces(MediaType.APPLICATION_JSON) @@ -61,11 +63,12 @@ public class GroupResource extends BaseResource { public Response add(Group entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDeviceManager().addGroup(entity); - Context.getDataManager().linkGroup(getUserId(), entity.getId()); + LinkedHashMap link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("groupId", entity.getId()); + Context.getDataManager().linkObject(link, true); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } @@ -75,22 +78,42 @@ public class GroupResource extends BaseResource { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), entity.getId()); Context.getDeviceManager().updateGroup(entity); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); - } + Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); } - @Path("{id}") + @Path("{id : \\d+}") @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getPermissionsManager().checkGroup(getUserId(), id); Context.getDeviceManager().removeGroup(id); Context.getPermissionsManager().refreshPermissions(); - if (Context.getGeofenceManager() != null) { - Context.getGeofenceManager().refresh(); + Context.getPermissionsManager().refreshAllExtendedPermissions(); + return Response.noContent().build(); + } + + @Path("/{slave : (geofences|drivers|attributes)}") + @POST + public Response add(Map entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + for (String key : entity.keySet()) { + Context.getPermissionsManager().checkPermission(key.replace("Id", ""), getUserId(), entity.get(key)); + } + Context.getDataManager().linkObject(entity, true); + Context.getPermissionsManager().refreshPermissions(entity); + return Response.noContent().build(); + } + + @Path("/{slave : (geofences|drivers|attributes)}") + @DELETE + public Response remove(Map entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + for (String key : entity.keySet()) { + Context.getPermissionsManager().checkPermission(key.replace("Id", ""), getUserId(), entity.get(key)); } + Context.getDataManager().linkObject(entity, false); + Context.getPermissionsManager().refreshPermissions(entity); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/PermissionsResource.java b/src/org/traccar/api/resource/PermissionsResource.java new file mode 100644 index 000000000..ac7acb93f --- /dev/null +++ b/src/org/traccar/api/resource/PermissionsResource.java @@ -0,0 +1,62 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.api.resource; + +import java.sql.SQLException; +import java.util.Map; + +import javax.ws.rs.Consumes; +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; + +import org.traccar.Context; +import org.traccar.api.BaseResource; + +@Path("permissions") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class PermissionsResource extends BaseResource { + + @Path("/{slave : (users|devices|groups|geofences|drivers|attributes|calendars)}") + @POST + public Response add(Map entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + for (String key : entity.keySet()) { + Context.getPermissionsManager().checkPermission(key.replace("Id", ""), getUserId(), entity.get(key)); + } + Context.getDataManager().linkObject(entity, true); + Context.getPermissionsManager().refreshPermissions(entity); + return Response.noContent().build(); + } + + @Path("/{slave : (users|devices|groups|geofences|drivers|attributes|calendars)}") + @DELETE + public Response remove(Map entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + for (String key : entity.keySet()) { + Context.getPermissionsManager().checkPermission(key.replace("Id", ""), getUserId(), entity.get(key)); + } + Context.getDataManager().linkObject(entity, false); + Context.getPermissionsManager().refreshPermissions(entity); + return Response.noContent().build(); + } + +} diff --git a/src/org/traccar/api/resource/UserPermissionResource.java b/src/org/traccar/api/resource/UserPermissionResource.java deleted file mode 100644 index a97c4a665..000000000 --- a/src/org/traccar/api/resource/UserPermissionResource.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 2017 Anton Tananaev (anton@traccar.org) - * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.traccar.api.resource; - -import java.sql.SQLException; - -import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; -import javax.ws.rs.POST; -import javax.ws.rs.Path; -import javax.ws.rs.Produces; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; - -import org.traccar.Context; -import org.traccar.api.BaseResource; -import org.traccar.model.UserPermission; - -@Path("permissions/users") -@Produces(MediaType.APPLICATION_JSON) -@Consumes(MediaType.APPLICATION_JSON) -public class UserPermissionResource extends BaseResource { - - @POST - public Response add(UserPermission entity) throws SQLException { - Context.getPermissionsManager().checkAdmin(getUserId()); - if (entity.getUserId() != entity.getManagedUserId()) { - Context.getDataManager().linkUser(entity.getUserId(), entity.getManagedUserId()); - Context.getPermissionsManager().refreshUserPermissions(); - } - return Response.ok(entity).build(); - } - - @DELETE - public Response remove(UserPermission entity) throws SQLException { - Context.getPermissionsManager().checkAdmin(getUserId()); - Context.getDataManager().unlinkUser(entity.getUserId(), entity.getManagedUserId()); - Context.getPermissionsManager().refreshUserPermissions(); - return Response.noContent().build(); - } - -} diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index b9187fdbb..94cc56b47 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -34,6 +34,7 @@ import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; import java.util.Date; +import java.util.LinkedHashMap; @Path("users") @Produces(MediaType.APPLICATION_JSON) @@ -74,7 +75,10 @@ public class UserResource extends BaseResource { } Context.getPermissionsManager().addUser(entity); if (Context.getPermissionsManager().isManager(getUserId())) { - Context.getDataManager().linkUser(getUserId(), entity.getId()); + LinkedHashMap link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("managedUserId", entity.getId()); + Context.getDataManager().linkObject(link, true); } Context.getPermissionsManager().refreshUserPermissions(); if (Context.getNotificationManager() != null) { diff --git a/src/org/traccar/database/AttributesManager.java b/src/org/traccar/database/AttributesManager.java index 88bedfba4..ebfdbc9b4 100644 --- a/src/org/traccar/database/AttributesManager.java +++ b/src/org/traccar/database/AttributesManager.java @@ -29,7 +29,7 @@ public class AttributesManager extends ExtendedObjectManager { public AttributesManager(DataManager dataManager) { super(dataManager, Attribute.class, AttributePermission.class, DeviceAttribute.class, GroupAttribute.class); refreshItems(); - refresh(); + refreshExtendedPermissions(); } @Override diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 4bf4b1b9a..6c9cca86c 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -23,6 +23,7 @@ import java.sql.SQLException; import java.text.SimpleDateFormat; import java.util.Collection; import java.util.Date; +import java.util.Map; import javax.naming.InitialContext; import javax.sql.DataSource; @@ -39,17 +40,12 @@ import org.traccar.Config; import org.traccar.helper.Log; import org.traccar.model.AttributeAlias; import org.traccar.model.Device; -import org.traccar.model.DevicePermission; import org.traccar.model.Event; -import org.traccar.model.Group; -import org.traccar.model.GroupPermission; import org.traccar.model.Identifiable; -import org.traccar.model.Notification; import org.traccar.model.Position; import org.traccar.model.Server; import org.traccar.model.Statistics; import org.traccar.model.User; -import org.traccar.model.UserPermission; import com.zaxxer.hikari.HikariConfig; import com.zaxxer.hikari.HikariDataSource; @@ -153,17 +149,6 @@ public class DataManager { } } - public Collection getUsers() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectUsersAll")) - .executeQuery(User.class); - } - - public void addUser(User user) throws SQLException { - user.setId(QueryBuilder.create(dataSource, getQuery("database.insertUser"), true) - .setObject(user) - .executeUpdate()); - } - public void updateUser(User user) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.updateUser")) .setObject(user) @@ -175,102 +160,12 @@ public class DataManager { } } - public void removeUser(long userId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteUser")) - .setLong("id", userId) - .executeUpdate(); - } - - public Collection getDevicePermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDevicePermissions")) - .executeQuery(DevicePermission.class); - } - - public Collection getGroupPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupPermissions")) - .executeQuery(GroupPermission.class); - } - - public Collection getAllDevices() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectDevicesAll")) - .executeQuery(Device.class); - } - - public void addDevice(Device device) throws SQLException { - device.setId(QueryBuilder.create(dataSource, getQuery("database.insertDevice"), true) - .setObject(device) - .executeUpdate()); - } - - public void updateDevice(Device device) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateDevice")) - .setObject(device) - .executeUpdate(); - } - public void updateDeviceStatus(Device device) throws SQLException { QueryBuilder.create(dataSource, getQuery("database.updateDeviceStatus")) .setObject(device) .executeUpdate(); } - public void removeDevice(long deviceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteDevice")) - .setLong("id", deviceId) - .executeUpdate(); - } - - public void linkDevice(long userId, long deviceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDevice")) - .setLong("userId", userId) - .setLong("deviceId", deviceId) - .executeUpdate(); - } - - public void unlinkDevice(long userId, long deviceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDevice")) - .setLong("userId", userId) - .setLong("deviceId", deviceId) - .executeUpdate(); - } - - public Collection getAllGroups() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectGroupsAll")) - .executeQuery(Group.class); - } - - public void addGroup(Group group) throws SQLException { - group.setId(QueryBuilder.create(dataSource, getQuery("database.insertGroup"), true) - .setObject(group) - .executeUpdate()); - } - - public void updateGroup(Group group) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateGroup")) - .setObject(group) - .executeUpdate(); - } - - public void removeGroup(long groupId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteGroup")) - .setLong("id", groupId) - .executeUpdate(); - } - - public void linkGroup(long userId, long groupId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroup")) - .setLong("userId", userId) - .setLong("groupId", groupId) - .executeUpdate(); - } - - public void unlinkGroup(long userId, long groupId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroup")) - .setLong("userId", userId) - .setLong("groupId", groupId) - .executeUpdate(); - } - public Collection getPositions(long deviceId, Date from, Date to) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectPositions")) .setLong("deviceId", deviceId) @@ -323,24 +218,12 @@ public class DataManager { .executeQuerySingle(Server.class); } - public void updateServer(Server server) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateServer")) - .setObject(server) - .executeUpdate(); - } - public Event getEvent(long eventId) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectEvent")) .setLong("id", eventId) .executeQuerySingle(Event.class); } - public void addEvent(Event event) throws SQLException { - event.setId(QueryBuilder.create(dataSource, getQuery("database.insertEvent"), true) - .setObject(event) - .executeUpdate()); - } - public Collection getEvents(long deviceId, Date from, Date to) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectEvents")) .setLong("deviceId", deviceId) @@ -349,71 +232,6 @@ public class DataManager { .executeQuery(Event.class); } - public void linkGeofence(long userId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGeofence")) - .setLong("userId", userId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void unlinkGeofence(long userId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGeofence")) - .setLong("userId", userId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void linkGroupGeofence(long groupId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroupGeofence")) - .setLong("groupId", groupId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void unlinkGroupGeofence(long groupId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroupGeofence")) - .setLong("groupId", groupId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void linkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDeviceGeofence")) - .setLong("deviceId", deviceId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public void unlinkDeviceGeofence(long deviceId, long geofenceId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceGeofence")) - .setLong("deviceId", deviceId) - .setLong("geofenceId", geofenceId) - .executeUpdate(); - } - - public Collection getNotifications() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectNotifications")) - .executeQuery(Notification.class); - } - - public void addNotification(Notification notification) throws SQLException { - notification.setId(QueryBuilder.create(dataSource, getQuery("database.insertNotification"), true) - .setObject(notification) - .executeUpdate()); - } - - public void updateNotification(Notification notification) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.updateNotification")) - .setObject(notification) - .executeUpdate(); - } - - public void removeNotification(Notification notification) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.deleteNotification")) - .setLong("id", notification.getId()) - .executeUpdate(); - } - public Collection getAttributeAliases() throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectAttributeAliases")) .executeQuery(AttributeAlias.class); @@ -450,121 +268,16 @@ public class DataManager { .executeUpdate()); } - public void linkCalendar(long userId, long calendarId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkCalendar")) - .setLong("userId", userId) - .setLong("calendarId", calendarId) - .executeUpdate(); - } - - public void unlinkCalendar(long userId, long calendarId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkCalendar")) - .setLong("userId", userId) - .setLong("calendarId", calendarId) - .executeUpdate(); - } - - public Collection getUserPermissions() throws SQLException { - return QueryBuilder.create(dataSource, getQuery("database.selectUserPermissions")) - .executeQuery(UserPermission.class); - } - - public void linkUser(long userId, long managedUserId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkUser")) - .setLong("userId", userId) - .setLong("managedUserId", managedUserId) - .executeUpdate(); - } - - public void unlinkUser(long userId, long managedUserId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkUser")) - .setLong("userId", userId) - .setLong("managedUserId", managedUserId) - .executeUpdate(); - } - - public void linkAttribute(long userId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkAttribute")) - .setLong("userId", userId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void unlinkAttribute(long userId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkAttribute")) - .setLong("userId", userId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void linkGroupAttribute(long groupId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroupAttribute")) - .setLong("groupId", groupId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void unlinkGroupAttribute(long groupId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroupAttribute")) - .setLong("groupId", groupId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void linkDeviceAttribute(long deviceId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDeviceAttribute")) - .setLong("deviceId", deviceId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void unlinkDeviceAttribute(long deviceId, long attributeId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceAttribute")) - .setLong("deviceId", deviceId) - .setLong("attributeId", attributeId) - .executeUpdate(); - } - - public void linkDriver(long userId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDriver")) - .setLong("userId", userId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void unlinkDriver(long userId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDriver")) - .setLong("userId", userId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void linkGroupDriver(long groupId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkGroupDriver")) - .setLong("groupId", groupId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void unlinkGroupDriver(long groupId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkGroupDriver")) - .setLong("groupId", groupId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void linkDeviceDriver(long deviceId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.linkDeviceDriver")) - .setLong("deviceId", deviceId) - .setLong("driverId", driverId) - .executeUpdate(); - } - - public void unlinkDeviceDriver(long deviceId, long driverId) throws SQLException { - QueryBuilder.create(dataSource, getQuery("database.unlinkDeviceDriver")) - .setLong("deviceId", deviceId) - .setLong("driverId", driverId) - .executeUpdate(); + public void linkObject(Map permission, boolean link) throws SQLException { + String query = "database." + (!link ? "un" : "") + "link"; + for (String key : permission.keySet()) { + query += key.substring(0, 1).toUpperCase() + key.replace("Id", "").substring(1); + } + QueryBuilder queryBuilder = QueryBuilder.create(dataSource, getQuery(query)); + for (String key : permission.keySet()) { + queryBuilder.setLong(key, permission.get(key)); + } + queryBuilder.executeUpdate(); } public Collection getObjects(Class clazz) throws SQLException { diff --git a/src/org/traccar/database/DeviceManager.java b/src/org/traccar/database/DeviceManager.java index f2a2dd565..ba360f165 100644 --- a/src/org/traccar/database/DeviceManager.java +++ b/src/org/traccar/database/DeviceManager.java @@ -85,7 +85,7 @@ public class DeviceManager implements IdentityManager { if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) && devicesLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { GeofenceManager geofenceManager = Context.getGeofenceManager(); - Collection databaseDevices = dataManager.getAllDevices(); + Collection databaseDevices = dataManager.getObjects(Device.class); if (devicesById == null) { devicesById = new ConcurrentHashMap<>(databaseDevices.size()); } @@ -199,7 +199,7 @@ public class DeviceManager implements IdentityManager { } public void addDevice(Device device) throws SQLException { - dataManager.addDevice(device); + dataManager.addObject(device); devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); @@ -209,7 +209,7 @@ public class DeviceManager implements IdentityManager { } public void updateDevice(Device device) throws SQLException { - dataManager.updateDevice(device); + dataManager.updateObject(device); devicesById.put(device.getId(), device); devicesByUniqueId.put(device.getUniqueId(), device); @@ -227,7 +227,7 @@ public class DeviceManager implements IdentityManager { } public void removeDevice(long deviceId) throws SQLException { - dataManager.removeDevice(deviceId); + dataManager.removeObject(Device.class, deviceId); if (devicesById.containsKey(deviceId)) { String deviceUniqueId = devicesById.get(deviceId).getUniqueId(); @@ -289,7 +289,7 @@ public class DeviceManager implements IdentityManager { long lastUpdate = groupsLastUpdate.get(); if ((force || System.currentTimeMillis() - lastUpdate > dataRefreshDelay) && groupsLastUpdate.compareAndSet(lastUpdate, System.currentTimeMillis())) { - Collection databaseGroups = dataManager.getAllGroups(); + Collection databaseGroups = dataManager.getObjects(Group.class); if (groupsById == null) { groupsById = new ConcurrentHashMap<>(databaseGroups.size()); } @@ -359,18 +359,18 @@ public class DeviceManager implements IdentityManager { public void addGroup(Group group) throws SQLException { checkGroupCycles(group); - dataManager.addGroup(group); + dataManager.addObject(group); groupsById.put(group.getId(), group); } public void updateGroup(Group group) throws SQLException { checkGroupCycles(group); - dataManager.updateGroup(group); + dataManager.updateObject(group); groupsById.put(group.getId(), group); } public void removeGroup(long groupId) throws SQLException { - dataManager.removeGroup(groupId); + dataManager.removeObject(Group.class, groupId); groupsById.remove(groupId); } diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java index 391708f64..9b01f1a70 100644 --- a/src/org/traccar/database/DriversManager.java +++ b/src/org/traccar/database/DriversManager.java @@ -34,7 +34,7 @@ public class DriversManager extends ExtendedObjectManager { public DriversManager(DataManager dataManager) { super(dataManager, Driver.class, DriverPermission.class, DeviceDriver.class, GroupDriver.class); refreshItems(); - refresh(); + refreshExtendedPermissions(); } @Override @@ -83,7 +83,7 @@ public class DriversManager extends ExtendedObjectManager { driversByUniqueId.remove(driverUniqueId); } refreshUserItems(); - refresh(); + refreshExtendedPermissions(); } public Driver getDriverByUniqueId(String uniqueId) { diff --git a/src/org/traccar/database/ExtendedObjectManager.java b/src/org/traccar/database/ExtendedObjectManager.java index 5147b9f9f..a6508f066 100644 --- a/src/org/traccar/database/ExtendedObjectManager.java +++ b/src/org/traccar/database/ExtendedObjectManager.java @@ -82,10 +82,10 @@ public abstract class ExtendedObjectManager extends SimpleObjectManager { @Override public void removeItem(long itemId) throws SQLException { super.removeItem(itemId); - refresh(); + refreshExtendedPermissions(); } - public void refresh() { + public void refreshExtendedPermissions() { if (getDataManager() != null) { try { diff --git a/src/org/traccar/database/GeofenceManager.java b/src/org/traccar/database/GeofenceManager.java index 868804d8f..ed1f26f78 100644 --- a/src/org/traccar/database/GeofenceManager.java +++ b/src/org/traccar/database/GeofenceManager.java @@ -31,11 +31,12 @@ public class GeofenceManager extends ExtendedObjectManager { public GeofenceManager(DataManager dataManager) { super(dataManager, Geofence.class, GeofencePermission.class, DeviceGeofence.class, GroupGeofence.class); refreshItems(); - refresh(); + refreshExtendedPermissions(); } - public final void refresh() { - super.refresh(); + @Override + public final void refreshExtendedPermissions() { + super.refreshExtendedPermissions(); recalculateDevicesGeofences(); } diff --git a/src/org/traccar/database/NotificationManager.java b/src/org/traccar/database/NotificationManager.java index 11d5a9aca..0987f255b 100644 --- a/src/org/traccar/database/NotificationManager.java +++ b/src/org/traccar/database/NotificationManager.java @@ -49,7 +49,7 @@ public class NotificationManager { public void updateEvent(Event event, Position position) { try { - dataManager.addEvent(event); + dataManager.addObject(event); } catch (SQLException error) { Log.warning(error); } @@ -105,7 +105,7 @@ public class NotificationManager { notificationsLock.writeLock().lock(); try { userNotifications.clear(); - for (Notification notification : dataManager.getNotifications()) { + for (Notification notification : dataManager.getObjects(Notification.class)) { getUserNotificationsUnsafe(notification.getUserId()).add(notification); } } finally { @@ -139,7 +139,7 @@ public class NotificationManager { || cachedNotification.getSms() != notification.getSms()) { if (!notification.getWeb() && !notification.getMail() && !notification.getSms()) { try { - dataManager.removeNotification(cachedNotification); + dataManager.removeObject(Notification.class, cachedNotification.getId()); } catch (SQLException error) { Log.warning(error); } @@ -160,7 +160,7 @@ public class NotificationManager { notificationsLock.writeLock().unlock(); } try { - dataManager.updateNotification(cachedNotification); + dataManager.updateObject(cachedNotification); } catch (SQLException error) { Log.warning(error); } @@ -170,7 +170,7 @@ public class NotificationManager { } } else if (notification.getWeb() || notification.getMail() || notification.getSms()) { try { - dataManager.addNotification(notification); + dataManager.addObject(notification); } catch (SQLException error) { Log.warning(error); } diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index f88e14bfe..caae86b84 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -98,7 +98,7 @@ public class PermissionsManager { usersTokens.clear(); try { server = dataManager.getServer(); - for (User user : dataManager.getUsers()) { + for (User user : dataManager.getObjects(User.class)) { users.put(user.getId(), user); if (user.getToken() != null) { usersTokens.put(user.getToken(), user.getId()); @@ -112,7 +112,7 @@ public class PermissionsManager { public final void refreshUserPermissions() { userPermissions.clear(); try { - for (UserPermission permission : dataManager.getUserPermissions()) { + for (UserPermission permission : dataManager.getObjects(UserPermission.class)) { getUserPermissions(permission.getUserId()).add(permission.getManagedUserId()); } } catch (SQLException error) { @@ -126,7 +126,7 @@ public class PermissionsManager { try { GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(), Context.getDeviceManager().getAllDevices()); - for (GroupPermission permission : dataManager.getGroupPermissions()) { + for (GroupPermission permission : dataManager.getObjects(GroupPermission.class)) { Set userGroupPermissions = getGroupPermissions(permission.getUserId()); Set userDevicePermissions = getDevicePermissions(permission.getUserId()); userGroupPermissions.add(permission.getGroupId()); @@ -137,7 +137,7 @@ public class PermissionsManager { userDevicePermissions.add(device.getId()); } } - for (DevicePermission permission : dataManager.getDevicePermissions()) { + for (DevicePermission permission : dataManager.getObjects(DevicePermission.class)) { getDevicePermissions(permission.getUserId()).add(permission.getDeviceId()); } @@ -302,6 +302,15 @@ public class PermissionsManager { SimpleObjectManager manager = null; switch (object) { + case "device": + checkDevice(userId, objectId); + break; + case "group": + checkGroup(userId, objectId); + break; + case "user": + checkUser(userId, objectId); + break; case "geofence": manager = Context.getGeofenceManager(); break; @@ -332,12 +341,51 @@ public class PermissionsManager { } } + public void refreshAllExtendedPermissions() { + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refreshExtendedPermissions(); + } + Context.getDriversManager().refreshExtendedPermissions(); + Context.getAttributesManager().refreshExtendedPermissions(); + } + + public void refreshPermissions(Map entity) { + if (entity.containsKey("userId")) { + if (entity.containsKey("deviceId") || entity.containsKey("groupId")) { + refreshPermissions(); + refreshAllExtendedPermissions(); + } else if (entity.containsKey("managedUserId")) { + refreshUserPermissions(); + } else if (entity.containsKey("geofenceId")) { + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refreshUserItems(); + } + } else if (entity.containsKey("driverId")) { + Context.getDriversManager().refreshUserItems(); + } else if (entity.containsKey("attributeId")) { + Context.getAttributesManager().refreshUserItems(); + } else if (entity.containsKey("calendarId")) { + Context.getCalendarManager().refreshUserItems(); + } + } else if (entity.containsKey("deviceId") || entity.containsKey("groupId")) { + if (entity.containsKey("geofenceId")) { + if (Context.getGeofenceManager() != null) { + Context.getGeofenceManager().refreshExtendedPermissions(); + } + } else if (entity.containsKey("driverId")) { + Context.getDriversManager().refreshExtendedPermissions(); + } else if (entity.containsKey("attributeId")) { + Context.getAttributesManager().refreshExtendedPermissions(); + } + } + } + public Server getServer() { return server; } public void updateServer(Server server) throws SQLException { - dataManager.updateServer(server); + dataManager.updateObject(server); this.server = server; } @@ -364,7 +412,7 @@ public class PermissionsManager { } public void addUser(User user) throws SQLException { - dataManager.addUser(user); + dataManager.addObject(user); users.put(user.getId(), user); if (user.getToken() != null) { usersTokens.put(user.getToken(), user.getId()); @@ -386,7 +434,7 @@ public class PermissionsManager { } public void removeUser(long userId) throws SQLException { - dataManager.removeUser(userId); + dataManager.removeObject(User.class, userId); usersTokens.remove(users.get(userId).getToken()); users.remove(userId); refreshPermissions(); diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java index 84d41a84f..f5f807850 100644 --- a/src/org/traccar/database/SimpleObjectManager.java +++ b/src/org/traccar/database/SimpleObjectManager.java @@ -132,14 +132,6 @@ public abstract class SimpleObjectManager { refreshUserItems(); } -// public final Collection getItems(Set itemIds) { -// Collection result = new LinkedList<>(); -// for (long itemId : itemIds) { -// result.add(getById(itemId)); -// } -// return result; -// } - public final Collection getItems(Class clazz, Set itemIds) { Collection result = new LinkedList<>(); for (long itemId : itemIds) { -- cgit v1.2.3 From 69c46399ee0f6b3dd2b0589cb412e9e17f040369 Mon Sep 17 00:00:00 2001 From: Abyss777 Date: Fri, 21 Jul 2017 18:41:49 +0500 Subject: Use classes instead of constants everywhere --- setup/default.xml | 14 ++-- src/org/traccar/Context.java | 8 -- src/org/traccar/api/BaseResource.java | 29 +++---- .../traccar/api/resource/AttributeResource.java | 6 +- src/org/traccar/api/resource/CalendarResource.java | 6 +- src/org/traccar/api/resource/DeviceResource.java | 2 +- src/org/traccar/api/resource/DriverResource.java | 6 +- src/org/traccar/api/resource/EventResource.java | 3 +- src/org/traccar/api/resource/GeofenceResource.java | 6 +- src/org/traccar/api/resource/GroupResource.java | 2 +- .../traccar/api/resource/PermissionsResource.java | 10 +-- src/org/traccar/api/resource/UserResource.java | 3 +- src/org/traccar/database/DataManager.java | 21 +++-- src/org/traccar/database/DriversManager.java | 2 +- .../traccar/database/ExtendedObjectManager.java | 14 ++-- src/org/traccar/database/PermissionsManager.java | 96 +++++++++++----------- src/org/traccar/database/SimpleObjectManager.java | 16 ++-- src/org/traccar/model/ManagedUser.java | 21 +++++ 18 files changed, 144 insertions(+), 121 deletions(-) create mode 100644 src/org/traccar/model/ManagedUser.java (limited to 'setup') diff --git a/setup/default.xml b/setup/default.xml index dcf995af0..f0f9ef4a3 100644 --- a/setup/default.xml +++ b/setup/default.xml @@ -103,11 +103,11 @@ DELETE FROM users WHERE id = :id - + SELECT userId, deviceId FROM user_device - + SELECT userId, groupId FROM user_group @@ -230,7 +230,7 @@ DELETE FROM geofences WHERE id = :id - + SELECT userId, geofenceId FROM user_geofence @@ -349,7 +349,7 @@ DELETE FROM calendars WHERE id = :id - + SELECT userId, calendarId FROM user_calendar @@ -361,7 +361,7 @@ DELETE FROM user_calendar WHERE userId = :userId AND calendarId = :calendarId - + SELECT userId, managedUserId FROM user_user @@ -395,7 +395,7 @@ DELETE FROM attributes WHERE id = :id - + SELECT userId, attributeId FROM user_attribute @@ -452,7 +452,7 @@ DELETE FROM drivers WHERE id = :id - + SELECT userId, driverId FROM user_driver diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index 57c6bc908..a4fc5b679 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -59,14 +59,6 @@ import org.traccar.web.WebServer; public final class Context { - public static final String TYPE_USER = "User"; - public static final String TYPE_DEVICE = "Device"; - public static final String TYPE_GROUP = "Group"; - public static final String TYPE_GEOFENCE = "Geofence"; - public static final String TYPE_CALENDAR = "Calendar"; - public static final String TYPE_ATTRIBUTE = "Attribute"; - public static final String TYPE_DRIVER = "Driver"; - private Context() { } diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java index 4ad1477c2..920bb3931 100644 --- a/src/org/traccar/api/BaseResource.java +++ b/src/org/traccar/api/BaseResource.java @@ -1,5 +1,5 @@ /* - * Copyright 2015 - 2016 Anton Tananaev (anton@traccar.org) + * Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,13 +17,15 @@ package org.traccar.api; import java.sql.SQLException; import java.util.Iterator; -import java.util.Map; +import java.util.LinkedHashMap; import javax.ws.rs.core.SecurityContext; import org.traccar.Context; import org.traccar.database.DataManager; import org.traccar.model.BaseModel; +import org.traccar.model.Device; +import org.traccar.model.User; public class BaseResource { @@ -38,34 +40,33 @@ public class BaseResource { return 0; } - protected void handlePermission(Map entity, boolean link) throws SQLException { - if (entity.size() != 2) { - throw new IllegalArgumentException(); - } + protected void checkAndLinkPermission(LinkedHashMap entity, boolean link) + throws SQLException, ClassNotFoundException { Iterator iterator = entity.keySet().iterator(); String owner = iterator.next(); + Class ownerClass = DataManager.getClassByName(owner); String property = iterator.next(); + Class propertyClass = DataManager.getClassByName(property); long ownerId = entity.get(owner); long propertyId = entity.get(property); - if (!link && DataManager.makeName(owner).equals(Context.TYPE_USER) - && DataManager.makeName(property).equals(Context.TYPE_DEVICE)) { + if (!link && ownerClass.equals(User.class) + && propertyClass.equals(Device.class)) { if (getUserId() != ownerId) { Context.getPermissionsManager().checkUser(getUserId(), ownerId); } else { Context.getPermissionsManager().checkAdmin(getUserId()); } } else { - Context.getPermissionsManager().checkPermission(owner, getUserId(), ownerId); + Context.getPermissionsManager().checkPermission(ownerClass, getUserId(), ownerId); } - Context.getPermissionsManager().checkPermission(property, getUserId(), propertyId); + Context.getPermissionsManager().checkPermission(propertyClass, getUserId(), propertyId); - Context.getDataManager().linkObject(owner, ownerId, property, propertyId, link); + Context.getDataManager().linkObject(ownerClass, ownerId, propertyClass, propertyId, link); } - protected void linkNew(BaseModel entity) throws SQLException { - Context.getDataManager().linkObject("userId", getUserId(), - entity.getClass().getSimpleName(), entity.getId(), true); + protected void linkNewEntity(BaseModel entity) throws SQLException { + Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); } } diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index 63cdfb2a7..7e6947d26 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -87,7 +87,7 @@ public class AttributeResource extends BaseResource { private Response add(Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getAttributesManager().addItem(entity); - linkNew(entity); + linkNewEntity(entity); Context.getAttributesManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -128,7 +128,7 @@ public class AttributeResource extends BaseResource { @PUT public Response update(Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_ATTRIBUTE, getUserId(), entity.getId()); + Context.getPermissionsManager().checkPermission(Attribute.class, getUserId(), entity.getId()); Context.getAttributesManager().updateItem(entity); return Response.ok(entity).build(); } @@ -137,7 +137,7 @@ public class AttributeResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_ATTRIBUTE, getUserId(), id); + Context.getPermissionsManager().checkPermission(Attribute.class, getUserId(), id); Context.getAttributesManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/CalendarResource.java b/src/org/traccar/api/resource/CalendarResource.java index d29080ee2..30f5df6b8 100644 --- a/src/org/traccar/api/resource/CalendarResource.java +++ b/src/org/traccar/api/resource/CalendarResource.java @@ -66,7 +66,7 @@ public class CalendarResource extends BaseResource { public Response add(Calendar entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getCalendarManager().addItem(entity); - linkNew(entity); + linkNewEntity(entity); Context.getCalendarManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -75,7 +75,7 @@ public class CalendarResource extends BaseResource { @PUT public Response update(Calendar entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_CALENDAR, getUserId(), entity.getId()); + Context.getPermissionsManager().checkPermission(Calendar.class, getUserId(), entity.getId()); Context.getCalendarManager().updateItem(entity); return Response.ok(entity).build(); } @@ -84,7 +84,7 @@ public class CalendarResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_CALENDAR, getUserId(), id); + Context.getPermissionsManager().checkPermission(Calendar.class, getUserId(), id); Context.getCalendarManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/DeviceResource.java b/src/org/traccar/api/resource/DeviceResource.java index 285ed6a31..e1c0fc96d 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -77,7 +77,7 @@ public class DeviceResource extends BaseResource { Context.getPermissionsManager().checkDeviceReadonly(getUserId()); Context.getPermissionsManager().checkDeviceLimit(getUserId()); Context.getDeviceManager().addDevice(entity); - linkNew(entity); + linkNewEntity(entity); Context.getPermissionsManager().refreshPermissions(); Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); diff --git a/src/org/traccar/api/resource/DriverResource.java b/src/org/traccar/api/resource/DriverResource.java index b528a197a..cf80bf7df 100644 --- a/src/org/traccar/api/resource/DriverResource.java +++ b/src/org/traccar/api/resource/DriverResource.java @@ -86,7 +86,7 @@ public class DriverResource extends BaseResource { public Response add(Driver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDriversManager().addItem(entity); - linkNew(entity); + linkNewEntity(entity); Context.getDriversManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -95,7 +95,7 @@ public class DriverResource extends BaseResource { @PUT public Response update(Driver entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_DRIVER, getUserId(), entity.getId()); + Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), entity.getId()); Context.getDriversManager().updateItem(entity); return Response.ok(entity).build(); } @@ -104,7 +104,7 @@ public class DriverResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_DRIVER, getUserId(), id); + Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), id); Context.getDriversManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java index 85f981514..b7fda6f73 100644 --- a/src/org/traccar/api/resource/EventResource.java +++ b/src/org/traccar/api/resource/EventResource.java @@ -12,6 +12,7 @@ import javax.ws.rs.core.MediaType; import org.traccar.Context; import org.traccar.api.BaseResource; import org.traccar.model.Event; +import org.traccar.model.Geofence; @Path("events") @Produces(MediaType.APPLICATION_JSON) @@ -25,7 +26,7 @@ public class EventResource extends BaseResource { Event event = Context.getDataManager().getEvent(id); Context.getPermissionsManager().checkDevice(getUserId(), event.getDeviceId()); if (event.getGeofenceId() != 0) { - Context.getPermissionsManager().checkPermission(Context.TYPE_GEOFENCE, getUserId(), event.getGeofenceId()); + Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), event.getGeofenceId()); } return event; } diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index c9cc72bd7..341180cf8 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -85,7 +85,7 @@ public class GeofenceResource extends BaseResource { public Response add(Geofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getGeofenceManager().addItem(entity); - linkNew(entity); + linkNewEntity(entity); Context.getGeofenceManager().refreshUserItems(); return Response.ok(entity).build(); } @@ -94,7 +94,7 @@ public class GeofenceResource extends BaseResource { @PUT public Response update(Geofence entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_GEOFENCE, getUserId(), entity.getId()); + Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), entity.getId()); Context.getGeofenceManager().updateItem(entity); return Response.ok(entity).build(); } @@ -103,7 +103,7 @@ public class GeofenceResource extends BaseResource { @DELETE public Response remove(@PathParam("id") long id) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Context.TYPE_GEOFENCE, getUserId(), id); + Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), id); Context.getGeofenceManager().removeItem(id); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/GroupResource.java b/src/org/traccar/api/resource/GroupResource.java index 5d575ebfe..2ecd11fe5 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -61,7 +61,7 @@ public class GroupResource extends BaseResource { public Response add(Group entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); Context.getDeviceManager().addGroup(entity); - linkNew(entity); + linkNewEntity(entity); Context.getPermissionsManager().refreshPermissions(); Context.getPermissionsManager().refreshAllExtendedPermissions(); return Response.ok(entity).build(); diff --git a/src/org/traccar/api/resource/PermissionsResource.java b/src/org/traccar/api/resource/PermissionsResource.java index 707242cf1..515efbe41 100644 --- a/src/org/traccar/api/resource/PermissionsResource.java +++ b/src/org/traccar/api/resource/PermissionsResource.java @@ -17,7 +17,7 @@ package org.traccar.api.resource; import java.sql.SQLException; -import java.util.Map; +import java.util.LinkedHashMap; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -36,17 +36,17 @@ import org.traccar.api.BaseResource; public class PermissionsResource extends BaseResource { @POST - public Response add(Map entity) throws SQLException { + public Response add(LinkedHashMap entity) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); - handlePermission(entity, true); + checkAndLinkPermission(entity, true); Context.getPermissionsManager().refreshPermissions(entity); return Response.noContent().build(); } @DELETE - public Response remove(Map entity) throws SQLException { + public Response remove(LinkedHashMap entity) throws SQLException, ClassNotFoundException { Context.getPermissionsManager().checkReadonly(getUserId()); - handlePermission(entity, false); + checkAndLinkPermission(entity, false); Context.getPermissionsManager().refreshPermissions(entity); return Response.noContent().build(); } diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index e710c940d..98395e3cc 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -17,6 +17,7 @@ package org.traccar.api.resource; import org.traccar.Context; import org.traccar.api.BaseResource; +import org.traccar.model.ManagedUser; import org.traccar.model.User; import javax.annotation.security.PermitAll; @@ -74,7 +75,7 @@ public class UserResource extends BaseResource { } Context.getPermissionsManager().addUser(entity); if (Context.getPermissionsManager().isManager(getUserId())) { - Context.getDataManager().linkObject("userId", getUserId(), "managedUserId", entity.getId(), true); + Context.getDataManager().linkObject(User.class, getUserId(), ManagedUser.class, entity.getId(), true); } Context.getPermissionsManager().refreshUserPermissions(); if (Context.getNotificationManager() != null) { diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index 3bb367ece..80b9f98e9 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -268,17 +268,19 @@ public class DataManager { .executeUpdate()); } - public static String makeName(String object) { - return object.substring(0, 1).toUpperCase() + object.replace("Id", "").substring(1); + public static Class getClassByName(String name) throws ClassNotFoundException { + return Class.forName("org.traccar.model." + + name.substring(0, 1).toUpperCase() + name.replace("Id", "").substring(1)); } - public static String makeNameId(String object) { - return object.substring(0, 1).toLowerCase() + object.substring(1) + (object.indexOf("Id") == -1 ? "Id" : ""); + public static String makeNameId(Class clazz) { + String name = clazz.getSimpleName(); + return name.substring(0, 1).toLowerCase() + name.substring(1) + (name.indexOf("Id") == -1 ? "Id" : ""); } - public void linkObject(String owner, long ownerId, String property, long propertyId, - boolean link) throws SQLException { - String query = "database." + (!link ? "un" : "") + "link" + makeName(owner) + makeName(property); + public void linkObject(Class owner, long ownerId, Class property, long propertyId, boolean link) + throws SQLException { + String query = "database." + (link ? "link" : "unlink") + owner.getSimpleName() + property.getSimpleName(); QueryBuilder queryBuilder = QueryBuilder.create(dataSource, getQuery(query)); queryBuilder.setLong(makeNameId(owner), ownerId); @@ -291,8 +293,9 @@ public class DataManager { return QueryBuilder.create(dataSource, getQuery(query)).executeQuery(clazz); } - public Collection> getPermissions(String owner, String property) throws SQLException { - String query = "database.select" + makeName(owner) + makeName(property) + "s"; + public Collection> getPermissions(Class owner, + Class property) throws SQLException { + String query = "database.select" + owner.getSimpleName() + property.getSimpleName() + "s"; return QueryBuilder.create(dataSource, getQuery(query)).executeMapQuery(Long.class); } diff --git a/src/org/traccar/database/DriversManager.java b/src/org/traccar/database/DriversManager.java index 8c35f3ddf..0dc2b102d 100644 --- a/src/org/traccar/database/DriversManager.java +++ b/src/org/traccar/database/DriversManager.java @@ -73,7 +73,7 @@ public class DriversManager extends ExtendedObjectManager { @Override public void removeItem(long driverId) throws SQLException { Driver cachedDriver = (Driver) getById(driverId); - getDataManager().removeObject(cachedDriver.getClass(), driverId); + getDataManager().removeObject(Driver.class, driverId); if (cachedDriver != null) { String driverUniqueId = cachedDriver.getUniqueId(); removeCachedItem(driverId); diff --git a/src/org/traccar/database/ExtendedObjectManager.java b/src/org/traccar/database/ExtendedObjectManager.java index fce32fb41..8380b9da2 100644 --- a/src/org/traccar/database/ExtendedObjectManager.java +++ b/src/org/traccar/database/ExtendedObjectManager.java @@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.Device; +import org.traccar.model.Group; import org.traccar.model.BaseModel; public abstract class ExtendedObjectManager extends SimpleObjectManager { @@ -79,23 +80,26 @@ public abstract class ExtendedObjectManager extends SimpleObjectManager { try { Collection> databaseGroupPermissions = - getDataManager().getPermissions("Group", getBaseClassName()); + getDataManager().getPermissions(Group.class, getBaseClass()); clearGroupItems(); for (Map groupPermission : databaseGroupPermissions) { - getGroupItems(groupPermission.get("groupId")).add(groupPermission.get(getBaseClassIdName())); + getGroupItems(groupPermission.get(DataManager.makeNameId(Group.class))) + .add(groupPermission.get(getBaseClassIdName())); } Collection> databaseDevicePermissions = - getDataManager().getPermissions("Device", getBaseClassName()); + getDataManager().getPermissions(Device.class, getBaseClass()); Collection allDevices = Context.getDeviceManager().getAllDevices(); clearDeviceItems(); deviceItemsWithGroups.clear(); for (Map devicePermission : databaseDevicePermissions) { - getDeviceItems(devicePermission.get("deviceId")).add(devicePermission.get(getBaseClassIdName())); - getAllDeviceItems(devicePermission.get("deviceId")).add(devicePermission.get(getBaseClassIdName())); + getDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class))) + .add(devicePermission.get(getBaseClassIdName())); + getAllDeviceItems(devicePermission.get(DataManager.makeNameId(Device.class))) + .add(devicePermission.get(getBaseClassIdName())); } for (Device device : allDevices) { diff --git a/src/org/traccar/database/PermissionsManager.java b/src/org/traccar/database/PermissionsManager.java index 56a12e941..4008134ec 100644 --- a/src/org/traccar/database/PermissionsManager.java +++ b/src/org/traccar/database/PermissionsManager.java @@ -17,8 +17,13 @@ package org.traccar.database; import org.traccar.Context; import org.traccar.helper.Log; +import org.traccar.model.Attribute; +import org.traccar.model.Calendar; import org.traccar.model.Device; +import org.traccar.model.Driver; +import org.traccar.model.Geofence; import org.traccar.model.Group; +import org.traccar.model.ManagedUser; import org.traccar.model.Server; import org.traccar.model.User; @@ -109,8 +114,9 @@ public class PermissionsManager { public final void refreshUserPermissions() { userPermissions.clear(); try { - for (Map permission : dataManager.getPermissions("User", "Permission")) { - getUserPermissions(permission.get("userId")).add(permission.get("managedUserId")); + for (Map permission : dataManager.getPermissions(User.class, User.class)) { + getUserPermissions(permission.get(DataManager.makeNameId(User.class))) + .add(permission.get(DataManager.makeNameId(ManagedUser.class))); } } catch (SQLException error) { Log.warning(error); @@ -123,20 +129,23 @@ public class PermissionsManager { try { GroupTree groupTree = new GroupTree(Context.getDeviceManager().getAllGroups(), Context.getDeviceManager().getAllDevices()); - for (Map groupPermission : dataManager.getPermissions("Group", "Permission")) { - Set userGroupPermissions = getGroupPermissions(groupPermission.get("userId")); - Set userDevicePermissions = getDevicePermissions(groupPermission.get("userId")); - userGroupPermissions.add(groupPermission.get("groupId")); - for (Group group : groupTree.getGroups(groupPermission.get("groupId"))) { + for (Map groupPermission : dataManager.getPermissions(User.class, Group.class)) { + Set userGroupPermissions = getGroupPermissions(groupPermission + .get(DataManager.makeNameId(User.class))); + Set userDevicePermissions = getDevicePermissions(groupPermission + .get(DataManager.makeNameId(User.class))); + userGroupPermissions.add(groupPermission.get(DataManager.makeNameId(Group.class))); + for (Group group : groupTree.getGroups(groupPermission.get(DataManager.makeNameId(Group.class)))) { userGroupPermissions.add(group.getId()); } - for (Device device : groupTree.getDevices(groupPermission.get("groupId"))) { + for (Device device : groupTree.getDevices(groupPermission.get(DataManager.makeNameId(Group.class)))) { userDevicePermissions.add(device.getId()); } } - for (Map devicePermission : dataManager.getPermissions("Device", "Permission")) { - getDevicePermissions(devicePermission.get("userId")).add(devicePermission.get("deviceId")); + for (Map devicePermission : dataManager.getPermissions(User.class, Device.class)) { + getDevicePermissions(devicePermission.get(DataManager.makeNameId(User.class))) + .add(devicePermission.get(DataManager.makeNameId(Device.class))); } groupDevices.clear(); @@ -296,33 +305,26 @@ public class PermissionsManager { } } - public void checkPermission(String object, long userId, long objectId) throws SecurityException { + public void checkPermission(Class object, long userId, long objectId) + throws SecurityException { SimpleObjectManager manager = null; - switch (DataManager.makeName(object)) { - case Context.TYPE_DEVICE: - checkDevice(userId, objectId); - break; - case Context.TYPE_GROUP: - checkGroup(userId, objectId); - break; - case Context.TYPE_USER: - checkUser(userId, objectId); - break; - case Context.TYPE_GEOFENCE: - manager = Context.getGeofenceManager(); - break; - case Context.TYPE_ATTRIBUTE: - manager = Context.getAttributesManager(); - break; - case Context.TYPE_DRIVER: - manager = Context.getDriversManager(); - break; - case Context.TYPE_CALENDAR: - manager = Context.getCalendarManager(); - break; - default: - throw new IllegalArgumentException("Unknown object type"); + if (object.equals(Device.class)) { + checkDevice(userId, objectId); + } else if (object.equals(Group.class)) { + checkGroup(userId, objectId); + } else if (object.equals(User.class) || object.equals(ManagedUser.class)) { + checkUser(userId, objectId); + } else if (object.equals(Geofence.class)) { + manager = Context.getGeofenceManager(); + } else if (object.equals(Attribute.class)) { + manager = Context.getAttributesManager(); + } else if (object.equals(Driver.class)) { + manager = Context.getDriversManager(); + } else if (object.equals(Calendar.class)) { + manager = Context.getCalendarManager(); + } else { + throw new IllegalArgumentException("Unknown object type"); } if (manager != null) { @@ -347,31 +349,33 @@ public class PermissionsManager { } public void refreshPermissions(Map entity) { - if (entity.containsKey("userId")) { - if (entity.containsKey("deviceId") || entity.containsKey("groupId")) { + if (entity.containsKey(DataManager.makeNameId(User.class))) { + if (entity.containsKey(DataManager.makeNameId(Device.class)) + || entity.containsKey(DataManager.makeNameId(Group.class))) { refreshPermissions(); refreshAllExtendedPermissions(); - } else if (entity.containsKey("managedUserId")) { + } else if (entity.containsKey(DataManager.makeNameId(ManagedUser.class))) { refreshUserPermissions(); - } else if (entity.containsKey("geofenceId")) { + } else if (entity.containsKey(DataManager.makeNameId(Geofence.class))) { if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refreshUserItems(); } - } else if (entity.containsKey("driverId")) { + } else if (entity.containsKey(DataManager.makeNameId(Driver.class))) { Context.getDriversManager().refreshUserItems(); - } else if (entity.containsKey("attributeId")) { + } else if (entity.containsKey(DataManager.makeNameId(Attribute.class))) { Context.getAttributesManager().refreshUserItems(); - } else if (entity.containsKey("calendarId")) { + } else if (entity.containsKey(DataManager.makeNameId(Calendar.class))) { Context.getCalendarManager().refreshUserItems(); } - } else if (entity.containsKey("deviceId") || entity.containsKey("groupId")) { - if (entity.containsKey("geofenceId")) { + } else if (entity.containsKey(DataManager.makeNameId(Device.class)) + || entity.containsKey(DataManager.makeNameId(Group.class))) { + if (entity.containsKey(DataManager.makeNameId(Geofence.class))) { if (Context.getGeofenceManager() != null) { Context.getGeofenceManager().refreshExtendedPermissions(); } - } else if (entity.containsKey("driverId")) { + } else if (entity.containsKey(DataManager.makeNameId(Driver.class))) { Context.getDriversManager().refreshExtendedPermissions(); - } else if (entity.containsKey("attributeId")) { + } else if (entity.containsKey(DataManager.makeNameId(Attribute.class))) { Context.getAttributesManager().refreshExtendedPermissions(); } } diff --git a/src/org/traccar/database/SimpleObjectManager.java b/src/org/traccar/database/SimpleObjectManager.java index c4fc0e97a..0db8af658 100644 --- a/src/org/traccar/database/SimpleObjectManager.java +++ b/src/org/traccar/database/SimpleObjectManager.java @@ -27,6 +27,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.traccar.Context; import org.traccar.helper.Log; import org.traccar.model.BaseModel; +import org.traccar.model.User; public abstract class SimpleObjectManager { @@ -36,14 +37,12 @@ public abstract class SimpleObjectManager { private final Map> userItems = new ConcurrentHashMap<>(); private Class baseClass; - private String baseClassName; private String baseClassIdName; protected SimpleObjectManager(DataManager dataManager, Class baseClass) { this.dataManager = dataManager; this.baseClass = baseClass; - baseClassName = baseClass.getSimpleName(); - baseClassIdName = baseClassName.substring(0, 1).toLowerCase() + baseClassName.substring(1) + "Id"; + baseClassIdName = DataManager.makeNameId(baseClass); } protected final DataManager getDataManager() { @@ -54,10 +53,6 @@ public abstract class SimpleObjectManager { return baseClass; } - protected final String getBaseClassName() { - return baseClassName; - } - protected final String getBaseClassIdName() { return baseClassIdName; } @@ -111,8 +106,9 @@ public abstract class SimpleObjectManager { if (dataManager != null) { try { clearUserItems(); - for (Map permission : dataManager.getPermissions(baseClassName, "Permission")) { - getUserItems(permission.get("userId")).add(permission.get(baseClassIdName)); + for (Map permission : dataManager.getPermissions(User.class, baseClass)) { + getUserItems(permission.get(DataManager.makeNameId(User.class))) + .add(permission.get(baseClassIdName)); } } catch (SQLException error) { Log.warning(error); @@ -133,7 +129,7 @@ public abstract class SimpleObjectManager { public void removeItem(long itemId) throws SQLException { BaseModel item = getById(itemId); if (item != null) { - dataManager.removeObject(item.getClass(), itemId); + dataManager.removeObject(baseClass, itemId); removeCachedItem(itemId); } refreshUserItems(); diff --git a/src/org/traccar/model/ManagedUser.java b/src/org/traccar/model/ManagedUser.java new file mode 100644 index 000000000..03c5ef48d --- /dev/null +++ b/src/org/traccar/model/ManagedUser.java @@ -0,0 +1,21 @@ +/* + * Copyright 2017 Anton Tananaev (anton@traccar.org) + * Copyright 2017 Andrey Kunitsyn (andrey@traccar.org) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.traccar.model; + +public class ManagedUser extends User { + +} -- cgit v1.2.3