]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/EmailAddress.java
Move the "dbObject"s to their own package.
[gigi.git] / src / org / cacert / gigi / dbObjects / EmailAddress.java
1 package org.cacert.gigi.dbObjects;
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.GigiApiException;
9 import org.cacert.gigi.database.DatabaseConnection;
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 {
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) throws SQLException {
26         PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT memid, email, hash FROM `emails` WHERE id=? AND deleted=0");
27         ps.setInt(1, id);
28
29         ResultSet 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(String address, User owner) {
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     }
48
49     public void insert(Language l) {
50         if (id != 0) {
51             throw new IllegalStateException("already inserted.");
52         }
53         try {
54             PreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
55             ps.setInt(1, owner.getId());
56             ps.setString(2, hash);
57             ps.setString(3, address);
58             synchronized (EmailAddress.class) {
59                 ps.execute();
60                 id = DatabaseConnection.lastInsertId(ps);
61                 myCache.put(this);
62             }
63             MailProbe.sendMailProbe(l, "email", id, hash, address);
64         } catch (SQLException e) {
65             e.printStackTrace();
66         } catch (IOException e) {
67             e.printStackTrace();
68         }
69     }
70
71     public int getId() {
72         return id;
73     }
74
75     public String getAddress() {
76         return address;
77     }
78
79     public synchronized void verify(String hash) throws GigiApiException {
80         if (this.hash.equals(hash)) {
81
82             try {
83                 PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `emails` SET hash='' WHERE id=?");
84                 ps.setInt(1, id);
85                 ps.execute();
86                 hash = "";
87
88                 // Verify user with that primary email
89                 PreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
90                 ps2.setInt(1, owner.getId());
91                 ps2.setString(2, address);
92                 ps2.execute();
93                 this.hash = "";
94             } catch (SQLException e) {
95                 throw new GigiApiException(e);
96             }
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 EmailAddress getById(int id) throws IllegalArgumentException {
110         EmailAddress em = myCache.get(id);
111         if (em == null) {
112             try {
113                 synchronized (EmailAddress.class) {
114                     myCache.put(em = new EmailAddress(id));
115                 }
116             } catch (SQLException e1) {
117                 throw new IllegalArgumentException(e1);
118             }
119         }
120         return em;
121     }
122 }