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 | int maxConnections = xml.getIntValue("max-connections", -1);
|
---|
73 |
|
---|
74 | boolean enabled = "true".equalsIgnoreCase(xml.getStringValue("enabled", "true"));
|
---|
75 | boolean admin = "true".equalsIgnoreCase(xml.getStringValue("admin", "false"));
|
---|
76 | boolean exclude = "true".equalsIgnoreCase(xml.getStringValue("map-exclude", "false"));
|
---|
77 | boolean debug = "true".equalsIgnoreCase(xml.getStringValue("debug", "false"));
|
---|
78 |
|
---|
79 | UserEntry user = new UserEntry(xml.getStringValue("name"), xml.getStringValue("password"), ipMask, emailAddr,
|
---|
80 | maxConnections, enabled, admin, exclude, debug);
|
---|
81 |
|
---|
82 | try {
|
---|
83 | user.displayName = xml.getStringValue("display-name");
|
---|
84 | } catch (ConfigException e) {}
|
---|
85 |
|
---|
86 | try {
|
---|
87 | String profiles = xml.getStringValue("profiles");
|
---|
88 | for(StringTokenizer st = new StringTokenizer(profiles); st.hasMoreTokens(); ) user.profiles.add(st.nextToken());
|
---|
89 | } catch (ConfigException e) {}
|
---|
90 |
|
---|
91 | return user;
|
---|
92 | }
|
---|
93 |
|
---|
94 | public String[] getUserNames() {
|
---|
95 | List userNames = new ArrayList(users.keySet());
|
---|
96 | Collections.sort(userNames);
|
---|
97 | return (String[])userNames.toArray(new String[userNames.size()]);
|
---|
98 | }
|
---|
99 |
|
---|
100 | protected UserEntry getUser(String name) {
|
---|
101 | if(name == null) return null;
|
---|
102 | else {
|
---|
103 | UserEntry user = (UserEntry)users.get(name.toLowerCase());
|
---|
104 | if(user != null) return user;
|
---|
105 | else return defaultUser;
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | protected boolean matchesOpen(String user) {
|
---|
110 | if(openPrefix == null) return false;
|
---|
111 | if("".equals(openPrefix)) return true;
|
---|
112 | else return user.toLowerCase().startsWith(openPrefix.toLowerCase());
|
---|
113 | }
|
---|
114 |
|
---|
115 | public String getPassword(String user) {
|
---|
116 | UserEntry entry = getUser(user);
|
---|
117 | if(entry == null) {
|
---|
118 | if(matchesOpen(user)) return openPasswd;
|
---|
119 | else return null;
|
---|
120 | } else return entry.password;
|
---|
121 | }
|
---|
122 |
|
---|
123 | public String getUserName(String user) {
|
---|
124 | UserEntry entry = getUser(user);
|
---|
125 | if(entry == null) {
|
---|
126 | String suffix = Long.toString(System.currentTimeMillis(), Character.MAX_RADIX);
|
---|
127 | if("".equals(openPrefix)) return suffix;
|
---|
128 | else if(matchesOpen(user)) return openPrefix + suffix;
|
---|
129 | else return null;
|
---|
130 | } else if(entry == defaultUser) return user;
|
---|
131 | else return entry.name;
|
---|
132 | }
|
---|
133 |
|
---|
134 | public boolean authenticate(String user, String pass) {
|
---|
135 | UserEntry entry = getUser(user);
|
---|
136 | if(entry == null || entry == defaultUser) return false;
|
---|
137 | else return entry.password.equals(pass);
|
---|
138 | }
|
---|
139 |
|
---|
140 | public String getIpMask(String user) {
|
---|
141 | UserEntry entry = getUser(user);
|
---|
142 | if(entry == null) return "*";
|
---|
143 | else return entry.ipMask;
|
---|
144 | }
|
---|
145 |
|
---|
146 | public String getEmailAddress(String user) {
|
---|
147 | UserEntry entry = getUser(user);
|
---|
148 | if(entry == null) return null;
|
---|
149 | else return entry.email;
|
---|
150 | }
|
---|
151 |
|
---|
152 | public String getDisplayName(String user) {
|
---|
153 | UserEntry entry = getUser(user);
|
---|
154 | if(entry == null) {
|
---|
155 | if(matchesOpen(user)) return user;
|
---|
156 | else return null;
|
---|
157 | } else return entry.displayName;
|
---|
158 | }
|
---|
159 |
|
---|
160 | public int getMaxConnections(String user) {
|
---|
161 | UserEntry entry = getUser(user);
|
---|
162 | if(entry == null) return 1;
|
---|
163 | else return entry.maxConnections;
|
---|
164 | }
|
---|
165 |
|
---|
166 | public Set getAllowedProfiles(String user) {
|
---|
167 | UserEntry entry = getUser(user);
|
---|
168 | if(entry == null) {
|
---|
169 | if(matchesOpen(user)) return openProfiles;
|
---|
170 | else return Collections.EMPTY_SET;
|
---|
171 | }
|
---|
172 | else return entry.profiles;
|
---|
173 | }
|
---|
174 |
|
---|
175 | public boolean isEnabled(String user) {
|
---|
176 | UserEntry entry = getUser(user);
|
---|
177 | if(entry == null) return true;
|
---|
178 | else return entry.enabled;
|
---|
179 | }
|
---|
180 |
|
---|
181 | public boolean isAdmin(String user) {
|
---|
182 | UserEntry entry = getUser(user);
|
---|
183 | if(entry == null) return false;
|
---|
184 | else return entry.admin;
|
---|
185 | }
|
---|
186 |
|
---|
187 | public boolean exists(String user) {
|
---|
188 | return getPassword(user) != null;
|
---|
189 | }
|
---|
190 |
|
---|
191 | public boolean isMapExcluded(String user) {
|
---|
192 | UserEntry entry = getUser(user);
|
---|
193 | if(entry == null) return false;
|
---|
194 | else return entry.exclude;
|
---|
195 | }
|
---|
196 |
|
---|
197 | public boolean isDebug(String user) {
|
---|
198 | UserEntry entry = getUser(user);
|
---|
199 | if(entry == null) return false;
|
---|
200 | else return entry.debug;
|
---|
201 | }
|
---|
202 |
|
---|
203 | public void setDebug(String user, boolean debug) {
|
---|
204 | UserEntry entry = getUser(user);
|
---|
205 | if(entry != null) entry.debug = debug;
|
---|
206 | }
|
---|
207 |
|
---|
208 | public int getUserCount() {
|
---|
209 | return users.size();
|
---|
210 | }
|
---|
211 |
|
---|
212 | public void start() {
|
---|
213 | }
|
---|
214 |
|
---|
215 | // access control/limits
|
---|
216 | public Set getAllowedServices(String user, String profile) {
|
---|
217 | return null; // return Set of Integer, null for all
|
---|
218 | }
|
---|
219 |
|
---|
220 | public Set getBlockedServices(String user, String profile) {
|
---|
221 | return null; // return Set of Integer, null for all
|
---|
222 | }
|
---|
223 |
|
---|
224 | public Set getAllowedConnectors(String user) {
|
---|
225 | return null; // return Set of String, null for all
|
---|
226 | }
|
---|
227 |
|
---|
228 | public int getAllowedEcmRate(String user) {
|
---|
229 | return -1; // return minimum interval between ecm in seconds, -1 for no limit
|
---|
230 | }
|
---|
231 |
|
---|
232 | static class UserEntry {
|
---|
233 |
|
---|
234 | String name, password;
|
---|
235 | String ipMask;
|
---|
236 | String email, displayName;
|
---|
237 | int maxConnections;
|
---|
238 | boolean enabled, admin, exclude, debug;
|
---|
239 | Set profiles = new HashSet();
|
---|
240 |
|
---|
241 | public UserEntry(String name, String password, String ipMask, String email, int maxConnections, boolean enabled,
|
---|
242 | boolean admin, boolean exclude, boolean debug)
|
---|
243 | {
|
---|
244 | this.name = name;
|
---|
245 | this.displayName = name;
|
---|
246 | this.password = password;
|
---|
247 | this.ipMask = ipMask;
|
---|
248 | this.email = email;
|
---|
249 | this.maxConnections = maxConnections;
|
---|
250 | this.enabled = enabled;
|
---|
251 | this.admin = admin;
|
---|
252 | this.exclude = exclude;
|
---|
253 | this.debug = debug;
|
---|
254 | }
|
---|
255 |
|
---|
256 | }
|
---|
257 |
|
---|
258 | }
|
---|