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.localisation.Language;
11 import org.cacert.gigi.util.Notary;
12 import org.cacert.gigi.util.PasswordHash;
13 import org.cacert.gigi.util.PasswordStrengthChecker;
19 private Name name = new Name(null, null, null, null);
25 private Assurance[] receivedAssurances, madeAssurances;
32 private void updateName(int id) {
34 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
36 ResultSet rs = ps.executeQuery();
38 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
40 email = rs.getString(6);
43 } catch (SQLException e) {
54 public String getFname() {
58 public String getLname() {
62 public String getMname() {
66 public Name getName() {
70 public void setMname(String mname) {
71 this.name.mname = mname;
74 public String getSuffix() {
78 public void setSuffix(String suffix) {
79 this.name.suffix = suffix;
82 public Date getDob() {
86 public void setDob(Date dob) {
90 public String getEmail() {
94 public void setEmail(String email) {
98 public void setId(int id) {
102 public void setFname(String fname) {
103 this.name.fname = fname;
106 public void setLname(String lname) {
107 this.name.lname = lname;
110 public void insert(String password) throws SQLException {
112 throw new Error("refusing to insert");
114 PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
115 query.setString(1, email);
116 query.setString(2, PasswordHash.hash(password));
117 query.setString(3, name.fname);
118 query.setString(4, name.mname);
119 query.setString(5, name.lname);
120 query.setString(6, name.suffix);
121 query.setDate(7, new java.sql.Date(dob.getTime()));
123 id = DatabaseConnection.lastInsertId(query);
126 public void changePassword(String oldPass, String newPass) throws GigiApiException {
128 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
130 ResultSet rs = ps.executeQuery();
132 throw new GigiApiException("User not found... very bad.");
134 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
135 throw new GigiApiException("Old password does not match.");
138 PasswordStrengthChecker.assertStrongPassword(newPass, this);
139 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
140 ps.setString(1, PasswordHash.hash(newPass));
142 if (ps.executeUpdate() != 1) {
143 throw new GigiApiException("Password update failed.");
145 } catch (SQLException e) {
146 throw new GigiApiException(e);
150 public boolean canAssure() throws SQLException {
151 if (getAssurancePoints() < 100) {
155 return hasPassedCATS();
159 public boolean hasPassedCATS() throws SQLException {
160 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
162 ResultSet rs = query.executeQuery();
170 public int getAssurancePoints() throws SQLException {
171 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
173 ResultSet rs = query.executeQuery();
176 points = rs.getInt(1);
182 public int getExperiencePoints() throws SQLException {
183 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
185 ResultSet rs = query.executeQuery();
188 points = rs.getInt(1) * 2;
195 public boolean equals(Object obj) {
196 if ( !(obj instanceof User)) {
200 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
209 * Gets the maximum allowed points NOW. Note that an assurance needs to
210 * re-check PoJam as it has taken place in the past.
212 * @return the maximal points
213 * @throws SQLException
215 public int getMaxAssurePoints() throws SQLException {
216 int exp = getExperiencePoints();
218 Calendar c = Calendar.getInstance();
220 int year = c.get(Calendar.YEAR);
221 int month = c.get(Calendar.MONTH);
222 int day = c.get(Calendar.DAY_OF_MONTH);
223 c.set(year + 18, month, day);
224 if (System.currentTimeMillis() < c.getTime().getTime()) {
225 return points; // not 18 Years old.
246 public static User getById(int id) {
250 public EmailAddress[] getEmails() {
252 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
254 ResultSet rs = ps.executeQuery();
256 int count = rs.getRow();
257 EmailAddress[] data = new EmailAddress[count];
259 for (int i = 0; i < data.length; i++) {
261 throw new Error("Internal sql api violation.");
263 data[i] = EmailAddress.getById(rs.getInt(1));
267 } catch (SQLException e) {
274 public Domain[] getDomains() {
276 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
278 ResultSet rs = ps.executeQuery();
280 int count = rs.getRow();
281 Domain[] data = new Domain[count];
283 for (int i = 0; i < data.length; i++) {
285 throw new Error("Internal sql api violation.");
287 data[i] = Domain.getById(rs.getInt(1));
291 } catch (SQLException e) {
298 public Certificate[] getCertificates() {
300 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
302 ResultSet rs = ps.executeQuery();
304 int count = rs.getRow();
305 Certificate[] data = new Certificate[count];
307 for (int i = 0; i < data.length; i++) {
309 throw new Error("Internal sql api violation.");
311 data[i] = Certificate.getBySerial(rs.getString(1));
315 } catch (SQLException e) {
322 public boolean isValidDomain(String domainname) {
323 for (Domain d : getDomains()) {
324 String sfx = d.getSuffix();
325 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
332 public boolean isValidEmail(String email) {
333 for (EmailAddress em : getEmails()) {
334 if (em.getAddress().equals(email)) {
341 public boolean isValidName(String name) {
342 return getName().matches(name);
345 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
347 EmailAddress[] adrs = getEmails();
348 for (int i = 0; i < adrs.length; i++) {
349 if (adrs[i].getAddress().equals(newMail.getAddress())) {
350 if ( !adrs[i].isVerified()) {
351 throw new GigiApiException("Email not verified.");
353 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
354 ps.setString(1, newMail.getAddress());
355 ps.setInt(2, getId());
357 email = newMail.getAddress();
361 throw new GigiApiException("Given address not an address of the user.");
362 } catch (SQLException e) {
363 throw new GigiApiException(e);
367 public void deleteEmail(EmailAddress mail) throws GigiApiException {
368 if (getEmail().equals(mail.getAddress())) {
369 throw new GigiApiException("Can't delete user's default e-mail.");
371 EmailAddress[] emails = getEmails();
372 for (int i = 0; i < emails.length; i++) {
373 if (emails[i].getId() == mail.getId()) {
375 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
376 ps.setDate(1, new Date(System.currentTimeMillis()));
377 ps.setInt(2, mail.getId());
379 } catch (SQLException e) {
381 throw new GigiApiException(e);
386 throw new GigiApiException("Email not one of user's email addresses.");
389 public Assurance[] getReceivedAssurances() throws SQLException {
390 if (receivedAssurances == null) {
391 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
392 query.setInt(1, getId());
393 ResultSet res = query.executeQuery();
395 Assurance[] assurances = new Assurance[res.getRow()];
397 for (int i = 0; i < assurances.length; i++) {
399 assurances[i] = new Assurance(res);
401 this.receivedAssurances = assurances;
404 return receivedAssurances;
407 public Assurance[] getMadeAssurances() throws SQLException {
408 if (madeAssurances == null) {
409 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
410 query.setInt(1, getId());
411 ResultSet res = query.executeQuery();
413 Assurance[] assurances = new Assurance[res.getRow()];
415 for (int i = 0; i < assurances.length; i++) {
417 assurances[i] = new Assurance(res);
419 this.madeAssurances = assurances;
422 return madeAssurances;
425 public void invalidateMadeAssurances() {
426 madeAssurances = null;
429 public void invalidateReceivedAssurances() {
430 receivedAssurances = null;
433 public void updateUserData() throws SQLException, GigiApiException {
434 synchronized (Notary.class) {
435 if (getAssurancePoints() != 0) {
437 throw new GigiApiException("No change after assurance allowed.");
439 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
440 update.setString(1, getFname());
441 update.setString(2, getLname());
442 update.setString(3, getMname());
443 update.setString(4, getSuffix());
444 update.setDate(5, getDob());
445 update.setInt(6, getId());
446 update.executeUpdate();
450 public Language getPrefferedLanguage() {
451 return Language.getInstance("de");
454 public boolean wantsDirectoryListing() throws SQLException {
455 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
456 get.setInt(1, getId());
457 ResultSet exec = get.executeQuery();
459 return exec.getBoolean("listme");
462 public String getContactInformation() throws SQLException {
463 PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
464 get.setInt(1, getId());
465 ResultSet exec = get.executeQuery();
467 return exec.getString("contactinfo");
470 public void setDirectoryListing(boolean on) throws SQLException {
471 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
472 update.setBoolean(1, on);
473 update.setInt(2, getId());
474 update.executeUpdate();
477 public void setContactInformation(String contactInfo) throws SQLException {
478 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
479 update.setString(1, contactInfo);
480 update.setInt(2, getId());
481 update.executeUpdate();