]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/EmailAddress.java
b7bb080fa16adf67bc676e6de6b0edf94fbb74c6
[gigi.git] / src / org / cacert / gigi / dbObjects / EmailAddress.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.IOException;
4 import java.util.Locale;
5
6 import org.cacert.gigi.GigiApiException;
7 import org.cacert.gigi.database.DatabaseConnection;
8 import org.cacert.gigi.database.GigiPreparedStatement;
9 import org.cacert.gigi.database.GigiResultSet;
10 import org.cacert.gigi.email.EmailProvider;
11 import org.cacert.gigi.email.MailProbe;
12 import org.cacert.gigi.localisation.Language;
13 import org.cacert.gigi.util.RandomToken;
14
15 public class EmailAddress implements IdCachable, Verifyable {
16
17     private String address;
18
19     private int id;
20
21     private User owner;
22
23     private String hash = null;
24
25     private EmailAddress(int id) {
26         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `memid`, `email`, `hash` FROM `emails` WHERE `id`=? AND `deleted` IS NULL");
27         ps.setInt(1, id);
28
29         GigiResultSet rs = ps.executeQuery();
30         if ( !rs.next()) {
31             throw new IllegalArgumentException("Invalid email id " + id);
32         }
33         this.id = id;
34         owner = User.getById(rs.getInt(1));
35         address = rs.getString(2);
36         hash = rs.getString(3);
37         rs.close();
38     }
39
40     public EmailAddress(User owner, String address, Locale mailLocale) throws GigiApiException {
41         if ( !EmailProvider.MAIL.matcher(address).matches()) {
42             throw new IllegalArgumentException("Invalid email.");
43         }
44         this.address = address;
45         this.owner = owner;
46         this.hash = RandomToken.generateToken(16);
47         insert(Language.getInstance(mailLocale));
48     }
49
50     private void insert(Language l) throws GigiApiException {
51         try {
52             synchronized (EmailAddress.class) {
53                 if (id != 0) {
54                     throw new IllegalStateException("already inserted.");
55                 }
56                 GigiPreparedStatement psCheck = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `emails` WHERE email=? AND deleted is NULL");
57                 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
58                 ps.setInt(1, owner.getId());
59                 ps.setString(2, hash);
60                 ps.setString(3, address);
61                 psCheck.setString(1, address);
62                 GigiResultSet res = psCheck.executeQuery();
63                 if (res.next()) {
64                     throw new GigiApiException("The email is currently valid");
65                 }
66                 ps.execute();
67                 id = ps.lastInsertId();
68                 myCache.put(this);
69             }
70             MailProbe.sendMailProbe(l, "email", id, hash, address);
71         } catch (IOException e) {
72             e.printStackTrace();
73         }
74     }
75
76     public int getId() {
77         return id;
78     }
79
80     public String getAddress() {
81         return address;
82     }
83
84     public synchronized void verify(String hash) throws GigiApiException {
85         if (this.hash.equals(hash)) {
86             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `emails` SET hash='' WHERE id=?");
87             ps.setInt(1, id);
88             ps.execute();
89             hash = "";
90
91             // Verify user with that primary email
92             GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
93             ps2.setInt(1, owner.getId());
94             ps2.setString(2, address);
95             ps2.execute();
96             this.hash = "";
97
98         } else {
99             throw new GigiApiException("Email verification hash is invalid.");
100         }
101     }
102
103     public boolean isVerified() {
104         return hash.isEmpty();
105     }
106
107     private static ObjectCache<EmailAddress> myCache = new ObjectCache<>();
108
109     public static synchronized EmailAddress getById(int id) throws IllegalArgumentException {
110         EmailAddress em = myCache.get(id);
111         if (em == null) {
112             myCache.put(em = new EmailAddress(id));
113         }
114         return em;
115     }
116 }