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(-) (limited to 'src/org/traccar/web/server') 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 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(-) (limited to 'src/org/traccar/web/server') 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