import org.cacert.gigi.database.GigiPreparedStatement;
import org.cacert.gigi.database.GigiResultSet;
+import org.cacert.gigi.util.TimeConditions;
public class CATS {
- private static HashMap<String, Integer> names = new HashMap<>();
+ public enum CATSType {
+ ASSURER_CHALLENGE("Agent Qualifying Challenge"),
+
+ ORG_AGENT_CHALLENGE("Organisation Agent Qualifying Challenge"),
+
+ TTP_AGENT_CHALLENGE("TTP Agent Qualifying Challenge"),
+
+ TTP_TOPUP_AGENT_CHALLENGE_NAME("TTP TOPUP Agent Qualifying Challenge"),
+
+ CODE_SIGNING_CHALLENGE_NAME("Code Signing Challenge"),
+
+ ORG_ADMIN_DP_CHALLENGE_NAME("Organisation Administrator Data Protection Challenge"),
+
+ SUPPORT_DP_CHALLENGE_NAME("Support Data Protection Challenge");
+
+ private final String displayName;
+
+ private final int id;
+
+ private CATSType(String displayName) {
+ this.displayName = displayName;
+ id = getID(displayName);
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
- public static final String ASSURER_CHALLANGE_NAME = "Assurer's Challange";
+ public int getId() {
+ return id;
+ }
+ }
- public static final int ASSURER_CHALLANGE_ID;
+ /**
+ * The maximal number of months a passed test is considered "recent".
+ */
+ public static final int TEST_MONTHS = TimeConditions.getInstance().getTestMonths();
+
+ private static HashMap<String, Integer> names = new HashMap<>();
private CATS() {
names.put(res.getString(2), res.getInt(1));
}
}
- ASSURER_CHALLANGE_ID = getID(ASSURER_CHALLANGE_NAME);
}
public static synchronized int getID(String name) {
return i;
}
- public static void enterResult(User user, String testType, Date passDate) {
- try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?")) {
+ public static void enterResult(User user, CATSType testType, Date passDate, String language, String version) {
+ enterResult(user, testType.id, passDate, language, version);
+ }
+
+ public static void enterResult(User user, String testType, Date passDate, String language, String version) {
+ enterResult(user, getID(testType), passDate, language, version);
+ }
+
+ private static void enterResult(User user, int testTypeId, Date passDate, String language, String version) {
+
+ try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `cats_passed` SET `user_id`=?, `variant_id`=?, `pass_date`=?, `language`=?, `version`=?")) {
ps.setInt(1, user.getId());
- ps.setInt(2, getID(testType));
+ ps.setInt(2, testTypeId);
ps.setTimestamp(3, new Timestamp(passDate.getTime()));
+ ps.setString(4, language);
+ ps.setString(5, version);
ps.execute();
}
}
+
+ public static boolean isInCatsLimit(int uID, int testID) {
+ try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `cats_passed` WHERE `user_id` = ? AND `variant_id` = ? AND`pass_date` > (now() - interval '1 months' * ?)")) {
+ ps.setInt(1, uID);
+ ps.setInt(2, testID);
+ ps.setInt(3, TEST_MONTHS);
+
+ GigiResultSet rs = ps.executeQuery();
+ return rs.next();
+ }
+ }
}