// License: GPL. For details, see LICENSE file. package org.openstreetmap.josm.gui; import static org.openstreetmap.josm.tools.I18n.tr; import java.text.MessageFormat; import org.openstreetmap.josm.Main; import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent; import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener; import org.openstreetmap.josm.data.Preferences.StringSetting; import org.openstreetmap.josm.data.osm.User; import org.openstreetmap.josm.data.osm.UserInfo; import org.openstreetmap.josm.gui.preferences.server.OAuthAccessTokenHolder; import org.openstreetmap.josm.gui.progress.NullProgressMonitor; import org.openstreetmap.josm.io.OnlineResource; import org.openstreetmap.josm.io.OsmApi; import org.openstreetmap.josm.io.OsmServerUserInfoReader; import org.openstreetmap.josm.io.OsmTransferException; import org.openstreetmap.josm.io.auth.CredentialsManager; import org.openstreetmap.josm.tools.CheckParameterUtil; /** * JosmUserIdentityManager is a global object which keeps track of what JOSM knows about * the identity of the current user. * * JOSM can be operated anonymously provided the current user never invokes an operation * on the OSM server which required authentication. In this case JOSM neither knows * the user name of the OSM account of the current user nor its unique id. Perhaps the * user doesn't have one. * * If the current user supplies a user name and a password in the JOSM preferences JOSM * can partially identify the user. * * The current user is fully identified if JOSM knows both the user name and the unique * id of the users OSM account. The latter is retrieved from the OSM server with a * GET /api/0.6/user/details request, submitted with the user name and password * of the current user. * * The global JosmUserIdentityManager listens to {@link PreferenceChangeEvent}s and keeps track * of what the current JOSM instance knows about the current user. Other subsystems can * let the global JosmUserIdentityManager know in case they fully identify the current user, see * {@link #setFullyIdentified}. * * The information kept by the JosmUserIdentityManager can be used to *
username
is the current user
*
* @param username the user name
* @return true if the user with name username
is the current user
*/
public boolean isCurrentUser(String username) {
return username != null && this.userName != null && this.userName.equals(username);
}
/**
* Replies true if the current user is {@link #isFullyIdentified() fully identified} and the {@link #getUserId() user ids} match,
* or if the current user is not {@link #isFullyIdentified() fully identified} and the {@link #userName user names} match.
*
* @param user the user to test
* @return true if given user is the current user
*/
public boolean isCurrentUser(User user) {
if (user == null) {
return false;
} else if (isFullyIdentified()) {
return getUserId() == user.getId();
} else {
return isCurrentUser(user.getName());
}
}
/* ------------------------------------------------------------------- */
/* interface PreferenceChangeListener */
/* ------------------------------------------------------------------- */
@Override
public void preferenceChanged(PreferenceChangeEvent evt) {
switch (evt.getKey()) {
case "osm-server.username":
String newUserName = null;
if (evt.getNewValue() instanceof StringSetting) {
newUserName = ((StringSetting) evt.getNewValue()).getValue();
}
if (newUserName == null || newUserName.trim().isEmpty()) {
setAnonymous();
} else {
if (!newUserName.equals(userName)) {
setPartiallyIdentified(newUserName);
}
}
return;
case "osm-server.url":
String newUrl = null;
if (evt.getNewValue() instanceof StringSetting) {
newUrl = ((StringSetting) evt.getNewValue()).getValue();
}
if (newUrl == null || newUrl.trim().isEmpty()) {
setAnonymous();
} else if (isFullyIdentified()) {
setPartiallyIdentified(getUserName());
}
break;
case "oauth.access-token.key":
accessTokenKeyChanged = true;
break;
case "oauth.access-token.secret":
accessTokenSecretChanged = true;
break;
}
if (accessTokenKeyChanged && accessTokenSecretChanged) {
accessTokenKeyChanged = false;
accessTokenSecretChanged = false;
if (OsmApi.isUsingOAuth()) {
try {
getInstance().initFromOAuth();
} catch (Exception e) {
Main.error(e);
}
}
}
}
}