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