]> WPIA git - gigi.git/blob - src/org/cacert/gigi/User.java
Implement a Template-Foreach (and use in "new email certificate")
[gigi.git] / src / org / cacert / gigi / User.java
1 package org.cacert.gigi;
2
3 import java.sql.PreparedStatement;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.sql.Date;
7 import java.util.Calendar;
8
9 import org.cacert.gigi.database.DatabaseConnection;
10 import org.cacert.gigi.util.PasswordHash;
11
12 public class User {
13
14         private int id;
15         Name name = new Name(null, null, null, null);
16
17         Date dob;
18         String email;
19
20         public User(int id) {
21                 this.id = id;
22                 try {
23                         PreparedStatement ps = DatabaseConnection.getInstance().prepare(
24                                 "SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?");
25                         ps.setInt(1, id);
26                         ResultSet rs = ps.executeQuery();
27                         if (rs.next()) {
28                                 name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4));
29                                 dob = rs.getDate(5);
30                                 email = rs.getString(6);
31                         }
32                         rs.close();
33                 } catch (SQLException e) {
34                         e.printStackTrace();
35                 }
36         }
37
38         public User() {
39         }
40
41         public int getId() {
42                 return id;
43         }
44
45         public String getFname() {
46                 return name.fname;
47         }
48
49         public String getLname() {
50                 return name.lname;
51         }
52
53         public String getMname() {
54                 return name.mname;
55         }
56
57         public Name getName() {
58                 return name;
59         }
60
61         public void setMname(String mname) {
62                 this.name.mname = mname;
63         }
64
65         public String getSuffix() {
66                 return name.suffix;
67         }
68
69         public void setSuffix(String suffix) {
70                 this.name.suffix = suffix;
71         }
72
73         public Date getDob() {
74                 return dob;
75         }
76
77         public void setDob(Date dob) {
78                 this.dob = dob;
79         }
80
81         public String getEmail() {
82                 return email;
83         }
84
85         public void setEmail(String email) {
86                 this.email = email;
87         }
88
89         public void setId(int id) {
90                 this.id = id;
91         }
92
93         public void setFname(String fname) {
94                 this.name.fname = fname;
95         }
96
97         public void setLname(String lname) {
98                 this.name.lname = lname;
99         }
100
101         public void insert(String password) throws SQLException {
102                 if (id != 0) {
103                         throw new Error("refusing to insert");
104                 }
105                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
106                         "insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, "
107                                 + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0");
108                 query.setString(1, email);
109                 query.setString(2, PasswordHash.hash(password));
110                 query.setString(3, name.fname);
111                 query.setString(4, name.mname);
112                 query.setString(5, name.lname);
113                 query.setString(6, name.suffix);
114                 query.setDate(7, new java.sql.Date(dob.getTime()));
115                 query.execute();
116                 id = DatabaseConnection.lastInsertId(query);
117                 System.out.println("Inserted: " + id);
118         }
119
120         public boolean canAssure() throws SQLException {
121                 if (getAssurancePoints() < 100) {
122                         return false;
123                 }
124
125                 return hasPassedCATS();
126
127         }
128
129         public boolean hasPassedCATS() throws SQLException {
130                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
131                         "SELECT 1 FROM `cats_passed` where `user_id`=?");
132                 query.setInt(1, id);
133                 ResultSet rs = query.executeQuery();
134                 if (rs.next()) {
135                         return true;
136                 } else {
137                         return false;
138                 }
139         }
140
141         public int getAssurancePoints() throws SQLException {
142                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
143                         "SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
144                 query.setInt(1, id);
145                 ResultSet rs = query.executeQuery();
146                 int points = 0;
147                 if (rs.next()) {
148                         points = rs.getInt(1);
149                 }
150                 rs.close();
151                 return points;
152         }
153
154         public int getExperiencePoints() throws SQLException {
155                 PreparedStatement query = DatabaseConnection.getInstance().prepare(
156                         "SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
157                 query.setInt(1, id);
158                 ResultSet rs = query.executeQuery();
159                 int points = 0;
160                 if (rs.next()) {
161                         points = rs.getInt(1) * 2;
162                 }
163                 rs.close();
164                 return points;
165         }
166
167         @Override
168         public boolean equals(Object obj) {
169                 if (!(obj instanceof User)) {
170                         return false;
171                 }
172                 User s = (User) obj;
173                 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
174                                                                                                                                                                                                                 // is
175                                                                                                                                                                                                                 // due
176                                                                                                                                                                                                                 // to
177                                                                                                                                                                                                                 // day
178                                                                                                                                                                                                                 // cutoff
179         }
180
181         /**
182          * Gets the maximum allowed points NOW. Note that an assurance needs to
183          * re-check PoJam as it has taken place in the past.
184          * 
185          * @return the maximal points
186          * @throws SQLException
187          */
188         public int getMaxAssurePoints() throws SQLException {
189                 int exp = getExperiencePoints();
190                 int points = 10;
191                 Calendar c = Calendar.getInstance();
192                 c.setTime(dob);
193                 int year = c.get(Calendar.YEAR);
194                 int month = c.get(Calendar.MONTH);
195                 int day = c.get(Calendar.DAY_OF_MONTH);
196                 c.set(year + 18, month, day);
197                 if (System.currentTimeMillis() < c.getTime().getTime()) {
198                         return points; // not 18 Years old.
199                 }
200
201                 if (exp >= 10) {
202                         points += 5;
203                 }
204                 if (exp >= 20) {
205                         points += 5;
206                 }
207                 if (exp >= 30) {
208                         points += 5;
209                 }
210                 if (exp >= 40) {
211                         points += 5;
212                 }
213                 if (exp >= 50) {
214                         points += 5;
215                 }
216                 return points;
217         }
218 }