From 843a370660646499c3007256be953506567f23fb Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 17 Oct 2015 12:35:16 +1300 Subject: Add linking and unlinking of devices --- src/org/traccar/database/DataManager.java | 8 ++++++++ src/org/traccar/web/DeviceServlet.java | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) (limited to 'src/org/traccar') diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java index e249a7591..af2dd559a 100644 --- a/src/org/traccar/database/DataManager.java +++ b/src/org/traccar/database/DataManager.java @@ -321,6 +321,14 @@ public class DataManager implements IdentityManager { AsyncServlet.sessionRefreshUser(userId); } + public void unlinkDevice(long userId, long deviceId) throws SQLException { + QueryBuilder.create(dataSource, getQuery("database.unlinkDevice")) + .setLong("userId", userId) + .setLong("deviceId", deviceId) + .executeUpdate(); + AsyncServlet.sessionRefreshUser(userId); + } + public Collection getPositions(long userId, long deviceId, Date from, Date to) throws SQLException { return QueryBuilder.create(dataSource, getQuery("database.selectPositions")) .setLong("deviceId", deviceId) diff --git a/src/org/traccar/web/DeviceServlet.java b/src/org/traccar/web/DeviceServlet.java index 1ae94c1d0..8f983ad78 100644 --- a/src/org/traccar/web/DeviceServlet.java +++ b/src/org/traccar/web/DeviceServlet.java @@ -38,6 +38,12 @@ public class DeviceServlet extends BaseServlet { case "/remove": remove(req, resp); break; + case "/link": + link(req, resp); + break; + case "/unlink": + unlink(req, resp); + break; default: return false; } @@ -87,4 +93,22 @@ public class DeviceServlet extends BaseServlet { sendResponse(resp.getWriter(), true); } + private void link(HttpServletRequest req, HttpServletResponse resp) throws Exception { + Context.getPermissionsManager().checkAdmin(getUserId(req)); + Context.getDataManager().linkDevice( + Long.parseLong(req.getParameter("userId")), + Long.parseLong(req.getParameter("deviceId"))); + Context.getPermissionsManager().refresh(); + sendResponse(resp.getWriter(), true); + } + + private void unlink(HttpServletRequest req, HttpServletResponse resp) throws Exception { + Context.getPermissionsManager().checkAdmin(getUserId(req)); + Context.getDataManager().unlinkDevice( + Long.parseLong(req.getParameter("userId")), + Long.parseLong(req.getParameter("deviceId"))); + Context.getPermissionsManager().refresh(); + sendResponse(resp.getWriter(), true); + } + } -- cgit v1.2.3