1 package org.cacert.gigi;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.Calendar;
9 import org.cacert.gigi.database.DatabaseConnection;
10 import org.cacert.gigi.util.PasswordHash;
11 import org.cacert.gigi.util.PasswordStrengthChecker;
17 private Name name = new Name(null, null, null, null);
23 private Assurance[] receivedAssurances, madeAssurances;
28 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
30 ResultSet rs = ps.executeQuery();
32 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
34 email = rs.getString(6);
37 } catch (SQLException e) {
48 public String getFname() {
52 public String getLname() {
56 public String getMname() {
60 public Name getName() {
64 public void setMname(String mname) {
65 this.name.mname = mname;
68 public String getSuffix() {
72 public void setSuffix(String suffix) {
73 this.name.suffix = suffix;
76 public Date getDob() {
80 public void setDob(Date dob) {
84 public String getEmail() {
88 public void setEmail(String email) {
92 public void setId(int id) {
96 public void setFname(String fname) {
97 this.name.fname = fname;
100 public void setLname(String lname) {
101 this.name.lname = lname;
104 public void insert(String password) throws SQLException {
106 throw new Error("refusing to insert");
108 PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`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()));
117 id = DatabaseConnection.lastInsertId(query);
120 public void changePassword(String oldPass, String newPass) throws GigiApiException {
122 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
124 ResultSet rs = ps.executeQuery();
126 throw new GigiApiException("User not found... very bad.");
128 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
129 throw new GigiApiException("Old password does not match.");
132 PasswordStrengthChecker.assertStrongPassword(newPass, this);
133 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
134 ps.setString(1, PasswordHash.hash(newPass));
136 if (ps.executeUpdate() != 1) {
137 throw new GigiApiException("Password update failed.");
139 } catch (SQLException e) {
140 throw new GigiApiException(e);
144 public boolean canAssure() throws SQLException {
145 if (getAssurancePoints() < 100) {
149 return hasPassedCATS();
153 public boolean hasPassedCATS() throws SQLException {
154 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
156 ResultSet rs = query.executeQuery();
164 public int getAssurancePoints() throws SQLException {
165 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
167 ResultSet rs = query.executeQuery();
170 points = rs.getInt(1);
176 public int getExperiencePoints() throws SQLException {
177 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
179 ResultSet rs = query.executeQuery();
182 points = rs.getInt(1) * 2;
189 public boolean equals(Object obj) {
190 if ( !(obj instanceof User)) {
194 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
203 * Gets the maximum allowed points NOW. Note that an assurance needs to
204 * re-check PoJam as it has taken place in the past.
206 * @return the maximal points
207 * @throws SQLException
209 public int getMaxAssurePoints() throws SQLException {
210 int exp = getExperiencePoints();
212 Calendar c = Calendar.getInstance();
214 int year = c.get(Calendar.YEAR);
215 int month = c.get(Calendar.MONTH);
216 int day = c.get(Calendar.DAY_OF_MONTH);
217 c.set(year + 18, month, day);
218 if (System.currentTimeMillis() < c.getTime().getTime()) {
219 return points; // not 18 Years old.
240 public static User getById(int id) {
244 public EmailAddress[] getEmails() {
246 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
248 ResultSet rs = ps.executeQuery();
250 int count = rs.getRow();
251 EmailAddress[] data = new EmailAddress[count];
253 for (int i = 0; i < data.length; i++) {
255 throw new Error("Internal sql api violation.");
257 data[i] = EmailAddress.getById(rs.getInt(1));
261 } catch (SQLException e) {
268 public Domain[] getDomains() {
270 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
272 ResultSet rs = ps.executeQuery();
274 int count = rs.getRow();
275 Domain[] data = new Domain[count];
277 for (int i = 0; i < data.length; i++) {
279 throw new Error("Internal sql api violation.");
281 data[i] = Domain.getById(rs.getInt(1));
285 } catch (SQLException e) {
292 public Certificate[] getCertificates() {
294 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
296 ResultSet rs = ps.executeQuery();
298 int count = rs.getRow();
299 Certificate[] data = new Certificate[count];
301 for (int i = 0; i < data.length; i++) {
303 throw new Error("Internal sql api violation.");
305 data[i] = Certificate.getBySerial(rs.getString(1));
309 } catch (SQLException e) {
316 public boolean isValidDomain(String domainname) {
317 for (Domain d : getDomains()) {
318 String sfx = d.getSuffix();
319 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
326 public boolean isValidEmail(String email) {
327 for (EmailAddress em : getEmails()) {
328 if (em.getAddress().equals(email)) {
335 public boolean isValidName(String name) {
336 return getName().matches(name);
339 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
341 EmailAddress[] adrs = getEmails();
342 for (int i = 0; i < adrs.length; i++) {
343 if (adrs[i].getAddress().equals(newMail.getAddress())) {
344 if ( !adrs[i].isVerified()) {
345 throw new GigiApiException("Email not verified.");
347 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
348 ps.setString(1, newMail.getAddress());
349 ps.setInt(2, getId());
351 email = newMail.getAddress();
355 throw new GigiApiException("Given address not an address of the user.");
356 } catch (SQLException e) {
357 throw new GigiApiException(e);
361 public void deleteEmail(EmailAddress mail) throws GigiApiException {
362 if (getEmail().equals(mail.getAddress())) {
363 throw new GigiApiException("Can't delete user's default e-mail.");
365 EmailAddress[] emails = getEmails();
366 for (int i = 0; i < emails.length; i++) {
367 if (emails[i].getId() == mail.getId()) {
369 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
370 ps.setDate(1, new Date(System.currentTimeMillis()));
371 ps.setInt(2, mail.getId());
373 } catch (SQLException e) {
375 throw new GigiApiException(e);
380 throw new GigiApiException("Email not one of user's email addresses.");
383 public Assurance[] getReceivedAssurances() throws SQLException {
384 if (receivedAssurances == null) {
385 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
386 query.setInt(1, getId());
387 ResultSet res = query.executeQuery();
389 Assurance[] assurances = new Assurance[res.getRow()];
391 for (int i = 0; i < assurances.length; i++) {
393 assurances[i] = new Assurance(res);
395 this.receivedAssurances = assurances;
398 return receivedAssurances;
401 public Assurance[] getMadeAssurances() throws SQLException {
402 if (madeAssurances == null) {
403 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
404 query.setInt(1, getId());
405 ResultSet res = query.executeQuery();
407 Assurance[] assurances = new Assurance[res.getRow()];
409 for (int i = 0; i < assurances.length; i++) {
411 assurances[i] = new Assurance(res);
413 this.madeAssurances = assurances;
416 return madeAssurances;
419 public void invalidateMadeAssurances() {
420 madeAssurances = null;
423 public void invalidateReceivedAssurances() {
424 receivedAssurances = null;