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