]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/EmailAddress.java
Fix: synchronization for db objects
[gigi.git] / src / org / cacert / gigi / dbObjects / EmailAddress.java
1 package org.cacert.gigi.dbObjects;
2
3 import java.io.IOException;
4
5 import org.cacert.gigi.GigiApiException;
6 import org.cacert.gigi.database.DatabaseConnection;
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 {
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         GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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         rs.close();
37     }
38
39     public EmailAddress(User owner, String address) {
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     }
47
48     public void insert(Language l) throws GigiApiException {
49         try {
50             synchronized (EmailAddress.class) {
51                 if (id != 0) {
52                     throw new IllegalStateException("already inserted.");
53                 }
54                 GigiPreparedStatement psCheck = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `emails` WHERE email=? AND deleted is NULL");
55                 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("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 is currently valid");
63                 }
64                 ps.execute();
65                 id = ps.lastInsertId();
66                 myCache.put(this);
67             }
68             MailProbe.sendMailProbe(l, "email", id, hash, address);
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             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `emails` SET hash='' WHERE id=?");
85             ps.setInt(1, id);
86             ps.execute();
87             hash = "";
88
89             // Verify user with that primary email
90             GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
91             ps2.setInt(1, owner.getId());
92             ps2.setString(2, address);
93             ps2.execute();
94             this.hash = "";
95
96         } else {
97             throw new GigiApiException("Email verification hash is invalid.");
98         }
99     }
100
101     public boolean isVerified() {
102         return hash.isEmpty();
103     }
104
105     private static ObjectCache<EmailAddress> myCache = new ObjectCache<>();
106
107     public static synchronized EmailAddress getById(int id) throws IllegalArgumentException {
108         EmailAddress em = myCache.get(id);
109         if (em == null) {
110             myCache.put(em = new EmailAddress(id));
111         }
112         return em;
113     }
114 }