aboutsummaryrefslogtreecommitdiff
path: root/src/org/traccar/api
diff options
context:
space:
mode:
Diffstat (limited to 'src/org/traccar/api')
-rw-r--r--src/org/traccar/api/BaseObjectResource.java149
-rw-r--r--src/org/traccar/api/BaseResource.java2
-rw-r--r--src/org/traccar/api/ExtendedObjectResource.java62
-rw-r--r--src/org/traccar/api/SimpleObjectResource.java43
-rw-r--r--src/org/traccar/api/resource/AttributeAliasResource.java93
-rw-r--r--src/org/traccar/api/resource/AttributePermissionResource.java58
-rw-r--r--src/org/traccar/api/resource/AttributeResource.java93
-rw-r--r--src/org/traccar/api/resource/CalendarPermissionResource.java57
-rw-r--r--src/org/traccar/api/resource/CalendarResource.java61
-rw-r--r--src/org/traccar/api/resource/CommandResource.java57
-rw-r--r--src/org/traccar/api/resource/DeviceAttributeResource.java58
-rw-r--r--src/org/traccar/api/resource/DeviceGeofenceResource.java57
-rw-r--r--src/org/traccar/api/resource/DevicePermissionResource.java66
-rw-r--r--src/org/traccar/api/resource/DeviceResource.java96
-rw-r--r--src/org/traccar/api/resource/DriverResource.java (renamed from src/org/traccar/api/resource/CommandTypeResource.java)23
-rw-r--r--src/org/traccar/api/resource/EventResource.java5
-rw-r--r--src/org/traccar/api/resource/GeofencePermissionResource.java56
-rw-r--r--src/org/traccar/api/resource/GeofenceResource.java85
-rw-r--r--src/org/traccar/api/resource/GroupAttributeResource.java58
-rw-r--r--src/org/traccar/api/resource/GroupGeofenceResource.java56
-rw-r--r--src/org/traccar/api/resource/GroupPermissionResource.java62
-rw-r--r--src/org/traccar/api/resource/GroupResource.java70
-rw-r--r--src/org/traccar/api/resource/NotificationResource.java34
-rw-r--r--src/org/traccar/api/resource/PermissionsResource.java79
-rw-r--r--src/org/traccar/api/resource/PositionResource.java4
-rw-r--r--src/org/traccar/api/resource/SessionResource.java2
-rw-r--r--src/org/traccar/api/resource/UserPermissionResource.java56
-rw-r--r--src/org/traccar/api/resource/UserResource.java72
28 files changed, 487 insertions, 1127 deletions
diff --git a/src/org/traccar/api/BaseObjectResource.java b/src/org/traccar/api/BaseObjectResource.java
new file mode 100644
index 000000000..634957a49
--- /dev/null
+++ b/src/org/traccar/api/BaseObjectResource.java
@@ -0,0 +1,149 @@
+/*
+ * 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 java.util.Set;
+
+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.ManagableObjects;
+import org.traccar.database.SimpleObjectManager;
+import org.traccar.model.BaseModel;
+import org.traccar.model.Command;
+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;
+ }
+
+ protected final Class<T> getBaseClass() {
+ return baseClass;
+ }
+
+ protected final Set<Long> getSimpleManagerItems(BaseObjectManager<T> manager, boolean all, long userId) {
+ Set<Long> result = null;
+ if (all) {
+ if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
+ result = manager.getAllItems();
+ } else {
+ Context.getPermissionsManager().checkManager(getUserId());
+ result = ((ManagableObjects) manager).getManagedItems(getUserId());
+ }
+ } else {
+ if (userId == 0) {
+ userId = getUserId();
+ }
+ Context.getPermissionsManager().checkUser(getUserId(), userId);
+ result = ((ManagableObjects) manager).getUserItems(userId);
+ }
+ return result;
+ }
+
+ @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());
+ } else if (baseClass.equals(Command.class)) {
+ Context.getPermissionsManager().checkLimitCommands(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);
+ } else if (baseClass.equals(Command.class)) {
+ Context.getPermissionsManager().checkLimitCommands(getUserId());
+ }
+ 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();
+ }
+ 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());
+ } else if (baseClass.equals(Command.class)) {
+ Context.getPermissionsManager().checkLimitCommands(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();
+ }
+ }
+ return Response.noContent().build();
+ }
+
+}
diff --git a/src/org/traccar/api/BaseResource.java b/src/org/traccar/api/BaseResource.java
index 44ef33c53..cc272df9c 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.
diff --git a/src/org/traccar/api/ExtendedObjectResource.java b/src/org/traccar/api/ExtendedObjectResource.java
new file mode 100644
index 000000000..007a7b1bd
--- /dev/null
+++ b/src/org/traccar/api/ExtendedObjectResource.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;
+
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.QueryParam;
+
+import org.traccar.Context;
+import org.traccar.database.ExtendedObjectManager;
+import org.traccar.model.BaseModel;
+
+public class ExtendedObjectResource<T extends BaseModel> extends BaseObjectResource<T> {
+
+ public ExtendedObjectResource(Class<T> baseClass) {
+ super(baseClass);
+ }
+
+ @GET
+ public Collection<T> get(
+ @QueryParam("all") boolean all, @QueryParam("userId") long userId, @QueryParam("groupId") long groupId,
+ @QueryParam("deviceId") long deviceId, @QueryParam("refresh") boolean refresh) throws SQLException {
+
+ ExtendedObjectManager<T> manager = (ExtendedObjectManager<T>) Context.getManager(getBaseClass());
+ if (refresh) {
+ manager.refreshItems();
+ }
+
+ Set<Long> result = new HashSet<>(getSimpleManagerItems(manager, all, userId));
+
+ if (groupId != 0) {
+ Context.getPermissionsManager().checkGroup(getUserId(), groupId);
+ result.retainAll(manager.getGroupItems(groupId));
+ }
+
+ if (deviceId != 0) {
+ Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
+ result.retainAll(manager.getDeviceItems(deviceId));
+ }
+ return manager.getItems(result);
+
+ }
+
+}
diff --git a/src/org/traccar/api/SimpleObjectResource.java b/src/org/traccar/api/SimpleObjectResource.java
new file mode 100644
index 000000000..a7fcae0e7
--- /dev/null
+++ b/src/org/traccar/api/SimpleObjectResource.java
@@ -0,0 +1,43 @@
+/*
+ * 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 java.util.Collection;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.QueryParam;
+
+import org.traccar.Context;
+import org.traccar.database.BaseObjectManager;
+import org.traccar.model.BaseModel;
+
+public class SimpleObjectResource<T extends BaseModel> extends BaseObjectResource<T> {
+
+ public SimpleObjectResource(Class<T> baseClass) {
+ super(baseClass);
+ }
+
+ @GET
+ public Collection<T> get(
+ @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException {
+
+ BaseObjectManager<T> manager = Context.getManager(getBaseClass());
+ return manager.getItems(getSimpleManagerItems(manager, all, userId));
+ }
+
+}
diff --git a/src/org/traccar/api/resource/AttributeAliasResource.java b/src/org/traccar/api/resource/AttributeAliasResource.java
deleted file mode 100644
index b2636acf1..000000000
--- a/src/org/traccar/api/resource/AttributeAliasResource.java
+++ /dev/null
@@ -1,93 +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 java.util.Collection;
-
-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.model.AttributeAlias;
-
-@Path("attributes/aliases")
-@Produces(MediaType.APPLICATION_JSON)
-@Consumes(MediaType.APPLICATION_JSON)
-public class AttributeAliasResource extends BaseResource {
-
- @GET
- public Collection<AttributeAlias> get(@QueryParam("deviceId") long deviceId) throws SQLException {
- if (deviceId != 0) {
- if (!Context.getPermissionsManager().isAdmin(getUserId())) {
- Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
- }
- return Context.getAliasesManager().getAttributeAliases(deviceId);
- } else {
- return Context.getAliasesManager().getAllAttributeAliases(getUserId());
- }
- }
-
- @POST
- public Response add(AttributeAlias entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkDeviceReadonly(getUserId());
- if (!Context.getPermissionsManager().isAdmin(getUserId())) {
- Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId());
- }
- Context.getAliasesManager().addAttributeAlias(entity);
- return Response.ok(entity).build();
- }
-
- @Path("{id}")
- @PUT
- public Response update(AttributeAlias entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkDeviceReadonly(getUserId());
- if (!Context.getPermissionsManager().isAdmin(getUserId())) {
- AttributeAlias oldEntity = Context.getAliasesManager().getAttributeAlias(entity.getId());
- Context.getPermissionsManager().checkDevice(getUserId(), oldEntity.getDeviceId());
- Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId());
- }
- Context.getAliasesManager().updateAttributeAlias(entity);
- 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());
- if (!Context.getPermissionsManager().isAdmin(getUserId())) {
- AttributeAlias entity = Context.getAliasesManager().getAttributeAlias(id);
- Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId());
- }
- Context.getAliasesManager().removeArrtibuteAlias(id);
- return Response.noContent().build();
- }
-
-}
diff --git a/src/org/traccar/api/resource/AttributePermissionResource.java b/src/org/traccar/api/resource/AttributePermissionResource.java
deleted file mode 100644
index 1924bcdf1..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().checkAttribute(getUserId(), entity.getAttributeId());
- Context.getDataManager().linkAttribute(entity.getUserId(), entity.getAttributeId());
- Context.getAttributesManager().refreshUserAttributes();
- 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().checkAttribute(getUserId(), entity.getAttributeId());
- Context.getDataManager().unlinkAttribute(entity.getUserId(), entity.getAttributeId());
- Context.getAttributesManager().refreshUserAttributes();
- return Response.noContent().build();
- }
-
-}
diff --git a/src/org/traccar/api/resource/AttributeResource.java b/src/org/traccar/api/resource/AttributeResource.java
index 4d326779b..26a1f6931 100644
--- a/src/org/traccar/api/resource/AttributeResource.java
+++ b/src/org/traccar/api/resource/AttributeResource.java
@@ -17,25 +17,17 @@
package org.traccar.api.resource;
import java.sql.SQLException;
-import java.util.Collection;
-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.database.AttributesManager;
+import org.traccar.api.ExtendedObjectResource;
import org.traccar.model.Attribute;
import org.traccar.model.Position;
import org.traccar.processing.ComputedAttributesHandler;
@@ -43,55 +35,17 @@ 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 ExtendedObjectResource<Attribute> {
- @GET
- public Collection<Attribute> get(
- @QueryParam("all") boolean all, @QueryParam("userId") long userId, @QueryParam("groupId") long groupId,
- @QueryParam("deviceId") long deviceId, @QueryParam("refresh") boolean refresh) throws SQLException {
-
- AttributesManager attributesManager = Context.getAttributesManager();
- if (refresh) {
- attributesManager.refreshAttributes();
- }
-
- Set<Long> result = new HashSet<>();
- if (all) {
- if (Context.getPermissionsManager().isAdmin(getUserId())) {
- result.addAll(attributesManager.getAllAttributes());
- } else {
- Context.getPermissionsManager().checkManager(getUserId());
- result.addAll(attributesManager.getManagedAttributes(getUserId()));
- }
- } else {
- if (userId == 0) {
- userId = getUserId();
- }
- Context.getPermissionsManager().checkUser(getUserId(), userId);
- result.addAll(attributesManager.getUserAttributes(userId));
- }
-
- if (groupId != 0) {
- Context.getPermissionsManager().checkGroup(getUserId(), groupId);
- result.retainAll(attributesManager.getGroupAttributes(groupId));
- }
-
- if (deviceId != 0) {
- Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
- result.retainAll(attributesManager.getDeviceAttributes(deviceId));
- }
- return attributesManager.getAttributes(result);
-
- }
-
- private Response add(Attribute entity) throws SQLException {
- Context.getAttributesManager().addAttribute(entity);
- Context.getDataManager().linkAttribute(getUserId(), entity.getId());
- Context.getAttributesManager().refreshUserAttributes();
- return Response.ok(entity).build();
+ public AttributeResource() {
+ super(Attribute.class);
}
- private Response test(long deviceId, Attribute entity) {
+ @POST
+ @Path("test")
+ public Response test(@QueryParam("deviceId") long deviceId, Attribute entity) throws SQLException {
+ Context.getPermissionsManager().checkReadonly(getUserId());
+ Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
Position last = Context.getIdentityManager().getLastPosition(deviceId);
if (last != null) {
Object result = new ComputedAttributesHandler().computeAttribute(entity, last);
@@ -112,33 +66,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().checkAttribute(getUserId(), entity.getId());
- Context.getAttributesManager().updateAttribute(entity);
- return Response.ok(entity).build();
- }
-
- @Path("{id}")
- @DELETE
- public Response remove(@PathParam("id") long id) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkAttribute(getUserId(), id);
- Context.getAttributesManager().removeAttribute(id);
- return Response.noContent().build();
- }
-
}
diff --git a/src/org/traccar/api/resource/CalendarPermissionResource.java b/src/org/traccar/api/resource/CalendarPermissionResource.java
deleted file mode 100644
index a49254b6b..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().checkCalendar(getUserId(), entity.getCalendarId());
- Context.getDataManager().linkCalendar(entity.getUserId(), entity.getCalendarId());
- Context.getCalendarManager().refreshUserCalendars();
- 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().checkCalendar(getUserId(), entity.getCalendarId());
- Context.getDataManager().unlinkCalendar(entity.getUserId(), entity.getCalendarId());
- Context.getCalendarManager().refreshUserCalendars();
- return Response.noContent().build();
- }
-}
diff --git a/src/org/traccar/api/resource/CalendarResource.java b/src/org/traccar/api/resource/CalendarResource.java
index 641d3b4b5..9399c34a5 100644
--- a/src/org/traccar/api/resource/CalendarResource.java
+++ b/src/org/traccar/api/resource/CalendarResource.java
@@ -16,74 +16,21 @@
*/
package org.traccar.api.resource;
-import java.sql.SQLException;
-import java.util.Collection;
-
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.SimpleObjectResource;
import org.traccar.model.Calendar;
@Path("calendars")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
-public class CalendarResource extends BaseResource {
-
- @GET
- public Collection<Calendar> get(
- @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException {
-
- if (all) {
- if (Context.getPermissionsManager().isAdmin(getUserId())) {
- return Context.getCalendarManager().getAllCalendars();
- } else {
- Context.getPermissionsManager().checkManager(getUserId());
- return Context.getCalendarManager().getManagedCalendars(getUserId());
- }
- } else {
- if (userId == 0) {
- userId = getUserId();
- }
- Context.getPermissionsManager().checkUser(getUserId(), userId);
- return Context.getCalendarManager().getUserCalendars(userId);
- }
- }
+public class CalendarResource extends SimpleObjectResource<Calendar> {
- @POST
- public Response add(Calendar entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getCalendarManager().addCalendar(entity);
- Context.getDataManager().linkCalendar(getUserId(), entity.getId());
- Context.getCalendarManager().refreshUserCalendars();
- return Response.ok(entity).build();
+ public CalendarResource() {
+ super(Calendar.class);
}
- @Path("{id}")
- @PUT
- public Response update(Calendar entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkCalendar(getUserId(), entity.getId());
- Context.getCalendarManager().updateCalendar(entity);
- return Response.ok(entity).build();
- }
-
- @Path("{id}")
- @DELETE
- public Response remove(@PathParam("id") long id) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkCalendar(getUserId(), id);
- Context.getCalendarManager().removeCalendar(id);
- return Response.noContent().build();
- }
}
diff --git a/src/org/traccar/api/resource/CommandResource.java b/src/org/traccar/api/resource/CommandResource.java
index 9ed92d3d5..703638701 100644
--- a/src/org/traccar/api/resource/CommandResource.java
+++ b/src/org/traccar/api/resource/CommandResource.java
@@ -1,5 +1,7 @@
/*
* Copyright 2015 - 2017 Anton Tananaev (anton@traccar.org)
+ * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com)
+ * 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.
@@ -16,27 +18,72 @@
package org.traccar.api.resource;
import org.traccar.Context;
-import org.traccar.api.BaseResource;
+import org.traccar.api.ExtendedObjectResource;
+import org.traccar.database.CommandsManager;
import org.traccar.model.Command;
+import org.traccar.model.Typed;
+
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
import javax.ws.rs.Consumes;
+import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
+import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
@Path("commands")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
-public class CommandResource extends BaseResource {
+public class CommandResource extends ExtendedObjectResource<Command> {
+
+ public CommandResource() {
+ super(Command.class);
+ }
+
+ @GET
+ @Path("send")
+ public Collection<Command> get(@QueryParam("deviceId") long deviceId) throws SQLException {
+ Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
+ CommandsManager commandsManager = Context.getCommandsManager();
+ Set<Long> result = new HashSet<>(commandsManager.getUserItems(getUserId()));
+ result.retainAll(commandsManager.getSupportedCommands(deviceId));
+ return commandsManager.getItems(result);
+ }
@POST
- public Response add(Command entity) throws Exception {
+ @Path("send")
+ public Response send(Command entity) throws Exception {
Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkDevice(getUserId(), entity.getDeviceId());
- Context.getDeviceManager().sendCommand(entity);
+ long deviceId = entity.getDeviceId();
+ long id = entity.getId();
+ Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
+ if (id != 0) {
+ Context.getPermissionsManager().checkPermission(Command.class, getUserId(), id);
+ Context.getPermissionsManager().checkUserDeviceCommand(getUserId(), deviceId, id);
+ } else {
+ Context.getPermissionsManager().checkLimitCommands(getUserId());
+ }
+ if (!Context.getCommandsManager().sendCommand(entity)) {
+ return Response.accepted(entity).build();
+ }
return Response.ok(entity).build();
}
+ @GET
+ @Path("types")
+ public Collection<Typed> get(@QueryParam("deviceId") long deviceId,
+ @QueryParam("textChannel") boolean textChannel) {
+ if (deviceId != 0) {
+ Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
+ return Context.getCommandsManager().getCommandTypes(deviceId, textChannel);
+ } else {
+ return Context.getCommandsManager().getAllCommandTypes();
+ }
+ }
}
diff --git a/src/org/traccar/api/resource/DeviceAttributeResource.java b/src/org/traccar/api/resource/DeviceAttributeResource.java
deleted file mode 100644
index 8d80c9235..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().checkAttribute(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().checkGeofence(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/DeviceGeofenceResource.java b/src/org/traccar/api/resource/DeviceGeofenceResource.java
deleted file mode 100644
index 6254fe3cf..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().checkGeofence(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().checkGeofence(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..1fae92dc7 100644
--- a/src/org/traccar/api/resource/DeviceResource.java
+++ b/src/org/traccar/api/resource/DeviceResource.java
@@ -16,102 +16,68 @@
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 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.ArrayList;
import java.util.Collection;
+import java.util.HashSet;
import java.util.List;
+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(
@QueryParam("all") boolean all, @QueryParam("userId") long userId,
+ @QueryParam("uniqueId") List<String> uniqueIds,
@QueryParam("id") List<Long> deviceIds) throws SQLException {
- if (deviceIds.isEmpty()) {
- if (all) {
- if (Context.getPermissionsManager().isAdmin(getUserId())) {
- return Context.getDeviceManager().getAllDevices();
- } else {
- Context.getPermissionsManager().checkManager(getUserId());
- return Context.getDeviceManager().getManagedDevices(getUserId());
- }
+ DeviceManager deviceManager = Context.getDeviceManager();
+ Set<Long> result = null;
+ if (all) {
+ if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
+ result = deviceManager.getAllItems();
} else {
- if (userId == 0) {
- userId = getUserId();
- }
- Context.getPermissionsManager().checkUser(getUserId(), userId);
- return Context.getDeviceManager().getDevices(userId);
+ Context.getPermissionsManager().checkManager(getUserId());
+ result = deviceManager.getManagedItems(getUserId());
}
+ } else if (uniqueIds.isEmpty() && deviceIds.isEmpty()) {
+ if (userId == 0) {
+ userId = getUserId();
+ }
+ Context.getPermissionsManager().checkUser(getUserId(), userId);
+ result = deviceManager.getUserItems(userId);
} else {
- ArrayList<Device> devices = new ArrayList<>();
+ result = new HashSet<Long>();
+ for (String uniqueId : uniqueIds) {
+ Device device = deviceManager.getByUniqueId(uniqueId);
+ Context.getPermissionsManager().checkDevice(getUserId(), device.getId());
+ result.add(device.getId());
+ }
for (Long deviceId : deviceIds) {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
- devices.add(Context.getDeviceManager().getDeviceById(deviceId));
+ result.add(deviceId);
}
- return devices;
}
- }
-
- @POST
- public Response add(Device entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkDeviceReadonly(getUserId());
- Context.getPermissionsManager().checkDeviceLimit(getUserId());
- Context.getDeviceManager().addDevice(entity);
- Context.getDataManager().linkDevice(getUserId(), entity.getId());
- Context.getPermissionsManager().refreshPermissions();
- if (Context.getGeofenceManager() != null) {
- Context.getGeofenceManager().refresh();
- }
- 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().updateDevice(entity);
- Context.getPermissionsManager().refreshPermissions();
- if (Context.getGeofenceManager() != null) {
- Context.getGeofenceManager().refresh();
- }
- 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().removeDevice(id);
- Context.getPermissionsManager().refreshPermissions();
- if (Context.getGeofenceManager() != null) {
- Context.getGeofenceManager().refresh();
- }
- Context.getAliasesManager().removeDevice(id);
- return Response.noContent().build();
+ return deviceManager.getItems(result);
}
@Path("{id}/distance")
diff --git a/src/org/traccar/api/resource/CommandTypeResource.java b/src/org/traccar/api/resource/DriverResource.java
index d5d220547..91aa54c5e 100644
--- a/src/org/traccar/api/resource/CommandTypeResource.java
+++ b/src/org/traccar/api/resource/DriverResource.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com)
+ * Copyright 2017 Anton Tananaev (anton@traccar.org)
* Copyright 2017 Andrey Kunitsyn (andrey@traccar.org)
*
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,28 +16,21 @@
*/
package org.traccar.api.resource;
-import org.traccar.Context;
-import org.traccar.api.BaseResource;
-import org.traccar.model.CommandType;
-
import javax.ws.rs.Consumes;
-import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
-import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
-import java.util.Collection;
-@Path("commandtypes")
+import org.traccar.api.ExtendedObjectResource;
+import org.traccar.model.Driver;
+
+@Path("drivers")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
-public class CommandTypeResource extends BaseResource {
+public class DriverResource extends ExtendedObjectResource<Driver> {
- @GET
- public Collection<CommandType> get(@QueryParam("deviceId") long deviceId,
- @QueryParam("textChannel") boolean textChannel) {
- Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
- return Context.getDeviceManager().getCommandTypes(deviceId, textChannel);
+ public DriverResource() {
+ super(Driver.class);
}
}
diff --git a/src/org/traccar/api/resource/EventResource.java b/src/org/traccar/api/resource/EventResource.java
index 0ef5456af..a7cf9edbd 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)
@@ -22,10 +23,10 @@ public class EventResource extends BaseResource {
@Path("{id}")
@GET
public Event get(@PathParam("id") long id) throws SQLException {
- Event event = Context.getDataManager().getEvent(id);
+ Event event = Context.getDataManager().getObject(Event.class, id);
Context.getPermissionsManager().checkDevice(getUserId(), event.getDeviceId());
if (event.getGeofenceId() != 0) {
- Context.getPermissionsManager().checkGeofence(getUserId(), event.getGeofenceId());
+ Context.getPermissionsManager().checkPermission(Geofence.class, getUserId(), event.getGeofenceId());
}
return event;
}
diff --git a/src/org/traccar/api/resource/GeofencePermissionResource.java b/src/org/traccar/api/resource/GeofencePermissionResource.java
deleted file mode 100644
index 8faa63d85..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().checkGeofence(getUserId(), entity.getGeofenceId());
- Context.getDataManager().linkGeofence(entity.getUserId(), entity.getGeofenceId());
- Context.getGeofenceManager().refreshUserGeofences();
- 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().checkGeofence(getUserId(), entity.getGeofenceId());
- Context.getDataManager().unlinkGeofence(entity.getUserId(), entity.getGeofenceId());
- Context.getGeofenceManager().refreshUserGeofences();
- return Response.noContent().build();
- }
-
-}
diff --git a/src/org/traccar/api/resource/GeofenceResource.java b/src/org/traccar/api/resource/GeofenceResource.java
index d5acf106a..58f2c188c 100644
--- a/src/org/traccar/api/resource/GeofenceResource.java
+++ b/src/org/traccar/api/resource/GeofenceResource.java
@@ -15,98 +15,21 @@
*/
package org.traccar.api.resource;
-import org.traccar.Context;
-import org.traccar.api.BaseResource;
-import org.traccar.database.GeofenceManager;
+import org.traccar.api.ExtendedObjectResource;
import org.traccar.model.Geofence;
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.HashSet;
-import java.util.Set;
@Path("geofences")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
-public class GeofenceResource extends BaseResource {
-
- @GET
- public Collection<Geofence> get(
- @QueryParam("all") boolean all, @QueryParam("userId") long userId, @QueryParam("groupId") long groupId,
- @QueryParam("deviceId") long deviceId, @QueryParam("refresh") boolean refresh) throws SQLException {
-
- GeofenceManager geofenceManager = Context.getGeofenceManager();
- if (refresh) {
- geofenceManager.refreshGeofences();
- }
-
- Set<Long> result = new HashSet<>();
- if (all) {
- if (Context.getPermissionsManager().isAdmin(getUserId())) {
- result.addAll(geofenceManager.getAllGeofencesIds());
- } else {
- Context.getPermissionsManager().checkManager(getUserId());
- result.addAll(geofenceManager.getManagedGeofencesIds(getUserId()));
- }
- } else {
- if (userId == 0) {
- userId = getUserId();
- }
- Context.getPermissionsManager().checkUser(getUserId(), userId);
- result.addAll(geofenceManager.getUserGeofencesIds(userId));
- }
-
- if (groupId != 0) {
- Context.getPermissionsManager().checkGroup(getUserId(), groupId);
- result.retainAll(geofenceManager.getGroupGeofencesIds(groupId));
- }
-
- if (deviceId != 0) {
- Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
- result.retainAll(geofenceManager.getDeviceGeofencesIds(deviceId));
- }
- return geofenceManager.getGeofences(result);
-
- }
-
- @POST
- public Response add(Geofence entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getDataManager().addGeofence(entity);
- Context.getDataManager().linkGeofence(getUserId(), entity.getId());
- Context.getGeofenceManager().refreshGeofences();
- return Response.ok(entity).build();
- }
-
- @Path("{id}")
- @PUT
- public Response update(Geofence entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkGeofence(getUserId(), entity.getId());
- Context.getGeofenceManager().updateGeofence(entity);
- return Response.ok(entity).build();
- }
+public class GeofenceResource extends ExtendedObjectResource<Geofence> {
- @Path("{id}")
- @DELETE
- public Response remove(@PathParam("id") long id) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkGeofence(getUserId(), id);
- Context.getDataManager().removeGeofence(id);
- Context.getGeofenceManager().refreshGeofences();
- return Response.noContent().build();
+ public GeofenceResource() {
+ super(Geofence.class);
}
}
diff --git a/src/org/traccar/api/resource/GroupAttributeResource.java b/src/org/traccar/api/resource/GroupAttributeResource.java
deleted file mode 100644
index 84b876d34..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().checkAttribute(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().checkGeofence(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/GroupGeofenceResource.java b/src/org/traccar/api/resource/GroupGeofenceResource.java
deleted file mode 100644
index 81fd4e45f..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().checkGeofence(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().checkGeofence(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..fcea15d0a 100644
--- a/src/org/traccar/api/resource/GroupResource.java
+++ b/src/org/traccar/api/resource/GroupResource.java
@@ -15,83 +15,21 @@
*/
package org.traccar.api.resource;
-import org.traccar.Context;
-import org.traccar.api.BaseResource;
+import org.traccar.api.SimpleObjectResource;
import org.traccar.model.Group;
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;
@Path("groups")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
-public class GroupResource extends BaseResource {
+public class GroupResource extends SimpleObjectResource<Group> {
- @GET
- public Collection<Group> get(
- @QueryParam("all") boolean all, @QueryParam("userId") long userId) throws SQLException {
- if (all) {
- if (Context.getPermissionsManager().isAdmin(getUserId())) {
- return Context.getDeviceManager().getAllGroups();
- } else {
- Context.getPermissionsManager().checkManager(getUserId());
- return Context.getDeviceManager().getManagedGroups(getUserId());
- }
- } else {
- if (userId == 0) {
- userId = getUserId();
- }
- Context.getPermissionsManager().checkUser(getUserId(), userId);
- return Context.getDeviceManager().getGroups(userId);
- }
- }
-
- @POST
- public Response add(Group entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getDeviceManager().addGroup(entity);
- Context.getDataManager().linkGroup(getUserId(), entity.getId());
- Context.getPermissionsManager().refreshPermissions();
- if (Context.getGeofenceManager() != null) {
- Context.getGeofenceManager().refresh();
- }
- 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.getDeviceManager().updateGroup(entity);
- if (Context.getGeofenceManager() != null) {
- Context.getGeofenceManager().refresh();
- }
- 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.getDeviceManager().removeGroup(id);
- Context.getPermissionsManager().refreshPermissions();
- if (Context.getGeofenceManager() != null) {
- Context.getGeofenceManager().refresh();
- }
- return Response.noContent().build();
+ public GroupResource() {
+ super(Group.class);
}
}
diff --git a/src/org/traccar/api/resource/NotificationResource.java b/src/org/traccar/api/resource/NotificationResource.java
index dee972607..540f02926 100644
--- a/src/org/traccar/api/resource/NotificationResource.java
+++ b/src/org/traccar/api/resource/NotificationResource.java
@@ -15,7 +15,6 @@
*/
package org.traccar.api.resource;
-import java.sql.SQLException;
import java.util.Collection;
import javax.mail.MessagingException;
@@ -24,14 +23,14 @@ import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
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.ExtendedObjectResource;
import org.traccar.model.Event;
import org.traccar.model.Notification;
+import org.traccar.model.Typed;
import org.traccar.notification.NotificationMail;
import org.traccar.notification.NotificationSms;
@@ -40,34 +39,23 @@ import com.cloudhopper.smpp.type.SmppChannelException;
import com.cloudhopper.smpp.type.SmppTimeoutException;
import com.cloudhopper.smpp.type.UnrecoverablePduException;
-@Path("users/notifications")
+@Path("notifications")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
-public class NotificationResource extends BaseResource {
+public class NotificationResource extends ExtendedObjectResource<Notification> {
- @GET
- public Collection<Notification> get(@QueryParam("all") boolean all,
- @QueryParam("userId") long userId) throws SQLException {
- if (all) {
- return Context.getNotificationManager().getAllNotifications();
- }
- if (userId == 0) {
- userId = getUserId();
- }
- Context.getPermissionsManager().checkUser(getUserId(), userId);
- return Context.getNotificationManager().getAllUserNotifications(userId);
+ public NotificationResource() {
+ super(Notification.class);
}
- @POST
- public Response update(Notification entity) throws SQLException {
- Context.getPermissionsManager().checkReadonly(getUserId());
- Context.getPermissionsManager().checkUser(getUserId(), entity.getUserId());
- Context.getNotificationManager().updateNotification(entity);
- return Response.ok(entity).build();
+ @GET
+ @Path("types")
+ public Collection<Typed> get() {
+ return Context.getNotificationManager().getAllNotificationTypes();
}
- @Path("test")
@POST
+ @Path("test")
public Response testMessage() throws MessagingException, RecoverablePduException,
UnrecoverablePduException, SmppTimeoutException, SmppChannelException, InterruptedException {
NotificationMail.sendMailSync(getUserId(), new Event("test", 0), null);
diff --git a/src/org/traccar/api/resource/PermissionsResource.java b/src/org/traccar/api/resource/PermissionsResource.java
new file mode 100644
index 000000000..9b9f65ad1
--- /dev/null
+++ b/src/org/traccar/api/resource/PermissionsResource.java
@@ -0,0 +1,79 @@
+/*
+ * 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.LinkedHashMap;
+
+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.Device;
+import org.traccar.model.Permission;
+import org.traccar.model.User;
+
+@Path("permissions")
+@Produces(MediaType.APPLICATION_JSON)
+@Consumes(MediaType.APPLICATION_JSON)
+public class PermissionsResource extends BaseResource {
+
+ private void checkPermission(Permission permission, boolean link) {
+ if (!link && permission.getOwnerClass().equals(User.class)
+ && permission.getPropertyClass().equals(Device.class)) {
+ if (getUserId() != permission.getOwnerId()) {
+ Context.getPermissionsManager().checkUser(getUserId(), permission.getOwnerId());
+ } else {
+ Context.getPermissionsManager().checkAdmin(getUserId());
+ }
+ } else {
+ Context.getPermissionsManager().checkPermission(
+ permission.getOwnerClass(), getUserId(), permission.getOwnerId());
+ }
+ Context.getPermissionsManager().checkPermission(
+ permission.getPropertyClass(), getUserId(), permission.getPropertyId());
+ }
+
+ @POST
+ public Response add(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException {
+ Context.getPermissionsManager().checkReadonly(getUserId());
+ Permission permission = new Permission(entity);
+ checkPermission(permission, true);
+ Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(),
+ permission.getPropertyClass(), permission.getPropertyId(), true);
+ Context.getPermissionsManager().refreshPermissions(permission);
+ return Response.noContent().build();
+ }
+
+ @DELETE
+ public Response remove(LinkedHashMap<String, Long> entity) throws SQLException, ClassNotFoundException {
+ Context.getPermissionsManager().checkReadonly(getUserId());
+ Permission permission = new Permission(entity);
+ checkPermission(permission, false);
+ Context.getDataManager().linkObject(permission.getOwnerClass(), permission.getOwnerId(),
+ permission.getPropertyClass(), permission.getPropertyId(), false);
+ Context.getPermissionsManager().refreshPermissions(permission);
+ return Response.noContent().build();
+ }
+
+}
diff --git a/src/org/traccar/api/resource/PositionResource.java b/src/org/traccar/api/resource/PositionResource.java
index 9d3cd9ae6..c031b842f 100644
--- a/src/org/traccar/api/resource/PositionResource.java
+++ b/src/org/traccar/api/resource/PositionResource.java
@@ -54,7 +54,7 @@ public class PositionResource extends BaseResource {
if (!positionIds.isEmpty()) {
ArrayList<Position> positions = new ArrayList<>();
for (Long positionId : positionIds) {
- Position position = Context.getDataManager().getPosition(positionId);
+ Position position = Context.getDataManager().getObject(Position.class, positionId);
Context.getPermissionsManager().checkDevice(getUserId(), position.getDeviceId());
positions.add(position);
}
@@ -87,7 +87,7 @@ public class PositionResource extends BaseResource {
@QueryParam("deviceId") long deviceId, @QueryParam("from") String from, @QueryParam("to") String to)
throws SQLException {
Context.getPermissionsManager().checkDevice(getUserId(), deviceId);
- GpxBuilder gpx = new GpxBuilder(Context.getIdentityManager().getDeviceById(deviceId).getName());
+ GpxBuilder gpx = new GpxBuilder(Context.getIdentityManager().getById(deviceId).getName());
gpx.addPositions(Context.getDataManager().getPositions(
deviceId, DateUtil.parseDate(from), DateUtil.parseDate(to)));
return Response.ok(gpx.build()).header(HttpHeaders.CONTENT_DISPOSITION, CONTENT_DISPOSITION_VALUE_GPX).build();
diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java
index acdbb7c87..fa2a14c6f 100644
--- a/src/org/traccar/api/resource/SessionResource.java
+++ b/src/org/traccar/api/resource/SessionResource.java
@@ -80,7 +80,7 @@ public class SessionResource extends BaseResource {
request.getSession().setAttribute(USER_ID_KEY, userId);
}
} else if (token != null) {
- User user = Context.getPermissionsManager().getUserByToken(token);
+ User user = Context.getUsersManager().getUserByToken(token);
if (user != null) {
userId = user.getId();
request.getSession().setAttribute(USER_ID_KEY, userId);
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 4d8a8b3a4..0f6f6edba 100644
--- a/src/org/traccar/api/resource/UserResource.java
+++ b/src/org/traccar/api/resource/UserResource.java
@@ -16,17 +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;
@@ -34,33 +33,42 @@ import javax.ws.rs.core.Response;
import java.sql.SQLException;
import java.util.Collection;
import java.util.Date;
+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 {
- if (Context.getPermissionsManager().isAdmin(getUserId())) {
+ UsersManager usersManager = Context.getUsersManager();
+ Set<Long> result = null;
+ if (Context.getPermissionsManager().getUserAdmin(getUserId())) {
if (userId != 0) {
- return Context.getPermissionsManager().getUsers(userId);
+ result = usersManager.getUserItems(userId);
} else {
- return Context.getPermissionsManager().getAllUsers();
+ result = usersManager.getAllItems();
}
- } else if (Context.getPermissionsManager().isManager(getUserId())) {
- return Context.getPermissionsManager().getManagedUsers(getUserId());
+ } else if (Context.getPermissionsManager().getUserManager(getUserId())) {
+ result = usersManager.getManagedItems(getUserId());
} else {
throw new SecurityException("Admin or manager access required");
}
+ return usersManager.getItems(result);
}
+ @Override
@PermitAll
@POST
public Response add(User entity) throws SQLException {
- if (!Context.getPermissionsManager().isAdmin(getUserId())) {
+ if (!Context.getPermissionsManager().getUserAdmin(getUserId())) {
Context.getPermissionsManager().checkUserUpdate(getUserId(), new User(), entity);
- if (Context.getPermissionsManager().isManager(getUserId())) {
+ if (Context.getPermissionsManager().getUserManager(getUserId())) {
Context.getPermissionsManager().checkUserLimit(getUserId());
} else {
Context.getPermissionsManager().checkRegistration(getUserId());
@@ -72,44 +80,12 @@ public class UserResource extends BaseResource {
}
}
}
- Context.getPermissionsManager().addUser(entity);
- if (Context.getPermissionsManager().isManager(getUserId())) {
- Context.getDataManager().linkUser(getUserId(), entity.getId());
- }
- Context.getPermissionsManager().refreshUserPermissions();
- if (Context.getNotificationManager() != null) {
- Context.getNotificationManager().refresh();
+ Context.getUsersManager().addItem(entity);
+ if (Context.getPermissionsManager().getUserManager(getUserId())) {
+ Context.getDataManager().linkObject(User.class, getUserId(), ManagedUser.class, entity.getId(), true);
}
+ Context.getUsersManager().refreshUserItems();
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.getPermissionsManager().updateUser(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.getPermissionsManager().removeUser(id);
- if (Context.getGeofenceManager() != null) {
- Context.getGeofenceManager().refreshUserGeofences();
- }
- if (Context.getNotificationManager() != null) {
- Context.getNotificationManager().refresh();
- }
- return Response.noContent().build();
- }
-
}