]> WPIA git - gigi.git/blob - src/org/cacert/gigi/User.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[gigi.git] / src / org / cacert / gigi / User.java
1 package org.cacert.gigi;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.util.Date;
7
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.util.PasswordHash;
10
11 public class User {
12
13         private int id;
14         Name name = new Name(null, null);
15
16         Date dob;
17         String email;
18
19         public User(int id) {
20                 this.id = id;
21                 try {
22                         PreparedStatement ps = DatabaseConnection
23                                         .getInstance()
24                                         .prepare(
25                                                         "SELECT `fname`, `lname`, `dob`, `email` FROM `users` WHERE id=?");
26                         ps.setInt(1, id);
27                         ResultSet rs = ps.executeQuery();
28                         if (rs.next()) {
29                                 name = new Name(rs.getString(1), rs.getString(2));
30                                 dob = rs.getDate(3);
31                                 email = rs.getString(4);
32                         }
33                         rs.close();
34                 } catch (SQLException e) {
35                         e.printStackTrace();
36                 }
37         }
38         public User() {
39         }
40         public int getId() {
41                 return id;
42         }
43         public String getFname() {
44                 return name.fname;
45         }
46         public String getLname() {
47                 return name.lname;
48         }
49         public String getMname() {
50                 return name.mname;
51         }
52         public Name getName() {
53                 return name;
54         }
55         public void setMname(String mname) {
56                 this.name.mname = mname;
57         }
58         public String getSuffix() {
59                 return name.suffix;
60         }
61         public void setSuffix(String suffix) {
62                 this.name.suffix = suffix;
63         }
64         public Date getDob() {
65                 return dob;
66         }
67         public void setDob(Date dob) {
68                 this.dob = dob;
69         }
70         public String getEmail() {
71                 return email;
72         }
73         public void setEmail(String email) {
74                 this.email = email;
75         }
76         public void setId(int id) {
77                 this.id = id;
78         }
79         public void setFname(String fname) {
80                 this.name.fname = fname;
81         }
82         public void setLname(String lname) {
83                 this.name.lname = lname;
84         }
85         public void insert(String password) throws SQLException {
86                 if (id != 0) {
87                         throw new Error("refusing to insert");
88                 }
89                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
90                                 "insert into `users` set `email`=?, `password`=?, "
91                                                 + "`fname`=?, `mname`=?, `lname`=?, "
92                                                 + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
93                 query.setString(1, email);
94                 query.setString(2, PasswordHash.hash(password));
95                 query.setString(3, name.fname);
96                 query.setString(4, name.mname);
97                 query.setString(5, name.lname);
98                 query.setString(6, name.suffix);
99                 query.setDate(7, new java.sql.Date(dob.getTime()));
100                 query.execute();
101                 id = DatabaseConnection.lastInsertId(query);
102                 System.out.println("Inserted: " + id);
103         }
104
105         public boolean canAssure() throws SQLException {
106                 if (getAssurancePoints() < 100) {
107                         return false;
108                 }
109
110                 return hasPassedCATS();
111
112         }
113         public boolean hasPassedCATS() throws SQLException {
114                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
115                                 "SELECT 1 FROM `cats_passed` where `user_id`=?");
116                 query.setInt(1, id);
117                 ResultSet rs = query.executeQuery();
118                 if (rs.next()) {
119                         return true;
120                 } else {
121                         return false;
122                 }
123         }
124         public int getAssurancePoints() throws SQLException {
125                 PreparedStatement query = DatabaseConnection
126                                 .getInstance()
127                                 .prepare(
128                                                 "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
129                 query.setInt(1, id);
130                 ResultSet rs = query.executeQuery();
131                 int points = 0;
132                 if (rs.next()) {
133                         points = rs.getInt(1);
134                 }
135                 rs.close();
136                 return points;
137         }
138         public int getExperiencePoints() throws SQLException {
139                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
140                                 "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
141                 query.setInt(1, id);
142                 ResultSet rs = query.executeQuery();
143                 int points = 0;
144                 if (rs.next()) {
145                         points = rs.getInt(1) * 2;
146                 }
147                 rs.close();
148                 return points;
149         }
150         @Override
151         public boolean equals(Object obj) {
152                 if (!(obj instanceof User)) {
153                         return false;
154                 }
155                 User s = (User) obj;
156                 return name.equals(s.name) && email.equals(s.email)
157                                 && dob.equals(s.dob);
158         }
159         public int getMaxAssurePoints() throws SQLException {
160                 int exp = getExperiencePoints();
161                 int points = 10;
162                 if (exp >= 10) {
163                         points += 5;
164                 }
165                 if (exp >= 20) {
166                         points += 5;
167                 }
168                 if (exp >= 30) {
169                         points += 5;
170                 }
171                 if (exp >= 40) {
172                         points += 5;
173                 }
174                 if (exp >= 50) {
175                         points += 5;
176                 }
177                 return points;
178         }
179 }