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.PasswordHash;
11 import org.cacert.gigi.util.PasswordStrengthChecker;
17 Name name = new Name(null, null, null, null);
26 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
28 ResultSet rs = ps.executeQuery();
30 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
32 email = rs.getString(6);
35 } catch (SQLException e) {
46 public String getFname() {
50 public String getLname() {
54 public String getMname() {
58 public Name getName() {
62 public void setMname(String mname) {
63 this.name.mname = mname;
66 public String getSuffix() {
70 public void setSuffix(String suffix) {
71 this.name.suffix = suffix;
74 public Date getDob() {
78 public void setDob(Date dob) {
82 public String getEmail() {
86 public void setEmail(String email) {
90 public void setId(int id) {
94 public void setFname(String fname) {
95 this.name.fname = fname;
98 public void setLname(String lname) {
99 this.name.lname = lname;
102 public void insert(String password) throws SQLException {
104 throw new Error("refusing to insert");
106 PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
107 query.setString(1, email);
108 query.setString(2, PasswordHash.hash(password));
109 query.setString(3, name.fname);
110 query.setString(4, name.mname);
111 query.setString(5, name.lname);
112 query.setString(6, name.suffix);
113 query.setDate(7, new java.sql.Date(dob.getTime()));
115 id = DatabaseConnection.lastInsertId(query);
118 public void changePassword(String oldPass, String newPass) throws GigiApiException {
120 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
122 ResultSet rs = ps.executeQuery();
124 throw new GigiApiException("User not found... very bad.");
126 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
127 throw new GigiApiException("Old password does not match.");
130 PasswordStrengthChecker.assertStrongPassword(newPass, this);
131 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
132 ps.setString(1, PasswordHash.hash(newPass));
134 if (ps.executeUpdate() != 1) {
135 throw new GigiApiException("Password update failed.");
137 } catch (SQLException e) {
138 throw new GigiApiException(e);
142 public boolean canAssure() throws SQLException {
143 if (getAssurancePoints() < 100) {
147 return hasPassedCATS();
151 public boolean hasPassedCATS() throws SQLException {
152 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
154 ResultSet rs = query.executeQuery();
162 public int getAssurancePoints() throws SQLException {
163 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
165 ResultSet rs = query.executeQuery();
168 points = rs.getInt(1);
174 public int getExperiencePoints() throws SQLException {
175 PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
177 ResultSet rs = query.executeQuery();
180 points = rs.getInt(1) * 2;
187 public boolean equals(Object obj) {
188 if ( !(obj instanceof User)) {
192 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
201 * Gets the maximum allowed points NOW. Note that an assurance needs to
202 * re-check PoJam as it has taken place in the past.
204 * @return the maximal points
205 * @throws SQLException
207 public int getMaxAssurePoints() throws SQLException {
208 int exp = getExperiencePoints();
210 Calendar c = Calendar.getInstance();
212 int year = c.get(Calendar.YEAR);
213 int month = c.get(Calendar.MONTH);
214 int day = c.get(Calendar.DAY_OF_MONTH);
215 c.set(year + 18, month, day);
216 if (System.currentTimeMillis() < c.getTime().getTime()) {
217 return points; // not 18 Years old.
238 public static User getById(int id) {
242 public EmailAddress[] getEmails() {
244 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
246 ResultSet rs = ps.executeQuery();
248 int count = rs.getRow();
249 EmailAddress[] data = new EmailAddress[count];
251 for (int i = 0; i < data.length; i++) {
253 throw new Error("Internal sql api violation.");
255 data[i] = EmailAddress.getById(rs.getInt(1));
259 } catch (SQLException e) {
266 public Domain[] getDomains() {
268 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
270 ResultSet rs = ps.executeQuery();
272 int count = rs.getRow();
273 Domain[] data = new Domain[count];
275 for (int i = 0; i < data.length; i++) {
277 throw new Error("Internal sql api violation.");
279 data[i] = Domain.getById(rs.getInt(1));
283 } catch (SQLException e) {
290 public Certificate[] getCertificates() {
292 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
294 ResultSet rs = ps.executeQuery();
296 int count = rs.getRow();
297 Certificate[] data = new Certificate[count];
299 for (int i = 0; i < data.length; i++) {
301 throw new Error("Internal sql api violation.");
303 data[i] = Certificate.getBySerial(rs.getString(1));
307 } catch (SQLException e) {
314 public boolean isValidDomain(String domainname) {
315 for (Domain d : getDomains()) {
316 String sfx = d.getSuffix();
317 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
324 public boolean isValidEmail(String email) {
325 for (EmailAddress em : getEmails()) {
326 if (em.getAddress().equals(email)) {
333 public boolean isValidName(String name) {
334 return getName().matches(name);
337 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
339 EmailAddress[] adrs = getEmails();
340 for (int i = 0; i < adrs.length; i++) {
341 if (adrs[i].getAddress().equals(newMail.getAddress())) {
342 if ( !adrs[i].isVerified()) {
343 throw new GigiApiException("Email not verified.");
345 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
346 ps.setString(1, newMail.getAddress());
347 ps.setInt(2, getId());
349 email = newMail.getAddress();
353 throw new GigiApiException("Given address not an address of the user.");
354 } catch (SQLException e) {
355 throw new GigiApiException(e);
359 public void deleteEmail(EmailAddress mail) throws GigiApiException {
360 if (getEmail().equals(mail.getAddress())) {
361 throw new GigiApiException("Can't delete user's default e-mail.");
363 EmailAddress[] emails = getEmails();
364 for (int i = 0; i < emails.length; i++) {
365 if (emails[i].getId() == mail.getId()) {
367 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
368 ps.setDate(1, new Date(System.currentTimeMillis()));
369 ps.setInt(2, mail.getId());
371 } catch (SQLException e) {
373 throw new GigiApiException(e);
378 throw new GigiApiException("Email not one of user's email addresses.");