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);
117 System.out.println("Inserted: " + id);
120 public boolean canAssure() throws SQLException {
121 if (getAssurancePoints() < 100) {
125 return hasPassedCATS();
129 public boolean hasPassedCATS() throws SQLException {
130 PreparedStatement query = DatabaseConnection.getInstance().prepare(
131 "SELECT 1 FROM `cats_passed` where `user_id`=?");
133 ResultSet rs = query.executeQuery();
141 public int getAssurancePoints() throws SQLException {
142 PreparedStatement query = DatabaseConnection.getInstance().prepare(
143 "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
145 ResultSet rs = query.executeQuery();
148 points = rs.getInt(1);
154 public int getExperiencePoints() throws SQLException {
155 PreparedStatement query = DatabaseConnection.getInstance().prepare(
156 "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
158 ResultSet rs = query.executeQuery();
161 points = rs.getInt(1) * 2;
168 public boolean equals(Object obj) {
169 if (!(obj instanceof User)) {
173 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
182 * Gets the maximum allowed points NOW. Note that an assurance needs to
183 * re-check PoJam as it has taken place in the past.
185 * @return the maximal points
186 * @throws SQLException
188 public int getMaxAssurePoints() throws SQLException {
189 int exp = getExperiencePoints();
191 Calendar c = Calendar.getInstance();
193 int year = c.get(Calendar.YEAR);
194 int month = c.get(Calendar.MONTH);
195 int day = c.get(Calendar.DAY_OF_MONTH);
196 c.set(year + 18, month, day);
197 if (System.currentTimeMillis() < c.getTime().getTime()) {
198 return points; // not 18 Years old.