1 package org.cacert.gigi.dbObjects;
4 import java.util.ArrayList;
5 import java.util.Calendar;
6 import java.util.Collections;
7 import java.util.HashSet;
8 import java.util.LinkedList;
10 import java.util.Locale;
13 import org.cacert.gigi.GigiApiException;
14 import org.cacert.gigi.database.DatabaseConnection;
15 import org.cacert.gigi.database.GigiPreparedStatement;
16 import org.cacert.gigi.database.GigiResultSet;
17 import org.cacert.gigi.localisation.Language;
18 import org.cacert.gigi.util.Notary;
19 import org.cacert.gigi.util.PasswordHash;
20 import org.cacert.gigi.util.PasswordStrengthChecker;
22 public class User extends CertificateOwner {
24 private Name name = new Name(null, null, null, null);
30 private Assurance[] receivedAssurances, madeAssurances;
32 private Locale locale;
34 private Set<Group> groups = new HashSet<>();
36 protected User(GigiResultSet rs) {
37 super(rs.getInt("id"));
41 private void updateName(GigiResultSet rs) {
42 name = new Name(rs.getString("fname"), rs.getString("lname"), rs.getString("mname"), rs.getString("suffix"));
43 dob = rs.getDate("dob");
44 email = rs.getString("email");
46 String localeStr = rs.getString("language");
47 if (localeStr == null || localeStr.equals("")) {
48 locale = Locale.getDefault();
50 locale = Language.getLocaleFromString(localeStr);
53 GigiPreparedStatement psg = DatabaseConnection.getInstance().prepare("SELECT permission FROM user_groups WHERE user=? AND deleted is NULL");
54 psg.setInt(1, rs.getInt("id"));
55 GigiResultSet rs2 = psg.executeQuery();
57 groups.add(Group.getByString(rs2.getString(1)));
64 public String getFName() {
68 public String getLName() {
72 public String getMName() {
76 public Name getName() {
80 public void setMName(String mname) {
81 this.name.mname = mname;
84 public String getSuffix() {
88 public void setSuffix(String suffix) {
89 this.name.suffix = suffix;
92 public Date getDoB() {
96 public void setDoB(Date dob) {
100 public String getEmail() {
104 public void setEmail(String email) {
108 public void setFName(String fname) {
109 this.name.fname = fname;
112 public void setLName(String lname) {
113 this.name.lname = lname;
116 public void insert(String password) {
117 int id = super.insert();
118 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `language`=?, id=?");
119 query.setString(1, email);
120 query.setString(2, PasswordHash.hash(password));
121 query.setString(3, name.fname);
122 query.setString(4, name.mname);
123 query.setString(5, name.lname);
124 query.setString(6, name.suffix);
125 query.setDate(7, new java.sql.Date(dob.getTime()));
126 query.setString(8, locale.toString());
131 public void changePassword(String oldPass, String newPass) throws GigiApiException {
132 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
133 ps.setInt(1, getId());
134 GigiResultSet rs = ps.executeQuery();
136 throw new GigiApiException("User not found... very bad.");
138 if (PasswordHash.verifyHash(oldPass, rs.getString(1)) == null) {
139 throw new GigiApiException("Old password does not match.");
142 PasswordStrengthChecker.assertStrongPassword(newPass, this);
143 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
144 ps.setString(1, PasswordHash.hash(newPass));
145 ps.setInt(2, getId());
149 public void setName(Name name) {
153 public boolean canAssure() {
154 if ( !isOfAge(14)) { // PoJAM
157 if (getAssurancePoints() < 100) {
161 return hasPassedCATS();
165 public boolean hasPassedCATS() {
166 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
167 query.setInt(1, getId());
168 GigiResultSet rs = query.executeQuery();
176 public int getAssurancePoints() {
177 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL");
178 query.setInt(1, getId());
179 GigiResultSet rs = query.executeQuery();
182 points = rs.getInt(1);
188 public int getExperiencePoints() {
189 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted` is NULL");
190 query.setInt(1, getId());
191 GigiResultSet rs = query.executeQuery();
194 points = rs.getInt(1) * 2;
201 * Gets the maximum allowed points NOW. Note that an assurance needs to
202 * re-check PoJam as it has taken place in the past.
204 * @return the maximal points @
206 public int getMaxAssurePoints() {
211 int exp = getExperiencePoints();
233 public boolean isOfAge(int desiredAge) {
234 Calendar c = Calendar.getInstance();
236 int year = c.get(Calendar.YEAR);
237 int month = c.get(Calendar.MONTH);
238 int day = c.get(Calendar.DAY_OF_MONTH);
239 c.set(year, month, day);
240 c.add(Calendar.YEAR, desiredAge);
241 return System.currentTimeMillis() >= c.getTime().getTime();
244 public boolean isValidName(String name) {
245 return getName().matches(name);
248 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
249 EmailAddress[] adrs = getEmails();
250 for (int i = 0; i < adrs.length; i++) {
251 if (adrs[i].getAddress().equals(newMail.getAddress())) {
252 if ( !adrs[i].isVerified()) {
253 throw new GigiApiException("Email not verified.");
255 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
256 ps.setString(1, newMail.getAddress());
257 ps.setInt(2, getId());
259 email = newMail.getAddress();
263 throw new GigiApiException("Given address not an address of the user.");
266 public void deleteEmail(EmailAddress mail) throws GigiApiException {
267 if (getEmail().equals(mail.getAddress())) {
268 throw new GigiApiException("Can't delete user's default e-mail.");
270 EmailAddress[] emails = getEmails();
271 for (int i = 0; i < emails.length; i++) {
272 if (emails[i].getId() == mail.getId()) {
273 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
274 ps.setDate(1, new Date(System.currentTimeMillis()));
275 ps.setInt(2, mail.getId());
280 throw new GigiApiException("Email not one of user's email addresses.");
283 public Assurance[] getReceivedAssurances() {
284 if (receivedAssurances == null) {
285 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted IS NULL");
286 query.setInt(1, getId());
287 GigiResultSet res = query.executeQuery();
289 Assurance[] assurances = new Assurance[res.getRow()];
291 for (int i = 0; i < assurances.length; i++) {
293 assurances[i] = new Assurance(res);
295 this.receivedAssurances = assurances;
298 return receivedAssurances;
301 public Assurance[] getMadeAssurances() {
302 if (madeAssurances == null) {
303 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted is NULL");
304 query.setInt(1, getId());
305 GigiResultSet res = query.executeQuery();
307 Assurance[] assurances = new Assurance[res.getRow()];
309 for (int i = 0; i < assurances.length; i++) {
311 assurances[i] = new Assurance(res);
313 this.madeAssurances = assurances;
316 return madeAssurances;
319 public void invalidateMadeAssurances() {
320 madeAssurances = null;
323 public void invalidateReceivedAssurances() {
324 receivedAssurances = null;
327 public void updateUserData() throws GigiApiException {
328 synchronized (Notary.class) {
329 // FIXME: No assurance, not no points.
330 if (getAssurancePoints() != 0) {
331 throw new GigiApiException("No change after assurance allowed.");
334 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
335 update.setString(1, getFName());
336 update.setString(2, getLName());
337 update.setString(3, getMName());
338 update.setString(4, getSuffix());
339 update.setDate(5, getDoB());
340 update.setInt(6, getId());
341 update.executeUpdate();
345 public Locale getPreferredLocale() {
349 public void setPreferredLocale(Locale locale) {
350 this.locale = locale;
354 public boolean wantsDirectoryListing() {
355 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
356 get.setInt(1, getId());
357 GigiResultSet exec = get.executeQuery();
359 return exec.getBoolean("listme");
362 public String getContactInformation() {
363 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
364 get.setInt(1, getId());
365 GigiResultSet exec = get.executeQuery();
367 return exec.getString("contactinfo");
370 public void setDirectoryListing(boolean on) {
371 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
372 update.setBoolean(1, on);
373 update.setInt(2, getId());
374 update.executeUpdate();
377 public void setContactInformation(String contactInfo) {
378 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
379 update.setString(1, contactInfo);
380 update.setInt(2, getId());
381 update.executeUpdate();
384 public boolean isInGroup(Group g) {
385 return groups.contains(g);
388 public Set<Group> getGroups() {
389 return Collections.unmodifiableSet(groups);
392 public void grantGroup(User granter, Group toGrant) {
394 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
395 ps.setInt(1, getId());
396 ps.setString(2, toGrant.getDatabaseName());
397 ps.setInt(3, granter.getId());
401 public void revokeGroup(User revoker, Group toRevoke) {
402 groups.remove(toRevoke);
403 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
404 ps.setInt(1, revoker.getId());
405 ps.setString(2, toRevoke.getDatabaseName());
406 ps.setInt(3, getId());
410 public List<Organisation> getOrganisations() {
411 List<Organisation> orgas = new ArrayList<>();
412 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT orgid FROM org_admin WHERE `memid`=? AND deleted is NULL");
413 query.setInt(1, getId());
414 GigiResultSet res = query.executeQuery();
417 orgas.add(Organisation.getById(res.getInt(1)));
422 public static synchronized User getById(int id) {
423 CertificateOwner co = CertificateOwner.getById(id);
424 if (co instanceof User) {
431 public static User getByEmail(String mail) {
432 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT users.id FROM users inner join certOwners on certOwners.id=users.id WHERE email=? AND deleted is null");
433 ps.setString(1, mail);
434 GigiResultSet rs = ps.executeQuery();
438 return User.getById(rs.getInt(1));
441 public static User[] findByEmail(String mail) {
442 LinkedList<User> results = new LinkedList<User>();
443 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT users.id FROM users inner join certOwners on certOwners.id=users.id WHERE users.email LIKE ? AND deleted is null GROUP BY users.id ASC LIMIT 100");
444 ps.setString(1, mail);
445 GigiResultSet rs = ps.executeQuery();
447 results.add(User.getById(rs.getInt(1)));
449 return results.toArray(new User[results.size()]);
452 public boolean canIssue(CertificateProfile p) {
453 switch (p.getCAId()) {
457 return getAssurancePoints() > 50;
459 return getAssurancePoints() > 50 && isInGroup(Group.getByString("codesigning"));
462 return getOrganisations().size() > 0;