1 package org.cacert.gigi.dbObjects;
4 import java.util.ArrayList;
5 import java.util.Calendar;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.LinkedList;
10 import java.util.Locale;
13 import org.cacert.gigi.GigiApiException;
14 import org.cacert.gigi.database.DatabaseConnection;
15 import org.cacert.gigi.database.GigiPreparedStatement;
16 import org.cacert.gigi.database.GigiResultSet;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.util.Notary;
19 import org.cacert.gigi.util.PasswordHash;
20 import org.cacert.gigi.util.PasswordStrengthChecker;
22 public class User extends CertificateOwner {
24 private Name name = new Name(null, null, null, null);
30 private Assurance[] receivedAssurances;
32 private Assurance[] madeAssurances;
34 private Locale locale;
36 private Set<Group> groups = new HashSet<>();
38 protected User(GigiResultSet rs) {
39 super(rs.getInt("id"));
43 private void updateName(GigiResultSet rs) {
44 name = new Name(rs.getString("fname"), rs.getString("lname"), rs.getString("mname"), rs.getString("suffix"));
45 dob = rs.getDate("dob");
46 email = rs.getString("email");
48 String localeStr = rs.getString("language");
49 if (localeStr == null || localeStr.equals("")) {
50 locale = Locale.getDefault();
52 locale = Language.getLocaleFromString(localeStr);
55 GigiPreparedStatement psg = DatabaseConnection.getInstance().prepare("SELECT permission FROM user_groups WHERE user=? AND deleted is NULL");
56 psg.setInt(1, rs.getInt("id"));
58 try (GigiResultSet rs2 = psg.executeQuery()) {
60 groups.add(Group.getByString(rs2.getString(1)));
67 public Name getName() {
71 public Date getDoB() {
75 public void setDoB(Date dob) {
79 public String getEmail() {
83 public void setEmail(String email) {
87 public void insert(String password) {
88 int id = super.insert();
89 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `language`=?, id=?");
90 query.setString(1, email);
91 query.setString(2, PasswordHash.hash(password));
92 query.setString(3, name.getFname());
93 query.setString(4, name.getMname());
94 query.setString(5, name.getLname());
95 query.setString(6, name.getSuffix());
96 query.setDate(7, new java.sql.Date(dob.getTime()));
97 query.setString(8, locale.toString());
102 public void changePassword(String oldPass, String newPass) throws GigiApiException {
103 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
104 ps.setInt(1, getId());
105 try (GigiResultSet rs = ps.executeQuery()) {
107 throw new GigiApiException("User not found... very bad.");
109 if (PasswordHash.verifyHash(oldPass, rs.getString(1)) == null) {
110 throw new GigiApiException("Old password does not match.");
114 PasswordStrengthChecker.assertStrongPassword(newPass, this);
115 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
116 ps.setString(1, PasswordHash.hash(newPass));
117 ps.setInt(2, getId());
121 public void setName(Name name) {
125 public boolean canAssure() {
126 if ( !isOfAge(14)) { // PoJAM
129 if (getAssurancePoints() < 100) {
133 return hasPassedCATS();
137 public boolean hasPassedCATS() {
138 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
139 query.setInt(1, getId());
140 try (GigiResultSet rs = query.executeQuery()) {
149 public int getAssurancePoints() {
150 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL");
151 query.setInt(1, getId());
153 try (GigiResultSet rs = query.executeQuery()) {
157 points = rs.getInt(1);
164 public int getExperiencePoints() {
165 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted` is NULL");
166 query.setInt(1, getId());
168 try (GigiResultSet rs = query.executeQuery()) {
172 points = rs.getInt(1) * 2;
180 * Gets the maximum allowed points NOW. Note that an assurance needs to
181 * re-check PoJam as it has taken place in the past.
183 * @return the maximal points @
185 public int getMaxAssurePoints() {
190 int exp = getExperiencePoints();
212 public boolean isOfAge(int desiredAge) {
213 Calendar c = Calendar.getInstance();
215 int year = c.get(Calendar.YEAR);
216 int month = c.get(Calendar.MONTH);
217 int day = c.get(Calendar.DAY_OF_MONTH);
218 c.set(year, month, day);
219 c.add(Calendar.YEAR, desiredAge);
220 return System.currentTimeMillis() >= c.getTime().getTime();
223 public boolean isValidName(String name) {
224 return getName().matches(name);
227 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
228 for (EmailAddress email : getEmails()) {
229 if (email.getAddress().equals(newMail.getAddress())) {
230 if ( !email.isVerified()) {
231 throw new GigiApiException("Email not verified.");
234 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
235 ps.setString(1, newMail.getAddress());
236 ps.setInt(2, getId());
239 this.email = newMail.getAddress();
244 throw new GigiApiException("Given address not an address of the user.");
247 public void deleteEmail(EmailAddress delMail) throws GigiApiException {
248 if (getEmail().equals(delMail.getAddress())) {
249 throw new GigiApiException("Can't delete user's default e-mail.");
252 for (EmailAddress email : getEmails()) {
253 if (email.getId() == delMail.getId()) {
254 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
255 ps.setDate(1, new Date(System.currentTimeMillis()));
256 ps.setInt(2, delMail.getId());
261 throw new GigiApiException("Email not one of user's email addresses.");
264 public synchronized Assurance[] getReceivedAssurances() {
265 if (receivedAssurances == null) {
266 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted IS NULL");
267 query.setInt(1, getId());
269 try (GigiResultSet res = query.executeQuery()) {
270 List<Assurance> assurances = new LinkedList<Assurance>();
273 assurances.add(new Assurance(res));
276 this.receivedAssurances = assurances.toArray(new Assurance[0]);
280 return receivedAssurances;
283 public synchronized Assurance[] getMadeAssurances() {
284 if (madeAssurances == null) {
285 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted is NULL");
286 query.setInt(1, getId());
288 try (GigiResultSet res = query.executeQuery()) {
289 List<Assurance> assurances = new LinkedList<Assurance>();
292 assurances.add(new Assurance(res));
295 this.madeAssurances = assurances.toArray(new Assurance[0]);
299 return madeAssurances;
302 public synchronized void invalidateMadeAssurances() {
303 madeAssurances = null;
306 public synchronized void invalidateReceivedAssurances() {
307 receivedAssurances = null;
310 public void updateUserData() throws GigiApiException {
311 synchronized (Notary.class) {
312 // FIXME: No assurance, not no points.
313 if (getAssurancePoints() != 0) {
314 throw new GigiApiException("No change after assurance allowed.");
320 protected void rawUpdateUserData() {
321 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
322 update.setString(1, name.getFname());
323 update.setString(2, name.getLname());
324 update.setString(3, name.getMname());
325 update.setString(4, name.getSuffix());
326 update.setDate(5, getDoB());
327 update.setInt(6, getId());
328 update.executeUpdate();
331 public Locale getPreferredLocale() {
335 public void setPreferredLocale(Locale locale) {
336 this.locale = locale;
340 public boolean wantsDirectoryListing() {
341 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
342 get.setInt(1, getId());
343 try (GigiResultSet exec = get.executeQuery()) {
344 return exec.next() && exec.getBoolean("listme");
348 public String getContactInformation() {
349 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
350 get.setInt(1, getId());
352 try (GigiResultSet exec = get.executeQuery()) {
354 return exec.getString("contactinfo");
358 public void setDirectoryListing(boolean on) {
359 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
360 update.setBoolean(1, on);
361 update.setInt(2, getId());
362 update.executeUpdate();
365 public void setContactInformation(String contactInfo) {
366 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
367 update.setString(1, contactInfo);
368 update.setInt(2, getId());
369 update.executeUpdate();
372 public boolean isInGroup(Group g) {
373 return groups.contains(g);
376 public Set<Group> getGroups() {
377 return Collections.unmodifiableSet(groups);
380 public void grantGroup(User granter, Group toGrant) {
382 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
383 ps.setInt(1, getId());
384 ps.setString(2, toGrant.getDatabaseName());
385 ps.setInt(3, granter.getId());
389 public void revokeGroup(User revoker, Group toRevoke) {
390 groups.remove(toRevoke);
391 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
392 ps.setInt(1, revoker.getId());
393 ps.setString(2, toRevoke.getDatabaseName());
394 ps.setInt(3, getId());
398 public List<Organisation> getOrganisations() {
399 List<Organisation> orgas = new ArrayList<>();
400 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT orgid FROM org_admin WHERE `memid`=? AND deleted is NULL");
401 query.setInt(1, getId());
402 try (GigiResultSet res = query.executeQuery()) {
404 orgas.add(Organisation.getById(res.getInt(1)));
411 public static synchronized User getById(int id) {
412 CertificateOwner co = CertificateOwner.getById(id);
413 if (co instanceof User) {
420 public static User getByEmail(String mail) {
421 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT users.id FROM users INNER JOIN certOwners ON certOwners.id = users.id WHERE email=? AND deleted IS NULL");
422 ps.setString(1, mail);
423 try (GigiResultSet rs = ps.executeQuery()) {
428 return User.getById(rs.getInt(1));
432 public static User[] findByEmail(String mail) {
433 LinkedList<User> results = new LinkedList<User>();
434 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT users.id FROM users INNER JOIN certOwners ON certOwners.id = users.id WHERE users.email LIKE ? AND deleted IS NULL GROUP BY users.id ASC LIMIT 100");
435 ps.setString(1, mail);
436 try (GigiResultSet rs = ps.executeQuery()) {
438 results.add(User.getById(rs.getInt(1)));
440 return results.toArray(new User[results.size()]);
444 public EmailAddress[] getEmails() {
445 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted is NULL");
446 ps.setInt(1, getId());
448 try (GigiResultSet rs = ps.executeQuery()) {
449 LinkedList<EmailAddress> data = new LinkedList<EmailAddress>();
452 data.add(EmailAddress.getById(rs.getInt(1)));
455 return data.toArray(new EmailAddress[0]);
460 public boolean isValidEmail(String email) {
461 for (EmailAddress em : getEmails()) {
462 if (em.getAddress().equals(email)) {