diff options
-rw-r--r-- | src/org/traccar/web/client/controller/LoginController.java | 4 | ||||
-rw-r--r-- | src/org/traccar/web/server/model/DataServiceImpl.java | 26 |
2 files changed, 24 insertions, 6 deletions
diff --git a/src/org/traccar/web/client/controller/LoginController.java b/src/org/traccar/web/client/controller/LoginController.java index f8fdc8c..1aa152b 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(); + } }); } } diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 2d678b0..3ae4d21 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<User> query = entityManager.createQuery( + "SELECT x FROM User x WHERE x.login = :login", User.class); + query.setParameter("login", login); + List<User> 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(); } |