1 package org.cacert.gigi;
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
7 import org.cacert.gigi.database.DatabaseConnection;
9 public class EmailAddress {
15 private EmailAddress(int id) throws SQLException {
16 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
17 "SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
20 ResultSet rs = ps.executeQuery();
22 throw new IllegalArgumentException("Invalid email id " + id);
25 owner = User.getById(rs.getInt(1));
26 address = rs.getString(2);
27 hash = rs.getString(3);
35 public String getAddress() {
39 public synchronized void verify(String hash) throws GigiApiException {
40 if (this.hash.equals(hash)) {
43 PreparedStatement ps = DatabaseConnection.getInstance()
44 .prepare("UPDATE `email` SET hash='' WHERE id=?");
49 // Verify user with that primary email
50 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
51 "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
52 ps2.setInt(1, owner.getId());
53 ps2.setString(2, address);
55 } catch (SQLException e) {
56 throw new GigiApiException(e);
60 throw new GigiApiException("Email verification hash is invalid.");
64 public static EmailAddress getById(int id) throws IllegalArgumentException {
67 EmailAddress e = new EmailAddress(id);
69 } catch (SQLException e) {
70 throw new IllegalArgumentException(e);