]> WPIA git - gigi.git/blob - src/org/cacert/gigi/EmailAddress.java
UPD: Send verify mail on insert
[gigi.git] / src / org / cacert / gigi / EmailAddress.java
1 package org.cacert.gigi;
2
3 import java.io.IOException;
4 import java.sql.PreparedStatement;
5 import java.sql.ResultSet;
6 import java.sql.SQLException;
7
8 import org.cacert.gigi.database.DatabaseConnection;
9 import org.cacert.gigi.email.EmailProvider;
10 import org.cacert.gigi.util.ServerConstants;
11
12 public class EmailAddress {
13         private String address;
14         private int id;
15         private User owner;
16         private String hash = null;
17
18         private EmailAddress(int id) throws SQLException {
19                 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
20                         "SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
21                 ps.setInt(1, id);
22
23                 ResultSet rs = ps.executeQuery();
24                 if (!rs.next()) {
25                         throw new IllegalArgumentException("Invalid email id " + id);
26                 }
27                 this.id = id;
28                 owner = User.getById(rs.getInt(1));
29                 address = rs.getString(2);
30                 hash = rs.getString(3);
31                 rs.close();
32         }
33
34         public EmailAddress(String address, User owner, String hash) {
35                 this.address = address;
36                 this.owner = owner;
37                 this.hash = hash;
38         }
39
40         public void insert(Language l) {
41                 if (id != 0) {
42                         throw new IllegalStateException("already inserted.");
43                 }
44                 try {
45                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
46                                 "INSERT INTO `email` SET memid=?, hash=?, email=?");
47                         ps.setInt(1, owner.getId());
48                         ps.setString(2, hash);
49                         ps.setString(3, address);
50                         ps.execute();
51                         id = DatabaseConnection.lastInsertId(ps);
52                         StringBuffer body = new StringBuffer();
53                         body.append(l
54                                 .getTranslation("Thanks for signing up with CAcert.org, below is the link you need to open to verify your account. Once your account is verified you will be able to start issuing certificates till your hearts' content!"));
55                         body.append("\n\nhttps://");
56                         body.append(ServerConstants.getWwwHostNamePort());
57                         body.append("/verify?type=email&id=");
58                         body.append(id);
59                         body.append("&hash=");
60                         body.append(hash);
61                         body.append("\n\n");
62                         body.append(l.getTranslation("Best regards"));
63                         body.append("\n");
64                         body.append(l.getTranslation("CAcert.org Support!"));
65                         EmailProvider.getInstance().sendmail(address, "[CAcert.org] " + l.getTranslation("Mail Probe"),
66                                 body.toString(), "support@cacert.org", null, null, null, null, false);
67                 } catch (SQLException e) {
68                         e.printStackTrace();
69                 } catch (IOException e) {
70                         e.printStackTrace();
71                 }
72         }
73
74         public int getId() {
75                 return id;
76         }
77
78         public String getAddress() {
79                 return address;
80         }
81
82         public synchronized void verify(String hash) throws GigiApiException {
83                 if (this.hash.equals(hash)) {
84
85                         try {
86                                 PreparedStatement ps = DatabaseConnection.getInstance()
87                                         .prepare("UPDATE `email` SET hash='' WHERE id=?");
88                                 ps.setInt(1, id);
89                                 ps.execute();
90                                 hash = "";
91
92                                 // Verify user with that primary email
93                                 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
94                                         "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
95                                 ps2.setInt(1, owner.getId());
96                                 ps2.setString(2, address);
97                                 ps2.execute();
98                         } catch (SQLException e) {
99                                 throw new GigiApiException(e);
100                         }
101
102                 } else {
103                         throw new GigiApiException("Email verification hash is invalid.");
104                 }
105         }
106
107         public static EmailAddress getById(int id) throws IllegalArgumentException {
108                 // TODO cache
109                 try {
110                         EmailAddress e = new EmailAddress(id);
111                         return e;
112                 } catch (SQLException e) {
113                         throw new IllegalArgumentException(e);
114                 }
115         }
116 }