aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/org/traccar/database/DataManager.java34
-rw-r--r--src/org/traccar/http/MainServlet.java9
-rw-r--r--src/org/traccar/model/User.java6
3 files changed, 28 insertions, 21 deletions
diff --git a/src/org/traccar/database/DataManager.java b/src/org/traccar/database/DataManager.java
index c62c7d7e6..2aca1ff87 100644
--- a/src/org/traccar/database/DataManager.java
+++ b/src/org/traccar/database/DataManager.java
@@ -37,8 +37,8 @@ import javax.sql.DataSource;
import org.traccar.helper.DriverDelegate;
import org.traccar.model.Device;
import org.traccar.model.Permission;
-import org.traccar.model.Event;
import org.traccar.model.Position;
+import org.traccar.model.User;
public class DataManager {
@@ -222,6 +222,7 @@ public class DataManager {
QueryBuilder.create(dataSource,
"CREATE TABLE user (" +
"id INT PRIMARY KEY AUTO_INCREMENT," +
+ "name VARCHAR(1024) NOT NULL," +
"email VARCHAR(1024) NOT NULL UNIQUE," +
"password VARCHAR(1024) NOT NULL," +
"salt VARCHAR(1024) NOT NULL," +
@@ -296,7 +297,11 @@ public class DataManager {
"CREATE TABLE traccar1 (" +
"id INT PRIMARY KEY AUTO_INCREMENT);").executeUpdate();
- addUser("admin", "admin", true);
+ User admin = new User();
+ admin.setName("admin");
+ admin.setEmail("admin");
+ admin.setPassword("admin");
+ addUser(admin);
}
public long login(String email, String password) throws SQLException {
@@ -321,25 +326,12 @@ public class DataManager {
}
}
- public void addUser(String email, String password, boolean admin) throws SQLException {
-
- Connection connection = dataSource.getConnection();
- try {
- PreparedStatement statement = connection.prepareStatement(
- "INSERT INTO user (email, password, salt, admin) " +
- "VALUES (?, CAST(HASH('SHA256', STRINGTOUTF8(?), 1000) AS VARCHAR), '', ?);");
- try {
- statement.setString(1, email);
- statement.setString(2, password);
- statement.setBoolean(3, admin);
-
- statement.executeUpdate();
- } finally {
- statement.close();
- }
- } finally {
- connection.close();
- }
+ public void addUser(User user) throws SQLException {
+ user.setId(QueryBuilder.create(dataSource,
+ "INSERT INTO user (name, email, password, salt, admin) " +
+ "VALUES (:name, :email, CAST(HASH('SHA256', STRINGTOUTF8(:password), 1000) AS VARCHAR), '', :admin);")
+ .setObject(user)
+ .executeUpdate());
}
public Collection<Permission> getPermissions() throws SQLException {
diff --git a/src/org/traccar/http/MainServlet.java b/src/org/traccar/http/MainServlet.java
index 8fa0a13af..036c472f8 100644
--- a/src/org/traccar/http/MainServlet.java
+++ b/src/org/traccar/http/MainServlet.java
@@ -20,6 +20,7 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.traccar.Context;
+import org.traccar.model.User;
public class MainServlet extends BaseServlet {
@@ -31,6 +32,8 @@ public class MainServlet extends BaseServlet {
login(req, resp);
} else if (command.equals("/logout")) {
logout(req, resp);
+ } else if (command.equals("/register")) {
+ register(req, resp);
} else {
return false;
}
@@ -52,4 +55,10 @@ public class MainServlet extends BaseServlet {
sendResponse(resp.getWriter());
}
+ private void register(HttpServletRequest req, HttpServletResponse resp) throws Exception {
+ User user = JsonConverter.objectFromJson(req.getReader(), new User());
+ Context.getDataManager().addUser(user);
+ sendResponse(resp.getWriter());
+ }
+
}
diff --git a/src/org/traccar/model/User.java b/src/org/traccar/model/User.java
index fb060d1d6..2df5f276d 100644
--- a/src/org/traccar/model/User.java
+++ b/src/org/traccar/model/User.java
@@ -26,6 +26,10 @@ public class User implements Factory {
public long getId() { return id; }
public void setId(long id) { this.id = id; }
+ private String name;
+ public String getName() { return name; }
+ public void setName(String name) { this.name = name; }
+
private String email;
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
@@ -37,6 +41,8 @@ public class User implements Factory {
private boolean readonly;
private boolean admin;
+ public boolean getAdmin() { return admin; }
+ public void setAdmin(boolean admin) { this.admin = admin; }
private String map;