diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2013-02-20 22:35:43 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2013-02-20 22:35:43 +1300 |
commit | 6801698dfc9c99b34d7ed081ebcfbec4a8853868 (patch) | |
tree | cecc80f811b6c3a594b9da272a7c65d3a9695315 | |
parent | 0e766c9024e2457320f64353e29bb5e2d8294b28 (diff) | |
download | etbsa-traccar-web-6801698dfc9c99b34d7ed081ebcfbec4a8853868.tar.gz etbsa-traccar-web-6801698dfc9c99b34d7ed081ebcfbec4a8853868.tar.bz2 etbsa-traccar-web-6801698dfc9c99b34d7ed081ebcfbec4a8853868.zip |
Added account dialog
11 files changed, 316 insertions, 23 deletions
diff --git a/src/org/traccar/web/client/ApplicationContext.java b/src/org/traccar/web/client/ApplicationContext.java index f505324..80b8998 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 d9795f3..d2b65ff 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<Boolean>() { + Application.getDataService().login(login, password, new BaseAsyncCallback<User>() { @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<Boolean>() { + Application.getDataService().register(login, password, new BaseAsyncCallback<User>() { @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 75130f2..c369cbb 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<User>() { + @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 a16ae51..859c812 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<Device> getDevices(); diff --git a/src/org/traccar/web/client/model/DataServiceAsync.java b/src/org/traccar/web/client/model/DataServiceAsync.java index c0478fb..689e699 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<Boolean> callback); - void login(String login, String password, AsyncCallback<Boolean> callback); + void login(String login, String password, AsyncCallback<User> callback); void logout(AsyncCallback<Boolean> callback); - void register(String login, String password, AsyncCallback<Boolean> callback); + void register(String login, String password, AsyncCallback<User> callback); + + void updateUser(User user, AsyncCallback<User> callback); void getDevices(AsyncCallback<List<Device>> 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 0000000..cba2996 --- /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<User> { + + ModelKeyProvider<User> id(); + + ValueProvider<User, String> login(); + + ValueProvider<User, String> password(); + + ValueProvider<User, Boolean> 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 0000000..ad7e8e0 --- /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<UserSettings> { + + ModelKeyProvider<UserSettings> id(); + + ValueProvider<UserSettings, UserSettings.SpeedUnit> 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 0000000..ac2eca3 --- /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<User> { + + private static UserDialogUiBinder uiBinder = GWT.create(UserDialogUiBinder.class); + + interface UserDialogUiBinder extends UiBinder<Widget, UserDialog> { + } + + private UserDriver driver = GWT.create(UserDriver.class); + + interface UserDriver extends SimpleBeanEditorDriver<User, UserDialog> { + } + + 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 0000000..c16ad4c --- /dev/null +++ b/src/org/traccar/web/client/view/UserDialog.ui.xml @@ -0,0 +1,47 @@ +<!DOCTYPE ui:UiBinder SYSTEM "http://dl.google.com/gwt/DTD/xhtml.ent"> +<ui:UiBinder + xmlns:ui="urn:ui:com.google.gwt.uibinder" + xmlns:g="urn:import:com.google.gwt.user.client.ui" + xmlns:gxt="urn:import:com.sencha.gxt.widget.core.client" + xmlns:container="urn:import:com.sencha.gxt.widget.core.client.container" + xmlns:form="urn:import:com.sencha.gxt.widget.core.client.form" + xmlns:button="urn:import:com.sencha.gxt.widget.core.client.button"> + + <ui:with type="com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer.VerticalLayoutData" field="verticalLayoutData"> + <ui:attributes width="1" height="-1" /> + </ui:with> + + <gxt:Window ui:field="window" pixelSize="300, 140" modal="true" headingText="Account" focusWidget="{saveButton}"> + <container:VerticalLayoutContainer> + <container:child layoutData="{verticalLayoutData}"> + <form:FieldLabel text="User"> + <form:widget> + <form:TextField ui:field="login" /> + </form:widget> + </form:FieldLabel> + </container:child> + <container:child layoutData="{verticalLayoutData}"> + <form:FieldLabel text="Password"> + <form:widget> + <form:PasswordField ui:field="password" /> + </form:widget> + </form:FieldLabel> + </container:child> + <container:child layoutData="{verticalLayoutData}"> + <form:FieldLabel text="Administrator"> + <form:widget> + <form:CheckBox ui:field="admin" enabled="false" /> + </form:widget> + </form:FieldLabel> + </container:child> + </container:VerticalLayoutContainer> + + <gxt:button> + <button:TextButton ui:field="saveButton" text="Save" /> + </gxt:button> + <gxt:button> + <button:TextButton ui:field="cancelButton" text="Cancel" /> + </gxt:button> + </gxt:Window> + +</ui:UiBinder> diff --git a/src/org/traccar/web/server/model/DataServiceImpl.java b/src/org/traccar/web/server/model/DataServiceImpl.java index 28c6167..1f6ff60 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<User> query = entityManager.createQuery( @@ -116,10 +116,11 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService List<User> 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 9a67c6d..77a17ee 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<Device> devices = new LinkedList<Device>(); + public void setDevices(List<Device> devices) { + this.devices = devices; + } + public List<Device> getDevices() { return devices; } + @OneToOne + private UserSettings userSettings; + + public void setUserSettings(UserSettings userSettings) { + this.userSettings = userSettings; + } + + public UserSettings getUserSettings() { + return userSettings; + } + } |