]> WPIA git - gigi.git/blob - src/org/cacert/gigi/EmailAddress.java
fda01ed06b69de28005be3555b2760b283a199e7
[gigi.git] / src / org / cacert / gigi / EmailAddress.java
1 package org.cacert.gigi;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6
7 import org.cacert.gigi.database.DatabaseConnection;
8
9 public class EmailAddress {
10         String address;
11         int id;
12         User owner;
13         String hash = null;
14
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");
18                 ps.setInt(1, id);
19
20                 ResultSet rs = ps.executeQuery();
21                 if (!rs.next()) {
22                         throw new IllegalArgumentException("Invalid email id " + id);
23                 }
24                 this.id = id;
25                 owner = User.getById(rs.getInt(1));
26                 address = rs.getString(2);
27                 hash = rs.getString(3);
28                 rs.close();
29         }
30
31         public int getId() {
32                 return id;
33         }
34
35         public String getAddress() {
36                 return address;
37         }
38
39         public synchronized void verify(String hash) throws GigiApiException {
40                 if (this.hash.equals(hash)) {
41
42                         try {
43                                 PreparedStatement ps = DatabaseConnection.getInstance()
44                                         .prepare("UPDATE `email` SET hash='' WHERE id=?");
45                                 ps.setInt(1, id);
46                                 ps.execute();
47                                 hash = "";
48
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);
54                                 ps2.execute();
55                         } catch (SQLException e) {
56                                 throw new GigiApiException(e);
57                         }
58
59                 } else {
60                         throw new GigiApiException("Email verification hash is invalid.");
61                 }
62         }
63
64         public static EmailAddress getById(int id) throws IllegalArgumentException {
65                 // TODO cache
66                 try {
67                         EmailAddress e = new EmailAddress(id);
68                         return e;
69                 } catch (SQLException e) {
70                         throw new IllegalArgumentException(e);
71                 }
72         }
73 }