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 static void writeUserAgreement(User member, String document, String method, String comment, boolean active, int secmemid) {
21 try (GigiPreparedStatement q = new GigiPreparedStatement("INSERT INTO `user_agreements` SET `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?")) {
22 q.setInt(1, member.getId());
23 q.setInt(2, secmemid);
24 q.setString(3, document);
25 q.setBoolean(4, active);
26 q.setString(5, method);
27 q.setString(6, comment);
32 public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
33 if (assurer.getId() == target.getId()) {
34 throw new GigiApiException("You cannot assure yourself.");
36 try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` where `to`=? and `from`=? AND `deleted` IS NULL")) {
37 ps.setInt(1, target.getId());
38 ps.setInt(2, assurer.getId());
39 GigiResultSet rs = ps.executeQuery();
42 throw new GigiApiException("You have already assured this member.");
45 if ( !assurer.canAssure()) {
46 throw new GigiApiException("You are not an assurer.");
50 public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
52 public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
55 * This method assures another user.
57 * @see User#canAssure() (for assurer)
58 * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
60 * the person that wants to assure
62 * the person that should be assured
64 * the Name that was personally verified
66 * the Date of birth that the assurer verified
68 * the points that should be awarded in total
70 * the location where the assurance took place
72 * the date when the assurance took place
73 * @throws GigiApiException
74 * if the assurance fails (for various reasons)
76 public synchronized static void assure(User assurer, User assuree, Name assureeName, DayDate dob, int awarded, String location, String date, AssuranceType type) throws GigiApiException {
77 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
78 GigiApiException gae = new GigiApiException();
79 if ( !gae.isEmpty()) {
82 if (date == null || date.equals("")) {
83 gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
86 Date d = DateSelector.getDateFormat().parse(date);
87 Calendar gc = GregorianCalendar.getInstance();
88 gc.setTimeInMillis(System.currentTimeMillis());
89 gc.add(Calendar.HOUR_OF_DAY, 12);
90 if (d.getTime() > gc.getTimeInMillis()) {
91 gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
93 } catch (ParseException e) {
94 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
97 // check location, min 3 characters
98 if (location == null || location.equals("")) {
99 gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
100 } else if (location.length() <= 2) {
101 gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
103 synchronized (assuree) {
106 checkAssuranceIsPossible(assurer, assuree);
107 } catch (GigiApiException e) {
111 if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
112 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
115 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
117 if (type == AssuranceType.NUCLEUS) {
119 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
122 if (awarded > assurer.getMaxAssurePoints()) {
123 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
128 if ( !gae.isEmpty()) {
132 if (type == AssuranceType.FACE_TO_FACE) {
133 assureF2F(assurer, assuree, awarded, location, date);
134 } else if (type == AssuranceType.NUCLEUS) {
135 assureNucleus(assurer, assuree, awarded, location, date);
136 } else if (type == AssuranceType.TTP_ASSISTED) {
137 assureTTP(assurer, assuree, awarded, location, date);
139 throw new GigiApiException(SprintfCommand.createSimple("Unknown Assurance type: {0}", type.toString()));
141 assurer.invalidateMadeAssurances();
142 assuree.invalidateReceivedAssurances();
146 private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
147 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
148 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
149 ps.setInt(1, assurer.getId());
150 ps.setInt(2, assuree.getId());
151 ps.setInt(3, awarded);
152 ps.setString(4, location);
153 ps.setString(5, date);
158 private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
159 may(assurer, assuree, AssuranceType.TTP_ASSISTED);
160 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
161 ps.setInt(1, assurer.getId());
162 ps.setInt(2, assuree.getId());
163 ps.setInt(3, awarded);
164 ps.setString(4, location);
165 ps.setString(5, date);
167 assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
171 public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
172 if (assuree.isInGroup(ASSUREE_BLOCKED)) {
173 throw new GigiApiException("The assuree is blocked.");
175 if (assurer.isInGroup(ASSURER_BLOCKED)) {
176 throw new GigiApiException("The assurer is blocked.");
179 if (t == AssuranceType.NUCLEUS) {
180 if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
181 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
184 } else if (t == AssuranceType.TTP_ASSISTED) {
185 if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
186 throw new GigiApiException("Assurer needs to be TTP Assurer.");
188 if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
189 throw new GigiApiException("Assuree needs to be TTP Applicant.");
192 } else if (t == AssuranceType.FACE_TO_FACE) {
195 throw new GigiApiException("Assurance type not possible.");
198 private static void assureNucleus(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
199 may(assurer, assuree, AssuranceType.NUCLEUS);
200 // Do up to 35 points as f2f
201 int f2fPoints = Math.min(assurer.getMaxAssurePoints(), awarded);
202 assureF2F(assurer, assuree, f2fPoints, location, date);
204 awarded -= f2fPoints;
209 // Assure remaining points as "Nucleus Bonus"
210 // Valid for 4 Weeks = 28 days
211 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='Nucleus Bonus', `expire` = CURRENT_TIMESTAMP + interval '28 days'")) {
212 ps.setInt(1, assurer.getId());
213 ps.setInt(2, assuree.getId());
214 ps.setInt(3, awarded);
215 ps.setString(4, location);
216 ps.setString(5, date);