1 package org.cacert.gigi.util;
3 import java.text.ParseException;
4 import java.util.Calendar;
6 import java.util.GregorianCalendar;
8 import org.cacert.gigi.GigiApiException;
9 import org.cacert.gigi.database.GigiPreparedStatement;
10 import org.cacert.gigi.database.GigiResultSet;
11 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
12 import org.cacert.gigi.dbObjects.Group;
13 import org.cacert.gigi.dbObjects.Name;
14 import org.cacert.gigi.dbObjects.User;
15 import org.cacert.gigi.output.DateSelector;
16 import org.cacert.gigi.output.template.SprintfCommand;
20 public final static int LIMIT_DAYS_VERIFICATION = 90; // conf.getProperty("limit_days_verification");
22 public static void writeUserAgreement(User member, String document, String method, String comment, boolean active, int secmemid) {
23 try (GigiPreparedStatement q = new GigiPreparedStatement("INSERT INTO `user_agreements` SET `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?")) {
24 q.setInt(1, member.getId());
25 q.setInt(2, secmemid);
26 q.setString(3, document);
27 q.setBoolean(4, active);
28 q.setString(5, method);
29 q.setString(6, comment);
34 public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
35 if (assurer.getId() == target.getId()) {
36 throw new GigiApiException("You cannot assure yourself.");
38 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` where `to`=? and `from`=? and `method` = ? ::`notaryType` AND `deleted` IS NULL AND `when` > (now() - interval '1 days' * ?)")) {
39 ps.setInt(1, target.getId());
40 ps.setInt(2, assurer.getId());
41 ps.setString(3, AssuranceType.FACE_TO_FACE.getDescription());
42 ps.setInt(4, LIMIT_DAYS_VERIFICATION);
43 GigiResultSet rs = ps.executeQuery();
46 throw new GigiApiException(SprintfCommand.createSimple("You have already verified this applicant within the last {0} days.", LIMIT_DAYS_VERIFICATION));
49 if ( !assurer.canAssure()) {
50 throw new GigiApiException("You are not an assurer.");
54 public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
56 public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
59 * This method assures another user.
61 * @see User#canAssure() (for assurer)
62 * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
64 * the person that wants to assure
66 * the person that should be assured
68 * the Name that was personally verified
70 * the Date of birth that the assurer verified
72 * the points that should be awarded in total
74 * the location where the assurance took place
76 * the date when the assurance took place
77 * @throws GigiApiException
78 * if the assurance fails (for various reasons)
80 public synchronized static void assure(User assurer, User assuree, Name assureeName, DayDate dob, int awarded, String location, String date, AssuranceType type) throws GigiApiException {
81 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
82 GigiApiException gae = new GigiApiException();
83 if ( !gae.isEmpty()) {
86 if (date == null || date.equals("")) {
87 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
90 Date d = DateSelector.getDateFormat().parse(date);
91 Calendar gc = GregorianCalendar.getInstance();
92 gc.setTimeInMillis(System.currentTimeMillis());
93 gc.add(Calendar.HOUR_OF_DAY, 12);
94 if (d.getTime() > gc.getTimeInMillis()) {
95 gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
97 } catch (ParseException e) {
98 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
101 // check location, min 3 characters
102 if (location == null || location.equals("")) {
103 gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
104 } else if (location.length() <= 2) {
105 gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
107 synchronized (assuree) {
110 checkAssuranceIsPossible(assurer, assuree);
111 } catch (GigiApiException e) {
115 if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
116 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
119 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
121 if (type == AssuranceType.NUCLEUS) {
123 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
126 if (awarded > assurer.getMaxAssurePoints()) {
127 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
132 if ( !gae.isEmpty()) {
136 if (type == AssuranceType.FACE_TO_FACE) {
137 assureF2F(assurer, assuree, awarded, location, date);
138 } else if (type == AssuranceType.NUCLEUS) {
139 assureNucleus(assurer, assuree, awarded, location, date);
140 } else if (type == AssuranceType.TTP_ASSISTED) {
141 assureTTP(assurer, assuree, awarded, location, date);
143 throw new GigiApiException(SprintfCommand.createSimple("Unknown Assurance type: {0}", type.toString()));
145 assurer.invalidateMadeAssurances();
146 assuree.invalidateReceivedAssurances();
150 private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
151 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
152 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
153 ps.setInt(1, assurer.getId());
154 ps.setInt(2, assuree.getId());
155 ps.setInt(3, awarded);
156 ps.setString(4, location);
157 ps.setString(5, date);
162 private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
163 may(assurer, assuree, AssuranceType.TTP_ASSISTED);
164 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
165 ps.setInt(1, assurer.getId());
166 ps.setInt(2, assuree.getId());
167 ps.setInt(3, awarded);
168 ps.setString(4, location);
169 ps.setString(5, date);
171 assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
175 public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
176 if (assuree.isInGroup(ASSUREE_BLOCKED)) {
177 throw new GigiApiException("The applicant is blocked.");
179 if (assurer.isInGroup(ASSURER_BLOCKED)) {
180 throw new GigiApiException("The RA Agent is blocked.");
183 if (t == AssuranceType.NUCLEUS) {
184 if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
185 throw new GigiApiException("RA Agent needs to be Nucleus RA Agent.");
188 } else if (t == AssuranceType.TTP_ASSISTED) {
189 if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
190 throw new GigiApiException("RA Agent needs to be TTP RA Agent.");
192 if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
193 throw new GigiApiException("Applicant needs to be TTP Applicant.");
196 } else if (t == AssuranceType.FACE_TO_FACE) {
199 throw new GigiApiException("Verification type not possible.");
202 private static void assureNucleus(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
203 may(assurer, assuree, AssuranceType.NUCLEUS);
204 // Do up to 35 points as f2f
205 int f2fPoints = Math.min(assurer.getMaxAssurePoints(), awarded);
206 assureF2F(assurer, assuree, f2fPoints, location, date);
208 awarded -= f2fPoints;
213 // Assure remaining points as "Nucleus Bonus"
214 // Valid for 4 Weeks = 28 days
215 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='Nucleus Bonus', `expire` = CURRENT_TIMESTAMP + interval '28 days'")) {
216 ps.setInt(1, assurer.getId());
217 ps.setInt(2, assuree.getId());
218 ps.setInt(3, awarded);
219 ps.setString(4, location);
220 ps.setString(5, date);