001: package com.espada.bugtracker.app;
002:
003: // Java core API
004: import java.io.*;
005: import java.sql.*;
006: import java.util.*;
007:
008: // Espada API
009: import com.espada.*;
010:
011: // Espada API
012: import org.webmacro.*;
013: import javax.servlet.*;
014:
015: // DB Connection Pool API
016: import com.sr.espada.se.util.db.pool.*;
017: import com.sr.espada.se.util.logwriter.LogWriter;
018: import com.sr.espada.se.util.config.ConfigFactory;
019:
020: /**
021: * This is the User class.
022: *
023: * When an instance of User is created, the class searches the database for an<BR>
024: * existing user with the corresponding user id, and fills up the object properties.<BR>
025: *
026: * <b>Currently not implemented! I add users manually for now ... </B><BR>
027: * To create a new user in the database, the static method User.createUser() must be used.<BR>
028: *
029: *
030: * @version: $Id: User.java,v 1.9 2001/04/16 06:45:47 manik Exp $<BR>
031: * @author: Manik Surtani<BR>
032: **/
033:
034: public class User {
035:
036: public int uid;
037:
038: public int rid;
039:
040: public int pid;
041:
042: public String username;
043:
044: public String password;
045:
046: public String email;
047:
048: public boolean admin = false;
049:
050: public boolean found = false;
051:
052: public User(String un) {
053: try {
054:
055: Connection d = DatabaseConnectionPool.getConnection();
056: Statement st = d.createStatement();
057: ResultSet rs = st
058: .executeQuery("select * from user where username='"
059: + un + "'");
060:
061: while (rs.next()) {
062: uid = rs.getInt(1);
063:
064: username = rs.getString(2);
065:
066: password = rs.getString(3);
067:
068: email = rs.getString(4);
069:
070: admin = rs.getInt(5) == 0 ? false : true;
071:
072: found = true;
073:
074: }
075:
076: st.close();
077: DatabaseConnectionPool.freeConnection(d);
078:
079: }
080:
081: catch (Exception E) {
082:
083: found = false;
084: }
085:
086: } //end of method User
087:
088: public User(int uidn) {
089: try {
090:
091: Connection d = DatabaseConnectionPool.getConnection();
092: Statement st = d.createStatement();
093: ResultSet rs = st
094: .executeQuery("select * from user where uid="
095: + uidn);
096:
097: while (rs.next()) {
098: uid = rs.getInt(1);
099:
100: username = rs.getString(2);
101:
102: password = rs.getString(3);
103:
104: email = rs.getString(4);
105:
106: found = true;
107:
108: }
109:
110: st.close();
111: DatabaseConnectionPool.freeConnection(d);
112:
113: }
114:
115: catch (Exception E) {
116:
117: found = false;
118:
119: }
120:
121: } //end of method User
122:
123: public void makeAdmin() {
124: this .admin = true;
125: try {
126:
127: Connection d = DatabaseConnectionPool.getConnection();
128: Statement st = d.createStatement();
129: st.execute("update projectuser set rid=1 where uid=" + uid);
130: st.execute("insert into projectuser select pid, " + uid
131: + ", 1 from projects");
132: st.close();
133: DatabaseConnectionPool.freeConnection(d);
134:
135: } catch (Exception e) {
136: LogWriter.write("problem making " + uid + " site admin.");
137: }
138:
139: update();
140: }
141:
142: /**
143: *This method removes a user from a project identified by pid.
144: */
145: public void delUserProject(int pid) {
146: String SQL = "delete from projectuser where pid=" + pid
147: + " and uid=" + uid;
148: delete(SQL);
149: }
150:
151: public boolean delUserProject() {
152: String SQL = "delete from projectuser where pid=" + pid
153: + " and uid=" + uid;
154:
155: return delete(SQL);
156:
157: } //end of method delUserProject
158:
159: public boolean delUser() {
160:
161: String SQL = "delete from user where uid=" + uid;
162:
163: return delete(SQL);
164:
165: } //end of method delUser
166:
167: public boolean delete(String SQL) {
168: try {
169:
170: Connection d = DatabaseConnectionPool.getConnection();
171: Statement st = d.createStatement();
172: int rs = st.executeUpdate(SQL);
173: st.close();
174: DatabaseConnectionPool.freeConnection(d);
175: return (rs != 0);
176:
177: }
178:
179: catch (Exception E) {
180:
181: return false;
182:
183: }
184:
185: } //end of method delete
186:
187: public boolean checkPassword(String myPasswd) {
188:
189: return (myPasswd.compareTo(password) == 0);
190:
191: } //end of method checkPassword
192:
193: public boolean update() {
194: try {
195:
196: Connection d = DatabaseConnectionPool.getConnection();
197: Statement st = d.createStatement();
198: String SQL = "update user set username='" + username
199: + "', password='" + password + "', email='" + email
200: + "' where uid=" + uid;
201: int rs = st.executeUpdate(SQL);
202: st.close();
203: DatabaseConnectionPool.freeConnection(d);
204: return (rs != 0);
205:
206: }
207:
208: catch (Exception E) {
209:
210: return false;
211:
212: }
213:
214: } //end of method update
215:
216: /**
217: * Returns 0 on success.
218: * 1 if unable to create user record.
219: * 2 if unable to assign projects.
220: * 3 if database connection/SQL error.
221: */
222:
223: public static int createUser(String name, String password,
224: String email) {
225:
226: int res = 0;
227: boolean cont = true;
228:
229: try {
230: Connection d = DatabaseConnectionPool.getConnection();
231: Statement st = d.createStatement();
232: ResultSet tempRS = st
233: .executeQuery("select count(uid) from user where username='"
234: + name + "';");
235:
236: if (tempRS.next()) {
237: res = (tempRS.getInt(1) == 0) ? 0 : 1; // username exists!!
238: cont = (res == 0);
239: }
240:
241: if (cont) {
242: int rs = st
243: .executeUpdate("insert into user values (0, '"
244: + name + "', '" + password + "', '"
245: + email + "', 0)");
246:
247: if (rs == 0) {
248: res = 1;// unable to create user record.
249: }
250:
251: cont = (res == 0);
252: }
253:
254: /*if( cont )
255: {
256: ResultSet rs1 = st.executeQuery("select uid from user where username='" + name + "'");
257:
258: if( rs1.next() )
259: {
260: int rs2 = st.executeUpdate("insert into projectuser select pid, " + rs1.getInt(1) + ", 4 from projects");
261: }
262: else
263: {
264: res = 2; // unable to assign any projects.
265: }
266: } */
267:
268: st.close();
269: DatabaseConnectionPool.freeConnection(d);
270: return (res);
271:
272: }
273:
274: catch (Exception E) {
275: res = 3;
276:
277: LogWriter.write("User.createUser()\n");
278: LogWriter.write(" FALSE\n");
279: LogWriter.write(" " + E.getMessage() + "\n");
280:
281: return res;
282:
283: }
284:
285: } //end of method createUser
286:
287: public static String forgotPassword(String eml, ServletContext sc) {
288:
289: String emailMessage = null;
290:
291: try {
292:
293: Connection d = DatabaseConnectionPool.getConnection();
294: Statement st = d.createStatement();
295: ResultSet rs = st
296: .executeQuery("select username, password from user where email='"
297: + eml + "';");
298:
299: if (rs.next()) {
300: /******************************************
301: This webmacro bit needs SERIOUS debugging!!
302: *******************************************
303: try
304: {
305: WebMacro _wm = new WM();
306: LogWriter.write("WM: " + _wm);
307: Context c = _wm.getContext();
308: LogWriter.write("Ctx: " + c);
309: Template t = _wm.getTemplate(
310: sc.getRealPath("templates/" +
311: ConfigFactory.getInstance().getValue("templates", "forgotPasswordPlain")
312: ) );
313: LogWriter.write("Tpl: " + t);
314: c.put("userName", rs.getString(1) );
315: c.put("password", rs.getString(2) );
316: emailMessage = (String) t.evaluate(c);
317: }
318: catch (Exception wme)
319: {
320: emailMessage = wme.getMessage();
321: LogWriter.write("User.forgotPassword(): WM Exception: " + emailMessage );
322:
323: }
324: **********************************
325: end Webmacro bit
326: *********************************/
327: // Temporary hardcoded text solution. :(
328: emailMessage = "Hello.\n\nYour login details at BUGTRACKER is as follows:\nUsername: "
329: + rs.getString(1)
330: + "\nPassword: "
331: + rs.getString(2) + "\n\nThank you.";
332:
333: }
334:
335: st.close();
336: DatabaseConnectionPool.freeConnection(d);
337: return emailMessage;
338: }
339:
340: catch (Exception E) {
341: LogWriter.write("User.forgotPassword(): " + E.getMessage());
342: return null;
343: }
344:
345: } //end of method forgotPassword
346:
347: public static Vector getUsers() {
348:
349: Vector v = new Vector();
350:
351: try {
352:
353: Connection d = DatabaseConnectionPool.getConnection();
354: Statement st = d.createStatement();
355: ResultSet rs = st.executeQuery("select uid from user");
356:
357: while (rs.next()) {
358:
359: v.add(new User(rs.getInt(1)));
360:
361: }
362:
363: st.close();
364: DatabaseConnectionPool.freeConnection(d);
365: return v;
366:
367: }
368:
369: catch (Exception E) {
370:
371: LogWriter.write("User.getUsers(): " + E.getMessage());
372: return v;
373:
374: }
375: } //end of method getUsers
376:
377: public boolean isAdmin() {
378: boolean i = false;
379: try {
380:
381: Connection d = DatabaseConnectionPool.getConnection();
382: Statement st = d.createStatement();
383: ResultSet rs = st
384: .executeQuery("select admin from user where uid="
385: + uid);
386: rs.next();
387: i = rs.getBoolean(1);
388: st.close();
389: DatabaseConnectionPool.freeConnection(d);
390:
391: } catch (Exception e) {
392: LogWriter.write(e.getMessage());
393: }
394: return i;
395: }
396:
397: public int getMostProminentRole() {
398: int i = 0;
399: try {
400:
401: Connection d = DatabaseConnectionPool.getConnection();
402: Statement st = d.createStatement();
403: ResultSet rs = st
404: .executeQuery("select min(rid) from projectuser where uid="
405: + uid);
406: rs.next();
407: i = rs.getInt(1);
408: st.close();
409: DatabaseConnectionPool.freeConnection(d);
410:
411: } catch (Exception e) {
412: LogWriter.write(e.getMessage());
413: }
414: return i;
415:
416: }
417:
418: /**
419: *This method will get a listing of users in a particular project identified by proId.
420: */
421:
422: public static Vector getUserRolesByProject(int proId) {
423: Vector v = new Vector();
424:
425: try {
426:
427: Connection d = DatabaseConnectionPool.getConnection();
428: Statement st = d.createStatement();
429: ResultSet rs = st
430: .executeQuery("select uid, rid, pid from projectuser where pid="
431: + proId);
432: User prouser = null;
433:
434: while (rs.next()) {
435: prouser = new User(rs.getInt(1));
436:
437: prouser.rid = rs.getInt(2);
438:
439: prouser.pid = rs.getInt(3);
440:
441: v.add(prouser);
442:
443: }
444:
445: st.close();
446: DatabaseConnectionPool.freeConnection(d);
447:
448: }
449:
450: catch (Exception E) {
451:
452: LogWriter.write("User Roles " + E.getMessage());
453:
454: }
455:
456: return v;
457:
458: } //end of method getUserRolesByProject
459:
460: /**
461: *This method returns the role a user plays in a particular project identified by pid.
462: */
463: public Roles getRoleInProject(int pid) {
464: Roles r = null;
465: try {
466: Connection d = DatabaseConnectionPool.getConnection();
467: Statement st = d.createStatement();
468: ResultSet rs = st
469: .executeQuery("select rid from projectuser where pid="
470: + pid + " and uid=" + uid);
471: if (rs.next()) {
472: r = new Roles(rs.getInt(1));
473: }
474: st.close();
475: DatabaseConnectionPool.freeConnection(d);
476: }
477:
478: catch (Exception E) {
479:
480: LogWriter.write("Error getting a role for project " + pid
481: + ": " + E.getMessage());
482:
483: }
484: return r;
485: }
486:
487: /**
488: *This method returns a vector of users NOT in a project identified by pid.
489: *
490: *@author Kishan Pieris
491: *@author Manik Surtani (since v0.2beta2)
492: */
493:
494: public static Vector getUsersNotInProject(int pid) {
495:
496: // ok, a fiendishly messy way to do this - but I cant think of a better one right now
497: // given MySQL's current limitations with subqueries. - Manik
498:
499: Vector v = new Vector();
500: try {
501: Connection d = DatabaseConnectionPool.getConnection();
502: Statement st = d.createStatement();
503: int uidn = 0;
504: ResultSet rs = st
505: .executeQuery("select uid from projectuser where pid="
506: + pid);
507: StringBuffer sql = new StringBuffer(
508: "select user.uid from user where user.uid not in (");
509: while (rs.next()) {
510: sql.append(rs.getString(1));
511: sql.append(",");
512: }
513: sql.setCharAt(sql.length() - 1, ')');
514:
515: LogWriter.write(sql.toString());
516: rs = st.executeQuery(sql.toString());
517: while (rs.next()) {
518: User gotuser = new User(rs.getInt(1));
519: v.add(gotuser);
520: }
521:
522: st.close();
523: DatabaseConnectionPool.freeConnection(d);
524: }
525:
526: catch (Exception E) {
527:
528: LogWriter.write("User Roles " + E.getMessage());
529:
530: }
531:
532: return v;
533:
534: } //end of method getUsersNotInProject
535:
536: public static void addUsersToProjectsRoles(int pid, int uid, int rid) {
537:
538: try {
539:
540: Connection d = DatabaseConnectionPool.getConnection();
541:
542: String SQL = "insert into projectuser values ( " + pid
543: + " , " + uid + " , " + rid + " )";
544:
545: Statement st = d.createStatement();
546:
547: st.executeQuery(SQL);
548:
549: st.close();
550:
551: DatabaseConnectionPool.freeConnection(d);
552:
553: }
554:
555: catch (Exception E) {
556:
557: LogWriter
558: .write("Error: Can not add user " + E.getMessage());
559:
560: }
561:
562: } //end of method addUserRoleProjects
563:
564: } //end of
|