]> WPIA git - gigi.git/blob - src/org/cacert/gigi/EmailAddress.java
Merge remote-tracking branch 'origin/emailMgmt'
[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.RandomToken;
11 import org.cacert.gigi.util.ServerConstants;
12
13 public class EmailAddress {
14         private String address;
15         private int id;
16         private User owner;
17         private String hash = null;
18
19         private EmailAddress(int id) throws SQLException {
20                 PreparedStatement ps = DatabaseConnection.getInstance().prepare(
21                         "SELECT memid, email, hash FROM `email` WHERE id=? AND deleted=0");
22                 ps.setInt(1, id);
23
24                 ResultSet rs = ps.executeQuery();
25                 if (!rs.next()) {
26                         throw new IllegalArgumentException("Invalid email id " + id);
27                 }
28                 this.id = id;
29                 owner = User.getById(rs.getInt(1));
30                 address = rs.getString(2);
31                 hash = rs.getString(3);
32                 rs.close();
33         }
34
35         public EmailAddress(String address, User owner) {
36                 if (!EmailProvider.MAIL.matcher(address).matches()) {
37                         throw new IllegalArgumentException("Invalid email.");
38                 }
39                 this.address = address;
40                 this.owner = owner;
41                 this.hash = RandomToken.generateToken(16);
42         }
43
44         public void insert(Language l) {
45                 if (id != 0) {
46                         throw new IllegalStateException("already inserted.");
47                 }
48                 try {
49                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
50                                 "INSERT INTO `email` SET memid=?, hash=?, email=?");
51                         ps.setInt(1, owner.getId());
52                         ps.setString(2, hash);
53                         ps.setString(3, address);
54                         ps.execute();
55                         id = DatabaseConnection.lastInsertId(ps);
56                         StringBuffer body = new StringBuffer();
57                         body.append(l
58                                 .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!"));
59                         body.append("\n\nhttps://");
60                         body.append(ServerConstants.getWwwHostNamePort());
61                         body.append("/verify?type=email&id=");
62                         body.append(id);
63                         body.append("&hash=");
64                         body.append(hash);
65                         body.append("\n\n");
66                         body.append(l.getTranslation("Best regards"));
67                         body.append("\n");
68                         body.append(l.getTranslation("CAcert.org Support!"));
69                         EmailProvider.getInstance().sendmail(address, "[CAcert.org] " + l.getTranslation("Mail Probe"),
70                                 body.toString(), "support@cacert.org", null, null, null, null, false);
71                 } catch (SQLException e) {
72                         e.printStackTrace();
73                 } catch (IOException e) {
74                         e.printStackTrace();
75                 }
76         }
77
78         public int getId() {
79                 return id;
80         }
81
82         public String getAddress() {
83                 return address;
84         }
85
86         public synchronized void verify(String hash) throws GigiApiException {
87                 if (this.hash.equals(hash)) {
88
89                         try {
90                                 PreparedStatement ps = DatabaseConnection.getInstance()
91                                         .prepare("UPDATE `email` SET hash='' WHERE id=?");
92                                 ps.setInt(1, id);
93                                 ps.execute();
94                                 hash = "";
95
96                                 // Verify user with that primary email
97                                 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare(
98                                         "update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
99                                 ps2.setInt(1, owner.getId());
100                                 ps2.setString(2, address);
101                                 ps2.execute();
102                                 this.hash = "";
103                         } catch (SQLException e) {
104                                 throw new GigiApiException(e);
105                         }
106
107                 } else {
108                         throw new GigiApiException("Email verification hash is invalid.");
109                 }
110         }
111
112         public static EmailAddress getById(int id) throws IllegalArgumentException {
113                 // TODO cache
114                 try {
115                         EmailAddress e = new EmailAddress(id);
116                         return e;
117                 } catch (SQLException e) {
118                         throw new IllegalArgumentException(e);
119                 }
120         }
121
122         public boolean isVerified() {
123                 return hash.isEmpty();
124         }
125 }