1
15
16 package gate.security;
17
18 import java.sql.*;
19 import java.util.List;
20 import java.util.Vector;
21
22 import junit.framework.Assert;
23
24 import gate.Gate;
25 import gate.event.*;
26 import gate.persist.DBHelper;
27 import gate.persist.PersistenceException;
28 import gate.util.MethodNotImplementedException;
29
30
31 public class UserImpl
32 implements User, ObjectModificationListener {
33
34
35 private Long id;
36
37
38 private String name;
39
40
41 private List groups;
42
43
45 private Connection conn;
46
47
48 private int dbType;
49
50
51 private AccessControllerImpl ac;
52
53
55 private Vector omModificationListeners;
56
57
59 private Vector omCreationListeners;
60
61
63 private Vector omDeletionListeners;
64
65
66
67 public UserImpl(Long id, String name, List groups,AccessControllerImpl ac,Connection conn) {
68
69 this.id = id;
70 this.name = name;
71 this.groups = groups;
72 this.ac = ac;
73 this.conn = conn;
74
75 try {
76 String jdbcURL = conn.getMetaData().getURL();
77 this.dbType = DBHelper.getDatabaseType(jdbcURL);
78 Assert.assertTrue(this.dbType == DBHelper.ORACLE_DB ||
79 this.dbType == DBHelper.POSTGRES_DB);
80 }
81 catch(SQLException sqex) {
82 sqex.printStackTrace();
83 }
84
85 this.omModificationListeners = new Vector();
86 this.omCreationListeners = new Vector();
87 this.omDeletionListeners = new Vector();
88
89 this.ac.registerObjectModificationListener(
94 this,
95 ObjectModificationEvent.OBJECT_DELETED);
96
97 }
98
99
100
101
102
106
107 public Long getID() {
108
109 return id;
110 }
111
112
115 public String getName() {
116
117 return name;
118 }
119
120
122 public List getGroups() {
123
124
126 Vector copy = new Vector();
127 copy.addAll(this.groups);
128 return copy;
129 }
130
131
136 public void setName(String newName, Session s)
137 throws PersistenceException,SecurityException {
138
139 if (this.ac.isValidSession(s) == false) {
141 throw new SecurityException("invalid session supplied");
142 }
143
144 if (s.getID() != this.id && false == s.isPrivilegedSession()) {
146 throw new SecurityException("insufficient privileges");
147 }
148
149 CallableStatement stmt = null;
150 PreparedStatement pstmt = null;
151
152
154 if (this.dbType == DBHelper.ORACLE_DB) {
156 try {
157 stmt = this.conn.prepareCall(
158 "{ call "+Gate.DB_OWNER+".security.set_user_name(?,?)} ");
159 stmt.setLong(1,this.id.longValue());
160 stmt.setString(2,newName);
161 stmt.execute();
162 }
163 catch(SQLException sqle) {
164 throw new PersistenceException("can't change user name in DB: ["+ sqle.getMessage()+"]");
165 }
166 finally {
167 DBHelper.cleanup(stmt);
168 }
169 }
170
171 else if (this.dbType == DBHelper.POSTGRES_DB) {
172 try {
173 String sql = "select security_set_user_name(?,?)";
174 pstmt = this.conn.prepareStatement(sql);
175 pstmt.setLong(1,this.id.longValue());
176 pstmt.setString(2,newName);
177 pstmt.execute();
178 }
179 catch(SQLException sqle) {
180 throw new PersistenceException("can't change user name in DB: ["+ sqle.getMessage()+"]");
181 }
182 finally {
183 DBHelper.cleanup(pstmt);
184 }
185 }
186
187 else {
188 throw new IllegalArgumentException();
189 }
190
191 ObjectModificationEvent e = new ObjectModificationEvent(
193 this,
194 ObjectModificationEvent.OBJECT_MODIFIED,
195 User.OBJECT_CHANGE_NAME);
196
197 this.name = newName;
199
200 fireObjectModifiedEvent(e);
202 }
203
204
205
208 public void setPassword(String newPass, Session s)
209 throws PersistenceException,SecurityException {
210
211 if (this.ac.isValidSession(s) == false) {
213 throw new SecurityException("invalid session supplied");
214 }
215
216 if (false == s.isPrivilegedSession() && s.getID() != this.id) {
218 throw new SecurityException("insuffieicent privileges");
219 }
220
221 CallableStatement stmt = null;
222 PreparedStatement pstmt = null;
223
224 if (this.dbType == DBHelper.ORACLE_DB) {
226 try {
227 stmt = this.conn.prepareCall(
228 "{ call "+Gate.DB_OWNER+".security.set_user_password(?,?)} ");
229 stmt.setLong(1,this.id.longValue());
230 stmt.setString(2,newPass);
231 stmt.execute();
232 }
234 catch(SQLException sqle) {
235 throw new PersistenceException("can't change user password in DB: ["+ sqle.getMessage()+"]");
236 }
237 finally {
238 DBHelper.cleanup(stmt);
239 }
240 }
241
242 else if (this.dbType == DBHelper.POSTGRES_DB) {
243 try {
244 String sql = "select security_set_user_password(?,?)";
245 pstmt = this.conn.prepareStatement(sql);
246 pstmt.setLong(1,this.id.longValue());
247 pstmt.setString(2,newPass);
248 pstmt.execute();
249 }
251 catch(SQLException sqle) {
252 throw new PersistenceException("can't change user password in DB: ["+ sqle.getMessage()+"]");
253 }
254 finally {
255 DBHelper.cleanup(pstmt);
256 }
257 }
258
259 else {
260 throw new IllegalArgumentException();
261 }
262
263 }
264
265
274 public boolean equals(Object obj)
275 {
276 Assert.assertTrue(obj instanceof User);
277
278 User usr2 = (User)obj;
279
280 return (this.id.equals(usr2.getID()));
281 }
282
283
288 public void registerObjectModificationListener(ObjectModificationListener l,
289 int eventType) {
290
291 if (eventType != ObjectModificationEvent.OBJECT_DELETED &&
292 eventType != ObjectModificationEvent.OBJECT_MODIFIED) {
293
294 throw new IllegalArgumentException();
295 }
296
297 switch(eventType) {
298 case ObjectModificationEvent.OBJECT_CREATED :
299 this.omCreationListeners.add(l);
300 break;
301 case ObjectModificationEvent.OBJECT_DELETED :
302 this.omDeletionListeners.add(l);
303 break;
304 case ObjectModificationEvent.OBJECT_MODIFIED :
305 this.omModificationListeners.add(l);
306 break;
307 default:
308 Assert.fail();
309 }
310
311 }
312
313
318 public void unregisterObjectModificationListener(ObjectModificationListener l,
319 int eventType) {
320
321 if (eventType != ObjectModificationEvent.OBJECT_DELETED &&
322 eventType != ObjectModificationEvent.OBJECT_MODIFIED) {
323
324 throw new IllegalArgumentException();
325 }
326
327 switch(eventType) {
328 case ObjectModificationEvent.OBJECT_CREATED :
329 this.omCreationListeners.remove(l);
330 break;
331 case ObjectModificationEvent.OBJECT_DELETED :
332 this.omDeletionListeners.remove(l);
333 break;
334 case ObjectModificationEvent.OBJECT_MODIFIED :
335 this.omModificationListeners.remove(l);
336 break;
337 default:
338 Assert.fail();
339 }
340 }
341
342
344 private void fireObjectModifiedEvent(ObjectModificationEvent e) {
345
346 if (e.getType() != ObjectModificationEvent.OBJECT_MODIFIED) {
348 throw new IllegalArgumentException();
349 }
350
351 for (int i=0; i< this.omModificationListeners.size(); i++) {
352 ((ObjectModificationListener)omModificationListeners.elementAt(i)).objectModified(e);
353 }
354 }
355
356
358
361 public void objectCreated(ObjectModificationEvent e) {
362 return;
364 }
365
366
373 public void objectModified(ObjectModificationEvent e) {
374
375 Assert.assertTrue(e.getSubType() == Group.OBJECT_CHANGE_ADDUSER ||
377 e.getSubType() == Group.OBJECT_CHANGE_REMOVEUSER ||
378 e.getSubType() == Group.OBJECT_CHANGE_NAME);
379
380 Group grp = (Group)e.getSource();
382
383 switch(e.getSubType()) {
384
385 case Group.OBJECT_CHANGE_ADDUSER:
386
387 Assert.assertTrue(false == this.groups.contains(grp));
389 Assert.assertTrue(grp instanceof Group);
391 this.groups.add(grp);
393 ;
396 break;
397
398 case Group.OBJECT_CHANGE_REMOVEUSER:
399 Assert.assertTrue(true == this.groups.contains(grp));
401 this.groups.remove(grp);
403 ;
406 break;
407
408 case Group.OBJECT_CHANGE_NAME:
409 break;
411
412 default:
413 throw new IllegalArgumentException();
414 }
415
416
417 }
418
419
426 public void objectDeleted(ObjectModificationEvent e) {
427
428 if (e.getSource() instanceof Group) {
429
430 Group grp = (Group)e.getSource();
431 if (true == this.groups.contains(grp)) {
433 this.groups.remove(grp);
434 }
435
436 }
437 }
438
439
440 public void processGateEvent(GateEvent e){
441 throw new MethodNotImplementedException();
442 }
443
444
445 void setGroups(Vector groupIDs) {
446
447 for (int i=0; i< groupIDs.size(); i++) {
448 Long grp_id = (Long)groupIDs.elementAt(i);
449 Group grp = null;
450
451 try {
452 grp = (Group)this.ac.findGroup(grp_id);
453 }
454 catch(SecurityException se) {
455 Assert.fail();
456 }
457 catch(PersistenceException se) {
458 Assert.fail();
459 }
460
461 Assert.assertNotNull(grp);
463 Assert.assertTrue(grp instanceof Group);
464 this.groups.add(grp);
466 }
467 }
468
469
470 }
471