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"));
56 try (GigiResultSet rs2 = psg.executeQuery()) {
58 groups.add(Group.getByString(rs2.getString(1)));
65 public String getFName() {
69 public String getLName() {
73 public String getMName() {
77 public Name getName() {
81 public void setMName(String mname) {
82 this.name.mname = mname;
85 public String getSuffix() {
89 public void setSuffix(String suffix) {
90 this.name.suffix = suffix;
93 public Date getDoB() {
97 public void setDoB(Date dob) {
101 public String getEmail() {
105 public void setEmail(String email) {
109 public void setFName(String fname) {
110 this.name.fname = fname;
113 public void setLName(String lname) {
114 this.name.lname = lname;
117 public void insert(String password) {
118 int id = super.insert();
119 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `language`=?, id=?");
120 query.setString(1, email);
121 query.setString(2, PasswordHash.hash(password));
122 query.setString(3, name.fname);
123 query.setString(4, name.mname);
124 query.setString(5, name.lname);
125 query.setString(6, name.suffix);
126 query.setDate(7, new java.sql.Date(dob.getTime()));
127 query.setString(8, locale.toString());
132 public void changePassword(String oldPass, String newPass) throws GigiApiException {
133 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
134 ps.setInt(1, getId());
135 try (GigiResultSet rs = ps.executeQuery()) {
137 throw new GigiApiException("User not found... very bad.");
139 if (PasswordHash.verifyHash(oldPass, rs.getString(1)) == null) {
140 throw new GigiApiException("Old password does not match.");
144 PasswordStrengthChecker.assertStrongPassword(newPass, this);
145 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
146 ps.setString(1, PasswordHash.hash(newPass));
147 ps.setInt(2, getId());
151 public void setName(Name name) {
155 public boolean canAssure() {
156 if ( !isOfAge(14)) { // PoJAM
159 if (getAssurancePoints() < 100) {
163 return hasPassedCATS();
167 public boolean hasPassedCATS() {
168 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
169 query.setInt(1, getId());
170 try (GigiResultSet rs = query.executeQuery()) {
179 public int getAssurancePoints() {
180 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL");
181 query.setInt(1, getId());
183 try (GigiResultSet rs = query.executeQuery()) {
187 points = rs.getInt(1);
194 public int getExperiencePoints() {
195 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted` is NULL");
196 query.setInt(1, getId());
198 try (GigiResultSet rs = query.executeQuery()) {
202 points = rs.getInt(1) * 2;
210 * Gets the maximum allowed points NOW. Note that an assurance needs to
211 * re-check PoJam as it has taken place in the past.
213 * @return the maximal points @
215 public int getMaxAssurePoints() {
220 int exp = getExperiencePoints();
242 public boolean isOfAge(int desiredAge) {
243 Calendar c = Calendar.getInstance();
245 int year = c.get(Calendar.YEAR);
246 int month = c.get(Calendar.MONTH);
247 int day = c.get(Calendar.DAY_OF_MONTH);
248 c.set(year, month, day);
249 c.add(Calendar.YEAR, desiredAge);
250 return System.currentTimeMillis() >= c.getTime().getTime();
253 public boolean isValidName(String name) {
254 return getName().matches(name);
257 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
258 EmailAddress[] adrs = getEmails();
259 for (int i = 0; i < adrs.length; i++) {
260 if (adrs[i].getAddress().equals(newMail.getAddress())) {
261 if ( !adrs[i].isVerified()) {
262 throw new GigiApiException("Email not verified.");
264 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
265 ps.setString(1, newMail.getAddress());
266 ps.setInt(2, getId());
268 email = newMail.getAddress();
272 throw new GigiApiException("Given address not an address of the user.");
275 public void deleteEmail(EmailAddress mail) throws GigiApiException {
276 if (getEmail().equals(mail.getAddress())) {
277 throw new GigiApiException("Can't delete user's default e-mail.");
279 EmailAddress[] emails = getEmails();
280 for (int i = 0; i < emails.length; i++) {
281 if (emails[i].getId() == mail.getId()) {
282 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
283 ps.setDate(1, new Date(System.currentTimeMillis()));
284 ps.setInt(2, mail.getId());
289 throw new GigiApiException("Email not one of user's email addresses.");
292 public synchronized Assurance[] getReceivedAssurances() {
293 if (receivedAssurances == null) {
294 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted IS NULL");
295 query.setInt(1, getId());
297 try (GigiResultSet res = query.executeQuery()) {
298 List<Assurance> assurances = new LinkedList<Assurance>();
301 assurances.add(new Assurance(res));
304 this.receivedAssurances = assurances.toArray(new Assurance[0]);
308 return receivedAssurances;
311 public Assurance[] getMadeAssurances() {
312 if (madeAssurances == null) {
313 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted is NULL");
314 query.setInt(1, getId());
316 try (GigiResultSet res = query.executeQuery()) {
317 List<Assurance> assurances = new LinkedList<Assurance>();
320 assurances.add(new Assurance(res));
323 this.madeAssurances = assurances.toArray(new Assurance[0]);
327 return madeAssurances;
330 public void invalidateMadeAssurances() {
331 madeAssurances = null;
334 public void invalidateReceivedAssurances() {
335 receivedAssurances = null;
338 public void updateUserData() throws GigiApiException {
339 synchronized (Notary.class) {
340 // FIXME: No assurance, not no points.
341 if (getAssurancePoints() != 0) {
342 throw new GigiApiException("No change after assurance allowed.");
345 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
346 update.setString(1, getFName());
347 update.setString(2, getLName());
348 update.setString(3, getMName());
349 update.setString(4, getSuffix());
350 update.setDate(5, getDoB());
351 update.setInt(6, getId());
352 update.executeUpdate();
356 public Locale getPreferredLocale() {
360 public void setPreferredLocale(Locale locale) {
361 this.locale = locale;
365 public boolean wantsDirectoryListing() {
366 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
367 get.setInt(1, getId());
368 try (GigiResultSet exec = get.executeQuery()) {
369 return exec.next() && exec.getBoolean("listme");
373 public String getContactInformation() {
374 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
375 get.setInt(1, getId());
377 try (GigiResultSet exec = get.executeQuery()) {
379 return exec.getString("contactinfo");
383 public void setDirectoryListing(boolean on) {
384 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
385 update.setBoolean(1, on);
386 update.setInt(2, getId());
387 update.executeUpdate();
390 public void setContactInformation(String contactInfo) {
391 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
392 update.setString(1, contactInfo);
393 update.setInt(2, getId());
394 update.executeUpdate();
397 public boolean isInGroup(Group g) {
398 return groups.contains(g);
401 public Set<Group> getGroups() {
402 return Collections.unmodifiableSet(groups);
405 public void grantGroup(User granter, Group toGrant) {
407 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
408 ps.setInt(1, getId());
409 ps.setString(2, toGrant.getDatabaseName());
410 ps.setInt(3, granter.getId());
414 public void revokeGroup(User revoker, Group toRevoke) {
415 groups.remove(toRevoke);
416 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
417 ps.setInt(1, revoker.getId());
418 ps.setString(2, toRevoke.getDatabaseName());
419 ps.setInt(3, getId());
423 public List<Organisation> getOrganisations() {
424 List<Organisation> orgas = new ArrayList<>();
425 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT orgid FROM org_admin WHERE `memid`=? AND deleted is NULL");
426 query.setInt(1, getId());
427 try (GigiResultSet res = query.executeQuery()) {
429 orgas.add(Organisation.getById(res.getInt(1)));
436 public static synchronized User getById(int id) {
437 CertificateOwner co = CertificateOwner.getById(id);
438 if (co instanceof User) {
445 public static User getByEmail(String mail) {
446 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT users.id FROM users INNER JOIN certOwners ON certOwners.id = users.id WHERE email=? AND deleted IS NULL");
447 ps.setString(1, mail);
448 try (GigiResultSet rs = ps.executeQuery()) {
453 return User.getById(rs.getInt(1));
457 public static User[] findByEmail(String mail) {
458 LinkedList<User> results = new LinkedList<User>();
459 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");
460 ps.setString(1, mail);
461 try (GigiResultSet rs = ps.executeQuery()) {
463 results.add(User.getById(rs.getInt(1)));
465 return results.toArray(new User[results.size()]);
469 public boolean canIssue(CertificateProfile p) {
470 switch (p.getCAId()) {
474 return getAssurancePoints() > 50;
476 return getAssurancePoints() > 50 && isInGroup(Group.getByString("codesigning"));
479 return getOrganisations().size() > 0;