]> WPIA git - gigi.git/blob - src/org/cacert/gigi/EmailAddress.java
Add api for adding email addresses.
[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 import org.cacert.gigi.database.DatabaseConnection;
7
8 public class EmailAddress {
9         private String address;
10         private int id;
11         private User owner;
12         private String hash = null;
13
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");
17                 ps.setInt(1, id);
18
19                 ResultSet rs = ps.executeQuery();
20                 if (!rs.next()) {
21                         throw new IllegalArgumentException("Invalid email id " + id);
22                 }
23                 this.id = id;
24                 owner = User.getById(rs.getInt(1));
25                 address = rs.getString(2);
26                 hash = rs.getString(3);
27                 rs.close();
28         }
29
30         public EmailAddress(String address, User owner, String hash) {
31                 this.address = address;
32                 this.owner = owner;
33                 this.hash = hash;
34         }
35
36         public void insert() {
37                 if (id != 0) {
38                         throw new IllegalStateException("already inserted.");
39                 }
40                 try {
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);
46                         ps.execute();
47                         id = DatabaseConnection.lastInsertId(ps);
48                 } catch (SQLException e) {
49                         e.printStackTrace();
50                 }
51         }
52
53         public int getId() {
54                 return id;
55         }
56
57         public String getAddress() {
58                 return address;
59         }
60
61         public synchronized void verify(String hash) throws GigiApiException {
62                 if (this.hash.equals(hash)) {
63
64                         try {
65                                 PreparedStatement ps = DatabaseConnection.getInstance()
66                                         .prepare("UPDATE `email` SET hash='' WHERE id=?");
67                                 ps.setInt(1, id);
68                                 ps.execute();
69                                 hash = "";
70
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);
76                                 ps2.execute();
77                         } catch (SQLException e) {
78                                 throw new GigiApiException(e);
79                         }
80
81                 } else {
82                         throw new GigiApiException("Email verification hash is invalid.");
83                 }
84         }
85
86         public static EmailAddress getById(int id) throws IllegalArgumentException {
87                 // TODO cache
88                 try {
89                         EmailAddress e = new EmailAddress(id);
90                         return e;
91                 } catch (SQLException e) {
92                         throw new IllegalArgumentException(e);
93                 }
94         }
95 }