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.util.Notary;
11 import org.cacert.gigi.util.PasswordHash;
12 import org.cacert.gigi.util.PasswordStrengthChecker;
18 private Name name = new Name(null, null, null, null);
24 private Assurance[] receivedAssurances, madeAssurances;
31 private void updateName(int id) {
33 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
35 ResultSet rs = ps.executeQuery();
37 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
39 email = rs.getString(6);
42 } catch (SQLException e) {
53 public String getFname() {
57 public String getLname() {
61 public String getMname() {
65 public Name getName() {
69 public void setMname(String mname) {
70 this.name.mname = mname;
73 public String getSuffix() {
77 public void setSuffix(String suffix) {
78 this.name.suffix = suffix;
81 public Date getDob() {
85 public void setDob(Date dob) {
89 public String getEmail() {
93 public void setEmail(String email) {
97 public void setId(int id) {
101 public void setFname(String fname) {
102 this.name.fname = fname;
105 public void setLname(String lname) {
106 this.name.lname = lname;
109 public void insert(String password) throws SQLException {
111 throw new Error("refusing to insert");
113 PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
114 query.setString(1, email);
115 query.setString(2, PasswordHash.hash(password));
116 query.setString(3, name.fname);
117 query.setString(4, name.mname);
118 query.setString(5, name.lname);
119 query.setString(6, name.suffix);
120 query.setDate(7, new java.sql.Date(dob.getTime()));
122 id = DatabaseConnection.lastInsertId(query);
125 public void changePassword(String oldPass, String newPass) throws GigiApiException {
127 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
129 ResultSet rs = ps.executeQuery();
131 throw new GigiApiException("User not found... very bad.");
133 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
134 throw new GigiApiException("Old password does not match.");
137 PasswordStrengthChecker.assertStrongPassword(newPass, this);
138 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
139 ps.setString(1, PasswordHash.hash(newPass));
141 if (ps.executeUpdate() != 1) {
142 throw new GigiApiException("Password update failed.");
144 } catch (SQLException e) {
145 throw new GigiApiException(e);
149 public boolean canAssure() throws SQLException {
150 if (getAssurancePoints() < 100) {
154 return hasPassedCATS();
158 public boolean hasPassedCATS() throws SQLException {
159 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
161 ResultSet rs = query.executeQuery();
169 public int getAssurancePoints() throws SQLException {
170 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
172 ResultSet rs = query.executeQuery();
175 points = rs.getInt(1);
181 public int getExperiencePoints() throws SQLException {
182 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
184 ResultSet rs = query.executeQuery();
187 points = rs.getInt(1) * 2;
194 public boolean equals(Object obj) {
195 if ( !(obj instanceof User)) {
199 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
208 * Gets the maximum allowed points NOW. Note that an assurance needs to
209 * re-check PoJam as it has taken place in the past.
211 * @return the maximal points
212 * @throws SQLException
214 public int getMaxAssurePoints() throws SQLException {
215 int exp = getExperiencePoints();
217 Calendar c = Calendar.getInstance();
219 int year = c.get(Calendar.YEAR);
220 int month = c.get(Calendar.MONTH);
221 int day = c.get(Calendar.DAY_OF_MONTH);
222 c.set(year + 18, month, day);
223 if (System.currentTimeMillis() < c.getTime().getTime()) {
224 return points; // not 18 Years old.
245 public static User getById(int id) {
249 public EmailAddress[] getEmails() {
251 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
253 ResultSet rs = ps.executeQuery();
255 int count = rs.getRow();
256 EmailAddress[] data = new EmailAddress[count];
258 for (int i = 0; i < data.length; i++) {
260 throw new Error("Internal sql api violation.");
262 data[i] = EmailAddress.getById(rs.getInt(1));
266 } catch (SQLException e) {
273 public Domain[] getDomains() {
275 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
277 ResultSet rs = ps.executeQuery();
279 int count = rs.getRow();
280 Domain[] data = new Domain[count];
282 for (int i = 0; i < data.length; i++) {
284 throw new Error("Internal sql api violation.");
286 data[i] = Domain.getById(rs.getInt(1));
290 } catch (SQLException e) {
297 public Certificate[] getCertificates() {
299 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
301 ResultSet rs = ps.executeQuery();
303 int count = rs.getRow();
304 Certificate[] data = new Certificate[count];
306 for (int i = 0; i < data.length; i++) {
308 throw new Error("Internal sql api violation.");
310 data[i] = Certificate.getBySerial(rs.getString(1));
314 } catch (SQLException e) {
321 public boolean isValidDomain(String domainname) {
322 for (Domain d : getDomains()) {
323 String sfx = d.getSuffix();
324 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
331 public boolean isValidEmail(String email) {
332 for (EmailAddress em : getEmails()) {
333 if (em.getAddress().equals(email)) {
340 public boolean isValidName(String name) {
341 return getName().matches(name);
344 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
346 EmailAddress[] adrs = getEmails();
347 for (int i = 0; i < adrs.length; i++) {
348 if (adrs[i].getAddress().equals(newMail.getAddress())) {
349 if ( !adrs[i].isVerified()) {
350 throw new GigiApiException("Email not verified.");
352 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
353 ps.setString(1, newMail.getAddress());
354 ps.setInt(2, getId());
356 email = newMail.getAddress();
360 throw new GigiApiException("Given address not an address of the user.");
361 } catch (SQLException e) {
362 throw new GigiApiException(e);
366 public void deleteEmail(EmailAddress mail) throws GigiApiException {
367 if (getEmail().equals(mail.getAddress())) {
368 throw new GigiApiException("Can't delete user's default e-mail.");
370 EmailAddress[] emails = getEmails();
371 for (int i = 0; i < emails.length; i++) {
372 if (emails[i].getId() == mail.getId()) {
374 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
375 ps.setDate(1, new Date(System.currentTimeMillis()));
376 ps.setInt(2, mail.getId());
378 } catch (SQLException e) {
380 throw new GigiApiException(e);
385 throw new GigiApiException("Email not one of user's email addresses.");
388 public Assurance[] getReceivedAssurances() throws SQLException {
389 if (receivedAssurances == null) {
390 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
391 query.setInt(1, getId());
392 ResultSet res = query.executeQuery();
394 Assurance[] assurances = new Assurance[res.getRow()];
396 for (int i = 0; i < assurances.length; i++) {
398 assurances[i] = new Assurance(res);
400 this.receivedAssurances = assurances;
403 return receivedAssurances;
406 public Assurance[] getMadeAssurances() throws SQLException {
407 if (madeAssurances == null) {
408 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
409 query.setInt(1, getId());
410 ResultSet res = query.executeQuery();
412 Assurance[] assurances = new Assurance[res.getRow()];
414 for (int i = 0; i < assurances.length; i++) {
416 assurances[i] = new Assurance(res);
418 this.madeAssurances = assurances;
421 return madeAssurances;
424 public void invalidateMadeAssurances() {
425 madeAssurances = null;
428 public void invalidateReceivedAssurances() {
429 receivedAssurances = null;
432 public void updateUserData() throws SQLException, GigiApiException {
433 synchronized (Notary.class) {
434 if (getAssurancePoints() != 0) {
436 throw new GigiApiException("No change after assurance allowed.");
438 PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
439 update.setString(1, getFname());
440 update.setString(2, getLname());
441 update.setString(3, getMname());
442 update.setString(4, getSuffix());
443 update.setDate(5, getDob());
444 update.setInt(6, getId());
445 update.executeUpdate();