1 package org.cacert.gigi.util;
3 import java.text.ParseException;
4 import java.util.Arrays;
5 import java.util.Calendar;
7 import java.util.GregorianCalendar;
9 import org.cacert.gigi.GigiApiException;
10 import org.cacert.gigi.database.GigiPreparedStatement;
11 import org.cacert.gigi.database.GigiResultSet;
12 import org.cacert.gigi.dbObjects.Assurance.AssuranceType;
13 import org.cacert.gigi.dbObjects.Group;
14 import org.cacert.gigi.dbObjects.Name;
15 import org.cacert.gigi.dbObjects.User;
16 import org.cacert.gigi.output.DateSelector;
17 import org.cacert.gigi.output.template.SprintfCommand;
21 public static void writeUserAgreement(User member, String document, String method, String comment, boolean active, int secmemid) {
22 try (GigiPreparedStatement q = new GigiPreparedStatement("INSERT INTO `user_agreements` SET `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?")) {
23 q.setInt(1, member.getId());
24 q.setInt(2, secmemid);
25 q.setString(3, document);
26 q.setBoolean(4, active);
27 q.setString(5, method);
28 q.setString(6, comment);
33 public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
34 if (assurer.getId() == target.getId()) {
35 throw new GigiApiException("You cannot assure yourself.");
37 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted` IS NULL")) {
38 ps.setInt(1, target.getId());
39 ps.setInt(2, assurer.getId());
40 GigiResultSet rs = ps.executeQuery();
43 throw new GigiApiException("You have already assured this member.");
46 if ( !assurer.canAssure()) {
47 throw new GigiApiException("You are not an assurer.");
51 public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
53 public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
56 * This method assures another user.
58 * @see User#canAssure() (for assurer)
59 * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
61 * the person that wants to assure
63 * the person that should be assured
65 * the Name that was personally verified
67 * the Date of birth that the assurer verified
69 * the points that should be awarded in total
71 * the location where the assurance took place
73 * the date when the assurance took place
74 * @throws GigiApiException
75 * if the assurance fails (for various reasons)
77 public synchronized static void assure(User assurer, User assuree, Name assureeName, DayDate dob, int awarded, String location, String date, AssuranceType type) throws GigiApiException {
78 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
79 GigiApiException gae = new GigiApiException();
80 if ( !gae.isEmpty()) {
83 if (date == null || date.equals("")) {
84 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
87 Date d = DateSelector.getDateFormat().parse(date);
88 Calendar gc = GregorianCalendar.getInstance();
89 gc.setTimeInMillis(System.currentTimeMillis());
90 gc.add(Calendar.HOUR_OF_DAY, 12);
91 if (d.getTime() > gc.getTimeInMillis()) {
92 gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
94 } catch (ParseException e) {
95 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
98 // check location, min 3 characters
99 if (location == null || location.equals("")) {
100 gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
101 } else if (location.length() <= 2) {
102 gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
104 synchronized (assuree) {
107 checkAssuranceIsPossible(assurer, assuree);
108 } catch (GigiApiException e) {
112 if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
113 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
116 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
118 if (type == AssuranceType.NUCLEUS) {
120 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
123 if (awarded > assurer.getMaxAssurePoints()) {
124 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
129 if ( !gae.isEmpty()) {
133 if (type == AssuranceType.FACE_TO_FACE) {
134 assureF2F(assurer, assuree, awarded, location, date);
135 } else if (type == AssuranceType.NUCLEUS) {
136 assureNucleus(assurer, assuree, awarded, location, date);
137 } else if (type == AssuranceType.TTP_ASSISTED) {
138 assureTTP(assurer, assuree, awarded, location, date);
140 throw new GigiApiException(new SprintfCommand("Unknown Assurance type: %s", Arrays.asList(type.toString())));
142 assurer.invalidateMadeAssurances();
143 assuree.invalidateReceivedAssurances();
147 private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
148 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
149 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
150 ps.setInt(1, assurer.getId());
151 ps.setInt(2, assuree.getId());
152 ps.setInt(3, awarded);
153 ps.setString(4, location);
154 ps.setString(5, date);
159 private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
160 may(assurer, assuree, AssuranceType.TTP_ASSISTED);
161 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
162 ps.setInt(1, assurer.getId());
163 ps.setInt(2, assuree.getId());
164 ps.setInt(3, awarded);
165 ps.setString(4, location);
166 ps.setString(5, date);
168 assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
172 public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
173 if (assuree.isInGroup(ASSUREE_BLOCKED)) {
174 throw new GigiApiException("The assuree is blocked.");
176 if (assurer.isInGroup(ASSURER_BLOCKED)) {
177 throw new GigiApiException("The assurer is blocked.");
180 if (t == AssuranceType.NUCLEUS) {
181 if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
182 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
185 } else if (t == AssuranceType.TTP_ASSISTED) {
186 if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
187 throw new GigiApiException("Assurer needs to be TTP Assurer.");
189 if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
190 throw new GigiApiException("Assuree needs to be TTP Applicant.");
193 } else if (t == AssuranceType.FACE_TO_FACE) {
196 throw new GigiApiException("Assurance type not possible.");
199 private static void assureNucleus(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
200 may(assurer, assuree, AssuranceType.NUCLEUS);
201 // Do up to 35 points as f2f
202 int f2fPoints = Math.min(assurer.getMaxAssurePoints(), awarded);
203 assureF2F(assurer, assuree, f2fPoints, location, date);
205 awarded -= f2fPoints;
210 // Assure remaining points as "Nucleus Bonus"
211 // Valid for 4 Weeks = 28 days
212 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='Nucleus Bonus', `expire` = CURRENT_TIMESTAMP + interval '28 days'")) {
213 ps.setInt(1, assurer.getId());
214 ps.setInt(2, assuree.getId());
215 ps.setInt(3, awarded);
216 ps.setString(4, location);
217 ps.setString(5, date);