package org.cacert.gigi.dbObjects;
import java.util.ArrayList;
-import java.util.Calendar;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedList;
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;
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);
}
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;
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;
}
public int getAssurancePoints() {
- try (GigiPreparedStatement query = new GigiPreparedStatement("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL AND (`expire` IS NULL OR `expire` > CURRENT_TIMESTAMP)")) {
+ 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();
}
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;
* @return the maximal points @
*/
public int getMaxAssurePoints() {
- if ( !isOfAge(18)) {
+ if ( !CalendarUtil.isOfAge(dob, ADULT_AGE) && POJAM_ENABLED) {
return 10; // PoJAM
}
return points;
}
- public boolean isOfAge(int desiredAge) {
- Calendar c = Calendar.getInstance();
- c.setTimeInMillis(dob.getTime());
- 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);
}
}
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]);
}
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;
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.");
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();
+ }
+ }
}