]> WPIA git - gigi.git/blob - src/org/cacert/gigi/User.java
Remove "list all emails from user"-queries.
[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.sql.Date;
7 import java.util.Calendar;
8
9 import org.cacert.gigi.database.DatabaseConnection;
10 import org.cacert.gigi.util.PasswordHash;
11
12 public class User {
13
14         private int id;
15         Name name = new Name(null, null, null, null);
16
17         Date dob;
18         String email;
19
20         public User(int id) {
21                 this.id = id;
22                 try {
23                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
24                                 "SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
25                         ps.setInt(1, id);
26                         ResultSet rs = ps.executeQuery();
27                         if (rs.next()) {
28                                 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
29                                 dob = rs.getDate(5);
30                                 email = rs.getString(6);
31                         }
32                         rs.close();
33                 } catch (SQLException e) {
34                         e.printStackTrace();
35                 }
36         }
37
38         public User() {
39         }
40
41         public int getId() {
42                 return id;
43         }
44
45         public String getFname() {
46                 return name.fname;
47         }
48
49         public String getLname() {
50                 return name.lname;
51         }
52
53         public String getMname() {
54                 return name.mname;
55         }
56
57         public Name getName() {
58                 return name;
59         }
60
61         public void setMname(String mname) {
62                 this.name.mname = mname;
63         }
64
65         public String getSuffix() {
66                 return name.suffix;
67         }
68
69         public void setSuffix(String suffix) {
70                 this.name.suffix = suffix;
71         }
72
73         public Date getDob() {
74                 return dob;
75         }
76
77         public void setDob(Date dob) {
78                 this.dob = dob;
79         }
80
81         public String getEmail() {
82                 return email;
83         }
84
85         public void setEmail(String email) {
86                 this.email = email;
87         }
88
89         public void setId(int id) {
90                 this.id = id;
91         }
92
93         public void setFname(String fname) {
94                 this.name.fname = fname;
95         }
96
97         public void setLname(String lname) {
98                 this.name.lname = lname;
99         }
100
101         public void insert(String password) throws SQLException {
102                 if (id != 0) {
103                         throw new Error("refusing to insert");
104                 }
105                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
106                         "insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, "
107                                 + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
108                 query.setString(1, email);
109                 query.setString(2, PasswordHash.hash(password));
110                 query.setString(3, name.fname);
111                 query.setString(4, name.mname);
112                 query.setString(5, name.lname);
113                 query.setString(6, name.suffix);
114                 query.setDate(7, new java.sql.Date(dob.getTime()));
115                 query.execute();
116                 id = DatabaseConnection.lastInsertId(query);
117         }
118
119         public boolean canAssure() throws SQLException {
120                 if (getAssurancePoints() < 100) {
121                         return false;
122                 }
123
124                 return hasPassedCATS();
125
126         }
127
128         public boolean hasPassedCATS() throws SQLException {
129                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
130                         "SELECT 1 FROM `cats_passed` where `user_id`=?");
131                 query.setInt(1, id);
132                 ResultSet rs = query.executeQuery();
133                 if (rs.next()) {
134                         return true;
135                 } else {
136                         return false;
137                 }
138         }
139
140         public int getAssurancePoints() throws SQLException {
141                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
142                         "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
143                 query.setInt(1, id);
144                 ResultSet rs = query.executeQuery();
145                 int points = 0;
146                 if (rs.next()) {
147                         points = rs.getInt(1);
148                 }
149                 rs.close();
150                 return points;
151         }
152
153         public int getExperiencePoints() throws SQLException {
154                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
155                         "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
156                 query.setInt(1, id);
157                 ResultSet rs = query.executeQuery();
158                 int points = 0;
159                 if (rs.next()) {
160                         points = rs.getInt(1) * 2;
161                 }
162                 rs.close();
163                 return points;
164         }
165
166         @Override
167         public boolean equals(Object obj) {
168                 if (!(obj instanceof User)) {
169                         return false;
170                 }
171                 User s = (User) obj;
172                 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
173                                                                                                                                                                                                                 // is
174                                                                                                                                                                                                                 // due
175                                                                                                                                                                                                                 // to
176                                                                                                                                                                                                                 // day
177                                                                                                                                                                                                                 // cutoff
178         }
179
180         /**
181          * Gets the maximum allowed points NOW. Note that an assurance needs to
182          * re-check PoJam as it has taken place in the past.
183          * 
184          * @return the maximal points
185          * @throws SQLException
186          */
187         public int getMaxAssurePoints() throws SQLException {
188                 int exp = getExperiencePoints();
189                 int points = 10;
190                 Calendar c = Calendar.getInstance();
191                 c.setTime(dob);
192                 int year = c.get(Calendar.YEAR);
193                 int month = c.get(Calendar.MONTH);
194                 int day = c.get(Calendar.DAY_OF_MONTH);
195                 c.set(year + 18, month, day);
196                 if (System.currentTimeMillis() < c.getTime().getTime()) {
197                         return points; // not 18 Years old.
198                 }
199
200                 if (exp >= 10) {
201                         points += 5;
202                 }
203                 if (exp >= 20) {
204                         points += 5;
205                 }
206                 if (exp >= 30) {
207                         points += 5;
208                 }
209                 if (exp >= 40) {
210                         points += 5;
211                 }
212                 if (exp >= 50) {
213                         points += 5;
214                 }
215                 return points;
216         }
217
218         public static User getById(int id) {
219                 return new User(id);
220         }
221
222         public EmailAddress[] getEmails() {
223                 try {
224                         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM email WHERE memid=?");
225                         ps.setInt(1, id);
226                         ResultSet rs = ps.executeQuery();
227                         rs.last();
228                         int count = rs.getRow();
229                         EmailAddress[] data = new EmailAddress[count];
230                         rs.beforeFirst();
231                         for (int i = 0; i < data.length; i++) {
232                                 if (!rs.next()) {
233                                         throw new Error("Internal sql api violation.");
234                                 }
235                                 data[i] = EmailAddress.getById(rs.getInt(1));
236                         }
237                         rs.close();
238                         return data;
239                 } catch (SQLException e) {
240                         e.printStackTrace();
241                 }
242
243                 return null;
244         }
245 }