1 | package com.bowman.cardserv;
|
---|
2 |
|
---|
3 | import com.bowman.cardserv.interfaces.UserManager;
|
---|
4 | import com.bowman.cardserv.util.ProxyXmlConfig;
|
---|
5 |
|
---|
6 | import java.util.*;
|
---|
7 |
|
---|
8 | /**
|
---|
9 | * Created by IntelliJ IDEA.
|
---|
10 | * User: bowman
|
---|
11 | * Date: Oct 8, 2005
|
---|
12 | * Time: 5:48:16 PM
|
---|
13 | */
|
---|
14 | public class SimpleUserManager implements UserManager {
|
---|
15 |
|
---|
16 | protected Map users = new HashMap();
|
---|
17 | protected UserEntry defaultUser;
|
---|
18 |
|
---|
19 | protected String openPrefix, openPasswd;
|
---|
20 | protected Set openProfiles;
|
---|
21 |
|
---|
22 | public void configUpdated(ProxyXmlConfig xml) throws ConfigException {
|
---|
23 | users.clear();
|
---|
24 |
|
---|
25 | ProxyXmlConfig openConf = null;
|
---|
26 | try {
|
---|
27 | openConf = xml.getSubConfig("open-access");
|
---|
28 | } catch (ConfigException e) {}
|
---|
29 | if(openConf != null && "true".equalsIgnoreCase(openConf.getStringValue("enabled", "true"))) {
|
---|
30 | openPrefix = openConf.getStringValue("open-username-prefix", "");
|
---|
31 | openPasswd = openConf.getStringValue("open-password");
|
---|
32 | String profiles = openConf.getStringValue("open-profiles", "");
|
---|
33 | if(profiles.length() > 0) openProfiles = new HashSet(Arrays.asList(profiles.split(" ")));
|
---|
34 | else openProfiles = Collections.EMPTY_SET;
|
---|
35 | } else {
|
---|
36 | openPrefix = null;
|
---|
37 | openPasswd = null;
|
---|
38 | openProfiles = Collections.EMPTY_SET;
|
---|
39 | }
|
---|
40 |
|
---|
41 | Iterator iter = xml.getMultipleSubConfigs("user");
|
---|
42 | ProxyXmlConfig userConf;
|
---|
43 | try {
|
---|
44 | userConf = xml.getSubConfig("default-user");
|
---|
45 | defaultUser = parseUser(userConf);
|
---|
46 | } catch (ConfigException e) {
|
---|
47 | defaultUser = null;
|
---|
48 | }
|
---|
49 | while(iter.hasNext()) {
|
---|
50 | userConf = (ProxyXmlConfig)iter.next();
|
---|
51 | addUser(userConf);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | protected void addUser(ProxyXmlConfig xml) throws ConfigException {
|
---|
56 | addUser(parseUser(xml), users, false);
|
---|
57 | }
|
---|
58 |
|
---|
59 | protected void addUser(UserEntry user, Map users, boolean overwrite) throws ConfigException {
|
---|
60 | if(users.put(user.name.toLowerCase(), user) != null && !overwrite)
|
---|
61 | throw new ConfigException("Duplicate user definition: " + user.name);
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected UserEntry parseUser(ProxyXmlConfig xml) throws ConfigException {
|
---|
65 | String ipMask = xml.getStringValue("ip-mask", "*");
|
---|
66 |
|
---|
67 | String emailAddr = null;
|
---|
68 | try {
|
---|
69 | emailAddr = xml.getStringValue("email-address");
|
---|
70 | } catch (ConfigException e) {}
|
---|
71 |
|
---|
72 | String expireDate = null;
|
---|
73 | try {
|
---|
74 | expireDate = xml.getStringValue("expire-date");
|
---|
75 | } catch(ConfigException e) {
|
---|
76 | expireDate = null;
|
---|
77 | }
|
---|
78 |
|
---|
79 | String startDate = null;
|
---|
80 | try {
|
---|
81 | startDate = xml.getStringValue("start-date");
|
---|
82 | } catch(ConfigException e) {
|
---|
83 | startDate = null;
|
---|
84 | }
|
---|
85 |
|
---|
86 | int EcmRate = -1;
|
---|
87 | try {
|
---|
88 | EcmRate = xml.getIntValue("ecm-rate");
|
---|
89 | } catch(ConfigException e) {
|
---|
90 | EcmRate = -1;
|
---|
91 | }
|
---|
92 |
|
---|
93 | int maxConnections = xml.getIntValue("max-connections", -1);
|
---|
94 |
|
---|
95 | boolean enabled = "true".equalsIgnoreCase(xml.getStringValue("enabled", "true"));
|
---|
96 | boolean admin = "true".equalsIgnoreCase(xml.getStringValue("admin", "false"));
|
---|
97 | boolean exclude = "true".equalsIgnoreCase(xml.getStringValue("map-exclude", "false"));
|
---|
98 | boolean debug = "true".equalsIgnoreCase(xml.getStringValue("debug", "false"));
|
---|
99 | boolean spider = "true".equalsIgnoreCase(xml.getStringValue("spider", "true"));
|
---|
100 |
|
---|
101 | UserEntry user = new UserEntry(xml.getStringValue("name"), xml.getStringValue("password"), ipMask, emailAddr,
|
---|
102 | maxConnections, enabled, admin, exclude, debug, startDate, expireDate, EcmRate, spider);
|
---|
103 |
|
---|
104 | try {
|
---|
105 | user.displayName = xml.getStringValue("display-name");
|
---|
106 | } catch (ConfigException e) {}
|
---|
107 |
|
---|
108 | try {
|
---|
109 | String profiles = xml.getStringValue("profiles");
|
---|
110 | for(StringTokenizer st = new StringTokenizer(profiles); st.hasMoreTokens(); ) user.profiles.add(st.nextToken());
|
---|
111 | } catch (ConfigException e) {}
|
---|
112 |
|
---|
113 | return user;
|
---|
114 | }
|
---|
115 |
|
---|
116 | public String[] getUserNames() {
|
---|
117 | List userNames = new ArrayList(users.keySet());
|
---|
118 | Collections.sort(userNames);
|
---|
119 | return (String[])userNames.toArray(new String[userNames.size()]);
|
---|
120 | }
|
---|
121 |
|
---|
122 | protected UserEntry getUser(String name) {
|
---|
123 | if(name == null) return null;
|
---|
124 | else {
|
---|
125 | UserEntry user = (UserEntry)users.get(name.toLowerCase());
|
---|
126 | if(user != null) return user;
|
---|
127 | else return defaultUser;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | protected boolean matchesOpen(String user) {
|
---|
132 | if(openPrefix == null) return false;
|
---|
133 | if("".equals(openPrefix)) return true;
|
---|
134 | else return user.toLowerCase().startsWith(openPrefix.toLowerCase());
|
---|
135 | }
|
---|
136 |
|
---|
137 | public String getPassword(String user) {
|
---|
138 | UserEntry entry = getUser(user);
|
---|
139 | if(entry == null) {
|
---|
140 | if(matchesOpen(user)) return openPasswd;
|
---|
141 | else return null;
|
---|
142 | } else return entry.password;
|
---|
143 | }
|
---|
144 |
|
---|
145 | public String getUserName(String user) {
|
---|
146 | UserEntry entry = getUser(user);
|
---|
147 | if(entry == null) {
|
---|
148 | String suffix = Long.toString(System.currentTimeMillis(), Character.MAX_RADIX);
|
---|
149 | if("".equals(openPrefix)) return suffix;
|
---|
150 | else if(matchesOpen(user)) return openPrefix + suffix;
|
---|
151 | else return null;
|
---|
152 | } else if(entry == defaultUser) return user;
|
---|
153 | else return entry.name;
|
---|
154 | }
|
---|
155 |
|
---|
156 | public boolean authenticate(String user, String pass) {
|
---|
157 | UserEntry entry = getUser(user);
|
---|
158 | if(entry == null || entry == defaultUser) return false;
|
---|
159 | else return entry.password.equals(pass);
|
---|
160 | }
|
---|
161 |
|
---|
162 | public String getIpMask(String user) {
|
---|
163 | UserEntry entry = getUser(user);
|
---|
164 | if(entry == null) return "*";
|
---|
165 | else return entry.ipMask;
|
---|
166 | }
|
---|
167 |
|
---|
168 | public String getEmailAddress(String user) {
|
---|
169 | UserEntry entry = getUser(user);
|
---|
170 | if(entry == null) return null;
|
---|
171 | else return entry.email;
|
---|
172 | }
|
---|
173 |
|
---|
174 | public String getDisplayName(String user) {
|
---|
175 | UserEntry entry = getUser(user);
|
---|
176 | if(entry == null) {
|
---|
177 | if(matchesOpen(user)) return user;
|
---|
178 | else return null;
|
---|
179 | } else return entry.displayName;
|
---|
180 | }
|
---|
181 |
|
---|
182 | public int getMaxConnections(String user) {
|
---|
183 | UserEntry entry = getUser(user);
|
---|
184 | if(entry == null) return 1;
|
---|
185 | else return entry.maxConnections;
|
---|
186 | }
|
---|
187 |
|
---|
188 | public String getExpireDate(String user) {
|
---|
189 | UserEntry entry = getUser(user);
|
---|
190 | if(entry == null) return null;
|
---|
191 | else return entry.expireDate;
|
---|
192 | }
|
---|
193 |
|
---|
194 | public boolean isSpider(String user) {
|
---|
195 | UserEntry entry = getUser(user);
|
---|
196 | if(entry == null) return false;
|
---|
197 | else return entry.spider;
|
---|
198 | }
|
---|
199 |
|
---|
200 | public String getStartDate(String user) {
|
---|
201 | UserEntry entry = getUser(user);
|
---|
202 | if(entry == null) return null;
|
---|
203 | else return entry.startDate;
|
---|
204 | }
|
---|
205 |
|
---|
206 | public Set getAllowedProfiles(String user) {
|
---|
207 | UserEntry entry = getUser(user);
|
---|
208 | if(entry == null) {
|
---|
209 | if(matchesOpen(user)) return openProfiles;
|
---|
210 | else return Collections.EMPTY_SET;
|
---|
211 | }
|
---|
212 | else return entry.profiles;
|
---|
213 | }
|
---|
214 |
|
---|
215 | public boolean isEnabled(String user) {
|
---|
216 | UserEntry entry = getUser(user);
|
---|
217 | if(entry == null) return true;
|
---|
218 | else return entry.enabled;
|
---|
219 | }
|
---|
220 |
|
---|
221 | public boolean isAdmin(String user) {
|
---|
222 | UserEntry entry = getUser(user);
|
---|
223 | if(entry == null) return false;
|
---|
224 | else return entry.admin;
|
---|
225 | }
|
---|
226 |
|
---|
227 | public boolean exists(String user) {
|
---|
228 | return getPassword(user) != null;
|
---|
229 | }
|
---|
230 |
|
---|
231 | public boolean isMapExcluded(String user) {
|
---|
232 | UserEntry entry = getUser(user);
|
---|
233 | if(entry == null) return false;
|
---|
234 | else return entry.exclude;
|
---|
235 | }
|
---|
236 |
|
---|
237 | public boolean isDebug(String user) {
|
---|
238 | UserEntry entry = getUser(user);
|
---|
239 | if(entry == null) return false;
|
---|
240 | else return entry.debug;
|
---|
241 | }
|
---|
242 |
|
---|
243 | public void setDebug(String user, boolean debug) {
|
---|
244 | UserEntry entry = getUser(user);
|
---|
245 | if(entry != null) entry.debug = debug;
|
---|
246 | }
|
---|
247 |
|
---|
248 | public int getUserCount() {
|
---|
249 | return users.size();
|
---|
250 | }
|
---|
251 |
|
---|
252 | public void start() {
|
---|
253 | }
|
---|
254 |
|
---|
255 | // access control/limits
|
---|
256 | public Set getAllowedServices(String user, String profile) {
|
---|
257 | return null; // return Set of Integer, null for all
|
---|
258 | }
|
---|
259 |
|
---|
260 | public Set getBlockedServices(String user, String profile) {
|
---|
261 | return null; // return Set of Integer, null for all
|
---|
262 | }
|
---|
263 |
|
---|
264 | public Set getAllowedConnectors(String user) {
|
---|
265 | return null; // return Set of String, null for all
|
---|
266 | }
|
---|
267 |
|
---|
268 | public int getAllowedEcmRate(String user) {
|
---|
269 | UserEntry entry = getUser(user);
|
---|
270 | if(entry == null) return -1;
|
---|
271 | else return entry.EcmRate;
|
---|
272 | }
|
---|
273 |
|
---|
274 | static class UserEntry {
|
---|
275 |
|
---|
276 | String name, password;
|
---|
277 | String ipMask;
|
---|
278 | String email, displayName, expireDate, startDate;
|
---|
279 | int maxConnections, EcmRate;
|
---|
280 | boolean enabled, admin, exclude, debug, spider;
|
---|
281 | Set profiles = new HashSet();
|
---|
282 |
|
---|
283 | public UserEntry(String name, String password, String ipMask, String email, int maxConnections, boolean enabled,
|
---|
284 | boolean admin, boolean exclude, boolean debug, String startDate, String expireDate, int EcmRate, boolean spider) {
|
---|
285 | this.name = name;
|
---|
286 | this.displayName = name;
|
---|
287 | this.password = password;
|
---|
288 | this.ipMask = ipMask;
|
---|
289 | this.email = email;
|
---|
290 | this.maxConnections = maxConnections;
|
---|
291 | this.enabled = enabled;
|
---|
292 | this.admin = admin;
|
---|
293 | this.exclude = exclude;
|
---|
294 | this.debug = debug;
|
---|
295 | this.spider = spider;
|
---|
296 | this.expireDate = expireDate;
|
---|
297 | this.EcmRate = EcmRate;
|
---|
298 | this.startDate = startDate;
|
---|
299 | }
|
---|
300 | }
|
---|
301 | }
|
---|