1 package org.cacert.gigi.dbObjects;
3 import java.util.ArrayList;
4 import java.util.Collections;
5 import java.util.HashSet;
6 import java.util.LinkedList;
8 import java.util.Locale;
11 import org.cacert.gigi.GigiApiException;
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.output.DateSelector;
16 import org.cacert.gigi.pages.PasswordResetPage;
17 import org.cacert.gigi.util.CalendarUtil;
18 import org.cacert.gigi.util.DayDate;
19 import org.cacert.gigi.util.Notary;
20 import org.cacert.gigi.util.PasswordHash;
21 import org.cacert.gigi.util.PasswordStrengthChecker;
24 * Represents an acting, assurable, user. Synchronizing on user means: no
25 * name-change and no assurance.
27 public class User extends CertificateOwner {
29 private Name name = new Name(null, null, null, null);
35 private Assurance[] receivedAssurances;
37 private Assurance[] madeAssurances;
39 private Locale locale;
41 private final Set<Group> groups = new HashSet<>();
43 public static final int MINIMUM_AGE = 16;
45 public static final int POJAM_AGE = 14;
47 public static final int ADULT_AGE = 18;
49 public static final boolean POJAM_ENABLED = false;
51 protected User(GigiResultSet rs) {
52 super(rs.getInt("id"));
56 private void updateName(GigiResultSet rs) {
57 name = new Name(rs.getString("fname"), rs.getString("lname"), rs.getString("mname"), rs.getString("suffix"));
58 dob = new DayDate(rs.getDate("dob"));
59 email = rs.getString("email");
61 String localeStr = rs.getString("language");
62 if (localeStr == null || localeStr.equals("")) {
63 locale = Locale.getDefault();
65 locale = Language.getLocaleFromString(localeStr);
68 try (GigiPreparedStatement psg = new GigiPreparedStatement("SELECT `permission` FROM `user_groups` WHERE `user`=? AND `deleted` is NULL")) {
69 psg.setInt(1, rs.getInt("id"));
71 try (GigiResultSet rs2 = psg.executeQuery()) {
73 groups.add(Group.getByString(rs2.getString(1)));
79 public User(String email, String password, Name name, DayDate dob, Locale locale) throws GigiApiException {
84 try (GigiPreparedStatement query = new GigiPreparedStatement("INSERT INTO `users` SET `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `language`=?, id=?")) {
85 query.setString(1, email);
86 query.setString(2, PasswordHash.hash(password));
87 query.setString(3, name.getFname());
88 query.setString(4, name.getMname());
89 query.setString(5, name.getLname());
90 query.setString(6, name.getSuffix());
91 query.setDate(7, dob.toSQLDate());
92 query.setString(8, locale.toString());
93 query.setInt(9, getId());
96 new EmailAddress(this, email, locale);
99 public Name getName() {
103 public DayDate getDoB() {
107 public void setDoB(DayDate dob) {
111 public String getEmail() {
115 public void changePassword(String oldPass, String newPass) throws GigiApiException {
116 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `password` FROM `users` WHERE `id`=?")) {
117 ps.setInt(1, getId());
118 try (GigiResultSet rs = ps.executeQuery()) {
120 throw new GigiApiException("User not found... very bad.");
122 if (PasswordHash.verifyHash(oldPass, rs.getString(1)) == null) {
123 throw new GigiApiException("Old password does not match.");
127 setPassword(newPass);
130 private void setPassword(String newPass) throws GigiApiException {
131 PasswordStrengthChecker.assertStrongPassword(newPass, getName(), getEmail());
132 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE users SET `password`=? WHERE id=?")) {
133 ps.setString(1, PasswordHash.hash(newPass));
134 ps.setInt(2, getId());
139 public void setName(Name name) {
143 public boolean canAssure() {
145 if ( !CalendarUtil.isOfAge(dob, POJAM_AGE)) { // PoJAM
149 if ( !CalendarUtil.isOfAge(dob, ADULT_AGE)) {
153 if (getAssurancePoints() < 100) {
157 return hasPassedCATS();
161 public boolean hasPassedCATS() {
162 try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT 1 FROM `cats_passed` where `user_id`=? AND `variant_id`=?")) {
163 query.setInt(1, getId());
164 query.setInt(2, CATS.ASSURER_CHALLENGE_ID);
165 try (GigiResultSet rs = query.executeQuery()) {
175 public int getAssurancePoints() {
176 try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP)")) {
177 query.setInt(1, getId());
179 GigiResultSet rs = query.executeQuery();
183 points = rs.getInt(1);
190 public int getExperiencePoints() {
191 try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT count(*) FROM `notary` where `from`=? AND `deleted` is NULL")) {
192 query.setInt(1, getId());
194 GigiResultSet rs = query.executeQuery();
198 points = rs.getInt(1) * 2;
206 * Gets the maximum allowed points NOW. Note that an assurance needs to
207 * re-check PoJam as it has taken place in the past.
209 * @return the maximal points @
211 public int getMaxAssurePoints() {
212 if ( !CalendarUtil.isOfAge(dob, ADULT_AGE) && POJAM_ENABLED) {
216 int exp = getExperiencePoints();
238 public boolean isValidName(String name) {
239 return getName().matches(name);
242 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
243 for (EmailAddress email : getEmails()) {
244 if (email.getAddress().equals(newMail.getAddress())) {
245 if ( !email.isVerified()) {
246 throw new GigiApiException("Email not verified.");
249 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE users SET email=? WHERE id=?")) {
250 ps.setString(1, newMail.getAddress());
251 ps.setInt(2, getId());
255 this.email = newMail.getAddress();
260 throw new GigiApiException("Given address not an address of the user.");
263 public void deleteEmail(EmailAddress delMail) throws GigiApiException {
264 if (getEmail().equals(delMail.getAddress())) {
265 throw new GigiApiException("Can't delete user's default e-mail.");
268 for (EmailAddress email : getEmails()) {
269 if (email.getId() == delMail.getId()) {
270 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `emails` SET `deleted`=CURRENT_TIMESTAMP WHERE `id`=?")) {
271 ps.setInt(1, delMail.getId());
277 throw new GigiApiException("Email not one of user's email addresses.");
280 public synchronized Assurance[] getReceivedAssurances() {
281 if (receivedAssurances == null) {
282 try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM `notary` WHERE `to`=? AND `deleted` IS NULL")) {
283 query.setInt(1, getId());
285 GigiResultSet res = query.executeQuery();
286 List<Assurance> assurances = new LinkedList<Assurance>();
289 assurances.add(assuranceByRes(res));
292 this.receivedAssurances = assurances.toArray(new Assurance[0]);
296 return receivedAssurances;
299 public synchronized Assurance[] getMadeAssurances() {
300 if (madeAssurances == null) {
301 try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT * FROM notary WHERE `from`=? AND deleted is NULL")) {
302 query.setInt(1, getId());
304 try (GigiResultSet res = query.executeQuery()) {
305 List<Assurance> assurances = new LinkedList<Assurance>();
308 assurances.add(assuranceByRes(res));
311 this.madeAssurances = assurances.toArray(new Assurance[0]);
316 return madeAssurances;
319 public synchronized void invalidateMadeAssurances() {
320 madeAssurances = null;
323 public synchronized void invalidateReceivedAssurances() {
324 receivedAssurances = null;
327 public void updateUserData() throws GigiApiException {
328 synchronized (Notary.class) {
329 if (getReceivedAssurances().length != 0) {
330 throw new GigiApiException("No change after assurance allowed.");
336 protected void rawUpdateUserData() {
337 try (GigiPreparedStatement update = new GigiPreparedStatement("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?")) {
338 update.setString(1, name.getFname());
339 update.setString(2, name.getLname());
340 update.setString(3, name.getMname());
341 update.setString(4, name.getSuffix());
342 update.setDate(5, getDoB().toSQLDate());
343 update.setInt(6, getId());
344 update.executeUpdate();
348 public Locale getPreferredLocale() {
352 public void setPreferredLocale(Locale locale) {
353 this.locale = locale;
357 public boolean wantsDirectoryListing() {
358 try (GigiPreparedStatement get = new GigiPreparedStatement("SELECT listme FROM users WHERE id=?")) {
359 get.setInt(1, getId());
360 GigiResultSet exec = get.executeQuery();
361 return exec.next() && exec.getBoolean("listme");
365 public String getContactInformation() {
366 try (GigiPreparedStatement get = new GigiPreparedStatement("SELECT contactinfo FROM users WHERE id=?")) {
367 get.setInt(1, getId());
369 GigiResultSet exec = get.executeQuery();
371 return exec.getString("contactinfo");
375 public void setDirectoryListing(boolean on) {
376 try (GigiPreparedStatement update = new GigiPreparedStatement("UPDATE users SET listme = ? WHERE id = ?")) {
377 update.setBoolean(1, on);
378 update.setInt(2, getId());
379 update.executeUpdate();
383 public void setContactInformation(String contactInfo) {
384 try (GigiPreparedStatement update = new GigiPreparedStatement("UPDATE users SET contactinfo = ? WHERE id = ?")) {
385 update.setString(1, contactInfo);
386 update.setInt(2, getId());
387 update.executeUpdate();
391 public boolean isInGroup(Group g) {
392 return groups.contains(g);
395 public Set<Group> getGroups() {
396 return Collections.unmodifiableSet(groups);
399 public void grantGroup(User granter, Group toGrant) {
401 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `user_groups` SET `user`=?, `permission`=?::`userGroup`, `grantedby`=?")) {
402 ps.setInt(1, getId());
403 ps.setString(2, toGrant.getDatabaseName());
404 ps.setInt(3, granter.getId());
409 public void revokeGroup(User revoker, Group toRevoke) {
410 groups.remove(toRevoke);
411 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `user_groups` SET `deleted`=CURRENT_TIMESTAMP, `revokedby`=? WHERE `deleted` IS NULL AND `permission`=?::`userGroup` AND `user`=?")) {
412 ps.setInt(1, revoker.getId());
413 ps.setString(2, toRevoke.getDatabaseName());
414 ps.setInt(3, getId());
419 public List<Organisation> getOrganisations() {
420 return getOrganisations(false);
423 public List<Organisation> getOrganisations(boolean isAdmin) {
424 List<Organisation> orgas = new ArrayList<>();
425 try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT `orgid` FROM `org_admin` WHERE `memid`=? AND `deleted` IS NULL" + (isAdmin ? " AND master='y'" : ""))) {
426 query.setInt(1, getId());
427 try (GigiResultSet res = query.executeQuery()) {
429 orgas.add(Organisation.getById(res.getInt(1)));
437 public static synchronized User getById(int id) {
438 CertificateOwner co = CertificateOwner.getById(id);
439 if (co instanceof User) {
446 public static User getByEmail(String mail) {
447 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `users`.`id` FROM `users` INNER JOIN `certOwners` ON `certOwners`.`id` = `users`.`id` WHERE `email`=? AND `deleted` IS NULL")) {
448 ps.setString(1, mail);
449 GigiResultSet rs = ps.executeQuery();
454 return User.getById(rs.getInt(1));
458 public static User[] findByEmail(String mail) {
459 LinkedList<User> results = new LinkedList<User>();
460 try (GigiPreparedStatement ps = new GigiPreparedStatement("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` LIMIT 100")) {
461 ps.setString(1, mail);
462 GigiResultSet rs = ps.executeQuery();
464 results.add(User.getById(rs.getInt(1)));
466 return results.toArray(new User[results.size()]);
470 public EmailAddress[] getEmails() {
471 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `id` FROM `emails` WHERE `memid`=? AND `deleted` IS NULL")) {
472 ps.setInt(1, getId());
474 GigiResultSet rs = ps.executeQuery();
475 LinkedList<EmailAddress> data = new LinkedList<EmailAddress>();
478 data.add(EmailAddress.getById(rs.getInt(1)));
481 return data.toArray(new EmailAddress[0]);
486 public boolean isValidEmail(String email) {
487 for (EmailAddress em : getEmails()) {
488 if (em.getAddress().equals(email)) {
489 return em.isVerified();
496 public String[] getTrainings() {
497 try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `pass_date`, `type_text`, `language`, `version` FROM `cats_passed` LEFT JOIN `cats_type` ON `cats_type`.`id`=`cats_passed`.`variant_id` WHERE `user_id`=? ORDER BY `pass_date` ASC")) {
498 prep.setInt(1, getId());
499 GigiResultSet res = prep.executeQuery();
500 List<String> entries = new LinkedList<String>();
503 StringBuilder training = new StringBuilder();
504 training.append(DateSelector.getDateFormat().format(res.getTimestamp(1)));
505 training.append(" (");
506 training.append(res.getString(2));
507 if (res.getString(3).length() > 0) {
508 training.append(" ");
509 training.append(res.getString(3));
511 if (res.getString(4).length() > 0) {
512 training.append(", ");
513 training.append(res.getString(4));
515 training.append(")");
516 entries.add(training.toString());
519 return entries.toArray(new String[0]);
524 public int generatePasswordResetTicket(User actor, String token, String privateToken) {
525 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `passwordResetTickets` SET `memid`=?, `creator`=?, `token`=?, `private_token`=?")) {
526 ps.setInt(1, getId());
527 ps.setInt(2, getId());
528 ps.setString(3, token);
529 ps.setString(4, PasswordHash.hash(privateToken));
531 return ps.lastInsertId();
535 public static User getResetWithToken(int id, String token) {
536 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid` FROM `passwordResetTickets` WHERE `id`=? AND `token`=? AND `used` IS NULL AND `created` > CURRENT_TIMESTAMP - interval '1 hours' * ?")) {
538 ps.setString(2, token);
539 ps.setInt(3, PasswordResetPage.HOUR_MAX);
540 GigiResultSet res = ps.executeQuery();
544 return User.getById(res.getInt(1));
548 public synchronized void consumePasswordResetTicket(int id, String private_token, String newPassword) throws GigiApiException {
549 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `private_token` FROM `passwordResetTickets` WHERE `id`=? AND `memid`=? AND `used` IS NULL")) {
551 ps.setInt(2, getId());
552 GigiResultSet rs = ps.executeQuery();
554 throw new GigiApiException("Token could not be found, has already been used, or is expired.");
556 if (PasswordHash.verifyHash(private_token, rs.getString(1)) == null) {
557 throw new GigiApiException("Private token does not match.");
559 setPassword(newPassword);
561 try (GigiPreparedStatement ps = new GigiPreparedStatement("UPDATE `passwordResetTickets` SET `used` = CURRENT_TIMESTAMP WHERE `id`=?")) {
567 private Assurance assuranceByRes(GigiResultSet res) {
568 return new Assurance(res.getInt("id"), User.getById(res.getInt("from")), User.getById(res.getInt("to")), res.getString("location"), res.getString("method"), res.getInt("points"), res.getString("date"));