1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui;
|
---|
3 |
|
---|
4 | import org.openstreetmap.josm.spi.preferences.PreferenceChangeEvent;
|
---|
5 | import org.openstreetmap.josm.data.UserIdentityManager;
|
---|
6 | import org.openstreetmap.josm.data.osm.User;
|
---|
7 | import org.openstreetmap.josm.data.osm.UserInfo;
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * JosmUserIdentityManager is a global object which keeps track of what JOSM knows about
|
---|
11 | * the identity of the current user.
|
---|
12 | *
|
---|
13 | * JOSM can be operated anonymously provided the current user never invokes an operation
|
---|
14 | * on the OSM server which required authentication. In this case JOSM neither knows
|
---|
15 | * the user name of the OSM account of the current user nor its unique id. Perhaps the
|
---|
16 | * user doesn't have one.
|
---|
17 | *
|
---|
18 | * If the current user supplies a user name and a password in the JOSM preferences JOSM
|
---|
19 | * can partially identify the user.
|
---|
20 | *
|
---|
21 | * The current user is fully identified if JOSM knows both the user name and the unique
|
---|
22 | * id of the users OSM account. The latter is retrieved from the OSM server with a
|
---|
23 | * <tt>GET /api/0.6/user/details</tt> request, submitted with the user name and password
|
---|
24 | * of the current user.
|
---|
25 | *
|
---|
26 | * The global JosmUserIdentityManager listens to {@link PreferenceChangeEvent}s and keeps track
|
---|
27 | * of what the current JOSM instance knows about the current user. Other subsystems can
|
---|
28 | * let the global JosmUserIdentityManager know in case they fully identify the current user, see
|
---|
29 | * {@link #setFullyIdentified}.
|
---|
30 | *
|
---|
31 | * The information kept by the JosmUserIdentityManager can be used to
|
---|
32 | * <ul>
|
---|
33 | * <li>safely query changesets owned by the current user based on its user id, not on its user name</li>
|
---|
34 | * <li>safely search for objects last touched by the current user based on its user id, not on its user name</li>
|
---|
35 | * </ul>
|
---|
36 | * @since 2689 (creation)
|
---|
37 | * @deprecated to be removed end of 2017. Use {@link UserIdentityManager} instead
|
---|
38 | */
|
---|
39 | @Deprecated
|
---|
40 | public final class JosmUserIdentityManager {
|
---|
41 |
|
---|
42 | private static JosmUserIdentityManager instance;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Replies the unique instance of the JOSM user identity manager
|
---|
46 | *
|
---|
47 | * @return the unique instance of the JOSM user identity manager
|
---|
48 | */
|
---|
49 | public static synchronized JosmUserIdentityManager getInstance() {
|
---|
50 | if (instance == null) {
|
---|
51 | instance = new JosmUserIdentityManager();
|
---|
52 | UserIdentityManager.getInstance();
|
---|
53 | }
|
---|
54 | return instance;
|
---|
55 | }
|
---|
56 |
|
---|
57 | private JosmUserIdentityManager() {
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Remembers the fact that the current JOSM user is anonymous.
|
---|
62 | */
|
---|
63 | public void setAnonymous() {
|
---|
64 | UserIdentityManager.getInstance().setAnonymous();
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Remembers the fact that the current JOSM user is partially identified
|
---|
69 | * by the user name of its OSM account.
|
---|
70 | *
|
---|
71 | * @param userName the user name. Must not be null. Must not be empty (whitespace only).
|
---|
72 | * @throws IllegalArgumentException if userName is null
|
---|
73 | * @throws IllegalArgumentException if userName is empty
|
---|
74 | */
|
---|
75 | public void setPartiallyIdentified(String userName) {
|
---|
76 | UserIdentityManager.getInstance().setPartiallyIdentified(userName);
|
---|
77 | }
|
---|
78 |
|
---|
79 | /**
|
---|
80 | * Remembers the fact that the current JOSM user is fully identified with a
|
---|
81 | * verified pair of user name and user id.
|
---|
82 | *
|
---|
83 | * @param userName the user name. Must not be null. Must not be empty.
|
---|
84 | * @param userInfo additional information about the user, retrieved from the OSM server and including the user id
|
---|
85 | * @throws IllegalArgumentException if userName is null
|
---|
86 | * @throws IllegalArgumentException if userName is empty
|
---|
87 | * @throws IllegalArgumentException if userInfo is null
|
---|
88 | */
|
---|
89 | public void setFullyIdentified(String userName, UserInfo userInfo) {
|
---|
90 | UserIdentityManager.getInstance().setFullyIdentified(userName, userInfo);
|
---|
91 | }
|
---|
92 |
|
---|
93 | /**
|
---|
94 | * Replies true if the current JOSM user is anonymous.
|
---|
95 | *
|
---|
96 | * @return {@code true} if the current user is anonymous.
|
---|
97 | */
|
---|
98 | public boolean isAnonymous() {
|
---|
99 | return UserIdentityManager.getInstance().isAnonymous();
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Replies true if the current JOSM user is partially identified.
|
---|
104 | *
|
---|
105 | * @return true if the current JOSM user is partially identified.
|
---|
106 | */
|
---|
107 | public boolean isPartiallyIdentified() {
|
---|
108 | return UserIdentityManager.getInstance().isPartiallyIdentified();
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Replies true if the current JOSM user is fully identified.
|
---|
113 | *
|
---|
114 | * @return true if the current JOSM user is fully identified.
|
---|
115 | */
|
---|
116 | public boolean isFullyIdentified() {
|
---|
117 | return UserIdentityManager.getInstance().isFullyIdentified();
|
---|
118 | }
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * Replies the user name of the current JOSM user. null, if {@link #isAnonymous()} is true.
|
---|
122 | *
|
---|
123 | * @return the user name of the current JOSM user
|
---|
124 | */
|
---|
125 | public String getUserName() {
|
---|
126 | return UserIdentityManager.getInstance().getUserName();
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Replies the user id of the current JOSM user. 0, if {@link #isAnonymous()} or
|
---|
131 | * {@link #isPartiallyIdentified()} is true.
|
---|
132 | *
|
---|
133 | * @return the user id of the current JOSM user
|
---|
134 | */
|
---|
135 | public int getUserId() {
|
---|
136 | return UserIdentityManager.getInstance().getUserId();
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Replies verified additional information about the current user if the user is
|
---|
141 | * {@link #isFullyIdentified()}.
|
---|
142 | *
|
---|
143 | * @return verified additional information about the current user
|
---|
144 | */
|
---|
145 | public UserInfo getUserInfo() {
|
---|
146 | return UserIdentityManager.getInstance().getUserInfo();
|
---|
147 | }
|
---|
148 |
|
---|
149 | /**
|
---|
150 | * Returns the identity as a {@link User} object
|
---|
151 | *
|
---|
152 | * @return the identity as user, or {@link User#getAnonymous()} if {@link #isAnonymous()}
|
---|
153 | */
|
---|
154 | public User asUser() {
|
---|
155 | return UserIdentityManager.getInstance().asUser();
|
---|
156 | }
|
---|
157 |
|
---|
158 | /**
|
---|
159 | * Initializes the user identity manager from Basic Authentication values in the {@link org.openstreetmap.josm.data.Preferences}
|
---|
160 | * This method should be called if {@code osm-server.auth-method} is set to {@code basic}.
|
---|
161 | * @see #initFromOAuth
|
---|
162 | */
|
---|
163 | public void initFromPreferences() {
|
---|
164 | UserIdentityManager.getInstance().initFromPreferences();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Initializes the user identity manager from OAuth request of user details.
|
---|
169 | * This method should be called if {@code osm-server.auth-method} is set to {@code oauth}.
|
---|
170 | * @see #initFromPreferences
|
---|
171 | * @since 5434
|
---|
172 | */
|
---|
173 | public void initFromOAuth() {
|
---|
174 | UserIdentityManager.getInstance().initFromOAuth();
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Replies true if the user with name <code>username</code> is the current user
|
---|
179 | *
|
---|
180 | * @param username the user name
|
---|
181 | * @return true if the user with name <code>username</code> is the current user
|
---|
182 | */
|
---|
183 | public boolean isCurrentUser(String username) {
|
---|
184 | return UserIdentityManager.getInstance().isCurrentUser(username);
|
---|
185 | }
|
---|
186 |
|
---|
187 | /**
|
---|
188 | * Replies true if the current user is {@link #isFullyIdentified() fully identified} and the {@link #getUserId() user ids} match,
|
---|
189 | * or if the current user is not {@link #isFullyIdentified() fully identified} and the {@link #getUserName() user names} match.
|
---|
190 | *
|
---|
191 | * @param user the user to test
|
---|
192 | * @return true if given user is the current user
|
---|
193 | */
|
---|
194 | public boolean isCurrentUser(User user) {
|
---|
195 | return UserIdentityManager.getInstance().isCurrentUser(user);
|
---|
196 | }
|
---|
197 | }
|
---|