diff options
-rw-r--r-- | src/org/traccar/web/server/model/DataServiceImpl.java | 30 | ||||
-rw-r--r-- | src/org/traccar/web/shared/model/User.java | 11 |
2 files changed, 36 insertions, 5 deletions
diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 287a5684..3df7b686 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -61,6 +61,22 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService } entityManagerFactory = Persistence.createEntityManagerFactory(persistenceUnit); + + // Create Administrator account + EntityManager entityManager = entityManagerFactory.createEntityManager(); + try { + TypedQuery<User> query = entityManager.createQuery("SELECT x FROM User x WHERE x.login = 'admin'", User.class); + List<User> results = query.getResultList(); + if (results.isEmpty()) { + User user = new User(); + user.setLogin("admin"); + user.setPassword("admin"); + user.setAdmin(true); + createUser(user); + } + } finally { + entityManager.close(); + } } @Override @@ -115,17 +131,21 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService @Override public boolean register(String login, String password) { + User user = new User(); + user.setLogin(login); + user.setPassword(password); + createUser(user); + setUser(user); + return true; + } + + private void createUser(User user) { EntityManager entityManager = entityManagerFactory.createEntityManager(); try { - User user = new User(); - user.setLogin(login); - user.setPassword(password); entityManager.getTransaction().begin(); try { entityManager.persist(user); entityManager.getTransaction().commit(); - setUser(user); - return true; } catch (RuntimeException e) { entityManager.getTransaction().rollback(); throw e; diff --git a/src/org/traccar/web/shared/model/User.java b/src/org/traccar/web/shared/model/User.java index 89bebc07..9a67c6d9 100644 --- a/src/org/traccar/web/shared/model/User.java +++ b/src/org/traccar/web/shared/model/User.java @@ -40,6 +40,7 @@ public class User implements Serializable, Cloneable { id = user.id; login = user.login; password = user.password; + admin = user.admin; } @Id @@ -71,6 +72,16 @@ public class User implements Serializable, Cloneable { return password; } + private boolean admin; + + public void setAdmin(boolean admin) { + this.admin = admin; + } + + public boolean getAdmin() { + return admin; + } + @OneToMany(fetch = FetchType.EAGER) private List<Device> devices = new LinkedList<Device>(); |