From 6801698dfc9c99b34d7ed081ebcfbec4a8853868 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Wed, 20 Feb 2013 22:35:43 +1300 Subject: Added account dialog --- src/org/traccar/web/client/ApplicationContext.java | 19 ++++- .../web/client/controller/LoginController.java | 16 ++-- .../web/client/controller/SettingsController.java | 18 +++- src/org/traccar/web/client/model/DataService.java | 7 +- .../traccar/web/client/model/DataServiceAsync.java | 7 +- .../traccar/web/client/model/UserProperties.java | 34 ++++++++ .../web/client/model/UserSettingsProperties.java | 30 +++++++ src/org/traccar/web/client/view/UserDialog.java | 95 ++++++++++++++++++++++ src/org/traccar/web/client/view/UserDialog.ui.xml | 47 +++++++++++ .../traccar/web/server/model/DataServiceImpl.java | 47 +++++++++-- src/org/traccar/web/shared/model/User.java | 19 +++++ 11 files changed, 316 insertions(+), 23 deletions(-) create mode 100644 src/org/traccar/web/client/model/UserProperties.java create mode 100644 src/org/traccar/web/client/model/UserSettingsProperties.java create mode 100644 src/org/traccar/web/client/view/UserDialog.java create mode 100644 src/org/traccar/web/client/view/UserDialog.ui.xml (limited to 'src') diff --git a/src/org/traccar/web/client/ApplicationContext.java b/src/org/traccar/web/client/ApplicationContext.java index f5053243..80b8998d 100644 --- a/src/org/traccar/web/client/ApplicationContext.java +++ b/src/org/traccar/web/client/ApplicationContext.java @@ -1,6 +1,7 @@ package org.traccar.web.client; import org.traccar.web.shared.model.ApplicationSettings; +import org.traccar.web.shared.model.User; import org.traccar.web.shared.model.UserSettings; public class ApplicationContext { @@ -38,15 +39,25 @@ public class ApplicationContext { } } - private UserSettings userSettings; + private User user; + + public void setUser(User user) { + this.user = user; + } + + public User getUser() { + return user; + } public void setUserSettings(UserSettings userSettings) { - this.userSettings = userSettings; + if (user != null) { + user.setUserSettings(userSettings); + } } public UserSettings getUserSettings() { - if (userSettings != null) { - return userSettings; + if (user != null && user.getUserSettings() != null) { + return user.getUserSettings(); } else { return new UserSettings(); // default settings } diff --git a/src/org/traccar/web/client/controller/LoginController.java b/src/org/traccar/web/client/controller/LoginController.java index d9795f30..d2b65ff5 100644 --- a/src/org/traccar/web/client/controller/LoginController.java +++ b/src/org/traccar/web/client/controller/LoginController.java @@ -16,8 +16,10 @@ package org.traccar.web.client.controller; import org.traccar.web.client.Application; +import org.traccar.web.client.ApplicationContext; import org.traccar.web.client.model.BaseAsyncCallback; import org.traccar.web.client.view.LoginDialog; +import org.traccar.web.shared.model.User; import com.sencha.gxt.widget.core.client.box.AlertMessageBox; @@ -58,10 +60,11 @@ public class LoginController implements LoginDialog.LoginHandler { @Override public void onLogin(String login, String password) { if (validate(login, password)) { - Application.getDataService().login(login, password, new BaseAsyncCallback() { + Application.getDataService().login(login, password, new BaseAsyncCallback() { @Override - public void onSuccess(Boolean result) { - if (result) { + public void onSuccess(User result) { + if (result != null) { + ApplicationContext.getInstance().setUser(result); if (loginHandler != null) { dialog.hide(); loginHandler.onLogin(); @@ -77,10 +80,11 @@ public class LoginController implements LoginDialog.LoginHandler { @Override public void onRegister(String login, String password) { if (validate(login, password)) { - Application.getDataService().register(login, password, new BaseAsyncCallback() { + Application.getDataService().register(login, password, new BaseAsyncCallback() { @Override - public void onSuccess(Boolean result) { - if (result) { + public void onSuccess(User result) { + if (result != null) { + ApplicationContext.getInstance().setUser(result); if (loginHandler != null) { dialog.hide(); loginHandler.onLogin(); diff --git a/src/org/traccar/web/client/controller/SettingsController.java b/src/org/traccar/web/client/controller/SettingsController.java index 75130f2b..c369cbb1 100644 --- a/src/org/traccar/web/client/controller/SettingsController.java +++ b/src/org/traccar/web/client/controller/SettingsController.java @@ -20,14 +20,27 @@ import org.traccar.web.client.ApplicationContext; import org.traccar.web.client.model.BaseAsyncCallback; import org.traccar.web.client.view.ApplicationSettingsDialog; import org.traccar.web.client.view.DeviceView; +import org.traccar.web.client.view.UserDialog; import org.traccar.web.shared.model.ApplicationSettings; +import org.traccar.web.shared.model.User; public class SettingsController implements DeviceView.SettingsHandler { @Override public void onAccountSelected() { - // TODO Auto-generated method stub - + new UserDialog( + ApplicationContext.getInstance().getUser(), + new UserDialog.UserHandler() { + @Override + public void onSave(User user) { + Application.getDataService().updateUser(user, new BaseAsyncCallback() { + @Override + public void onSuccess(User result) { + ApplicationContext.getInstance().setUser(result); + } + }); + } + }).show(); } @Override @@ -55,7 +68,6 @@ public class SettingsController implements DeviceView.SettingsHandler { ApplicationContext.getInstance().setApplicationSettings(result); } }); - } }).show(); } diff --git a/src/org/traccar/web/client/model/DataService.java b/src/org/traccar/web/client/model/DataService.java index a16ae51d..859c8128 100644 --- a/src/org/traccar/web/client/model/DataService.java +++ b/src/org/traccar/web/client/model/DataService.java @@ -21,6 +21,7 @@ import java.util.List; import org.traccar.web.shared.model.ApplicationSettings; import org.traccar.web.shared.model.Device; import org.traccar.web.shared.model.Position; +import org.traccar.web.shared.model.User; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; @@ -30,11 +31,13 @@ public interface DataService extends RemoteService { boolean authenticated(); - boolean login(String login, String password); + User login(String login, String password); boolean logout(); - boolean register(String login, String password); + User register(String login, String password); + + User updateUser(User user); List getDevices(); diff --git a/src/org/traccar/web/client/model/DataServiceAsync.java b/src/org/traccar/web/client/model/DataServiceAsync.java index c0478fb8..689e6993 100644 --- a/src/org/traccar/web/client/model/DataServiceAsync.java +++ b/src/org/traccar/web/client/model/DataServiceAsync.java @@ -21,6 +21,7 @@ import java.util.List; import org.traccar.web.shared.model.ApplicationSettings; import org.traccar.web.shared.model.Device; import org.traccar.web.shared.model.Position; +import org.traccar.web.shared.model.User; import com.google.gwt.user.client.rpc.AsyncCallback; @@ -28,11 +29,13 @@ public interface DataServiceAsync { void authenticated(AsyncCallback callback); - void login(String login, String password, AsyncCallback callback); + void login(String login, String password, AsyncCallback callback); void logout(AsyncCallback callback); - void register(String login, String password, AsyncCallback callback); + void register(String login, String password, AsyncCallback callback); + + void updateUser(User user, AsyncCallback callback); void getDevices(AsyncCallback> callback); diff --git a/src/org/traccar/web/client/model/UserProperties.java b/src/org/traccar/web/client/model/UserProperties.java new file mode 100644 index 00000000..cba2996f --- /dev/null +++ b/src/org/traccar/web/client/model/UserProperties.java @@ -0,0 +1,34 @@ +/* + * Copyright 2013 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. + */ +package org.traccar.web.client.model; + +import org.traccar.web.shared.model.User; + +import com.sencha.gxt.core.client.ValueProvider; +import com.sencha.gxt.data.shared.ModelKeyProvider; +import com.sencha.gxt.data.shared.PropertyAccess; + +public interface UserProperties extends PropertyAccess { + + ModelKeyProvider id(); + + ValueProvider login(); + + ValueProvider password(); + + ValueProvider admin(); + +} diff --git a/src/org/traccar/web/client/model/UserSettingsProperties.java b/src/org/traccar/web/client/model/UserSettingsProperties.java new file mode 100644 index 00000000..ad7e8e0d --- /dev/null +++ b/src/org/traccar/web/client/model/UserSettingsProperties.java @@ -0,0 +1,30 @@ +/* + * Copyright 2013 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. + */ +package org.traccar.web.client.model; + +import org.traccar.web.shared.model.UserSettings; + +import com.sencha.gxt.core.client.ValueProvider; +import com.sencha.gxt.data.shared.ModelKeyProvider; +import com.sencha.gxt.data.shared.PropertyAccess; + +public interface UserSettingsProperties extends PropertyAccess { + + ModelKeyProvider id(); + + ValueProvider speedUnit(); + +} diff --git a/src/org/traccar/web/client/view/UserDialog.java b/src/org/traccar/web/client/view/UserDialog.java new file mode 100644 index 00000000..ac2eca32 --- /dev/null +++ b/src/org/traccar/web/client/view/UserDialog.java @@ -0,0 +1,95 @@ +/* + * Copyright 2013 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. + */ +package org.traccar.web.client.view; + +import org.traccar.web.client.ApplicationContext; +import org.traccar.web.shared.model.User; + +import com.google.gwt.core.client.GWT; +import com.google.gwt.editor.client.Editor; +import com.google.gwt.editor.client.SimpleBeanEditorDriver; +import com.google.gwt.uibinder.client.UiBinder; +import com.google.gwt.uibinder.client.UiField; +import com.google.gwt.uibinder.client.UiHandler; +import com.google.gwt.user.client.ui.Widget; +import com.sencha.gxt.widget.core.client.Window; +import com.sencha.gxt.widget.core.client.event.SelectEvent; +import com.sencha.gxt.widget.core.client.form.CheckBox; +import com.sencha.gxt.widget.core.client.form.PasswordField; +import com.sencha.gxt.widget.core.client.form.TextField; + +public class UserDialog implements Editor { + + private static UserDialogUiBinder uiBinder = GWT.create(UserDialogUiBinder.class); + + interface UserDialogUiBinder extends UiBinder { + } + + private UserDriver driver = GWT.create(UserDriver.class); + + interface UserDriver extends SimpleBeanEditorDriver { + } + + public interface UserHandler { + public void onSave(User user); + } + + private UserHandler userHandler; + + @UiField + Window window; + + @UiField + TextField login; + + @UiField + PasswordField password; + + @UiField + CheckBox admin; + + public UserDialog(User user, UserHandler userHandler) { + this.userHandler = userHandler; + uiBinder.createAndBindUi(this); + + if (ApplicationContext.getInstance().getUser().getAdmin()) { + admin.setEnabled(true); + } + + driver.initialize(this); + driver.edit(user); + } + + public void show() { + window.show(); + } + + public void hide() { + window.hide(); + } + + @UiHandler("saveButton") + public void onLoginClicked(SelectEvent event) { + window.hide(); + userHandler.onSave(driver.flush()); + } + + @UiHandler("cancelButton") + public void onRegisterClicked(SelectEvent event) { + window.hide(); + } + +} diff --git a/src/org/traccar/web/client/view/UserDialog.ui.xml b/src/org/traccar/web/client/view/UserDialog.ui.xml new file mode 100644 index 00000000..c16ad4c3 --- /dev/null +++ b/src/org/traccar/web/client/view/UserDialog.ui.xml @@ -0,0 +1,47 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 28c61678..1f6ff608 100644 --- a/src/org/traccar/web/server/model/DataServiceImpl.java +++ b/src/org/traccar/web/server/model/DataServiceImpl.java @@ -107,7 +107,7 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService } @Override - public boolean login(String login, String password) { + public User login(String login, String password) { EntityManager entityManager = entityManagerFactory.createEntityManager(); try { TypedQuery query = entityManager.createQuery( @@ -116,10 +116,11 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService List results = query.getResultList(); if (!results.isEmpty() && password.equals(results.get(0).getPassword())) { - setUser(results.get(0)); - return true; + User user = results.get(0); + setUser(user); + return user; } - return false; + return null; } finally { entityManager.close(); } @@ -132,14 +133,48 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService } @Override - public boolean register(String login, String password) { + public User register(String login, String password) { if (getApplicationSettings().getRegistrationEnabled()) { User user = new User(); user.setLogin(login); user.setPassword(password); createUser(user); setUser(user); - return true; + return user; + } else { + throw new SecurityException(); + } + } + + @Override + public User updateUser(User user) { + User currentUser = getUser(); + if (currentUser.getAdmin() || (currentUser.getId() == user.getId() && !user.getAdmin())) { + EntityManager entityManager = entityManagerFactory.createEntityManager(); + try { + // TODO: better solution? + if (currentUser.getId() == user.getId()) { + currentUser.setLogin(user.getLogin()); + currentUser.setPassword(user.getPassword()); + currentUser.setUserSettings(user.getUserSettings()); + user = currentUser; + } else { + // TODO: handle other users + } + + entityManager.getTransaction().begin(); + try { + entityManager.merge(user); + entityManager.getTransaction().commit(); + setUser(user); + return user; + } catch (RuntimeException e) { + entityManager.getTransaction().rollback(); + throw e; + } + } finally { + entityManager.close(); + } } else { throw new SecurityException(); } diff --git a/src/org/traccar/web/shared/model/User.java b/src/org/traccar/web/shared/model/User.java index 9a67c6d9..77a17eef 100644 --- a/src/org/traccar/web/shared/model/User.java +++ b/src/org/traccar/web/shared/model/User.java @@ -25,8 +25,11 @@ import javax.persistence.FetchType; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.OneToMany; +import javax.persistence.OneToOne; import javax.persistence.Table; +import com.google.gwt.user.client.rpc.GwtTransient; + @Entity @Table(name="users") public class User implements Serializable, Cloneable { @@ -82,11 +85,27 @@ public class User implements Serializable, Cloneable { return admin; } + @GwtTransient @OneToMany(fetch = FetchType.EAGER) private List devices = new LinkedList(); + public void setDevices(List devices) { + this.devices = devices; + } + public List getDevices() { return devices; } + @OneToOne + private UserSettings userSettings; + + public void setUserSettings(UserSettings userSettings) { + this.userSettings = userSettings; + } + + public UserSettings getUserSettings() { + return userSettings; + } + } -- cgit v1.2.3