]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/User.java
add: handling of multiple verifications by the same RA Agent
[gigi.git] / src / org / cacert / gigi / dbObjects / User.java
index 55e567f5dd6a93a793a9c358cde85c7242360748..fe9ef7541d6f51fa616e84f7e1ee505bf695abc9 100644 (file)
@@ -1,8 +1,6 @@
 package org.cacert.gigi.dbObjects;
 
-import java.sql.Date;
 import java.util.ArrayList;
-import java.util.Calendar;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.LinkedList;
@@ -13,17 +11,26 @@ import java.util.Set;
 import org.cacert.gigi.GigiApiException;
 import org.cacert.gigi.database.GigiPreparedStatement;
 import org.cacert.gigi.database.GigiResultSet;
+import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
+import org.cacert.gigi.dbObjects.CATS.CATSType;
 import org.cacert.gigi.localisation.Language;
 import org.cacert.gigi.output.DateSelector;
+import org.cacert.gigi.pages.PasswordResetPage;
+import org.cacert.gigi.util.CalendarUtil;
+import org.cacert.gigi.util.DayDate;
 import org.cacert.gigi.util.Notary;
 import org.cacert.gigi.util.PasswordHash;
 import org.cacert.gigi.util.PasswordStrengthChecker;
 
+/**
+ * Represents an acting, assurable, user. Synchronizing on user means: no
+ * name-change and no assurance.
+ */
 public class User extends CertificateOwner {
 
     private Name name = new Name(null, null, null, null);
 
-    private Date dob;
+    private DayDate dob;
 
     private String email;
 
@@ -35,6 +42,19 @@ public class User extends CertificateOwner {
 
     private final Set<Group> groups = new HashSet<>();
 
+    public static final int MINIMUM_AGE = 16;
+
+    public static final int POJAM_AGE = 14;
+
+    public static final int ADULT_AGE = 18;
+
+    public static final boolean POJAM_ENABLED = false;
+
+    /**
+     * Time in months a verification is considered "recent".
+     */
+    public static final int VERIFICATION_MONTHS = 39;
+
     protected User(GigiResultSet rs) {
         super(rs.getInt("id"));
         updateName(rs);
@@ -42,7 +62,7 @@ public class User extends CertificateOwner {
 
     private void updateName(GigiResultSet rs) {
         name = new Name(rs.getString("fname"), rs.getString("lname"), rs.getString("mname"), rs.getString("suffix"));
-        dob = rs.getDate("dob");
+        dob = new DayDate(rs.getDate("dob"));
         email = rs.getString("email");
 
         String localeStr = rs.getString("language");
@@ -63,7 +83,7 @@ public class User extends CertificateOwner {
         }
     }
 
-    public User(String email, String password, Name name, Date dob, Locale locale) throws GigiApiException {
+    public User(String email, String password, Name name, DayDate dob, Locale locale) throws GigiApiException {
         this.email = email;
         this.dob = dob;
         this.name = name;
@@ -75,7 +95,7 @@ public class User extends CertificateOwner {
             query.setString(4, name.getMname());
             query.setString(5, name.getLname());
             query.setString(6, name.getSuffix());
-            query.setDate(7, dob);
+            query.setDate(7, dob.toSQLDate());
             query.setString(8, locale.toString());
             query.setInt(9, getId());
             query.execute();
@@ -87,11 +107,11 @@ public class User extends CertificateOwner {
         return name;
     }
 
-    public Date getDoB() {
+    public DayDate getDoB() {
         return dob;
     }
 
-    public void setDoB(Date dob) {
+    public void setDoB(DayDate dob) {
         this.dob = dob;
     }
 
@@ -128,8 +148,14 @@ public class User extends CertificateOwner {
     }
 
     public boolean canAssure() {
-        if ( !isOfAge(14)) { // PoJAM
-            return false;
+        if (POJAM_ENABLED) {
+            if ( !CalendarUtil.isOfAge(dob, POJAM_AGE)) { // PoJAM
+                return false;
+            }
+        } else {
+            if ( !CalendarUtil.isOfAge(dob, ADULT_AGE)) {
+                return false;
+            }
         }
         if (getAssurancePoints() < 100) {
             return false;
@@ -142,7 +168,7 @@ public class User extends CertificateOwner {
     public boolean hasPassedCATS() {
         try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT 1 FROM `cats_passed` where `user_id`=? AND `variant_id`=?")) {
             query.setInt(1, getId());
-            query.setInt(2, CATS.ASSURER_CHALLANGE_ID);
+            query.setInt(2, CATSType.ASSURER_CHALLENGE.getId());
             try (GigiResultSet rs = query.executeQuery()) {
                 if (rs.next()) {
                     return true;
@@ -154,7 +180,7 @@ public class User extends CertificateOwner {
     }
 
     public int getAssurancePoints() {
-        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL")) {
+        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT SUM(lastpoints) FROM ( SELECT DISTINCT ON (`from`) `from`, `to`, `points` as lastpoints, `method` FROM `notary` WHERE `deleted` is NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP) AND `to` = ? ORDER BY  `from`, `when` DESC) as p")) {
             query.setInt(1, getId());
 
             GigiResultSet rs = query.executeQuery();
@@ -169,8 +195,9 @@ public class User extends CertificateOwner {
     }
 
     public int getExperiencePoints() {
-        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT count(*) FROM `notary` where `from`=? AND `deleted` is NULL")) {
+        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT count(*) FROM ( SELECT `to` FROM `notary` WHERE `from`=? AND `deleted` IS NULL AND `method` = ? ::`notaryType` GROUP BY `to`) as p")) {
             query.setInt(1, getId());
+            query.setString(2, AssuranceType.FACE_TO_FACE.getDescription());
 
             GigiResultSet rs = query.executeQuery();
             int points = 0;
@@ -190,7 +217,7 @@ public class User extends CertificateOwner {
      * @return the maximal points @
      */
     public int getMaxAssurePoints() {
-        if ( !isOfAge(18)) {
+        if ( !CalendarUtil.isOfAge(dob, ADULT_AGE) && POJAM_ENABLED) {
             return 10; // PoJAM
         }
 
@@ -216,17 +243,6 @@ public class User extends CertificateOwner {
         return points;
     }
 
-    public boolean isOfAge(int desiredAge) {
-        Calendar c = Calendar.getInstance();
-        c.setTime(dob);
-        int year = c.get(Calendar.YEAR);
-        int month = c.get(Calendar.MONTH);
-        int day = c.get(Calendar.DAY_OF_MONTH);
-        c.set(year, month, day);
-        c.add(Calendar.YEAR, desiredAge);
-        return System.currentTimeMillis() >= c.getTime().getTime();
-    }
-
     public boolean isValidName(String name) {
         return getName().matches(name);
     }
@@ -278,7 +294,7 @@ public class User extends CertificateOwner {
                 List<Assurance> assurances = new LinkedList<Assurance>();
 
                 while (res.next()) {
-                    assurances.add(new Assurance(res));
+                    assurances.add(assuranceByRes(res));
                 }
 
                 this.receivedAssurances = assurances.toArray(new Assurance[0]);
@@ -297,7 +313,7 @@ public class User extends CertificateOwner {
                     List<Assurance> assurances = new LinkedList<Assurance>();
 
                     while (res.next()) {
-                        assurances.add(new Assurance(res));
+                        assurances.add(assuranceByRes(res));
                     }
 
                     this.madeAssurances = assurances.toArray(new Assurance[0]);
@@ -331,7 +347,7 @@ public class User extends CertificateOwner {
             update.setString(2, name.getLname());
             update.setString(3, name.getMname());
             update.setString(4, name.getSuffix());
-            update.setDate(5, getDoB());
+            update.setDate(5, getDoB().toSQLDate());
             update.setInt(6, getId());
             update.executeUpdate();
         }
@@ -409,8 +425,12 @@ public class User extends CertificateOwner {
     }
 
     public List<Organisation> getOrganisations() {
+        return getOrganisations(false);
+    }
+
+    public List<Organisation> getOrganisations(boolean isAdmin) {
         List<Organisation> orgas = new ArrayList<>();
-        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT `orgid` FROM `org_admin` WHERE `memid`=? AND `deleted` IS NULL")) {
+        try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT `orgid` FROM `org_admin` WHERE `memid`=? AND `deleted` IS NULL" + (isAdmin ? " AND master='y'" : ""))) {
             query.setInt(1, getId());
             try (GigiResultSet res = query.executeQuery()) {
                 while (res.next()) {
@@ -482,14 +502,26 @@ public class User extends CertificateOwner {
     }
 
     public String[] getTrainings() {
-        try (GigiPreparedStatement prep = new GigiPreparedStatement("SELECT `pass_date`, `type_text` FROM `cats_passed` LEFT JOIN `cats_type` ON `cats_type`.`id`=`cats_passed`.`variant_id`  WHERE `user_id`=? ORDER BY `pass_date` ASC")) {
+        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")) {
             prep.setInt(1, getId());
             GigiResultSet res = prep.executeQuery();
             List<String> entries = new LinkedList<String>();
 
             while (res.next()) {
-
-                entries.add(DateSelector.getDateFormat().format(res.getTimestamp(1)) + " (" + res.getString(2) + ")");
+                StringBuilder training = new StringBuilder();
+                training.append(DateSelector.getDateFormat().format(res.getTimestamp(1)));
+                training.append(" (");
+                training.append(res.getString(2));
+                if (res.getString(3).length() > 0) {
+                    training.append(" ");
+                    training.append(res.getString(3));
+                }
+                if (res.getString(4).length() > 0) {
+                    training.append(", ");
+                    training.append(res.getString(4));
+                }
+                training.append(")");
+                entries.add(training.toString());
             }
 
             return entries.toArray(new String[0]);
@@ -509,9 +541,10 @@ public class User extends CertificateOwner {
     }
 
     public static User getResetWithToken(int id, String token) {
-        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid` FROM `passwordResetTickets` WHERE `id`=? AND `token`=? AND `used` IS NULL")) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT `memid` FROM `passwordResetTickets` WHERE `id`=? AND `token`=? AND `used` IS NULL AND `created` > CURRENT_TIMESTAMP - interval '1 hours' * ?")) {
             ps.setInt(1, id);
             ps.setString(2, token);
+            ps.setInt(3, PasswordResetPage.HOUR_MAX);
             GigiResultSet res = ps.executeQuery();
             if ( !res.next()) {
                 return null;
@@ -526,7 +559,7 @@ public class User extends CertificateOwner {
             ps.setInt(2, getId());
             GigiResultSet rs = ps.executeQuery();
             if ( !rs.next()) {
-                throw new GigiApiException("Token not found... very bad.");
+                throw new GigiApiException("Token could not be found, has already been used, or is expired.");
             }
             if (PasswordHash.verifyHash(private_token, rs.getString(1)) == null) {
                 throw new GigiApiException("Private token does not match.");
@@ -538,4 +571,18 @@ public class User extends CertificateOwner {
             ps.executeUpdate();
         }
     }
+
+    private Assurance assuranceByRes(GigiResultSet res) {
+        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"));
+    }
+
+    public static boolean isInVerificationLimit(int id) {
+        try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` WHERE `to` = ? AND `when` > (now() - (interval '1 month' * ?)) AND (`expire` IS NULL OR `expire` > now()) AND `deleted` IS NULL;")) {
+            ps.setInt(1, id);
+            ps.setInt(2, VERIFICATION_MONTHS);
+
+            GigiResultSet rs = ps.executeQuery();
+            return rs.next();
+        }
+    }
 }