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