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;
19 public static void writeUserAgreement(User member, String document, String method, String comment, boolean active, int secmemid) {
20 try (GigiPreparedStatement q = new GigiPreparedStatement("INSERT INTO `user_agreements` SET `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?")) {
21 q.setInt(1, member.getId());
22 q.setInt(2, secmemid);
23 q.setString(3, document);
24 q.setBoolean(4, active);
25 q.setString(5, method);
26 q.setString(6, comment);
31 public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
32 if (assurer.getId() == target.getId()) {
33 throw new GigiApiException("You cannot assure yourself.");
35 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted` IS NULL")) {
36 ps.setInt(1, target.getId());
37 ps.setInt(2, assurer.getId());
38 GigiResultSet rs = ps.executeQuery();
41 throw new GigiApiException("You have already assured this member.");
44 if ( !assurer.canAssure()) {
45 throw new GigiApiException("You are not an assurer.");
49 public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
51 public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
54 * This method assures another user.
56 * @see User#canAssure() (for assurer)
57 * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
59 * the person that wants to assure
61 * the person that should be assured
63 * the Name that was personally verified
65 * the Date of birth that the assurer verified
67 * the points that should be awarded in total
69 * the location where the assurance took place
71 * the date when the assurance took place
72 * @throws GigiApiException
73 * if the assurance fails (for various reasons)
75 public synchronized static void assure(User assurer, User assuree, Name assureeName, Date dob, int awarded, String location, String date, AssuranceType type) throws GigiApiException {
76 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
77 GigiApiException gae = new GigiApiException();
78 if ( !gae.isEmpty()) {
81 if (date == null || date.equals("")) {
82 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
85 Date d = DateSelector.getDateFormat().parse(date);
86 Calendar gc = GregorianCalendar.getInstance();
87 gc.setTimeInMillis(System.currentTimeMillis());
88 gc.add(Calendar.HOUR_OF_DAY, 12);
89 if (d.getTime() > gc.getTimeInMillis()) {
90 gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
92 } catch (ParseException e) {
93 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
96 // check location, min 3 characters
97 if (location == null || location.equals("")) {
98 gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
99 } else if (location.length() <= 2) {
100 gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
102 synchronized (assuree) {
105 checkAssuranceIsPossible(assurer, assuree);
106 } catch (GigiApiException e) {
110 if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
111 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
114 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
116 if (type == AssuranceType.NUCLEUS) {
118 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
121 if (awarded > assurer.getMaxAssurePoints()) {
122 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
127 if ( !gae.isEmpty()) {
131 if (type == AssuranceType.FACE_TO_FACE) {
132 assureF2F(assurer, assuree, awarded, location, date);
133 } else if (type == AssuranceType.NUCLEUS) {
134 assureNucleus(assurer, assuree, awarded, location, date);
135 } else if (type == AssuranceType.TTP_ASSISTED) {
136 assureTTP(assurer, assuree, awarded, location, date);
138 throw new GigiApiException("Unknown Assurance type: " + type);
140 assurer.invalidateMadeAssurances();
141 assuree.invalidateReceivedAssurances();
145 private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
146 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
147 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
148 ps.setInt(1, assurer.getId());
149 ps.setInt(2, assuree.getId());
150 ps.setInt(3, awarded);
151 ps.setString(4, location);
152 ps.setString(5, date);
157 private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
158 may(assurer, assuree, AssuranceType.TTP_ASSISTED);
159 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
160 ps.setInt(1, assurer.getId());
161 ps.setInt(2, assuree.getId());
162 ps.setInt(3, awarded);
163 ps.setString(4, location);
164 ps.setString(5, date);
166 assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
170 public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
171 if (assuree.isInGroup(ASSUREE_BLOCKED)) {
172 throw new GigiApiException("The assuree is blocked.");
174 if (assurer.isInGroup(ASSURER_BLOCKED)) {
175 throw new GigiApiException("The assurer is blocked.");
178 if (t == AssuranceType.NUCLEUS) {
179 if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
180 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
183 } else if (t == AssuranceType.TTP_ASSISTED) {
184 if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
185 throw new GigiApiException("Assurer needs to be TTP Assurer.");
187 if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
188 throw new GigiApiException("Assuree needs to be TTP Applicant.");
191 } else if (t == AssuranceType.FACE_TO_FACE) {
194 throw new GigiApiException("Assurance type not possible.");
197 private static void assureNucleus(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
198 may(assurer, assuree, AssuranceType.NUCLEUS);
199 // Do up to 35 points as f2f
200 int f2fPoints = Math.min(assurer.getMaxAssurePoints(), awarded);
201 assureF2F(assurer, assuree, f2fPoints, location, date);
203 awarded -= f2fPoints;
208 // Assure remaining points as "Nucleus Bonus"
209 // Valid for 4 Weeks = 28 days
210 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='Nucleus Bonus', `expire` = CURRENT_TIMESTAMP + interval '28 days'")) {
211 ps.setInt(1, assurer.getId());
212 ps.setInt(2, assuree.getId());
213 ps.setInt(3, awarded);
214 ps.setString(4, location);
215 ps.setString(5, date);