1 package org.cacert.gigi.dbObjects;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7 import java.util.Calendar;
8 import java.util.Locale;
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.DatabaseConnection;
12 import org.cacert.gigi.localisation.Language;
13 import org.cacert.gigi.util.Notary;
14 import org.cacert.gigi.util.PasswordHash;
15 import org.cacert.gigi.util.PasswordStrengthChecker;
17 public class User implements IdCachable {
21 private Name name = new Name(null, null, null, null);
27 private Assurance[] receivedAssurances, madeAssurances;
29 private Locale locale;
31 private User(int id) {
36 private void updateName(int id) {
38 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email`, `language` FROM `users` WHERE id=?");
40 ResultSet rs = ps.executeQuery();
42 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
44 email = rs.getString(6);
45 String localeStr = rs.getString(7);
46 if (localeStr == null || localeStr.equals("")) {
47 locale = Locale.getDefault();
49 locale = Language.getLocaleFromString(localeStr);
53 } catch (SQLException e) {
64 public String getFname() {
68 public String getLname() {
72 public String getMname() {
76 public Name getName() {
80 public void setMname(String mname) {
81 this.name.mname = mname;
84 public String getSuffix() {
88 public void setSuffix(String suffix) {
89 this.name.suffix = suffix;
92 public Date getDob() {
96 public void setDob(Date dob) {
100 public String getEmail() {
104 public void setEmail(String email) {
108 public void setId(int id) {
112 public void setFname(String fname) {
113 this.name.fname = fname;
116 public void setLname(String lname) {
117 this.name.lname = lname;
120 public void insert(String password) throws SQLException {
122 throw new Error("refusing to insert");
124 PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0, `language`=?");
125 query.setString(1, email);
126 query.setString(2, PasswordHash.hash(password));
127 query.setString(3, name.fname);
128 query.setString(4, name.mname);
129 query.setString(5, name.lname);
130 query.setString(6, name.suffix);
131 query.setDate(7, new java.sql.Date(dob.getTime()));
132 query.setString(8, locale.toString());
133 synchronized (User.class) {
135 id = DatabaseConnection.lastInsertId(query);
140 public void changePassword(String oldPass, String newPass) throws GigiApiException {
142 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
144 ResultSet rs = ps.executeQuery();
146 throw new GigiApiException("User not found... very bad.");
148 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
149 throw new GigiApiException("Old password does not match.");
152 PasswordStrengthChecker.assertStrongPassword(newPass, this);
153 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
154 ps.setString(1, PasswordHash.hash(newPass));
156 if (ps.executeUpdate() != 1) {
157 throw new GigiApiException("Password update failed.");
159 } catch (SQLException e) {
160 throw new GigiApiException(e);
164 public boolean canAssure() throws SQLException {
165 if ( !isOfAge(14)) { // PoJAM
168 if (getAssurancePoints() < 100) {
172 return hasPassedCATS();
176 public boolean hasPassedCATS() throws SQLException {
177 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
179 ResultSet rs = query.executeQuery();
187 public int getAssurancePoints() throws SQLException {
188 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
190 ResultSet rs = query.executeQuery();
193 points = rs.getInt(1);
199 public int getExperiencePoints() throws SQLException {
200 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
202 ResultSet rs = query.executeQuery();
205 points = rs.getInt(1) * 2;
212 public boolean equals(Object obj) {
213 if ( !(obj instanceof User)) {
217 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
226 * Gets the maximum allowed points NOW. Note that an assurance needs to
227 * re-check PoJam as it has taken place in the past.
229 * @return the maximal points
230 * @throws SQLException
232 public int getMaxAssurePoints() throws SQLException {
237 int exp = getExperiencePoints();
258 public boolean isOfAge(int desiredAge) {
259 Calendar c = Calendar.getInstance();
261 int year = c.get(Calendar.YEAR);
262 int month = c.get(Calendar.MONTH);
263 int day = c.get(Calendar.DAY_OF_MONTH);
264 c.set(year, month, day);
265 c.add(Calendar.YEAR, desiredAge);
266 return System.currentTimeMillis() >= c.getTime().getTime();
269 public EmailAddress[] getEmails() {
271 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
273 ResultSet rs = ps.executeQuery();
275 int count = rs.getRow();
276 EmailAddress[] data = new EmailAddress[count];
278 for (int i = 0; i < data.length; i++) {
280 throw new Error("Internal sql api violation.");
282 data[i] = EmailAddress.getById(rs.getInt(1));
286 } catch (SQLException e) {
293 public Domain[] getDomains() {
295 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
297 ResultSet rs = ps.executeQuery();
299 int count = rs.getRow();
300 Domain[] data = new Domain[count];
302 for (int i = 0; i < data.length; i++) {
304 throw new Error("Internal sql api violation.");
306 data[i] = Domain.getById(rs.getInt(1));
310 } catch (SQLException e) {
317 public Certificate[] getCertificates() {
319 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
321 ResultSet rs = ps.executeQuery();
323 int count = rs.getRow();
324 Certificate[] data = new Certificate[count];
326 for (int i = 0; i < data.length; i++) {
328 throw new Error("Internal sql api violation.");
330 data[i] = Certificate.getBySerial(rs.getString(1));
334 } catch (SQLException e) {
341 public boolean isValidDomain(String domainname) {
342 for (Domain d : getDomains()) {
343 String sfx = d.getSuffix();
344 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
351 public boolean isValidEmail(String email) {
352 for (EmailAddress em : getEmails()) {
353 if (em.getAddress().equals(email)) {
360 public boolean isValidName(String name) {
361 return getName().matches(name);
364 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
366 EmailAddress[] adrs = getEmails();
367 for (int i = 0; i < adrs.length; i++) {
368 if (adrs[i].getAddress().equals(newMail.getAddress())) {
369 if ( !adrs[i].isVerified()) {
370 throw new GigiApiException("Email not verified.");
372 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
373 ps.setString(1, newMail.getAddress());
374 ps.setInt(2, getId());
376 email = newMail.getAddress();
380 throw new GigiApiException("Given address not an address of the user.");
381 } catch (SQLException e) {
382 throw new GigiApiException(e);
386 public void deleteEmail(EmailAddress mail) throws GigiApiException {
387 if (getEmail().equals(mail.getAddress())) {
388 throw new GigiApiException("Can't delete user's default e-mail.");
390 EmailAddress[] emails = getEmails();
391 for (int i = 0; i < emails.length; i++) {
392 if (emails[i].getId() == mail.getId()) {
394 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
395 ps.setDate(1, new Date(System.currentTimeMillis()));
396 ps.setInt(2, mail.getId());
398 } catch (SQLException e) {
400 throw new GigiApiException(e);
405 throw new GigiApiException("Email not one of user's email addresses.");
408 public Assurance[] getReceivedAssurances() throws SQLException {
409 if (receivedAssurances == null) {
410 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
411 query.setInt(1, getId());
412 ResultSet res = query.executeQuery();
414 Assurance[] assurances = new Assurance[res.getRow()];
416 for (int i = 0; i < assurances.length; i++) {
418 assurances[i] = new Assurance(res);
420 this.receivedAssurances = assurances;
423 return receivedAssurances;
426 public Assurance[] getMadeAssurances() throws SQLException {
427 if (madeAssurances == null) {
428 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
429 query.setInt(1, getId());
430 ResultSet res = query.executeQuery();
432 Assurance[] assurances = new Assurance[res.getRow()];
434 for (int i = 0; i < assurances.length; i++) {
436 assurances[i] = new Assurance(res);
438 this.madeAssurances = assurances;
441 return madeAssurances;
444 public void invalidateMadeAssurances() {
445 madeAssurances = null;
448 public void invalidateReceivedAssurances() {
449 receivedAssurances = null;
452 public void updateUserData() throws SQLException, GigiApiException {
453 synchronized (Notary.class) {
454 if (getAssurancePoints() != 0) {
455 throw new GigiApiException("No change after assurance allowed.");
457 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
458 update.setString(1, getFname());
459 update.setString(2, getLname());
460 update.setString(3, getMname());
461 update.setString(4, getSuffix());
462 update.setDate(5, getDob());
463 update.setInt(6, getId());
464 update.executeUpdate();
468 public Locale getPreferredLocale() {
472 public void setPreferredLocale(Locale locale) {
473 this.locale = locale;
477 public boolean wantsDirectoryListing() throws SQLException {
478 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
479 get.setInt(1, getId());
480 ResultSet exec = get.executeQuery();
482 return exec.getBoolean("listme");
485 public String getContactInformation() throws SQLException {
486 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
487 get.setInt(1, getId());
488 ResultSet exec = get.executeQuery();
490 return exec.getString("contactinfo");
493 public void setDirectoryListing(boolean on) throws SQLException {
494 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
495 update.setBoolean(1, on);
496 update.setInt(2, getId());
497 update.executeUpdate();
500 public void setContactInformation(String contactInfo) throws SQLException {
501 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
502 update.setString(1, contactInfo);
503 update.setInt(2, getId());
504 update.executeUpdate();
507 private static ObjectCache<User> myCache = new ObjectCache<>();
509 public static User getById(int id) {
510 User u = myCache.get(id);
512 synchronized (User.class) {
513 myCache.put(u = new User(id));