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."));
104 checkAssuranceIsPossible(assurer, assuree);
105 } catch (GigiApiException e) {
109 if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
110 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
113 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
115 if (type == AssuranceType.NUCLEUS) {
117 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
120 if (awarded > assurer.getMaxAssurePoints()) {
121 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
126 if ( !gae.isEmpty()) {
130 if (type == AssuranceType.FACE_TO_FACE) {
131 assureF2F(assurer, assuree, awarded, location, date);
132 } else if (type == AssuranceType.TTP_ASSISTED) {
133 assureTTP(assurer, assuree, awarded, location, date);
135 throw new GigiApiException("Unknown Assurance type: " + type);
137 assurer.invalidateMadeAssurances();
138 assuree.invalidateReceivedAssurances();
141 private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
142 may(assurer, assuree, AssuranceType.FACE_TO_FACE);
143 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
144 ps.setInt(1, assurer.getId());
145 ps.setInt(2, assuree.getId());
146 ps.setInt(3, awarded);
147 ps.setString(4, location);
148 ps.setString(5, date);
153 private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
154 may(assurer, assuree, AssuranceType.TTP_ASSISTED);
155 try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
156 ps.setInt(1, assurer.getId());
157 ps.setInt(2, assuree.getId());
158 ps.setInt(3, awarded);
159 ps.setString(4, location);
160 ps.setString(5, date);
162 assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
166 public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
167 if (assuree.isInGroup(ASSUREE_BLOCKED)) {
168 throw new GigiApiException("The assuree is blocked.");
170 if (assurer.isInGroup(ASSURER_BLOCKED)) {
171 throw new GigiApiException("The assurer is blocked.");
174 if (t == AssuranceType.NUCLEUS) {
175 if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
176 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
179 } else if (t == AssuranceType.TTP_ASSISTED) {
180 if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
181 throw new GigiApiException("Assurer needs to be TTP Assurer.");
183 if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
184 throw new GigiApiException("Assuree needs to be TTP Applicant.");
187 } else if (t == AssuranceType.FACE_TO_FACE) {
190 throw new GigiApiException("Assurance type not possible.");