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;
8 import java.util.Locale;
10 import org.cacert.gigi.database.DatabaseConnection;
11 import org.cacert.gigi.localisation.Language;
12 import org.cacert.gigi.util.Notary;
13 import org.cacert.gigi.util.PasswordHash;
14 import org.cacert.gigi.util.PasswordStrengthChecker;
16 public class User implements IdCachable {
20 private Name name = new Name(null, null, null, null);
26 private Assurance[] receivedAssurances, madeAssurances;
28 private Locale locale;
35 private void updateName(int id) {
37 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email`, `language` FROM `users` WHERE id=?");
39 ResultSet rs = ps.executeQuery();
41 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
43 email = rs.getString(6);
44 String localeStr = rs.getString(7);
45 if (localeStr == null || localeStr.equals("")) {
46 locale = Locale.getDefault();
48 locale = Language.getLocaleFromString(localeStr);
52 } catch (SQLException e) {
63 public String getFname() {
67 public String getLname() {
71 public String getMname() {
75 public Name getName() {
79 public void setMname(String mname) {
80 this.name.mname = mname;
83 public String getSuffix() {
87 public void setSuffix(String suffix) {
88 this.name.suffix = suffix;
91 public Date getDob() {
95 public void setDob(Date dob) {
99 public String getEmail() {
103 public void setEmail(String email) {
107 public void setId(int id) {
111 public void setFname(String fname) {
112 this.name.fname = fname;
115 public void setLname(String lname) {
116 this.name.lname = lname;
119 public void insert(String password) throws SQLException {
121 throw new Error("refusing to insert");
123 PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0, `language`=?");
124 query.setString(1, email);
125 query.setString(2, PasswordHash.hash(password));
126 query.setString(3, name.fname);
127 query.setString(4, name.mname);
128 query.setString(5, name.lname);
129 query.setString(6, name.suffix);
130 query.setDate(7, new java.sql.Date(dob.getTime()));
131 query.setString(8, locale.toString());
132 synchronized (User.class) {
134 id = DatabaseConnection.lastInsertId(query);
139 public void changePassword(String oldPass, String newPass) throws GigiApiException {
141 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
143 ResultSet rs = ps.executeQuery();
145 throw new GigiApiException("User not found... very bad.");
147 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
148 throw new GigiApiException("Old password does not match.");
151 PasswordStrengthChecker.assertStrongPassword(newPass, this);
152 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
153 ps.setString(1, PasswordHash.hash(newPass));
155 if (ps.executeUpdate() != 1) {
156 throw new GigiApiException("Password update failed.");
158 } catch (SQLException e) {
159 throw new GigiApiException(e);
163 public boolean canAssure() throws SQLException {
164 if (getAssurancePoints() < 100) {
168 return hasPassedCATS();
172 public boolean hasPassedCATS() throws SQLException {
173 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
175 ResultSet rs = query.executeQuery();
183 public int getAssurancePoints() throws SQLException {
184 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
186 ResultSet rs = query.executeQuery();
189 points = rs.getInt(1);
195 public int getExperiencePoints() throws SQLException {
196 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
198 ResultSet rs = query.executeQuery();
201 points = rs.getInt(1) * 2;
208 public boolean equals(Object obj) {
209 if ( !(obj instanceof User)) {
213 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
222 * Gets the maximum allowed points NOW. Note that an assurance needs to
223 * re-check PoJam as it has taken place in the past.
225 * @return the maximal points
226 * @throws SQLException
228 public int getMaxAssurePoints() throws SQLException {
229 int exp = getExperiencePoints();
231 Calendar c = Calendar.getInstance();
233 int year = c.get(Calendar.YEAR);
234 int month = c.get(Calendar.MONTH);
235 int day = c.get(Calendar.DAY_OF_MONTH);
236 c.set(year + 18, month, day);
237 if (System.currentTimeMillis() < c.getTime().getTime()) {
238 return points; // not 18 Years old.
259 public EmailAddress[] getEmails() {
261 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
263 ResultSet rs = ps.executeQuery();
265 int count = rs.getRow();
266 EmailAddress[] data = new EmailAddress[count];
268 for (int i = 0; i < data.length; i++) {
270 throw new Error("Internal sql api violation.");
272 data[i] = EmailAddress.getById(rs.getInt(1));
276 } catch (SQLException e) {
283 public Domain[] getDomains() {
285 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
287 ResultSet rs = ps.executeQuery();
289 int count = rs.getRow();
290 Domain[] data = new Domain[count];
292 for (int i = 0; i < data.length; i++) {
294 throw new Error("Internal sql api violation.");
296 data[i] = Domain.getById(rs.getInt(1));
300 } catch (SQLException e) {
307 public Certificate[] getCertificates() {
309 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
311 ResultSet rs = ps.executeQuery();
313 int count = rs.getRow();
314 Certificate[] data = new Certificate[count];
316 for (int i = 0; i < data.length; i++) {
318 throw new Error("Internal sql api violation.");
320 data[i] = Certificate.getBySerial(rs.getString(1));
324 } catch (SQLException e) {
331 public boolean isValidDomain(String domainname) {
332 for (Domain d : getDomains()) {
333 String sfx = d.getSuffix();
334 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
341 public boolean isValidEmail(String email) {
342 for (EmailAddress em : getEmails()) {
343 if (em.getAddress().equals(email)) {
350 public boolean isValidName(String name) {
351 return getName().matches(name);
354 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
356 EmailAddress[] adrs = getEmails();
357 for (int i = 0; i < adrs.length; i++) {
358 if (adrs[i].getAddress().equals(newMail.getAddress())) {
359 if ( !adrs[i].isVerified()) {
360 throw new GigiApiException("Email not verified.");
362 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
363 ps.setString(1, newMail.getAddress());
364 ps.setInt(2, getId());
366 email = newMail.getAddress();
370 throw new GigiApiException("Given address not an address of the user.");
371 } catch (SQLException e) {
372 throw new GigiApiException(e);
376 public void deleteEmail(EmailAddress mail) throws GigiApiException {
377 if (getEmail().equals(mail.getAddress())) {
378 throw new GigiApiException("Can't delete user's default e-mail.");
380 EmailAddress[] emails = getEmails();
381 for (int i = 0; i < emails.length; i++) {
382 if (emails[i].getId() == mail.getId()) {
384 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
385 ps.setDate(1, new Date(System.currentTimeMillis()));
386 ps.setInt(2, mail.getId());
388 } catch (SQLException e) {
390 throw new GigiApiException(e);
395 throw new GigiApiException("Email not one of user's email addresses.");
398 public Assurance[] getReceivedAssurances() throws SQLException {
399 if (receivedAssurances == null) {
400 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
401 query.setInt(1, getId());
402 ResultSet res = query.executeQuery();
404 Assurance[] assurances = new Assurance[res.getRow()];
406 for (int i = 0; i < assurances.length; i++) {
408 assurances[i] = new Assurance(res);
410 this.receivedAssurances = assurances;
413 return receivedAssurances;
416 public Assurance[] getMadeAssurances() throws SQLException {
417 if (madeAssurances == null) {
418 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
419 query.setInt(1, getId());
420 ResultSet res = query.executeQuery();
422 Assurance[] assurances = new Assurance[res.getRow()];
424 for (int i = 0; i < assurances.length; i++) {
426 assurances[i] = new Assurance(res);
428 this.madeAssurances = assurances;
431 return madeAssurances;
434 public void invalidateMadeAssurances() {
435 madeAssurances = null;
438 public void invalidateReceivedAssurances() {
439 receivedAssurances = null;
442 public void updateUserData() throws SQLException, GigiApiException {
443 synchronized (Notary.class) {
444 if (getAssurancePoints() != 0) {
445 throw new GigiApiException("No change after assurance allowed.");
447 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
448 update.setString(1, getFname());
449 update.setString(2, getLname());
450 update.setString(3, getMname());
451 update.setString(4, getSuffix());
452 update.setDate(5, getDob());
453 update.setInt(6, getId());
454 update.executeUpdate();
458 public Locale getPreferredLocale() {
462 public void setPreferredLocale(Locale locale) {
463 this.locale = locale;
467 public boolean wantsDirectoryListing() throws SQLException {
468 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
469 get.setInt(1, getId());
470 ResultSet exec = get.executeQuery();
472 return exec.getBoolean("listme");
475 public String getContactInformation() throws SQLException {
476 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
477 get.setInt(1, getId());
478 ResultSet exec = get.executeQuery();
480 return exec.getString("contactinfo");
483 public void setDirectoryListing(boolean on) throws SQLException {
484 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
485 update.setBoolean(1, on);
486 update.setInt(2, getId());
487 update.executeUpdate();
490 public void setContactInformation(String contactInfo) throws SQLException {
491 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
492 update.setString(1, contactInfo);
493 update.setInt(2, getId());
494 update.executeUpdate();
497 private static ObjectCache<User> myCache = new ObjectCache<>();
499 public static User getById(int id) {
500 User u = myCache.get(id);
502 synchronized (User.class) {
503 myCache.put(u = new User(id));