From ebcedc38812405ba1cd03c518317bcf0cd52f925 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Mon, 25 Feb 2013 21:06:38 +1300 Subject: Added user management --- .../traccar/web/server/model/DataServiceImpl.java | 59 ++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'src/org/traccar/web/server/model') diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index c2d9f72e..663f0e4b 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -146,6 +146,41 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService } } + @Override + public List getUsers() { + List users = new LinkedList(); + EntityManager entityManager = entityManagerFactory.createEntityManager(); + try { + users.addAll(entityManager.createQuery("SELECT x FROM User x", User.class).getResultList()); + return users; + } finally { + entityManager.close(); + } + } + + @Override + public User addUser(User user) { + User currentUser = getUser(); + if (currentUser.getAdmin()) { + EntityManager entityManager = entityManagerFactory.createEntityManager(); + try { + entityManager.getTransaction().begin(); + try { + entityManager.persist(user); + entityManager.getTransaction().commit(); + return user; + } catch (RuntimeException e) { + entityManager.getTransaction().rollback(); + throw e; + } + } finally { + entityManager.close(); + } + } else { + throw new SecurityException(); + } + } + @Override public User updateUser(User user) { User currentUser = getUser(); @@ -181,6 +216,30 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService } } + @Override + public User removeUser(User user) { + User currentUser = getUser(); + if (currentUser.getAdmin()) { + EntityManager entityManager = entityManagerFactory.createEntityManager(); + try { + entityManager.getTransaction().begin(); + try { + user = entityManager.merge(user); + entityManager.remove(user); + entityManager.getTransaction().commit(); + return user; + } catch (RuntimeException e) { + entityManager.getTransaction().rollback(); + throw e; + } + } finally { + entityManager.close(); + } + } else { + throw new SecurityException(); + } + } + private void createUser(User user) { EntityManager entityManager = entityManagerFactory.createEntityManager(); try { -- cgit v1.2.3