]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
7cb15aad203dc14619143dfd442b238221c28b62
[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         synchronized (assuree) {
103
104             try {
105                 checkAssuranceIsPossible(assurer, assuree);
106             } catch (GigiApiException e) {
107                 gae.mergeInto(e);
108             }
109
110             if ( !assuree.getName().equals(assureeName) || !assuree.getDoB().equals(dob)) {
111                 gae.mergeInto(new GigiApiException("The person you are assuring changed his personal details."));
112             }
113             if (awarded < 0) {
114                 gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
115             } else {
116                 if (type == AssuranceType.NUCLEUS) {
117                     if (awarded > 50) {
118                         gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
119                     }
120                 } else {
121                     if (awarded > assurer.getMaxAssurePoints()) {
122                         gae.mergeInto(new GigiApiException("The points you are trying to award are out of range."));
123                     }
124                 }
125             }
126
127             if ( !gae.isEmpty()) {
128                 throw gae;
129             }
130
131             if (type == AssuranceType.FACE_TO_FACE) {
132                 assureF2F(assurer, assuree, awarded, location, date);
133             } else if (type == AssuranceType.NUCLEUS) {
134                 assureNucleus(assurer, assuree, awarded, location, date);
135             } else if (type == AssuranceType.TTP_ASSISTED) {
136                 assureTTP(assurer, assuree, awarded, location, date);
137             } else {
138                 throw new GigiApiException("Unknown Assurance type: " + type);
139             }
140             assurer.invalidateMadeAssurances();
141             assuree.invalidateReceivedAssurances();
142         }
143     }
144
145     private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
146         may(assurer, assuree, AssuranceType.FACE_TO_FACE);
147         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
148             ps.setInt(1, assurer.getId());
149             ps.setInt(2, assuree.getId());
150             ps.setInt(3, awarded);
151             ps.setString(4, location);
152             ps.setString(5, date);
153             ps.execute();
154         }
155     }
156
157     private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
158         may(assurer, assuree, AssuranceType.TTP_ASSISTED);
159         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
160             ps.setInt(1, assurer.getId());
161             ps.setInt(2, assuree.getId());
162             ps.setInt(3, awarded);
163             ps.setString(4, location);
164             ps.setString(5, date);
165             ps.execute();
166             assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
167         }
168     }
169
170     public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
171         if (assuree.isInGroup(ASSUREE_BLOCKED)) {
172             throw new GigiApiException("The assuree is blocked.");
173         }
174         if (assurer.isInGroup(ASSURER_BLOCKED)) {
175             throw new GigiApiException("The assurer is blocked.");
176         }
177
178         if (t == AssuranceType.NUCLEUS) {
179             if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
180                 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
181             }
182             return;
183         } else if (t == AssuranceType.TTP_ASSISTED) {
184             if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
185                 throw new GigiApiException("Assurer needs to be TTP Assurer.");
186             }
187             if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
188                 throw new GigiApiException("Assuree needs to be TTP Applicant.");
189             }
190             return;
191         } else if (t == AssuranceType.FACE_TO_FACE) {
192             return;
193         }
194         throw new GigiApiException("Assurance type not possible.");
195     }
196
197     private static void assureNucleus(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
198         may(assurer, assuree, AssuranceType.NUCLEUS);
199         // Do up to 35 points as f2f
200         int f2fPoints = Math.min(assurer.getMaxAssurePoints(), awarded);
201         assureF2F(assurer, assuree, f2fPoints, location, date);
202
203         awarded -= f2fPoints;
204         if (awarded <= 0) {
205             return;
206         }
207
208         // Assure remaining points as "Nucleus Bonus"
209         // Valid for 4 Weeks = 28 days
210         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='Nucleus Bonus', `expire` = CURRENT_TIMESTAMP + interval '28 days'")) {
211             ps.setInt(1, assurer.getId());
212             ps.setInt(2, assuree.getId());
213             ps.setInt(3, awarded);
214             ps.setString(4, location);
215             ps.setString(5, date);
216             ps.execute();
217         }
218     }
219 }