]> WPIA git - gigi.git/blob - src/org/cacert/gigi/dbObjects/EmailAddress.java
[EMPTY] Organize all imports
[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=0");
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) {
49         if (id != 0) {
50             throw new IllegalStateException("already inserted.");
51         }
52         try {
53             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO `emails` SET memid=?, hash=?, email=?");
54             ps.setInt(1, owner.getId());
55             ps.setString(2, hash);
56             ps.setString(3, address);
57             synchronized (EmailAddress.class) {
58                 ps.execute();
59                 id = ps.lastInsertId();
60                 myCache.put(this);
61             }
62             MailProbe.sendMailProbe(l, "email", id, hash, address);
63         } catch (IOException e) {
64             e.printStackTrace();
65         }
66     }
67
68     public int getId() {
69         return id;
70     }
71
72     public String getAddress() {
73         return address;
74     }
75
76     public synchronized void verify(String hash) throws GigiApiException {
77         if (this.hash.equals(hash)) {
78             GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE `emails` SET hash='' WHERE id=?");
79             ps.setInt(1, id);
80             ps.execute();
81             hash = "";
82
83             // Verify user with that primary email
84             GigiPreparedStatement ps2 = DatabaseConnection.getInstance().prepare("update `users` set `verified`='1' where `id`=? and `email`=? and `verified`='0'");
85             ps2.setInt(1, owner.getId());
86             ps2.setString(2, address);
87             ps2.execute();
88             this.hash = "";
89
90         } else {
91             throw new GigiApiException("Email verification hash is invalid.");
92         }
93     }
94
95     public boolean isVerified() {
96         return hash.isEmpty();
97     }
98
99     private static ObjectCache<EmailAddress> myCache = new ObjectCache<>();
100
101     public static synchronized EmailAddress getById(int id) throws IllegalArgumentException {
102         EmailAddress em = myCache.get(id);
103         if (em == null) {
104             myCache.put(em = new EmailAddress(id));
105         }
106         return em;
107     }
108 }