]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
901e5ef62e46891479bf7af5f12d827d0bd4a1db
[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 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);
28             q.execute();
29         }
30     }
31
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.");
35         }
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();
40             if (rs.next()) {
41                 rs.close();
42                 throw new GigiApiException("You have already assured this member.");
43             }
44         }
45         if ( !assurer.canAssure()) {
46             throw new GigiApiException("You are not an assurer.");
47         }
48     }
49
50     public static final Group ASSURER_BLOCKED = Group.getByString("blockedassurer");
51
52     public static final Group ASSUREE_BLOCKED = Group.getByString("blockedassuree");
53
54     /**
55      * This method assures another user.
56      * 
57      * @see User#canAssure() (for assurer)
58      * @see #checkAssuranceIsPossible(User, User) (for assurer or assuree)
59      * @param assurer
60      *            the person that wants to assure
61      * @param assuree
62      *            the person that should be assured
63      * @param assureeName
64      *            the Name that was personally verified
65      * @param dob
66      *            the Date of birth that the assurer verified
67      * @param awarded
68      *            the points that should be awarded in total
69      * @param location
70      *            the location where the assurance took place
71      * @param date
72      *            the date when the assurance took place
73      * @throws GigiApiException
74      *             if the assurance fails (for various reasons)
75      */
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()) {
80             throw gae;
81         }
82         if (date == null || date.equals("")) {
83             gae.mergeInto(new GigiApiException("You must enter the date when you met the assuree."));
84         } else {
85             try {
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."));
92                 }
93             } catch (ParseException e) {
94                 gae.mergeInto(new GigiApiException("You must enter the date in this format: YYYY-MM-DD."));
95             }
96         }
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."));
102         }
103         synchronized (assuree) {
104
105             try {
106                 checkAssuranceIsPossible(assurer, assuree);
107             } catch (GigiApiException e) {
108                 gae.mergeInto(e);
109             }
110
111             if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
112                 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
113             }
114             if (awarded < 0) {
115                 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
116             } else {
117                 if (type == AssuranceType.NUCLEUS) {
118                     if (awarded > 50) {
119                         gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
120                     }
121                 } else {
122                     if (awarded > assurer.getMaxAssurePoints()) {
123                         gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
124                     }
125                 }
126             }
127
128             if ( !gae.isEmpty()) {
129                 throw gae;
130             }
131
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);
138             } else {
139                 throw new GigiApiException(SprintfCommand.createSimple("Unknown Assurance type: {0}", type.toString()));
140             }
141             assurer.invalidateMadeAssurances();
142             assuree.invalidateReceivedAssurances();
143         }
144     }
145
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);
154             ps.execute();
155         }
156     }
157
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);
166             ps.execute();
167             assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
168         }
169     }
170
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.");
174         }
175         if (assurer.isInGroup(ASSURER_BLOCKED)) {
176             throw new GigiApiException("The assurer is blocked.");
177         }
178
179         if (t == AssuranceType.NUCLEUS) {
180             if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
181                 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
182             }
183             return;
184         } else if (t == AssuranceType.TTP_ASSISTED) {
185             if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
186                 throw new GigiApiException("Assurer needs to be TTP Assurer.");
187             }
188             if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
189                 throw new GigiApiException("Assuree needs to be TTP Applicant.");
190             }
191             return;
192         } else if (t == AssuranceType.FACE_TO_FACE) {
193             return;
194         }
195         throw new GigiApiException("Assurance type not possible.");
196     }
197
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);
203
204         awarded -= f2fPoints;
205         if (awarded <= 0) {
206             return;
207         }
208
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);
217             ps.execute();
218         }
219     }
220 }