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;
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 (getAssurancePoints() < 100) {
169 return hasPassedCATS();
173 public boolean hasPassedCATS() throws SQLException {
174 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
176 ResultSet rs = query.executeQuery();
184 public int getAssurancePoints() throws SQLException {
185 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
187 ResultSet rs = query.executeQuery();
190 points = rs.getInt(1);
196 public int getExperiencePoints() throws SQLException {
197 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
199 ResultSet rs = query.executeQuery();
202 points = rs.getInt(1) * 2;
209 public boolean equals(Object obj) {
210 if ( !(obj instanceof User)) {
214 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
223 * Gets the maximum allowed points NOW. Note that an assurance needs to
224 * re-check PoJam as it has taken place in the past.
226 * @return the maximal points
227 * @throws SQLException
229 public int getMaxAssurePoints() throws SQLException {
230 int exp = getExperiencePoints();
232 Calendar c = Calendar.getInstance();
234 int year = c.get(Calendar.YEAR);
235 int month = c.get(Calendar.MONTH);
236 int day = c.get(Calendar.DAY_OF_MONTH);
237 c.set(year + 18, month, day);
238 if (System.currentTimeMillis() < c.getTime().getTime()) {
239 return points; // not 18 Years old.
260 public EmailAddress[] getEmails() {
262 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
264 ResultSet rs = ps.executeQuery();
266 int count = rs.getRow();
267 EmailAddress[] data = new EmailAddress[count];
269 for (int i = 0; i < data.length; i++) {
271 throw new Error("Internal sql api violation.");
273 data[i] = EmailAddress.getById(rs.getInt(1));
277 } catch (SQLException e) {
284 public Domain[] getDomains() {
286 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
288 ResultSet rs = ps.executeQuery();
290 int count = rs.getRow();
291 Domain[] data = new Domain[count];
293 for (int i = 0; i < data.length; i++) {
295 throw new Error("Internal sql api violation.");
297 data[i] = Domain.getById(rs.getInt(1));
301 } catch (SQLException e) {
308 public Certificate[] getCertificates() {
310 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
312 ResultSet rs = ps.executeQuery();
314 int count = rs.getRow();
315 Certificate[] data = new Certificate[count];
317 for (int i = 0; i < data.length; i++) {
319 throw new Error("Internal sql api violation.");
321 data[i] = Certificate.getBySerial(rs.getString(1));
325 } catch (SQLException e) {
332 public boolean isValidDomain(String domainname) {
333 for (Domain d : getDomains()) {
334 String sfx = d.getSuffix();
335 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
342 public boolean isValidEmail(String email) {
343 for (EmailAddress em : getEmails()) {
344 if (em.getAddress().equals(email)) {
351 public boolean isValidName(String name) {
352 return getName().matches(name);
355 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
357 EmailAddress[] adrs = getEmails();
358 for (int i = 0; i < adrs.length; i++) {
359 if (adrs[i].getAddress().equals(newMail.getAddress())) {
360 if ( !adrs[i].isVerified()) {
361 throw new GigiApiException("Email not verified.");
363 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
364 ps.setString(1, newMail.getAddress());
365 ps.setInt(2, getId());
367 email = newMail.getAddress();
371 throw new GigiApiException("Given address not an address of the user.");
372 } catch (SQLException e) {
373 throw new GigiApiException(e);
377 public void deleteEmail(EmailAddress mail) throws GigiApiException {
378 if (getEmail().equals(mail.getAddress())) {
379 throw new GigiApiException("Can't delete user's default e-mail.");
381 EmailAddress[] emails = getEmails();
382 for (int i = 0; i < emails.length; i++) {
383 if (emails[i].getId() == mail.getId()) {
385 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
386 ps.setDate(1, new Date(System.currentTimeMillis()));
387 ps.setInt(2, mail.getId());
389 } catch (SQLException e) {
391 throw new GigiApiException(e);
396 throw new GigiApiException("Email not one of user's email addresses.");
399 public Assurance[] getReceivedAssurances() throws SQLException {
400 if (receivedAssurances == null) {
401 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
402 query.setInt(1, getId());
403 ResultSet res = query.executeQuery();
405 Assurance[] assurances = new Assurance[res.getRow()];
407 for (int i = 0; i < assurances.length; i++) {
409 assurances[i] = new Assurance(res);
411 this.receivedAssurances = assurances;
414 return receivedAssurances;
417 public Assurance[] getMadeAssurances() throws SQLException {
418 if (madeAssurances == null) {
419 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
420 query.setInt(1, getId());
421 ResultSet res = query.executeQuery();
423 Assurance[] assurances = new Assurance[res.getRow()];
425 for (int i = 0; i < assurances.length; i++) {
427 assurances[i] = new Assurance(res);
429 this.madeAssurances = assurances;
432 return madeAssurances;
435 public void invalidateMadeAssurances() {
436 madeAssurances = null;
439 public void invalidateReceivedAssurances() {
440 receivedAssurances = null;
443 public void updateUserData() throws SQLException, GigiApiException {
444 synchronized (Notary.class) {
445 if (getAssurancePoints() != 0) {
446 throw new GigiApiException("No change after assurance allowed.");
448 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
449 update.setString(1, getFname());
450 update.setString(2, getLname());
451 update.setString(3, getMname());
452 update.setString(4, getSuffix());
453 update.setDate(5, getDob());
454 update.setInt(6, getId());
455 update.executeUpdate();
459 public Locale getPreferredLocale() {
463 public void setPreferredLocale(Locale locale) {
464 this.locale = locale;
468 public boolean wantsDirectoryListing() throws SQLException {
469 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
470 get.setInt(1, getId());
471 ResultSet exec = get.executeQuery();
473 return exec.getBoolean("listme");
476 public String getContactInformation() throws SQLException {
477 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
478 get.setInt(1, getId());
479 ResultSet exec = get.executeQuery();
481 return exec.getString("contactinfo");
484 public void setDirectoryListing(boolean on) throws SQLException {
485 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
486 update.setBoolean(1, on);
487 update.setInt(2, getId());
488 update.executeUpdate();
491 public void setContactInformation(String contactInfo) throws SQLException {
492 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
493 update.setString(1, contactInfo);
494 update.setInt(2, getId());
495 update.executeUpdate();
498 private static ObjectCache<User> myCache = new ObjectCache<>();
500 public static User getById(int id) {
501 User u = myCache.get(id);
503 synchronized (User.class) {
504 myCache.put(u = new User(id));