1 package org.cacert.gigi;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
7 import java.util.Calendar;
9 import org.cacert.gigi.database.DatabaseConnection;
10 import org.cacert.gigi.util.PasswordHash;
15 Name name = new Name(null, null, null, null);
23 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
24 "SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
26 ResultSet rs = ps.executeQuery();
28 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
30 email = rs.getString(6);
33 } catch (SQLException e) {
45 public String getFname() {
49 public String getLname() {
53 public String getMname() {
57 public Name getName() {
61 public void setMname(String mname) {
62 this.name.mname = mname;
65 public String getSuffix() {
69 public void setSuffix(String suffix) {
70 this.name.suffix = suffix;
73 public Date getDob() {
77 public void setDob(Date dob) {
81 public String getEmail() {
85 public void setEmail(String email) {
89 public void setId(int id) {
93 public void setFname(String fname) {
94 this.name.fname = fname;
97 public void setLname(String lname) {
98 this.name.lname = lname;
101 public void insert(String password) throws SQLException {
103 throw new Error("refusing to insert");
105 PreparedStatement query = DatabaseConnection.getInstance().prepare(
106 "insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, "
107 + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
108 query.setString(1, email);
109 query.setString(2, PasswordHash.hash(password));
110 query.setString(3, name.fname);
111 query.setString(4, name.mname);
112 query.setString(5, name.lname);
113 query.setString(6, name.suffix);
114 query.setDate(7, new java.sql.Date(dob.getTime()));
116 id = DatabaseConnection.lastInsertId(query);
119 public boolean canAssure() throws SQLException {
120 if (getAssurancePoints() < 100) {
124 return hasPassedCATS();
128 public boolean hasPassedCATS() throws SQLException {
129 PreparedStatement query = DatabaseConnection.getInstance().prepare(
130 "SELECT 1 FROM `cats_passed` where `user_id`=?");
132 ResultSet rs = query.executeQuery();
140 public int getAssurancePoints() throws SQLException {
141 PreparedStatement query = DatabaseConnection.getInstance().prepare(
142 "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
144 ResultSet rs = query.executeQuery();
147 points = rs.getInt(1);
153 public int getExperiencePoints() throws SQLException {
154 PreparedStatement query = DatabaseConnection.getInstance().prepare(
155 "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
157 ResultSet rs = query.executeQuery();
160 points = rs.getInt(1) * 2;
167 public boolean equals(Object obj) {
168 if (!(obj instanceof User)) {
172 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
181 * Gets the maximum allowed points NOW. Note that an assurance needs to
182 * re-check PoJam as it has taken place in the past.
184 * @return the maximal points
185 * @throws SQLException
187 public int getMaxAssurePoints() throws SQLException {
188 int exp = getExperiencePoints();
190 Calendar c = Calendar.getInstance();
192 int year = c.get(Calendar.YEAR);
193 int month = c.get(Calendar.MONTH);
194 int day = c.get(Calendar.DAY_OF_MONTH);
195 c.set(year + 18, month, day);
196 if (System.currentTimeMillis() < c.getTime().getTime()) {
197 return points; // not 18 Years old.
218 public static User getById(int id) {
222 public EmailAddress[] getEmails() {
224 PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM email WHERE memid=?");
226 ResultSet rs = ps.executeQuery();
228 int count = rs.getRow();
229 EmailAddress[] data = new EmailAddress[count];
231 for (int i = 0; i < data.length; i++) {
233 throw new Error("Internal sql api violation.");
235 data[i] = EmailAddress.getById(rs.getInt(1));
239 } catch (SQLException e) {