1 package org.cacert.gigi.dbObjects;
4 import java.util.Calendar;
5 import java.util.Collections;
6 import java.util.HashSet;
7 import java.util.Locale;
10 import org.cacert.gigi.GigiApiException;
11 import org.cacert.gigi.database.DatabaseConnection;
12 import org.cacert.gigi.database.GigiPreparedStatement;
13 import org.cacert.gigi.database.GigiResultSet;
14 import org.cacert.gigi.localisation.Language;
15 import org.cacert.gigi.util.Notary;
16 import org.cacert.gigi.util.PasswordHash;
17 import org.cacert.gigi.util.PasswordStrengthChecker;
19 public class User extends CertificateOwner {
21 private Name name = new Name(null, null, null, null);
27 private Assurance[] receivedAssurances, madeAssurances;
29 private Locale locale;
31 private Set<Group> groups = new HashSet<>();
33 protected User(GigiResultSet rs) {
34 super(rs.getInt("id"));
38 private void updateName(GigiResultSet rs) {
39 name = new Name(rs.getString("fname"), rs.getString("lname"), rs.getString("mname"), rs.getString("suffix"));
40 dob = rs.getDate("dob");
41 email = rs.getString("email");
42 String localeStr = rs.getString("language");
43 if (localeStr == null || localeStr.equals("")) {
44 locale = Locale.getDefault();
46 locale = Language.getLocaleFromString(localeStr);
48 GigiPreparedStatement psg = DatabaseConnection.getInstance().prepare("SELECT permission FROM user_groups WHERE user=? AND deleted is NULL");
49 psg.setInt(1, rs.getInt("id"));
50 GigiResultSet rs2 = psg.executeQuery();
52 groups.add(Group.getByString(rs2.getString(1)));
59 public String getFname() {
63 public String getLname() {
67 public String getMname() {
71 public Name getName() {
75 public void setMname(String mname) {
76 this.name.mname = mname;
79 public String getSuffix() {
83 public void setSuffix(String suffix) {
84 this.name.suffix = suffix;
87 public Date getDob() {
91 public void setDob(Date dob) {
95 public String getEmail() {
99 public void setEmail(String email) {
103 public void setFname(String fname) {
104 this.name.fname = fname;
107 public void setLname(String lname) {
108 this.name.lname = lname;
111 public void insert(String password) {
112 int id = super.insert();
113 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `language`=?, id=?");
114 query.setString(1, email);
115 query.setString(2, PasswordHash.hash(password));
116 query.setString(3, name.fname);
117 query.setString(4, name.mname);
118 query.setString(5, name.lname);
119 query.setString(6, name.suffix);
120 query.setDate(7, new java.sql.Date(dob.getTime()));
121 query.setString(8, locale.toString());
126 public void changePassword(String oldPass, String newPass) throws GigiApiException {
127 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
128 ps.setInt(1, getId());
129 GigiResultSet rs = ps.executeQuery();
131 throw new GigiApiException("User not found... very bad.");
133 if ( !PasswordHash.verifyHash(oldPass, rs.getString(1))) {
134 throw new GigiApiException("Old password does not match.");
137 PasswordStrengthChecker.assertStrongPassword(newPass, this);
138 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
139 ps.setString(1, PasswordHash.hash(newPass));
140 ps.setInt(2, getId());
141 if (ps.executeUpdate() != 1) {
142 throw new GigiApiException("Password update failed.");
146 public boolean canAssure() {
147 if ( !isOfAge(14)) { // PoJAM
150 if (getAssurancePoints() < 100) {
154 return hasPassedCATS();
158 public boolean hasPassedCATS() {
159 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
160 query.setInt(1, getId());
161 GigiResultSet rs = query.executeQuery();
169 public int getAssurancePoints() {
170 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted`=0");
171 query.setInt(1, getId());
172 GigiResultSet rs = query.executeQuery();
175 points = rs.getInt(1);
181 public int getExperiencePoints() {
182 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted`=0");
183 query.setInt(1, getId());
184 GigiResultSet rs = query.executeQuery();
187 points = rs.getInt(1) * 2;
194 public boolean equals(Object obj) {
195 if ( !(obj instanceof User)) {
199 return name.equals(s.name) && email.equals(s.email) && dob.toString().equals(s.dob.toString()); // This
208 * Gets the maximum allowed points NOW. Note that an assurance needs to
209 * re-check PoJam as it has taken place in the past.
211 * @return the maximal points @
213 public int getMaxAssurePoints() {
218 int exp = getExperiencePoints();
239 public boolean isOfAge(int desiredAge) {
240 Calendar c = Calendar.getInstance();
242 int year = c.get(Calendar.YEAR);
243 int month = c.get(Calendar.MONTH);
244 int day = c.get(Calendar.DAY_OF_MONTH);
245 c.set(year, month, day);
246 c.add(Calendar.YEAR, desiredAge);
247 return System.currentTimeMillis() >= c.getTime().getTime();
250 public EmailAddress[] getEmails() {
251 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
252 ps.setInt(1, getId());
253 GigiResultSet rs = ps.executeQuery();
255 int count = rs.getRow();
256 EmailAddress[] data = new EmailAddress[count];
258 for (int i = 0; i < data.length; i++) {
260 throw new Error("Internal sql api violation.");
262 data[i] = EmailAddress.getById(rs.getInt(1));
269 public Domain[] getDomains() {
270 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
271 ps.setInt(1, getId());
272 GigiResultSet rs = ps.executeQuery();
274 int count = rs.getRow();
275 Domain[] data = new Domain[count];
277 for (int i = 0; i < data.length; i++) {
279 throw new Error("Internal sql api violation.");
281 data[i] = Domain.getById(rs.getInt(1));
288 public Certificate[] getCertificates() {
289 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
290 ps.setInt(1, getId());
291 GigiResultSet rs = ps.executeQuery();
293 int count = rs.getRow();
294 Certificate[] data = new Certificate[count];
296 for (int i = 0; i < data.length; i++) {
298 throw new Error("Internal sql api violation.");
300 data[i] = Certificate.getBySerial(rs.getString(1));
307 public boolean isValidDomain(String domainname) {
308 for (Domain d : getDomains()) {
309 String sfx = d.getSuffix();
310 if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
317 public boolean isValidEmail(String email) {
318 for (EmailAddress em : getEmails()) {
319 if (em.getAddress().equals(email)) {
326 public boolean isValidName(String name) {
327 return getName().matches(name);
330 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
331 EmailAddress[] adrs = getEmails();
332 for (int i = 0; i < adrs.length; i++) {
333 if (adrs[i].getAddress().equals(newMail.getAddress())) {
334 if ( !adrs[i].isVerified()) {
335 throw new GigiApiException("Email not verified.");
337 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
338 ps.setString(1, newMail.getAddress());
339 ps.setInt(2, getId());
341 email = newMail.getAddress();
345 throw new GigiApiException("Given address not an address of the user.");
348 public void deleteEmail(EmailAddress mail) throws GigiApiException {
349 if (getEmail().equals(mail.getAddress())) {
350 throw new GigiApiException("Can't delete user's default e-mail.");
352 EmailAddress[] emails = getEmails();
353 for (int i = 0; i < emails.length; i++) {
354 if (emails[i].getId() == mail.getId()) {
355 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
356 ps.setDate(1, new Date(System.currentTimeMillis()));
357 ps.setInt(2, mail.getId());
362 throw new GigiApiException("Email not one of user's email addresses.");
365 public Assurance[] getReceivedAssurances() {
366 if (receivedAssurances == null) {
367 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0");
368 query.setInt(1, getId());
369 GigiResultSet res = query.executeQuery();
371 Assurance[] assurances = new Assurance[res.getRow()];
373 for (int i = 0; i < assurances.length; i++) {
375 assurances[i] = new Assurance(res);
377 this.receivedAssurances = assurances;
380 return receivedAssurances;
383 public Assurance[] getMadeAssurances() {
384 if (madeAssurances == null) {
385 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0");
386 query.setInt(1, getId());
387 GigiResultSet res = query.executeQuery();
389 Assurance[] assurances = new Assurance[res.getRow()];
391 for (int i = 0; i < assurances.length; i++) {
393 assurances[i] = new Assurance(res);
395 this.madeAssurances = assurances;
398 return madeAssurances;
401 public void invalidateMadeAssurances() {
402 madeAssurances = null;
405 public void invalidateReceivedAssurances() {
406 receivedAssurances = null;
409 public void updateUserData() throws GigiApiException {
410 synchronized (Notary.class) {
411 if (getAssurancePoints() != 0) {
412 throw new GigiApiException("No change after assurance allowed.");
414 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
415 update.setString(1, getFname());
416 update.setString(2, getLname());
417 update.setString(3, getMname());
418 update.setString(4, getSuffix());
419 update.setDate(5, getDob());
420 update.setInt(6, getId());
421 update.executeUpdate();
425 public Locale getPreferredLocale() {
429 public void setPreferredLocale(Locale locale) {
430 this.locale = locale;
434 public boolean wantsDirectoryListing() {
435 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
436 get.setInt(1, getId());
437 GigiResultSet exec = get.executeQuery();
439 return exec.getBoolean("listme");
442 public String getContactInformation() {
443 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
444 get.setInt(1, getId());
445 GigiResultSet exec = get.executeQuery();
447 return exec.getString("contactinfo");
450 public void setDirectoryListing(boolean on) {
451 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
452 update.setBoolean(1, on);
453 update.setInt(2, getId());
454 update.executeUpdate();
457 public void setContactInformation(String contactInfo) {
458 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
459 update.setString(1, contactInfo);
460 update.setInt(2, getId());
461 update.executeUpdate();
464 public boolean isInGroup(Group g) {
465 return groups.contains(g);
468 public Set<Group> getGroups() {
469 return Collections.unmodifiableSet(groups);
472 public void grantGroup(User granter, Group toGrant) {
474 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
475 ps.setInt(1, getId());
476 ps.setString(2, toGrant.getDatabaseName());
477 ps.setInt(3, granter.getId());
481 public void revokeGroup(User revoker, Group toRevoke) {
482 groups.remove(toRevoke);
483 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
484 ps.setInt(1, revoker.getId());
485 ps.setString(2, toRevoke.getDatabaseName());
486 ps.setInt(3, getId());
490 public static synchronized User getById(int id) {
491 CertificateOwner co = CertificateOwner.getById(id);
492 if (co instanceof User) {
498 public boolean canIssue(CertificateProfile p) {
499 switch (p.getCAId()) {
503 return getAssurancePoints() > 50;
505 return getAssurancePoints() > 50 && isInGroup(Group.getByString("codesigning"));
508 return false; // has an orga