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