]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
upd: exchanged assure, assurance, Assurer with verification and RA Agent
[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 import org.cacert.gigi.output.template.SprintfCommand;
17
18 public class Notary {
19
20     public final static int LIMIT_DAYS_VERIFICATION = 90; // conf.getProperty("limit_days_verification");
21
22     public static void writeUserAgreement(User member, String document, String method, String comment, boolean active, int secmemid) {
23         try (GigiPreparedStatement q = new GigiPreparedStatement("INSERT INTO `user_agreements` SET `memid`=?, `secmemid`=?," + " `document`=?,`date`=NOW(), `active`=?,`method`=?,`comment`=?")) {
24             q.setInt(1, member.getId());
25             q.setInt(2, secmemid);
26             q.setString(3, document);
27             q.setBoolean(4, active);
28             q.setString(5, method);
29             q.setString(6, comment);
30             q.execute();
31         }
32     }
33
34     public static void checkAssuranceIsPossible(User assurer, User target) throws GigiApiException {
35         if (assurer.getId() == target.getId()) {
36             throw new GigiApiException("You cannot assure yourself.");
37         }
38         try (GigiPreparedStatement ps = new GigiPreparedStatement("SELECT 1 FROM `notary` where `to`=? and `from`=? and `method` = ? ::`notaryType` AND `deleted` IS NULL AND `when` > (now() - interval '1 days' * ?)")) {
39             ps.setInt(1, target.getId());
40             ps.setInt(2, assurer.getId());
41             ps.setString(3, AssuranceType.FACE_TO_FACE.getDescription());
42             ps.setInt(4, LIMIT_DAYS_VERIFICATION);
43             GigiResultSet rs = ps.executeQuery();
44             if (rs.next()) {
45                 rs.close();
46                 throw new GigiApiException(SprintfCommand.createSimple("You have already verified this applicant within the last {0} days.", LIMIT_DAYS_VERIFICATION));
47             }
48         }
49         if ( !assurer.canAssure()) {
50             throw new GigiApiException("You are not an assurer.");
51         }
52     }
53
54     public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
55
56     public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
57
58     /**
59      * This method assures another user.
60      * 
61      * @see User#canAssure() (for assurer)
62      * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
63      * @param assurer
64      *            the person that wants to assure
65      * @param assuree
66      *            the person that should be assured
67      * @param assureeName
68      *            the Name that was personally verified
69      * @param dob
70      *            the Date of birth that the assurer verified
71      * @param awarded
72      *            the points that should be awarded in total
73      * @param location
74      *            the location where the assurance took place
75      * @param date
76      *            the date when the assurance took place
77      * @throws GigiApiException
78      *             if the assurance fails (for various reasons)
79      */
80     public synchronized static void assure(User assurer, User assuree, Name assureeName, DayDate dob, int awarded, String location, String date, AssuranceType type) throws GigiApiException {
81         may(assurer, assuree, AssuranceType.FACE_TO_FACE);
82         GigiApiException gae = new GigiApiException();
83         if ( !gae.isEmpty()) {
84             throw gae;
85         }
86         if (date == null || date.equals("")) {
87             gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
88         } else {
89             try {
90                 Date d = DateSelector.getDateFormat().parse(date);
91                 Calendar gc = GregorianCalendar.getInstance();
92                 gc.setTimeInMillis(System.currentTimeMillis());
93                 gc.add(Calendar.HOUR_OF_DAY, 12);
94                 if (d.getTime() > gc.getTimeInMillis()) {
95                     gae.mergeInto(new GigiApiException("You must not enter a date in the future."));
96                 }
97             } catch (ParseException e) {
98                 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
99             }
100         }
101         // check location, min 3 characters
102         if (location == null || location.equals("")) {
103             gae.mergeInto(new GigiApiException("You failed to enter a location of your meeting."));
104         } else if (location.length() <= 2) {
105             gae.mergeInto(new GigiApiException("You must enter a location with at least 3 characters eg town and country."));
106         }
107         synchronized (assuree) {
108
109             try {
110                 checkAssuranceIsPossible(assurer, assuree);
111             } catch (GigiApiException e) {
112                 gae.mergeInto(e);
113             }
114
115             if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
116                 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
117             }
118             if (awarded < 0) {
119                 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
120             } else {
121                 if (type == AssuranceType.NUCLEUS) {
122                     if (awarded > 50) {
123                         gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
124                     }
125                 } else {
126                     if (awarded > assurer.getMaxAssurePoints()) {
127                         gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
128                     }
129                 }
130             }
131
132             if ( !gae.isEmpty()) {
133                 throw gae;
134             }
135
136             if (type == AssuranceType.FACE_TO_FACE) {
137                 assureF2F(assurer, assuree, awarded, location, date);
138             } else if (type == AssuranceType.NUCLEUS) {
139                 assureNucleus(assurer, assuree, awarded, location, date);
140             } else if (type == AssuranceType.TTP_ASSISTED) {
141                 assureTTP(assurer, assuree, awarded, location, date);
142             } else {
143                 throw new GigiApiException(SprintfCommand.createSimple("Unknown Assurance type: {0}", type.toString()));
144             }
145             assurer.invalidateMadeAssurances();
146             assuree.invalidateReceivedAssurances();
147         }
148     }
149
150     private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
151         may(assurer, assuree, AssuranceType.FACE_TO_FACE);
152         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
153             ps.setInt(1, assurer.getId());
154             ps.setInt(2, assuree.getId());
155             ps.setInt(3, awarded);
156             ps.setString(4, location);
157             ps.setString(5, date);
158             ps.execute();
159         }
160     }
161
162     private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
163         may(assurer, assuree, AssuranceType.TTP_ASSISTED);
164         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
165             ps.setInt(1, assurer.getId());
166             ps.setInt(2, assuree.getId());
167             ps.setInt(3, awarded);
168             ps.setString(4, location);
169             ps.setString(5, date);
170             ps.execute();
171             assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
172         }
173     }
174
175     public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
176         if (assuree.isInGroup(ASSUREE_BLOCKED)) {
177             throw new GigiApiException("The applicant is blocked.");
178         }
179         if (assurer.isInGroup(ASSURER_BLOCKED)) {
180             throw new GigiApiException("The RA Agent is blocked.");
181         }
182
183         if (t == AssuranceType.NUCLEUS) {
184             if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
185                 throw new GigiApiException("RA Agent needs to be Nucleus RA Agent.");
186             }
187             return;
188         } else if (t == AssuranceType.TTP_ASSISTED) {
189             if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
190                 throw new GigiApiException("RA Agent needs to be TTP RA Agent.");
191             }
192             if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
193                 throw new GigiApiException("Applicant needs to be TTP Applicant.");
194             }
195             return;
196         } else if (t == AssuranceType.FACE_TO_FACE) {
197             return;
198         }
199         throw new GigiApiException("Verification type not possible.");
200     }
201
202     private static void assureNucleus(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
203         may(assurer, assuree, AssuranceType.NUCLEUS);
204         // Do up to 35 points as f2f
205         int f2fPoints = Math.min(assurer.getMaxAssurePoints(), awarded);
206         assureF2F(assurer, assuree, f2fPoints, location, date);
207
208         awarded -= f2fPoints;
209         if (awarded <= 0) {
210             return;
211         }
212
213         // Assure remaining points as "Nucleus Bonus"
214         // Valid for 4 Weeks = 28 days
215         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='Nucleus Bonus', `expire` = CURRENT_TIMESTAMP + interval '28 days'")) {
216             ps.setInt(1, assurer.getId());
217             ps.setInt(2, assuree.getId());
218             ps.setInt(3, awarded);
219             ps.setString(4, location);
220             ps.setString(5, date);
221             ps.execute();
222         }
223     }
224 }