[2711] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
| 2 | package org.openstreetmap.josm.gui;
|
---|
| 3 |
|
---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 5 |
|
---|
[2850] | 6 | import java.text.MessageFormat;
|
---|
| 7 |
|
---|
[2711] | 8 | import org.openstreetmap.josm.Main;
|
---|
| 9 | import org.openstreetmap.josm.data.Preferences.PreferenceChangeEvent;
|
---|
| 10 | import org.openstreetmap.josm.data.Preferences.PreferenceChangedListener;
|
---|
| 11 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
[4263] | 12 | import org.openstreetmap.josm.io.auth.CredentialsManager;
|
---|
[2711] | 13 | import org.openstreetmap.josm.tools.CheckParameterUtil;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * JosmUserStateManager is a global object which keeps track of what JOSM knows about
|
---|
| 17 | * the identity of the current user.
|
---|
[2801] | 18 | *
|
---|
[2711] | 19 | * JOSM can be operated anonymously provided the current user never invokes an operation
|
---|
| 20 | * on the OSM server which required authentication. In this case JOSM neither knows
|
---|
| 21 | * the user name of the OSM account of the current user nor its unique id. Perhaps the
|
---|
| 22 | * user doesn't have one.
|
---|
[2801] | 23 | *
|
---|
[2711] | 24 | * If the current user supplies a user name and a password in the JOSM preferences JOSM
|
---|
| 25 | * can partially identify the user.
|
---|
[2801] | 26 | *
|
---|
[2711] | 27 | * The current user is fully identified if JOSM knows both the user name and the unique
|
---|
[4246] | 28 | * id of the users OSM account. The latter is retrieved from the OSM server with a
|
---|
[2711] | 29 | * <tt>GET /api/0.6/user/details</tt> request, submitted with the user name and password
|
---|
| 30 | * of the current user.
|
---|
[2801] | 31 | *
|
---|
[2711] | 32 | * The global JosmUserStateManager listens to {@see PreferenceChangeEvent}s and keeps track
|
---|
| 33 | * of what the current JOSM instance knows about the current user. Other subsystems can
|
---|
| 34 | * let the global JosmUserStateManager know in case they fully identify the current user, see
|
---|
| 35 | * {@see #setFullyIdentified(String, long)}.
|
---|
[2801] | 36 | *
|
---|
[2711] | 37 | * The information kept by the JosmUserStateManager can be used to
|
---|
| 38 | * <ul>
|
---|
| 39 | * <li>safely query changesets owned by the current user based on its user id, not on its user name</li>
|
---|
| 40 | * <li>safely search for objects last touched by the current user based on its user id, not on its user name</li>
|
---|
| 41 | * </ul>
|
---|
| 42 | *
|
---|
| 43 | */
|
---|
| 44 | public class JosmUserIdentityManager implements PreferenceChangedListener{
|
---|
| 45 |
|
---|
| 46 | static private JosmUserIdentityManager instance;
|
---|
| 47 |
|
---|
| 48 | /**
|
---|
[2990] | 49 | * Replies the unique instance of the JOSM user identity manager
|
---|
[2801] | 50 | *
|
---|
[2990] | 51 | * @return the unique instance of the JOSM user identity manager
|
---|
[2711] | 52 | */
|
---|
| 53 | static public JosmUserIdentityManager getInstance() {
|
---|
| 54 | if (instance == null) {
|
---|
| 55 | instance = new JosmUserIdentityManager();
|
---|
| 56 | instance.initFromPreferences();
|
---|
| 57 | Main.pref.addPreferenceChangeListener(instance);
|
---|
| 58 | }
|
---|
| 59 | return instance;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | private String userName;
|
---|
| 63 | private UserInfo userInfo;
|
---|
| 64 |
|
---|
| 65 | private JosmUserIdentityManager() {
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | /**
|
---|
| 69 | * Remembers the fact that the current JOSM user is anonymous.
|
---|
| 70 | */
|
---|
| 71 | public void setAnonymous() {
|
---|
| 72 | userName = null;
|
---|
| 73 | userInfo = null;
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | /**
|
---|
| 77 | * Remebers the fact that the current JOSM user is partially identified
|
---|
| 78 | * by the user name of its OSM account.
|
---|
[2801] | 79 | *
|
---|
[2711] | 80 | * @param userName the user name. Must not be null. Must not be empty (whitespace only).
|
---|
| 81 | * @throws IllegalArgumentException thrown if userName is null
|
---|
| 82 | * @throws IllegalArgumentException thrown if userName is empty
|
---|
| 83 | */
|
---|
| 84 | public void setPartiallyIdentified(String userName) throws IllegalArgumentException {
|
---|
| 85 | CheckParameterUtil.ensureParameterNotNull(userName, "userName");
|
---|
| 86 | if (userName.trim().equals(""))
|
---|
[2850] | 87 | throw new IllegalArgumentException(MessageFormat.format("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName));
|
---|
[2711] | 88 | this.userName = userName;
|
---|
| 89 | userInfo = null;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | /**
|
---|
| 93 | * Remembers the fact that the current JOSM user is fully identified with a
|
---|
| 94 | * verified pair of user name and user id.
|
---|
[2801] | 95 | *
|
---|
[2711] | 96 | * @param userName the user name. Must not be null. Must not be empty.
|
---|
| 97 | * @param userinfo additional information about the user, retrieved from the OSM server and including the user id
|
---|
| 98 | * @throws IllegalArgumentException thrown if userName is null
|
---|
| 99 | * @throws IllegalArgumentException thrown if userName is empty
|
---|
| 100 | * @throws IllegalArgumentException thrown if userinfo is null
|
---|
| 101 | */
|
---|
| 102 | public void setFullyIdentified(String username, UserInfo userinfo) throws IllegalArgumentException {
|
---|
| 103 | CheckParameterUtil.ensureParameterNotNull(username, "username");
|
---|
| 104 | if (username.trim().equals(""))
|
---|
| 105 | throw new IllegalArgumentException(tr("Expected non-empty value for parameter ''{0}'', got ''{1}''", "userName", userName));
|
---|
| 106 | CheckParameterUtil.ensureParameterNotNull(userinfo, "userinfo");
|
---|
| 107 | this.userName = username;
|
---|
| 108 | this.userInfo = userinfo;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | /**
|
---|
| 112 | * Replies true if the current JOSM user is anonymous.
|
---|
[2801] | 113 | *
|
---|
[2711] | 114 | * @return true if the current user is anonymous.
|
---|
| 115 | */
|
---|
| 116 | public boolean isAnonymous() {
|
---|
| 117 | return userName == null && userInfo == null;
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | /**
|
---|
| 121 | * Replies true if the current JOSM user is partially identified.
|
---|
[2801] | 122 | *
|
---|
[2711] | 123 | * @return true if the current JOSM user is partially identified.
|
---|
| 124 | */
|
---|
| 125 | public boolean isPartiallyIdentified() {
|
---|
| 126 | return userName != null && userInfo == null;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
| 129 | /**
|
---|
| 130 | * Replies true if the current JOSM user is fully identified.
|
---|
[2801] | 131 | *
|
---|
[2711] | 132 | * @return true if the current JOSM user is fully identified.
|
---|
| 133 | */
|
---|
| 134 | public boolean isFullyIdentified() {
|
---|
| 135 | return userName != null && userInfo != null;
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | /**
|
---|
| 139 | * Replies the user name of the current JOSM user. null, if {@see #isAnonymous()} is true.
|
---|
[2801] | 140 | *
|
---|
[2711] | 141 | * @return the user name of the current JOSM user
|
---|
| 142 | */
|
---|
| 143 | public String getUserName() {
|
---|
| 144 | return userName;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /**
|
---|
| 148 | * Replies the user id of the current JOSM user. 0, if {@see #isAnonymous()} or
|
---|
| 149 | * {@see #isPartiallyIdentified()} is true.
|
---|
[2801] | 150 | *
|
---|
[2711] | 151 | * @return the user id of the current JOSM user
|
---|
| 152 | */
|
---|
| 153 | public int getUserId() {
|
---|
| 154 | if (userInfo == null) return 0;
|
---|
| 155 | return userInfo.getId();
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | /**
|
---|
| 159 | * Replies verified additional information about the current user if the user is
|
---|
| 160 | * {@see #isFullyIdentified()}.
|
---|
[2801] | 161 | *
|
---|
[2711] | 162 | * @return verified additional information about the current user
|
---|
| 163 | */
|
---|
| 164 | public UserInfo getUserInfo() {
|
---|
| 165 | return userInfo;
|
---|
| 166 | }
|
---|
| 167 | /**
|
---|
| 168 | * Initializes the user identity manager from values in the {@see org.openstreetmap.josm.data.Preferences}
|
---|
| 169 | */
|
---|
| 170 | public void initFromPreferences() {
|
---|
[4263] | 171 | String userName = CredentialsManager.getInstance().getUsername();
|
---|
[2711] | 172 | if (isAnonymous()) {
|
---|
| 173 | if (userName != null && ! userName.trim().equals("")) {
|
---|
| 174 | setPartiallyIdentified(userName);
|
---|
| 175 | }
|
---|
| 176 | } else {
|
---|
| 177 | if (!userName.equals(this.userName)) {
|
---|
| 178 | setPartiallyIdentified(userName);
|
---|
| 179 | } else {
|
---|
| 180 | // same name in the preferences as JOSM already knows about;
|
---|
| 181 | // keep the state, be it partially or fully identified
|
---|
| 182 | }
|
---|
| 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /**
|
---|
| 187 | * Replies true if the user with name <code>username</code> is the current
|
---|
| 188 | * user
|
---|
[2801] | 189 | *
|
---|
[2711] | 190 | * @param username the user name
|
---|
| 191 | * @return true if the user with name <code>username</code> is the current
|
---|
| 192 | * user
|
---|
| 193 | */
|
---|
| 194 | public boolean isCurrentUser(String username) {
|
---|
| 195 | if (username == null || this.userName == null) return false;
|
---|
| 196 | return this.userName.equals(username);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /* ------------------------------------------------------------------- */
|
---|
| 200 | /* interface PreferenceChangeListener */
|
---|
| 201 | /* ------------------------------------------------------------------- */
|
---|
| 202 | public void preferenceChanged(PreferenceChangeEvent evt) {
|
---|
| 203 | if (evt.getKey().equals("osm-server.username")) {
|
---|
| 204 | String newValue = evt.getNewValue();
|
---|
| 205 | if (newValue == null || newValue.trim().length() == 0) {
|
---|
| 206 | setAnonymous();
|
---|
| 207 | } else {
|
---|
| 208 | if (! newValue.equals(userName)) {
|
---|
| 209 | setPartiallyIdentified(newValue);
|
---|
| 210 | }
|
---|
| 211 | }
|
---|
| 212 | return;
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | if (evt.getKey().equals("osm-server.url")) {
|
---|
| 216 | String newValue = evt.getNewValue();
|
---|
| 217 | if (newValue == null || newValue.trim().equals("")) {
|
---|
| 218 | setAnonymous();
|
---|
| 219 | } else if (isFullyIdentified()) {
|
---|
| 220 | setPartiallyIdentified(getUserName());
|
---|
| 221 | }
|
---|
| 222 | }
|
---|
| 223 | }
|
---|
| 224 | }
|
---|