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;
16 Name name = new Name(null, null, null, null);
24 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
25 "SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
27 ResultSet rs = ps.executeQuery();
29 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
31 email = rs.getString(6);
34 } 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(
107 "insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, "
108 + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
109 query.setString(1, email);
110 query.setString(2, PasswordHash.hash(password));
111 query.setString(3, name.fname);
112 query.setString(4, name.mname);
113 query.setString(5, name.lname);
114 query.setString(6, name.suffix);
115 query.setDate(7, new java.sql.Date(dob.getTime()));
117 id = DatabaseConnection.lastInsertId(query);
120 public void changePassword(String oldPass, String newPass) throws GigiApiException {
122 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
124 ResultSet rs = ps.executeQuery();
126 throw new GigiApiException("User not found... very bad.");
128 if (!PasswordHash.verifyHash(oldPass, rs.getString(1))) {
129 throw new GigiApiException("Old password does not match.");
132 PasswordStrengthChecker.assertStrongPassword(newPass, this);
133 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
134 ps.setString(1, PasswordHash.hash(newPass));
136 if (ps.executeUpdate() != 1) {
137 throw new GigiApiException("Password update failed.");
139 } catch (SQLException e) {
140 throw new GigiApiException(e);
144 public boolean canAssure() throws SQLException {
145 if (getAssurancePoints() < 100) {
149 return hasPassedCATS();
153 public boolean hasPassedCATS() throws SQLException {
154 PreparedStatement query = DatabaseConnection.getInstance().prepare(
155 "SELECT 1 FROM `cats_passed` where `user_id`=?");
157 ResultSet rs = query.executeQuery();
165 public int getAssurancePoints() throws SQLException {
166 PreparedStatement query = DatabaseConnection.getInstance().prepare(
167 "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
169 ResultSet rs = query.executeQuery();
172 points = rs.getInt(1);
178 public int getExperiencePoints() throws SQLException {
179 PreparedStatement query = DatabaseConnection.getInstance().prepare(
180 "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
182 ResultSet rs = query.executeQuery();
185 points = rs.getInt(1) * 2;
192 public boolean equals(Object obj) {
193 if (!(obj instanceof User)) {
197 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
206 * Gets the maximum allowed points NOW. Note that an assurance needs to
207 * re-check PoJam as it has taken place in the past.
209 * @return the maximal points
210 * @throws SQLException
212 public int getMaxAssurePoints() throws SQLException {
213 int exp = getExperiencePoints();
215 Calendar c = Calendar.getInstance();
217 int year = c.get(Calendar.YEAR);
218 int month = c.get(Calendar.MONTH);
219 int day = c.get(Calendar.DAY_OF_MONTH);
220 c.set(year + 18, month, day);
221 if (System.currentTimeMillis() < c.getTime().getTime()) {
222 return points; // not 18 Years old.
243 public static User getById(int id) {
247 public EmailAddress[] getEmails() {
249 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM email WHERE memid=?");
251 ResultSet rs = ps.executeQuery();
253 int count = rs.getRow();
254 EmailAddress[] data = new EmailAddress[count];
256 for (int i = 0; i < data.length; i++) {
258 throw new Error("Internal sql api violation.");
260 data[i] = EmailAddress.getById(rs.getInt(1));
264 } catch (SQLException e) {
271 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
273 EmailAddress[] adrs = getEmails();
274 for (int i = 0; i < adrs.length; i++) {
275 if (adrs[i].getAddress().equals(newMail.getAddress())) {
276 if (!adrs[i].isVerified()) {
277 throw new GigiApiException("Email not verified.");
279 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
280 "UPDATE users SET email=? WHERE id=?");
281 ps.setString(1, newMail.getAddress());
282 ps.setInt(2, getId());
284 email = newMail.getAddress();
288 throw new GigiApiException("Given address not an address of the user.");
289 } catch (SQLException e) {
290 throw new GigiApiException(e);
294 public void deleteEmail(EmailAddress mail) throws GigiApiException {
295 if (getEmail().equals(mail.getAddress())) {
296 throw new GigiApiException("Can't delete user's default e-mail.");
299 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE email SET deleted=1 WHERE id=?");
300 ps.setInt(1, mail.getId());
302 } catch (SQLException e) {
303 throw new GigiApiException(e);