]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/Notary.java
general cleanup
[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.TTP_ASSISTED) {
133             assureTTP(assurer, assuree, awarded, location, date);
134         } else {
135             throw new GigiApiException("Unknown Assurance type: " + type);
136         }
137         assurer.invalidateMadeAssurances();
138         assuree.invalidateReceivedAssurances();
139     }
140
141     private static void assureF2F(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
142         may(assurer, assuree, AssuranceType.FACE_TO_FACE);
143         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?")) {
144             ps.setInt(1, assurer.getId());
145             ps.setInt(2, assuree.getId());
146             ps.setInt(3, awarded);
147             ps.setString(4, location);
148             ps.setString(5, date);
149             ps.execute();
150         }
151     }
152
153     private static void assureTTP(User assurer, User assuree, int awarded, String location, String date) throws GigiApiException {
154         may(assurer, assuree, AssuranceType.TTP_ASSISTED);
155         try (GigiPreparedStatement ps = new GigiPreparedStatement("INSERT INTO `notary` SET `from`=?, `to`=?, `points`=?, `location`=?, `date`=?, `method`='TTP-Assisted'")) {
156             ps.setInt(1, assurer.getId());
157             ps.setInt(2, assuree.getId());
158             ps.setInt(3, awarded);
159             ps.setString(4, location);
160             ps.setString(5, date);
161             ps.execute();
162             assuree.revokeGroup(assurer, Group.TTP_APPLICANT);
163         }
164     }
165
166     public static void may(User assurer, User assuree, AssuranceType t) throws GigiApiException {
167         if (assuree.isInGroup(ASSUREE_BLOCKED)) {
168             throw new GigiApiException("The assuree is blocked.");
169         }
170         if (assurer.isInGroup(ASSURER_BLOCKED)) {
171             throw new GigiApiException("The assurer is blocked.");
172         }
173
174         if (t == AssuranceType.NUCLEUS) {
175             if ( !assurer.isInGroup(Group.NUCLEUS_ASSURER)) {
176                 throw new GigiApiException("Assurer needs to be Nucleus Assurer.");
177             }
178             return;
179         } else if (t == AssuranceType.TTP_ASSISTED) {
180             if ( !assurer.isInGroup(Group.TTP_ASSURER)) {
181                 throw new GigiApiException("Assurer needs to be TTP Assurer.");
182             }
183             if ( !assuree.isInGroup(Group.TTP_APPLICANT)) {
184                 throw new GigiApiException("Assuree needs to be TTP Applicant.");
185             }
186             return;
187         } else if (t == AssuranceType.FACE_TO_FACE) {
188             return;
189         }
190         throw new GigiApiException("Assurance type not possible.");
191     }
192 }