1 2 package com.espada.bugtracker.app; 3 4 import java.io.*; 6 import java.sql.*; 7 import java.util.*; 8 9 import com.espada.*; 11 12 import org.webmacro.*; 14 import javax.servlet.*; 15 16 import com.espada.bugtracker.persistence.*; 18 19 20 33 34 public class User 35 { 36 37 public int uid; 38 39 public int rid; 40 41 public int pid; 42 43 public String username; 44 45 public String password; 46 47 public String email; 48 49 public boolean admin=false; 50 51 public boolean found=false; 52 53 public User(String un) 54 { 55 try 56 { 57 58 Connection d = DatabaseConnectionPool.getConnection(); 59 Statement st = d.createStatement(); 60 ResultSet rs = st.executeQuery("select * from user where username='" + un + "'"); 61 62 while (rs.next()) 63 { 64 uid = rs.getInt(1); 65 66 username = rs.getString(2); 67 68 password = rs.getString(3); 69 70 email = rs.getString(4); 71 72 admin = rs.getInt(5)==0?false:true; 73 74 found = true; 75 76 } 77 78 st.close(); 79 DatabaseConnectionPool.freeConnection(d); 80 81 } 82 83 catch (Exception E) 84 { 85 86 found = false; 87 } 88 89 } 91 92 public User(int uidn) 93 { 94 try 95 { 96 97 Connection d = DatabaseConnectionPool.getConnection(); 98 Statement st = d.createStatement(); 99 ResultSet rs = st.executeQuery("select * from user where uid=" + uidn); 100 101 while (rs.next()) 102 { 103 uid = rs.getInt(1); 104 105 username = rs.getString(2); 106 107 password = rs.getString(3); 108 109 email = rs.getString(4); 110 111 found = true; 112 113 } 114 115 st.close(); 116 DatabaseConnectionPool.freeConnection(d); 117 118 } 119 120 catch (Exception E) 121 { 122 123 found = false; 124 125 } 126 127 } 129 public void makeAdmin() 130 { 131 this.admin = true; 132 try 133 { 134 135 Connection d = DatabaseConnectionPool.getConnection(); 136 Statement st = d.createStatement(); 137 st.execute("update projectuser set rid=1 where uid=" + uid); 138 st.execute("insert into projectuser select pid, " + uid + ", 1 from projects"); 139 st.close(); 140 DatabaseConnectionPool.freeConnection(d); 141 142 } catch (Exception e) { } 143 144 update(); 145 } 146 147 150 public void delUserProject(int pid) 151 { 152 String SQL = "delete from projectuser where pid=" + pid + " and uid=" + uid; 153 delete(SQL); 154 } 155 156 public boolean delUserProject() 157 { 158 String SQL = "delete from projectuser where pid=" + pid + " and uid=" + uid; 159 160 return delete(SQL); 161 162 163 } 165 166 public boolean delUser() 167 { 168 169 String SQL = "delete from user where uid=" + uid; 170 171 return delete(SQL); 172 173 } 175 176 177 public boolean delete(String SQL) 178 { 179 try 180 { 181 182 Connection d = DatabaseConnectionPool.getConnection(); 183 Statement st = d.createStatement(); 184 int rs = st.executeUpdate(SQL); 185 st.close(); 186 DatabaseConnectionPool.freeConnection(d); 187 return (rs != 0); 188 189 } 190 191 catch (Exception E) 192 { 193 194 return false; 195 196 } 197 198 } 200 201 public boolean checkPassword(String myPasswd) 202 { 203 204 return (myPasswd.compareTo(password)==0); 205 206 } 208 209 public boolean update() 210 { 211 try 212 { 213 214 Connection d = DatabaseConnectionPool.getConnection(); 215 Statement st = d.createStatement(); 216 String SQL = "update user set username='" + username + "', password='" + password + "', email='" + email + "' where uid=" + uid; 217 int rs = st.executeUpdate(SQL); 218 st.close(); 219 DatabaseConnectionPool.freeConnection(d); 220 return (rs != 0); 221 222 } 223 224 catch (Exception E) 225 { 226 227 return false; 228 229 } 230 231 } 233 234 240 241 public static int createUser(String name, String password, String email) 242 { 243 244 int res = 0; 245 boolean cont = true; 246 247 try 248 { 249 Connection d = DatabaseConnectionPool.getConnection(); 250 Statement st = d.createStatement(); 251 ResultSet tempRS = st.executeQuery("select count(uid) from user where username='" + name + "';"); 252 253 if( tempRS.next() ) 254 { 255 res = ( tempRS.getInt(1) == 0 )?0:1; cont = ( res == 0 ); 257 } 258 259 if( cont ) 260 { 261 int rs = st.executeUpdate("insert into user values (0, '" + name + "', '" + password + "', '" + email + "', 0)"); 262 263 if( rs == 0 ) 264 { 265 res=1; } 267 268 cont = ( res == 0 ); 269 } 270 271 284 285 st.close(); 286 DatabaseConnectionPool.freeConnection(d); 287 return (res); 288 289 } 290 291 catch (Exception E) 292 { 293 res = 3; 294 295 return res; 296 297 } 298 299 } 301 public static String forgotPassword(String eml, ServletContext sc) 302 { 303 304 String emailMessage = null; 305 306 try 307 { 308 309 Connection d = DatabaseConnectionPool.getConnection(); 310 Statement st = d.createStatement(); 311 ResultSet rs = st.executeQuery("select username, password from user where email='" + eml + "';"); 312 313 if( rs.next() ) 314 { 315 338 340 emailMessage = "Hello.\n\nYour login details at BUGTRACKER is as follows:\nUsername: " + rs.getString(1) + "\nPassword: " + rs.getString(2) + "\n\nThank you."; 341 342 } 343 344 st.close(); 345 DatabaseConnectionPool.freeConnection(d); 346 return emailMessage; 347 } 348 349 catch (Exception E) 350 { 351 return null; 352 } 353 354 } 356 357 public static Vector getUsers() 358 { 359 360 Vector v = new Vector(); 361 362 try 363 { 364 365 Connection d = DatabaseConnectionPool.getConnection(); 366 Statement st = d.createStatement(); 367 ResultSet rs = st.executeQuery("select uid from user"); 368 369 while (rs.next()) 370 { 371 372 v.add( new User( rs.getInt( 1 ) ) ); 373 374 } 375 376 st.close(); 377 DatabaseConnectionPool.freeConnection(d); 378 return v; 379 380 } 381 382 catch (Exception E) 383 { 384 385 return v; 386 387 } 388 } 390 391 public boolean isAdmin() 392 { 393 boolean i=false; 394 try 395 { 396 397 Connection d = DatabaseConnectionPool.getConnection(); 398 Statement st = d.createStatement(); 399 ResultSet rs = st.executeQuery("select admin from user where uid=" + uid); 400 rs.next(); 401 i = rs.getBoolean(1); 402 st.close(); 403 DatabaseConnectionPool.freeConnection(d); 404 405 } 406 catch (Exception e) 407 { 408 } 409 return i; 410 } 411 412 public int getMostProminentRole() 413 { 414 int i=0; 415 try 416 { 417 418 Connection d = DatabaseConnectionPool.getConnection(); 419 Statement st = d.createStatement(); 420 ResultSet rs = st.executeQuery("select min(rid) from projectuser where uid=" + uid); 421 rs.next(); 422 i = rs.getInt(1); 423 st.close(); 424 DatabaseConnectionPool.freeConnection(d); 425 426 } 427 catch (Exception e) 428 { 429 } 430 return i; 431 432 } 433 436 437 public static Vector getUserRolesByProject(int proId) 438 { 439 Vector v = new Vector(); 440 441 try 442 { 443 444 Connection d = DatabaseConnectionPool.getConnection(); 445 Statement st = d.createStatement(); 446 ResultSet rs = st.executeQuery("select uid, rid, pid from projectuser where pid=" + proId); 447 User prouser=null; 448 449 while (rs.next()) 450 { 451 prouser = new User( rs.getInt( 1 ) ); 452 453 prouser.rid = rs.getInt( 2 ); 454 455 prouser.pid = rs.getInt( 3 ); 456 457 v.add( prouser ); 458 459 } 460 461 st.close(); 462 DatabaseConnectionPool.freeConnection(d); 463 464 } 465 466 catch (Exception E) 467 { 468 469 470 } 471 472 return v; 473 474 475 } 479 public Roles getRoleInProject(int pid) 480 { 481 Roles r = null; 482 try 483 { 484 Connection d = DatabaseConnectionPool.getConnection(); 485 Statement st = d.createStatement(); 486 ResultSet rs = st.executeQuery("select rid from projectuser where pid=" + pid + " and uid=" + uid); 487 if (rs.next()) 488 { 489 r = new Roles(rs.getInt(1)); 490 } 491 st.close(); 492 DatabaseConnectionPool.freeConnection(d); 493 } 494 495 catch (Exception E) 496 { 497 498 499 } 500 return r; 501 } 502 503 509 510 public static Vector getUsersNotInProject(int pid) 511 { 512 513 516 Vector v = new Vector(); 517 try 518 { 519 Connection d = DatabaseConnectionPool.getConnection(); 520 Statement st = d.createStatement(); 521 int uidn = 0; 522 ResultSet rs = st.executeQuery("select uid from projectuser where pid=" + pid); 523 StringBuffer sql = new StringBuffer ("select user.uid from user where user.uid not in ("); 524 while (rs.next()) 525 { 526 sql.append(rs.getString(1)); 527 sql.append(","); 528 } 529 sql.setCharAt(sql.length() - 1, ')'); 530 531 rs = st.executeQuery(sql.toString()); 532 while (rs.next()) 533 { 534 User gotuser = new User( rs.getInt( 1 ) ); 535 v.add( gotuser ); 536 } 537 538 st.close(); 539 DatabaseConnectionPool.freeConnection(d); 540 } 541 542 catch (Exception E) 543 { 544 545 546 } 547 548 return v; 549 550 } 552 553 554 public static void addUsersToProjectsRoles(int pid,int uid,int rid) 555 { 556 557 try 558 { 559 560 Connection d = DatabaseConnectionPool.getConnection(); 561 562 String SQL = "insert into projectuser values ( " + pid + " , " + uid + " , " + rid + " )"; 563 564 Statement st = d.createStatement(); 565 566 st.executeQuery(SQL); 567 568 st.close(); 569 570 DatabaseConnectionPool.freeConnection(d); 571 572 } 573 574 catch (Exception E) 575 { 576 577 578 } 579 580 } 582 583 584 585 586 } | Popular Tags |