diff options
Diffstat (limited to 'src/org/traccar/api')
20 files changed, 110 insertions, 741 deletions
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<String, Long> 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<String, Long> 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<String, Long> 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<String, Long> 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<String, Long> 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<String, Long> 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<String, Long> 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/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<String, Long> 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<String, Long> 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<String, Long> 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/GroupAttributeResource.java b/src/org/traccar/api/resource/PermissionsResource.java index 5df63194e..ac7acb93f 100644 --- a/src/org/traccar/api/resource/GroupAttributeResource.java +++ b/src/org/traccar/api/resource/PermissionsResource.java @@ -17,6 +17,7 @@ package org.traccar.api.resource; import java.sql.SQLException; +import java.util.Map; import javax.ws.rs.Consumes; import javax.ws.rs.DELETE; @@ -28,30 +29,33 @@ import javax.ws.rs.core.Response; import org.traccar.Context; import org.traccar.api.BaseResource; -import org.traccar.model.GroupAttribute; -@Path("groups/attributes") +@Path("permissions") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class GroupAttributeResource extends BaseResource { +public class PermissionsResource extends BaseResource { + @Path("/{slave : (users|devices|groups|geofences|drivers|attributes|calendars)}") @POST - public Response add(GroupAttribute entity) throws SQLException { + public Response add(Map<String, Long> 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(); + 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(GroupAttribute entity) throws SQLException { + public Response remove(Map<String, Long> 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(); + 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<String, Long> link = new LinkedHashMap<>(); + link.put("userId", getUserId()); + link.put("managedUserId", entity.getId()); + Context.getDataManager().linkObject(link, true); } Context.getPermissionsManager().refreshUserPermissions(); if (Context.getNotificationManager() != null) { |