]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
add: nucleus assurance
[gigi.git] / src / org / cacert / gigi / util / Notary.java
1 package org.cacert.gigi.util;
2
3 import java.text.ParseException;
4 import java.util.Calendar;
5 import java.util.Date;
6 import java.util.GregorianCalendar;
7
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
17 public class Notary {
18
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);
27             q.execute();
28         }
29     }
30
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.");
34         }
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();
39             if (rs.next()) {
40                 rs.close();
41                 throw new GigiApiException("You have already assured this member.");
42             }
43         }
44         if ( !assurer.canAssure()) {
45             throw new GigiApiException("You are not an assurer.");
46         }
47     }
48
49     public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
50
51     public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
52
53     /**
54      * This method assures another user.
55      * 
56      * @see User#canAssure() (for assurer)
57      * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
58      * @param assurer
59      *            the person that wants to assure
60      * @param assuree
61      *            the person that should be assured
62      * @param assureeName
63      *            the Name that was personally verified
64      * @param dob
65      *            the Date of birth that the assurer verified
66      * @param awarded
67      *            the points that should be awarded in total
68      * @param location
69      *            the location where the assurance took place
70      * @param date
71      *            the date when the assurance took place
72      * @throws GigiApiException
73      *             if the assurance fails (for various reasons)
74      */
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()) {
79             throw gae;
80         }
81         if (date == null || date.equals("")) {
82             gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
83         } else {
84             try {
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."));
91                 }
92             } catch (ParseException e) {
93                 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
94             }
95         }
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."));
101         }
102
103         try {
104             checkAssuranceIsPossible(assurer, assuree);
105         } catch (GigiApiException e) {
106             gae.mergeInto(e);
107         }
108
109         if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
110             gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
111         }
112         if (awarded < 0) {
113             gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
114         } else {
115             if (type == AssuranceType.NUCLEUS) {
116                 if (awarded > 50) {
117                     gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
118                 }
119             } else {
120                 if (awarded > assurer.getMaxAssurePoints()) {
121                     gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
122                 }
123             }
124         }
125
126         if ( !gae.isEmpty()) {
127             throw gae;
128         }
129
130         if (type == AssuranceType.FACE_TO_FACE) {
131             assureF2F(assurer, assuree, awarded, location, date);
132         } else if (type == AssuranceType.NUCLEUS) {
133             assureNucleus(assurer, assuree, awarded, location, date);
134         } else if (type == AssuranceType.TTP_ASSISTED) {
135             assureTTP(assurer, assuree, awarded, location, date);
136         } else {
137             throw new GigiApiException("Unknown Assurance type: " + type);
138         }
139         assurer.invalidateMadeAssurances();
140         assuree.invalidateReceivedAssurances();
141     }
142
143     private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
144         may(assurer, assuree, AssuranceType.FACE_TO_FACE);
145         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
146             ps.setInt(1, assurer.getId());
147             ps.setInt(2, assuree.getId());
148             ps.setInt(3, awarded);
149             ps.setString(4, location);
150             ps.setString(5, date);
151             ps.execute();
152         }
153     }
154
155     private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
156         may(assurer, assuree, AssuranceType.TTP_ASSISTED);
157         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
158             ps.setInt(1, assurer.getId());
159             ps.setInt(2, assuree.getId());
160             ps.setInt(3, awarded);
161             ps.setString(4, location);
162             ps.setString(5, date);
163             ps.execute();
164             assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
165         }
166     }
167
168     public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
169         if (assuree.isInGroup(ASSUREE_BLOCKED)) {
170             throw new GigiApiException("The assuree is blocked.");
171         }
172         if (assurer.isInGroup(ASSURER_BLOCKED)) {
173             throw new GigiApiException("The assurer is blocked.");
174         }
175
176         if (t == AssuranceType.NUCLEUS) {
177             if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
178                 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
179             }
180             return;
181         } else if (t == AssuranceType.TTP_ASSISTED) {
182             if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
183                 throw new GigiApiException("Assurer needs to be TTP Assurer.");
184             }
185             if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
186                 throw new GigiApiException("Assuree needs to be TTP Applicant.");
187             }
188             return;
189         } else if (t == AssuranceType.FACE_TO_FACE) {
190             return;
191         }
192         throw new GigiApiException("Assurance type not possible.");
193     }
194
195     private static void assureNucleus(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
196         may(assurer, assuree, AssuranceType.NUCLEUS);
197         // Do up to 35 points as f2f
198         int f2fPoints = Math.min(35, awarded);
199         assureF2F(assurer, assuree, f2fPoints, location, date);
200
201         awarded -= f2fPoints;
202         if (awarded <= 0) {
203             return;
204         }
205
206         // Assure remaining points as "Nucleus Bonus"
207         // Valid for 4 Weeks = 28 days
208         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='Nucleus Bonus', `expire` = CURRENT_TIMESTAMP + interval '28 days'")) {
209             ps.setInt(1, assurer.getId());
210             ps.setInt(2, assuree.getId());
211             ps.setInt(3, awarded);
212             ps.setString(4, location);
213             ps.setString(5, date);
214             ps.execute();
215         }
216     }
217 }