aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-07-25 19:53:11 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-07-25 19:53:11 +1200
commitdeb288877b8faa69c763c96ada7529380141af7f (patch)
tree6ae4210935ca851382d14a2388468811438677c5
parente637bd7c022560dcc9c48516dc22a884a5de8122 (diff)
downloadtrackermap-server-deb288877b8faa69c763c96ada7529380141af7f.tar.gz
trackermap-server-deb288877b8faa69c763c96ada7529380141af7f.tar.bz2
trackermap-server-deb288877b8faa69c763c96ada7529380141af7f.zip
Update sessions user info
-rw-r--r--debug.xml5
-rw-r--r--src/org/traccar/database/DataManager.java10
-rw-r--r--src/org/traccar/web/BaseServlet.java7
-rw-r--r--src/org/traccar/web/MainServlet.java13
4 files changed, 22 insertions, 13 deletions
diff --git a/debug.xml b/debug.xml
index 88f9a859e..f33a350c4 100644
--- a/debug.xml
+++ b/debug.xml
@@ -155,6 +155,11 @@
WHERE email = :email;
</entry>
+ <entry key='database.selectUser'>
+ SELECT * FROM "user"
+ WHERE id = :id;
+ </entry>
+
<entry key='database.selectUsersAll'>
SELECT * FROM "user";
</entry>
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index f68ff2ae6..4c12ecd77 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -34,14 +34,14 @@ import javax.sql.DataSource;
import org.traccar.Config;
import org.traccar.helper.DriverDelegate;
import org.traccar.helper.Log;
-import org.traccar.web.AsyncServlet;
-import org.traccar.web.JsonConverter;
import org.traccar.model.Device;
import org.traccar.model.MiscFormatter;
import org.traccar.model.Permission;
import org.traccar.model.Position;
import org.traccar.model.Server;
import org.traccar.model.User;
+import org.traccar.web.AsyncServlet;
+import org.traccar.web.JsonConverter;
public class DataManager implements IdentityManager {
@@ -227,6 +227,12 @@ public class DataManager implements IdentityManager {
.executeQuery(new User());
}
+ public User getUser(long userId) throws SQLException {
+ return QueryBuilder.create(dataSource, getQuery("database.selectUser"))
+ .setLong("id", userId)
+ .executeQuerySingle(new User());
+ }
+
public void addUser(User user) throws SQLException {
user.setId(QueryBuilder.create(dataSource, getQuery("database.insertUser"), true)
.setObject(user)
diff --git a/src/org/traccar/web/BaseServlet.java b/src/org/traccar/web/BaseServlet.java
index 28b4b467e..b7df841ca 100644
--- a/src/org/traccar/web/BaseServlet.java
+++ b/src/org/traccar/web/BaseServlet.java
@@ -25,7 +25,6 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
-import org.traccar.model.User;
public abstract class BaseServlet extends HttpServlet {
@@ -51,11 +50,11 @@ public abstract class BaseServlet extends HttpServlet {
protected abstract boolean handle(String command, HttpServletRequest req, HttpServletResponse resp) throws Exception;
public long getUserId(HttpServletRequest req) {
- User user = (User) req.getSession().getAttribute(USER_KEY);
- if (user == null) {
+ Long userId = (Long) req.getSession().getAttribute(USER_KEY);
+ if (userId == null) {
throw new AccessControlException("User not logged in");
}
- return user.getId();
+ return userId;
}
public void securityCheck(boolean check) throws SecurityException {
diff --git a/src/org/traccar/web/MainServlet.java b/src/org/traccar/web/MainServlet.java
index eaadef245..21ef04088 100644
--- a/src/org/traccar/web/MainServlet.java
+++ b/src/org/traccar/web/MainServlet.java
@@ -15,8 +15,6 @@
*/
package org.traccar.web;
-import java.io.IOException;
-import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.traccar.Context;
@@ -46,10 +44,11 @@ public class MainServlet extends BaseServlet {
return true;
}
- private void session(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
- User user = (User) req.getSession().getAttribute(USER_KEY);
- if (user != null) {
- sendResponse(resp.getWriter(), JsonConverter.objectToJson(user));
+ private void session(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ Long userId = (Long) req.getSession().getAttribute(USER_KEY);
+ if (userId != null) {
+ sendResponse(resp.getWriter(), JsonConverter.objectToJson(
+ Context.getDataManager().getUser(userId)));
} else {
sendResponse(resp.getWriter(), false);
}
@@ -59,7 +58,7 @@ public class MainServlet extends BaseServlet {
User user = Context.getDataManager().login(
req.getParameter("email"), req.getParameter("password"));
if (user != null) {
- req.getSession().setAttribute(USER_KEY, user);
+ req.getSession().setAttribute(USER_KEY, user.getId());
sendResponse(resp.getWriter(), JsonConverter.objectToJson(user));
} else {
sendResponse(resp.getWriter(), false);