From 4ed5277c256c3e1391e70d74b83c53aa05124ec1 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 20 Mar 2014 14:59:25 -0400 Subject: Don't register with an existing username Register method has been modified. A new user cannot register with an existing username. --- .../traccar/web/server/model/DataServiceImpl.java | 26 +++++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 2d678b09..e1f4748b 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -147,12 +147,26 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService @Override public User register(String login, String password) { if (getApplicationSettings().getRegistrationEnabled()) { - User user = new User(); - user.setLogin(login); - user.setPassword(password); - createUser(getSessionEntityManager(), user); - setSessionUser(user); - return user; + EntityManager entityManager = getSessionEntityManager(); + synchronized (entityManager) { + + TypedQuery query = entityManager.createQuery( + "SELECT x FROM User x WHERE x.login = :login", User.class); + query.setParameter("login", login); + List results = query.getResultList(); + if (results.isEmpty()) { + User user = new User(); + user.setLogin(login); + user.setPassword(password); + createUser(getSessionEntityManager(), user); + setSessionUser(user); + return user; + } + else + { + throw new IllegalStateException(); + } + } } else { throw new SecurityException(); } -- cgit v1.2.3 From 6cd0a8c9327d2628d022d3f2ad87ac2de7026a97 Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 20 Mar 2014 15:01:32 -0400 Subject: Prompt error if username is already taken Add onFailure to prompt an error about that a username is taken --- src/org/traccar/web/client/controller/LoginController.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/org/traccar/web/client/controller/LoginController.java b/src/org/traccar/web/client/controller/LoginController.java index f8fdc8c2..1aa152b8 100644 --- a/src/org/traccar/web/client/controller/LoginController.java +++ b/src/org/traccar/web/client/controller/LoginController.java @@ -90,6 +90,10 @@ public class LoginController implements LoginDialog.LoginHandler { loginHandler.onLogin(); } } + @Override + public void onFailure(Throwable caught) { + new AlertMessageBox("Error", "Username is already taken").show(); + } }); } } -- cgit v1.2.3 From f40f6e306d055ebc46b0e22c850a21f0cd0cfb6c Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Thu, 20 Mar 2014 15:03:48 -0400 Subject: Update DataServiceImpl.java --- src/org/traccar/web/server/model/DataServiceImpl.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index e1f4748b..3ae4d21d 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -155,12 +155,12 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService query.setParameter("login", login); List results = query.getResultList(); if (results.isEmpty()) { - User user = new User(); - user.setLogin(login); - user.setPassword(password); - createUser(getSessionEntityManager(), user); - setSessionUser(user); - return user; + User user = new User(); + user.setLogin(login); + user.setPassword(password); + createUser(getSessionEntityManager(), user); + setSessionUser(user); + return user; } else { -- cgit v1.2.3