source: josm/trunk/src/org/openstreetmap/josm/gui/JosmUserIdentityManager.java @ 5241

Revision 4612, 8.5 KB checked in by bastiK, 6 months ago (diff)

see #7027 - internal structures for preferences (expect some problems next few days)

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