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;
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());
133 id = DatabaseConnection.lastInsertId(query);
136 public void changePassword(String oldPass, String newPass) throws GigiApiException {
138 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
140 ResultSet rs = ps.executeQuery();
142 throw new GigiApiException("User not found... very bad.");
144 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
145 throw new GigiApiException("Old password does not match.");
148 PasswordStrengthChecker.assertStrongPassword(newPass, this);
149 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
150 ps.setString(1, PasswordHash.hash(newPass));
152 if (ps.executeUpdate() != 1) {
153 throw new GigiApiException("Password update failed.");
155 } catch (SQLException e) {
156 throw new GigiApiException(e);
160 public boolean canAssure() throws SQLException {
161 if (getAssurancePoints() < 100) {
165 return hasPassedCATS();
169 public boolean hasPassedCATS() throws SQLException {
170 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
172 ResultSet rs = query.executeQuery();
180 public int getAssurancePoints() throws SQLException {
181 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
183 ResultSet rs = query.executeQuery();
186 points = rs.getInt(1);
192 public int getExperiencePoints() throws SQLException {
193 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
195 ResultSet rs = query.executeQuery();
198 points = rs.getInt(1) * 2;
205 public boolean equals(Object obj) {
206 if ( !(obj instanceof User)) {
210 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
219 * Gets the maximum allowed points NOW. Note that an assurance needs to
220 * re-check PoJam as it has taken place in the past.
222 * @return the maximal points
223 * @throws SQLException
225 public int getMaxAssurePoints() throws SQLException {
226 int exp = getExperiencePoints();
228 Calendar c = Calendar.getInstance();
230 int year = c.get(Calendar.YEAR);
231 int month = c.get(Calendar.MONTH);
232 int day = c.get(Calendar.DAY_OF_MONTH);
233 c.set(year + 18, month, day);
234 if (System.currentTimeMillis() < c.getTime().getTime()) {
235 return points; // not 18 Years old.
256 public static User getById(int id) {
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) {
447 throw new GigiApiException("No change after assurance allowed.");
449 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
450 update.setString(1, getFname());
451 update.setString(2, getLname());
452 update.setString(3, getMname());
453 update.setString(4, getSuffix());
454 update.setDate(5, getDob());
455 update.setInt(6, getId());
456 update.executeUpdate();
460 public Locale getPreferredLocale() {
464 public void setPreferredLocale(Locale locale) {
465 this.locale = locale;
469 public boolean wantsDirectoryListing() throws SQLException {
470 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
471 get.setInt(1, getId());
472 ResultSet exec = get.executeQuery();
474 return exec.getBoolean("listme");
477 public String getContactInformation() throws SQLException {
478 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
479 get.setInt(1, getId());
480 ResultSet exec = get.executeQuery();
482 return exec.getString("contactinfo");
485 public void setDirectoryListing(boolean on) throws SQLException {
486 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
487 update.setBoolean(1, on);
488 update.setInt(2, getId());
489 update.executeUpdate();
492 public void setContactInformation(String contactInfo) throws SQLException {
493 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
494 update.setString(1, contactInfo);
495 update.setInt(2, getId());
496 update.executeUpdate();