aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2016-08-08 10:52:25 +0300
committerGitHub <noreply@github.com>2016-08-08 10:52:25 +0300
commit988ee3cc1067ef3cd63c5f5f4b4363fd9b048c84 (patch)
tree85f388c16000b33676172e512a7717c6932e3be5
parent1f0e1dddadd32a1f40695bf33d877d192ecd1403 (diff)
parentdc78308a2cd950c6997820147dc9ee89d610923a (diff)
downloadtraccar-server-988ee3cc1067ef3cd63c5f5f4b4363fd9b048c84.tar.gz
traccar-server-988ee3cc1067ef3cd63c5f5f4b4363fd9b048c84.tar.bz2
traccar-server-988ee3cc1067ef3cd63c5f5f4b4363fd9b048c84.zip
Merge pull request #2188 from ninioe/master
Automatically login remembered user
-rw-r--r--src/org/traccar/api/resource/SessionResource.java25
-rw-r--r--web/app/view/LoginController.js9
2 files changed, 26 insertions, 8 deletions
diff --git a/src/org/traccar/api/resource/SessionResource.java b/src/org/traccar/api/resource/SessionResource.java
index 745088a4d..49670c1f9 100644
--- a/src/org/traccar/api/resource/SessionResource.java
+++ b/src/org/traccar/api/resource/SessionResource.java
@@ -20,6 +20,7 @@ import org.traccar.api.BaseResource;
import org.traccar.model.User;
import javax.annotation.security.PermitAll;
+import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
@@ -39,6 +40,8 @@ import java.sql.SQLException;
public class SessionResource extends BaseResource {
public static final String USER_ID_KEY = "userId";
+ public static final String USER_COOKIE_KEY = "user";
+ public static final String PASS_COOKIE_KEY = "password";
@javax.ws.rs.core.Context
private HttpServletRequest request;
@@ -47,6 +50,28 @@ public class SessionResource extends BaseResource {
@GET
public User get() throws SQLException {
Long userId = (Long) request.getSession().getAttribute(USER_ID_KEY);
+ if (userId == null) {
+ Cookie[] cookies = request.getCookies();
+ String email = null, password = null;
+ if (cookies != null) {
+ for (int i = 0; i < cookies.length; i++) {
+ if (cookies[i].getName().equals(USER_COOKIE_KEY)) {
+ email = cookies[i].getValue();
+ }
+ if (cookies[i].getName().equals(PASS_COOKIE_KEY)) {
+ password = cookies[i].getValue();
+ }
+ }
+ }
+ if (email != null && password != null) {
+ User user = Context.getDataManager().login(email, password);
+ if (user != null) {
+ userId = user.getId();
+ request.getSession().setAttribute(USER_ID_KEY, userId);
+ }
+ }
+ }
+
if (userId != null) {
return Context.getDataManager().getUser(userId);
} else {
diff --git a/web/app/view/LoginController.js b/web/app/view/LoginController.js
index 7a78a6fb1..8beef1e04 100644
--- a/web/app/view/LoginController.js
+++ b/web/app/view/LoginController.js
@@ -1,5 +1,5 @@
/*
- * Copyright 2015 - 2016 Anton Tananaev (anton.tananaev@gmail.com)
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -26,13 +26,6 @@ Ext.define('Traccar.view.LoginController', {
this.lookupReference('registerButton').setDisabled(
!Traccar.app.getServer().get('registration'));
this.lookupReference('languageField').setValue(Locale.language);
- var user = Ext.util.Cookies.get('user');
- var password = Ext.util.Cookies.get('password');
- if (user && password) {
- this.lookupReference('userField').setValue(user);
- this.lookupReference('passwordField').setValue(password);
- this.login();
- }
},
login: function () {