Merge pull request #147 from khfayzullaev/master

Admin should not add an existing username
This commit is contained in:
Anton Tananaev 2014-03-26 21:18:54 +13:00
commit f97b868dc1
2 changed files with 20 additions and 3 deletions

View File

@ -33,6 +33,7 @@ import org.traccar.web.shared.model.UserSettings;
import com.google.gwt.core.client.GWT;
import com.sencha.gxt.data.shared.ListStore;
import com.sencha.gxt.widget.core.client.Dialog.PredefinedButton;
import com.sencha.gxt.widget.core.client.box.AlertMessageBox;
import com.sencha.gxt.widget.core.client.box.ConfirmMessageBox;
import com.sencha.gxt.widget.core.client.event.HideEvent;
@ -97,6 +98,10 @@ public class SettingsController implements DeviceView.SettingsHandler {
public void onSuccess(User result) {
userStore.add(result);
}
@Override
public void onFailure(Throwable caught) {
new AlertMessageBox("Error", "Username is already taken").show();
}
});
}
}).show();

View File

@ -193,9 +193,21 @@ public class DataServiceImpl extends RemoteServiceServlet implements DataService
synchronized (entityManager) {
entityManager.getTransaction().begin();
try {
entityManager.persist(user);
entityManager.getTransaction().commit();
return user;
String login = user.getLogin();
TypedQuery<User> query = entityManager.createQuery(
"SELECT x FROM User x WHERE x.login = :login", User.class);
query.setParameter("login", login);
List<User> results = query.getResultList();
if (results.isEmpty()) {
entityManager.persist(user);
entityManager.getTransaction().commit();
return user;
}
else
{
throw new IllegalStateException();
}
} catch (RuntimeException e) {
entityManager.getTransaction().rollback();
throw e;