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
26 "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),
31 rs.getString(3), rs.getString(4));
33 email = rs.getString(6);
36 } catch (SQLException e) {
45 public String getFname() {
48 public String getLname() {
51 public String getMname() {
54 public Name getName() {
57 public void setMname(String mname) {
58 this.name.mname = mname;
60 public String getSuffix() {
63 public void setSuffix(String suffix) {
64 this.name.suffix = suffix;
66 public Date getDob() {
69 public void setDob(Date dob) {
72 public String getEmail() {
75 public void setEmail(String email) {
78 public void setId(int id) {
81 public void setFname(String fname) {
82 this.name.fname = fname;
84 public void setLname(String lname) {
85 this.name.lname = lname;
87 public void insert(String password) throws SQLException {
89 throw new Error("refusing to insert");
91 PreparedStatement query = DatabaseConnection.getInstance().prepare(
92 "insert into `users` set `email`=?, `password`=?, "
93 + "`fname`=?, `mname`=?, `lname`=?, "
94 + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
95 query.setString(1, email);
96 query.setString(2, PasswordHash.hash(password));
97 query.setString(3, name.fname);
98 query.setString(4, name.mname);
99 query.setString(5, name.lname);
100 query.setString(6, name.suffix);
101 query.setDate(7, new java.sql.Date(dob.getTime()));
103 id = DatabaseConnection.lastInsertId(query);
104 System.out.println("Inserted: " + id);
107 public boolean canAssure() throws SQLException {
108 if (getAssurancePoints() < 100) {
112 return hasPassedCATS();
115 public boolean hasPassedCATS() throws SQLException {
116 PreparedStatement query = DatabaseConnection.getInstance().prepare(
117 "SELECT 1 FROM `cats_passed` where `user_id`=?");
119 ResultSet rs = query.executeQuery();
126 public int getAssurancePoints() throws SQLException {
127 PreparedStatement query = DatabaseConnection
130 "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
132 ResultSet rs = query.executeQuery();
135 points = rs.getInt(1);
140 public int getExperiencePoints() throws SQLException {
141 PreparedStatement query = DatabaseConnection.getInstance().prepare(
142 "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
144 ResultSet rs = query.executeQuery();
147 points = rs.getInt(1) * 2;
153 public boolean equals(Object obj) {
154 if (!(obj instanceof User)) {
158 return name.equals(s.name) && email.equals(s.email)
159 && dob.toString().equals(s.dob.toString()); // This is due to
164 * Gets the maximum allowed points NOW. Note that an assurance needs to
165 * re-check PoJam as it has taken place in the past.
167 * @return the maximal points
168 * @throws SQLException
170 public int getMaxAssurePoints() throws SQLException {
171 int exp = getExperiencePoints();
173 Calendar c = Calendar.getInstance();
175 int year = c.get(Calendar.YEAR);
176 int month = c.get(Calendar.MONTH);
177 int day = c.get(Calendar.DAY_OF_MONTH);
178 c.set(year + 18, month, day);
179 if (System.currentTimeMillis() < c.getTime().getTime()) {
180 return points; // not 18 Years old.