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