1 package org.cacert.gigi;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import org.cacert.gigi.database.DatabaseConnection;
8 public class EmailAddress {
9 private String address;
12 private String hash = null;
14 private EmailAddress(int id) throws SQLException {
15 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
16 "SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
19 ResultSet rs = ps.executeQuery();
21 throw new IllegalArgumentException("Invalid email id " + id);
24 owner = User.getById(rs.getInt(1));
25 address = rs.getString(2);
26 hash = rs.getString(3);
30 public EmailAddress(String address, User owner, String hash) {
31 this.address = address;
36 public void insert() {
38 throw new IllegalStateException("already inserted.");
41 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
42 "INSERT INTO `email` SET memid=?, hash=?, email=?");
43 ps.setInt(1, owner.getId());
44 ps.setString(2, hash);
45 ps.setString(3, address);
47 id = DatabaseConnection.lastInsertId(ps);
48 } catch (SQLException e) {
57 public String getAddress() {
61 public synchronized void verify(String hash) throws GigiApiException {
62 if (this.hash.equals(hash)) {
65 PreparedStatement ps = DatabaseConnection.getInstance()
66 .prepare("UPDATE `email` SET hash='' WHERE id=?");
71 // Verify user with that primary email
72 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
73 "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
74 ps2.setInt(1, owner.getId());
75 ps2.setString(2, address);
77 } catch (SQLException e) {
78 throw new GigiApiException(e);
82 throw new GigiApiException("Email verification hash is invalid.");
86 public static EmailAddress getById(int id) throws IllegalArgumentException {
89 EmailAddress e = new EmailAddress(id);
91 } catch (SQLException e) {
92 throw new IllegalArgumentException(e);