aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Tananaev <anton.tananaev@gmail.com>2015-05-06 22:23:26 +1200
committerAnton Tananaev <anton.tananaev@gmail.com>2015-05-06 22:23:26 +1200
commit80e166430b79df771abc73e55892dc9f5fd0b2f1 (patch)
tree72c8849812ec43bd97f6f8bf65d6e700d5bdf7df
parentb9d6befdbcc1a56c87d14066d67da574762d81bc (diff)
downloadtrackermap-server-80e166430b79df771abc73e55892dc9f5fd0b2f1.tar.gz
trackermap-server-80e166430b79df771abc73e55892dc9f5fd0b2f1.tar.bz2
trackermap-server-80e166430b79df771abc73e55892dc9f5fd0b2f1.zip
Add new user registration
-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
-rw-r--r--web/Application.js3
-rw-r--r--web/Login.js11
-rw-r--r--web/RegisterDialog.js77
-rw-r--r--web/Strings.js1
7 files changed, 118 insertions, 23 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;
diff --git a/web/Application.js b/web/Application.js
index 594ce0635..85826e6a5 100644
--- a/web/Application.js
+++ b/web/Application.js
@@ -51,6 +51,9 @@ Ext.Loader.loadScript({
} else {
Ext.create('Login').show();
}
+ },
+ failure: function() {
+ alert(error);
}
})
}
diff --git a/web/Login.js b/web/Login.js
index 871bef17b..cdc312bf8 100644
--- a/web/Login.js
+++ b/web/Login.js
@@ -37,7 +37,10 @@ Ext.define('LoginForm', {
}],
buttons: [{
- text: Strings.login_register
+ text: Strings.login_register,
+ handler: function() {
+ Ext.create('RegisterDialog').show();
+ }
}, {
text: Strings.login_login,
handler: function() {
@@ -59,10 +62,14 @@ Ext.define('LoginForm', {
Ext.define('Login', {
extend: 'Ext.window.Window',
- requires: [ 'MainView' ],
+ requires: [
+ 'MainView',
+ 'RegisterDialog'
+ ],
title: Strings.login_title,
closable: false,
+ resizable: false,
items: [{ xtype: 'login-form' }],
diff --git a/web/RegisterDialog.js b/web/RegisterDialog.js
new file mode 100644
index 000000000..c69026ea6
--- /dev/null
+++ b/web/RegisterDialog.js
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2015 Anton Tananaev (anton.tananaev@gmail.com)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+Ext.define('RegisterForm', {
+ extend: 'Ext.form.Panel',
+ xtype: 'register-form',
+
+ defaultType: 'textfield',
+ bodyPadding: Styles.panel_padding,
+
+ defaults: { anchor: '100%' },
+
+ url: '/api/register',
+ jsonSubmit: true,
+
+ items: [{
+ allowBlank: false,
+ fieldLabel: Strings.login_name,
+ name: 'name'
+ }, {
+ allowBlank: false,
+ fieldLabel: Strings.login_email,
+ name: 'email',
+ vtype: 'email'
+ }, {
+ allowBlank: false,
+ fieldLabel: Strings.login_password,
+ name: 'password',
+ inputType: 'password'
+ }],
+
+ buttons: [{
+ text: Strings.dialog_create,
+ handler: function() {
+ var win = this.up('window');
+ var form = this.up('form').getForm();
+ if (form.isValid()) {
+ form.submit({
+ success: function() {
+ win.close();
+ },
+ failure: function() {
+ // error
+ }
+ });
+ }
+ }
+
+ }, {
+ text: Strings.dialog_cancel,
+ handler: function() {
+ this.up('window').close();
+ }
+ }]
+});
+
+Ext.define('RegisterDialog', {
+ extend: 'Ext.window.Window',
+
+ title: Strings.login_register,
+ resizable: false,
+
+ items: [{ xtype: 'register-form' }]
+});
diff --git a/web/Strings.js b/web/Strings.js
index 9c89f7fa0..33ec25c49 100644
--- a/web/Strings.js
+++ b/web/Strings.js
@@ -18,6 +18,7 @@ Ext.define('Strings', {
singleton: true,
login_title: 'Login',
+ login_name: 'Name',
login_email: 'Email',
login_password: 'Password',
login_register: 'Register',