diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2017-07-28 16:15:08 +1200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-07-28 16:15:08 +1200 |
commit | d3021c9ae11b177fe05b79e98f1e560e8c3cbaf5 (patch) | |
tree | beb996f62fe8580360b823367aa04692d02dc3be | |
parent | 5d6dec7818a8059c958fc896eb1de838fda52a8c (diff) | |
parent | 34d02fbb63eb3d0cfbbcf63de50561831b0ab38d (diff) | |
download | trackermap-server-d3021c9ae11b177fe05b79e98f1e560e8c3cbaf5.tar.gz trackermap-server-d3021c9ae11b177fe05b79e98f1e560e8c3cbaf5.tar.bz2 trackermap-server-d3021c9ae11b177fe05b79e98f1e560e8c3cbaf5.zip |
Merge pull request #3396 from Abyss777/combine_resources
Combine PUT/POST/DELETE functions in object resources
-rw-r--r-- | src/org/traccar/Context.java | 28 | ||||
-rw-r--r-- | src/org/traccar/api/BaseObjectResource.java | 123 | ||||
-rw-r--r-- | src/org/traccar/api/resource/AttributeResource.java | 53 | ||||
-rw-r--r-- | src/org/traccar/api/resource/CalendarResource.java | 40 | ||||
-rw-r--r-- | src/org/traccar/api/resource/DeviceResource.java | 49 | ||||
-rw-r--r-- | src/org/traccar/api/resource/DriverResource.java | 41 | ||||
-rw-r--r-- | src/org/traccar/api/resource/GeofenceResource.java | 43 | ||||
-rw-r--r-- | src/org/traccar/api/resource/GroupResource.java | 47 | ||||
-rw-r--r-- | src/org/traccar/api/resource/UserResource.java | 37 |
9 files changed, 198 insertions, 263 deletions
diff --git a/src/org/traccar/Context.java b/src/org/traccar/Context.java index dfb8c5f47..306a37e83 100644 --- a/src/org/traccar/Context.java +++ b/src/org/traccar/Context.java @@ -28,6 +28,7 @@ import org.eclipse.jetty.util.URIUtil; import org.traccar.database.AliasesManager; import org.traccar.database.CalendarManager; import org.traccar.database.AttributesManager; +import org.traccar.database.BaseObjectManager; import org.traccar.database.ConnectionManager; import org.traccar.database.DataManager; import org.traccar.database.DeviceManager; @@ -51,6 +52,14 @@ import org.traccar.geocoder.OpenCageGeocoder; import org.traccar.geocoder.Geocoder; import org.traccar.geolocation.UnwiredGeolocationProvider; import org.traccar.helper.Log; +import org.traccar.model.Attribute; +import org.traccar.model.BaseModel; +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.User; import org.traccar.geolocation.GoogleGeolocationProvider; import org.traccar.geolocation.GeolocationProvider; import org.traccar.geolocation.MozillaGeolocationProvider; @@ -369,4 +378,23 @@ public final class Context { identityManager = testIdentityManager; } + public static <T extends BaseModel> BaseObjectManager<T> getManager(Class<T> clazz) { + if (clazz.equals(Device.class)) { + return (BaseObjectManager<T>) deviceManager; + } else if (clazz.equals(Group.class)) { + return (BaseObjectManager<T>) groupsManager; + } else if (clazz.equals(User.class)) { + return (BaseObjectManager<T>) usersManager; + } else if (clazz.equals(Calendar.class)) { + return (BaseObjectManager<T>) calendarManager; + } else if (clazz.equals(Attribute.class)) { + return (BaseObjectManager<T>) attributesManager; + } else if (clazz.equals(Geofence.class)) { + return (BaseObjectManager<T>) geofenceManager; + } else if (clazz.equals(Driver.class)) { + return (BaseObjectManager<T>) driversManager; + } + return null; + } + } diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java new file mode 100644 index 000000000..4475e3b4d --- /dev/null +++ b/src/org/traccar/api/BaseObjectResource.java @@ -0,0 +1,123 @@ +/* + * 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; + +import java.sql.SQLException; + +import javax.ws.rs.DELETE; +import javax.ws.rs.POST; +import javax.ws.rs.PUT; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.Response; + +import org.traccar.Context; +import org.traccar.database.BaseObjectManager; +import org.traccar.database.ExtendedObjectManager; +import org.traccar.database.SimpleObjectManager; +import org.traccar.model.BaseModel; +import org.traccar.model.Device; +import org.traccar.model.Group; +import org.traccar.model.User; + +public abstract class BaseObjectResource<T extends BaseModel> extends BaseResource { + + private Class<T> baseClass; + + public BaseObjectResource(Class<T> baseClass) { + this.baseClass = baseClass; + } + + @POST + public Response add(T entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + if (baseClass.equals(Device.class)) { + Context.getPermissionsManager().checkDeviceReadonly(getUserId()); + Context.getPermissionsManager().checkDeviceLimit(getUserId()); + } + + BaseObjectManager<T> manager = Context.getManager(baseClass); + manager.addItem(entity); + + Context.getDataManager().linkObject(User.class, getUserId(), baseClass, entity.getId(), true); + + if (manager instanceof SimpleObjectManager) { + ((SimpleObjectManager<T>) manager).refreshUserItems(); + } else if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { + Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); + Context.getPermissionsManager().refreshAllExtendedPermissions(); + } + return Response.ok(entity).build(); + } + + @Path("{id}") + @PUT + public Response update(T entity) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + if (baseClass.equals(Device.class)) { + Context.getPermissionsManager().checkDeviceReadonly(getUserId()); + } else if (baseClass.equals(User.class)) { + User before = Context.getPermissionsManager().getUser(entity.getId()); + Context.getPermissionsManager().checkUserUpdate(getUserId(), before, (User) entity); + } + Context.getPermissionsManager().checkPermission(baseClass, getUserId(), entity.getId()); + + Context.getManager(baseClass).updateItem(entity); + + if (baseClass.equals(Group.class) || baseClass.equals(Device.class)) { + Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); + Context.getPermissionsManager().refreshAllExtendedPermissions(); + } else if (baseClass.equals(User.class) && Context.getNotificationManager() != null) { + Context.getNotificationManager().refresh(); + } + return Response.ok(entity).build(); + } + + @Path("{id}") + @DELETE + public Response remove(@PathParam("id") long id) throws SQLException { + Context.getPermissionsManager().checkReadonly(getUserId()); + if (baseClass.equals(Device.class)) { + Context.getPermissionsManager().checkDeviceReadonly(getUserId()); + } + Context.getPermissionsManager().checkPermission(baseClass, getUserId(), id); + + BaseObjectManager<T> manager = Context.getManager(baseClass); + manager.removeItem(id); + + if (manager instanceof SimpleObjectManager) { + ((SimpleObjectManager<T>) manager).refreshUserItems(); + if (manager instanceof ExtendedObjectManager) { + ((ExtendedObjectManager<T>) manager).refreshExtendedPermissions(); + } + } + if (baseClass.equals(Group.class) || baseClass.equals(Device.class) || baseClass.equals(User.class)) { + Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); + if (baseClass.equals(User.class)) { + Context.getPermissionsManager().refreshAllUsersPermissions(); + } else { + Context.getPermissionsManager().refreshAllExtendedPermissions(); + } + } + // Next should be removed with Attribute Aliases + if (baseClass.equals(Device.class)) { + Context.getAliasesManager().removeDevice(id); + } + return Response.noContent().build(); + } + +} diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java index c12fcd9e6..55fd39fc6 100644 --- a/src/org/traccar/api/resource/AttributeResource.java +++ b/src/org/traccar/api/resource/AttributeResource.java @@ -22,29 +22,29 @@ import java.util.HashSet; import java.util.Set; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.AttributesManager; import org.traccar.model.Attribute; import org.traccar.model.Position; -import org.traccar.model.User; import org.traccar.processing.ComputedAttributesHandler; @Path("attributes/computed") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class AttributeResource extends BaseResource { +public class AttributeResource extends BaseObjectResource<Attribute> { + + public AttributeResource() { + super(Attribute.class); + } @GET public Collection<Attribute> get( @@ -85,15 +85,11 @@ public class AttributeResource extends BaseResource { } - private Response add(Attribute entity) throws SQLException { + @POST + @Path("test") + public Response test(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException { Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getAttributesManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getAttributesManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - private Response test(long deviceId, Attribute entity) { + Context.getPermissionsManager().checkDevice(getUserId(), deviceId); Position last = Context.getIdentityManager().getLastPosition(deviceId); if (last != null) { Object result = new ComputedAttributesHandler().computeAttribute(entity, last); @@ -114,33 +110,4 @@ public class AttributeResource extends BaseResource { } } - @POST - public Response post(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - if (deviceId != 0) { - Context.getPermissionsManager().checkDevice(getUserId(), deviceId); - return test(deviceId, entity); - } else { - return add(entity); - } - } - - @Path("{id}") - @PUT - public Response update(Attribute entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Attribute.class, getUserId(), entity.getId()); - Context.getAttributesManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - 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 f8d78a847..d43a3b02e 100644 --- a/src/org/traccar/api/resource/CalendarResource.java +++ b/src/org/traccar/api/resource/CalendarResource.java @@ -21,27 +21,25 @@ import java.util.Collection; import java.util.Set; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.CalendarManager; import org.traccar.model.Calendar; -import org.traccar.model.User; @Path("calendars") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class CalendarResource extends BaseResource { +public class CalendarResource extends BaseObjectResource<Calendar> { + + public CalendarResource() { + super(Calendar.class); + } @GET public Collection<Calendar> get( @@ -66,30 +64,4 @@ public class CalendarResource extends BaseResource { return calendarManager.getItems(result); } - @POST - public Response add(Calendar entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getCalendarManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getCalendarManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Calendar entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Calendar.class, getUserId(), entity.getId()); - Context.getCalendarManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - 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 a473b7bde..1c2c653a4 100644 --- a/src/org/traccar/api/resource/DeviceResource.java +++ b/src/org/traccar/api/resource/DeviceResource.java @@ -16,19 +16,15 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.DeviceManager; import org.traccar.model.Device; import org.traccar.model.DeviceTotalDistance; -import org.traccar.model.User; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @@ -43,7 +39,11 @@ import java.util.Set; @Path("devices") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class DeviceResource extends BaseResource { +public class DeviceResource extends BaseObjectResource<Device> { + + public DeviceResource() { + super(Device.class); + } @GET public Collection<Device> get( @@ -80,43 +80,6 @@ public class DeviceResource extends BaseResource { return deviceManager.getItems(result); } - @POST - public Response add(Device entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceLimit(getUserId()); - Context.getDeviceManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Device entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), entity.getId()); - Context.getDeviceManager().updateItem(entity); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkDeviceReadonly(getUserId()); - Context.getPermissionsManager().checkDevice(getUserId(), id); - Context.getDeviceManager().removeItem(id); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - Context.getAliasesManager().removeDevice(id); - return Response.noContent().build(); - } - @Path("{id}/distance") @PUT public Response updateTotalDistance(DeviceTotalDistance entity) throws SQLException { diff --git a/src/org/traccar/api/resource/DriverResource.java b/src/org/traccar/api/resource/DriverResource.java index d44979998..185f4a0dc 100644 --- a/src/org/traccar/api/resource/DriverResource.java +++ b/src/org/traccar/api/resource/DriverResource.java @@ -22,27 +22,25 @@ import java.util.HashSet; import java.util.Set; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.DriversManager; import org.traccar.model.Driver; -import org.traccar.model.User; @Path("drivers") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class DriverResource extends BaseResource { +public class DriverResource extends BaseObjectResource<Driver> { + + public DriverResource() { + super(Driver.class); + } @GET public Collection<Driver> get( @@ -83,31 +81,4 @@ public class DriverResource extends BaseResource { } - @POST - public Response add(Driver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getDriversManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getDriversManager().refreshUserItems(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Driver entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), entity.getId()); - Context.getDriversManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Driver.class, getUserId(), id); - Context.getDriversManager().removeItem(id); - return Response.noContent().build(); - } - } diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java index 9a110fbcf..d07810732 100644 --- a/src/org/traccar/api/resource/GeofenceResource.java +++ b/src/org/traccar/api/resource/GeofenceResource.java @@ -16,22 +16,16 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.GeofenceManager; import org.traccar.model.Geofence; -import org.traccar.model.User; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; @@ -41,7 +35,11 @@ import java.util.Set; @Path("geofences") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class GeofenceResource extends BaseResource { +public class GeofenceResource extends BaseObjectResource<Geofence> { + + public GeofenceResource() { + super(Geofence.class); + } @GET public Collection<Geofence> get( @@ -79,34 +77,5 @@ public class GeofenceResource extends BaseResource { result.retainAll(geofenceManager.getDeviceItems(deviceId)); } return geofenceManager.getItems(result); - - } - - @POST - public Response add(Geofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getGeofenceManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getGeofenceManager().refreshUserItems(); - return Response.ok(entity).build(); } - - @Path("{id}") - @PUT - public Response update(Geofence entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), entity.getId()); - Context.getGeofenceManager().updateItem(entity); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - 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 4482f06c0..2be4e6492 100644 --- a/src/org/traccar/api/resource/GroupResource.java +++ b/src/org/traccar/api/resource/GroupResource.java @@ -16,22 +16,16 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.GroupsManager; import org.traccar.model.Group; -import org.traccar.model.User; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; -import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; import java.sql.SQLException; import java.util.Collection; import java.util.Set; @@ -39,7 +33,11 @@ import java.util.Set; @Path("groups") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class GroupResource extends BaseResource { +public class GroupResource extends BaseObjectResource<Group> { + + public GroupResource() { + super(Group.class); + } @GET public Collection<Group> get( @@ -62,37 +60,4 @@ public class GroupResource extends BaseResource { } return groupsManager.getItems(result); } - - @POST - public Response add(Group entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getGroupsManager().addItem(entity); - Context.getDataManager().linkObject(User.class, getUserId(), entity.getClass(), entity.getId(), true); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @PUT - public Response update(Group entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), entity.getId()); - Context.getGroupsManager().updateItem(entity); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkGroup(getUserId(), id); - Context.getGroupsManager().removeItem(id); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllExtendedPermissions(); - return Response.noContent().build(); - } - } diff --git a/src/org/traccar/api/resource/UserResource.java b/src/org/traccar/api/resource/UserResource.java index deb2dd2b4..b22e01216 100644 --- a/src/org/traccar/api/resource/UserResource.java +++ b/src/org/traccar/api/resource/UserResource.java @@ -16,19 +16,16 @@ package org.traccar.api.resource; import org.traccar.Context; -import org.traccar.api.BaseResource; +import org.traccar.api.BaseObjectResource; import org.traccar.database.UsersManager; import org.traccar.model.ManagedUser; import org.traccar.model.User; import javax.annotation.security.PermitAll; import javax.ws.rs.Consumes; -import javax.ws.rs.DELETE; import javax.ws.rs.GET; import javax.ws.rs.POST; -import javax.ws.rs.PUT; import javax.ws.rs.Path; -import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; @@ -41,7 +38,11 @@ import java.util.Set; @Path("users") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.APPLICATION_JSON) -public class UserResource extends BaseResource { +public class UserResource extends BaseObjectResource<User> { + + public UserResource() { + super(User.class); + } @GET public Collection<User> get(@QueryParam("userId") long userId) throws SQLException { @@ -61,6 +62,7 @@ public class UserResource extends BaseResource { return usersManager.getItems(result); } + @Override @PermitAll @POST public Response add(User entity) throws SQLException { @@ -89,29 +91,4 @@ public class UserResource extends BaseResource { return Response.ok(entity).build(); } - @Path("{id}") - @PUT - public Response update(User entity) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - User before = Context.getPermissionsManager().getUser(entity.getId()); - Context.getPermissionsManager().checkUser(getUserId(), entity.getId()); - Context.getPermissionsManager().checkUserUpdate(getUserId(), before, entity); - Context.getUsersManager().updateItem(entity); - if (Context.getNotificationManager() != null) { - Context.getNotificationManager().refresh(); - } - return Response.ok(entity).build(); - } - - @Path("{id}") - @DELETE - public Response remove(@PathParam("id") long id) throws SQLException { - Context.getPermissionsManager().checkReadonly(getUserId()); - Context.getPermissionsManager().checkUser(getUserId(), id); - Context.getUsersManager().removeItem(id); - Context.getPermissionsManager().refreshDeviceAndGroupPermissions(); - Context.getPermissionsManager().refreshAllUsersPermissions(); - return Response.noContent().build(); - } - } |