From 4694754957f733c88e0e2f39fad6f02a42140adb Mon Sep 17 00:00:00 2001 From: Khurshid Fayzullaev Date: Mon, 24 Mar 2014 10:33:02 -0400 Subject: Admin cannot add a user with an existing username --- src/org/traccar/web/server/model/DataServiceImpl.java | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) (limited to 'src/org/traccar/web/server/model/DataServiceImpl.java') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 3ae4d21d..509bca4a 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -193,9 +193,21 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService synchronized (entityManager) { entityManager.getTransaction().begin(); try { - entityManager.persist(user); - entityManager.getTransaction().commit(); - return user; + String login = user.getLogin(); + 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()) { + entityManager.persist(user); + entityManager.getTransaction().commit(); + return user; + } + else + { + throw new IllegalStateException(); + } } catch (RuntimeException e) { entityManager.getTransaction().rollback(); throw e; -- cgit v1.2.3