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");
45 String localeStr = rs.getString("language");
46 if (localeStr == null || localeStr.equals("")) {
47 locale = Locale.getDefault();
49 locale = Language.getLocaleFromString(localeStr);
51 GigiPreparedStatement psg = DatabaseConnection.getInstance().prepare("SELECT permission FROM user_groups WHERE user=? AND deleted is NULL");
52 psg.setInt(1, rs.getInt("id"));
53 GigiResultSet rs2 = psg.executeQuery();
55 groups.add(Group.getByString(rs2.getString(1)));
62 public String getFname() {
66 public String getLname() {
70 public String getMname() {
74 public Name getName() {
78 public void setMname(String mname) {
79 this.name.mname = mname;
82 public String getSuffix() {
86 public void setSuffix(String suffix) {
87 this.name.suffix = suffix;
90 public Date getDob() {
94 public void setDob(Date dob) {
98 public String getEmail() {
102 public void setEmail(String email) {
106 public void setFname(String fname) {
107 this.name.fname = fname;
110 public void setLname(String lname) {
111 this.name.lname = lname;
114 public void insert(String password) {
115 int id = super.insert();
116 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `language`=?, id=?");
117 query.setString(1, email);
118 query.setString(2, PasswordHash.hash(password));
119 query.setString(3, name.fname);
120 query.setString(4, name.mname);
121 query.setString(5, name.lname);
122 query.setString(6, name.suffix);
123 query.setDate(7, new java.sql.Date(dob.getTime()));
124 query.setString(8, locale.toString());
129 public void changePassword(String oldPass, String newPass) throws GigiApiException {
130 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `password` FROM users WHERE id=?");
131 ps.setInt(1, getId());
132 GigiResultSet rs = ps.executeQuery();
134 throw new GigiApiException("User not found... very bad.");
136 if (PasswordHash.verifyHash(oldPass, rs.getString(1)) == null) {
137 throw new GigiApiException("Old password does not match.");
140 PasswordStrengthChecker.assertStrongPassword(newPass, this);
141 ps = DatabaseConnection.getInstance().prepare("UPDATE users SET `password`=? WHERE id=?");
142 ps.setString(1, PasswordHash.hash(newPass));
143 ps.setInt(2, getId());
147 public void setName(Name name) {
151 public boolean canAssure() {
152 if ( !isOfAge(14)) { // PoJAM
155 if (getAssurancePoints() < 100) {
159 return hasPassedCATS();
163 public boolean hasPassedCATS() {
164 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT 1 FROM `cats_passed` where `user_id`=?");
165 query.setInt(1, getId());
166 GigiResultSet rs = query.executeQuery();
174 public int getAssurancePoints() {
175 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT sum(points) FROM `notary` where `to`=? AND `deleted` is NULL");
176 query.setInt(1, getId());
177 GigiResultSet rs = query.executeQuery();
180 points = rs.getInt(1);
186 public int getExperiencePoints() {
187 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT count(*) FROM `notary` where `from`=? AND `deleted` is NULL");
188 query.setInt(1, getId());
189 GigiResultSet rs = query.executeQuery();
192 points = rs.getInt(1) * 2;
199 * Gets the maximum allowed points NOW. Note that an assurance needs to
200 * re-check PoJam as it has taken place in the past.
202 * @return the maximal points @
204 public int getMaxAssurePoints() {
209 int exp = getExperiencePoints();
230 public boolean isOfAge(int desiredAge) {
231 Calendar c = Calendar.getInstance();
233 int year = c.get(Calendar.YEAR);
234 int month = c.get(Calendar.MONTH);
235 int day = c.get(Calendar.DAY_OF_MONTH);
236 c.set(year, month, day);
237 c.add(Calendar.YEAR, desiredAge);
238 return System.currentTimeMillis() >= c.getTime().getTime();
241 public boolean isValidName(String name) {
242 return getName().matches(name);
245 public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
246 EmailAddress[] adrs = getEmails();
247 for (int i = 0; i < adrs.length; i++) {
248 if (adrs[i].getAddress().equals(newMail.getAddress())) {
249 if ( !adrs[i].isVerified()) {
250 throw new GigiApiException("Email not verified.");
252 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
253 ps.setString(1, newMail.getAddress());
254 ps.setInt(2, getId());
256 email = newMail.getAddress();
260 throw new GigiApiException("Given address not an address of the user.");
263 public void deleteEmail(EmailAddress mail) throws GigiApiException {
264 if (getEmail().equals(mail.getAddress())) {
265 throw new GigiApiException("Can't delete user's default e-mail.");
267 EmailAddress[] emails = getEmails();
268 for (int i = 0; i < emails.length; i++) {
269 if (emails[i].getId() == mail.getId()) {
270 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
271 ps.setDate(1, new Date(System.currentTimeMillis()));
272 ps.setInt(2, mail.getId());
277 throw new GigiApiException("Email not one of user's email addresses.");
280 public Assurance[] getReceivedAssurances() {
281 if (receivedAssurances == null) {
282 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted IS NULL");
283 query.setInt(1, getId());
284 GigiResultSet res = query.executeQuery();
286 Assurance[] assurances = new Assurance[res.getRow()];
288 for (int i = 0; i < assurances.length; i++) {
290 assurances[i] = new Assurance(res);
292 this.receivedAssurances = assurances;
295 return receivedAssurances;
298 public Assurance[] getMadeAssurances() {
299 if (madeAssurances == null) {
300 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted is NULL");
301 query.setInt(1, getId());
302 GigiResultSet res = query.executeQuery();
304 Assurance[] assurances = new Assurance[res.getRow()];
306 for (int i = 0; i < assurances.length; i++) {
308 assurances[i] = new Assurance(res);
310 this.madeAssurances = assurances;
313 return madeAssurances;
316 public void invalidateMadeAssurances() {
317 madeAssurances = null;
320 public void invalidateReceivedAssurances() {
321 receivedAssurances = null;
324 public void updateUserData() throws GigiApiException {
325 synchronized (Notary.class) {
326 if (getAssurancePoints() != 0) {
327 throw new GigiApiException("No change after assurance allowed.");
329 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
330 update.setString(1, getFname());
331 update.setString(2, getLname());
332 update.setString(3, getMname());
333 update.setString(4, getSuffix());
334 update.setDate(5, getDob());
335 update.setInt(6, getId());
336 update.executeUpdate();
340 public Locale getPreferredLocale() {
344 public void setPreferredLocale(Locale locale) {
345 this.locale = locale;
349 public boolean wantsDirectoryListing() {
350 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?");
351 get.setInt(1, getId());
352 GigiResultSet exec = get.executeQuery();
354 return exec.getBoolean("listme");
357 public String getContactInformation() {
358 GigiPreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?");
359 get.setInt(1, getId());
360 GigiResultSet exec = get.executeQuery();
362 return exec.getString("contactinfo");
365 public void setDirectoryListing(boolean on) {
366 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?");
367 update.setBoolean(1, on);
368 update.setInt(2, getId());
369 update.executeUpdate();
372 public void setContactInformation(String contactInfo) {
373 GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?");
374 update.setString(1, contactInfo);
375 update.setInt(2, getId());
376 update.executeUpdate();
379 public boolean isInGroup(Group g) {
380 return groups.contains(g);
383 public Set<Group> getGroups() {
384 return Collections.unmodifiableSet(groups);
387 public void grantGroup(User granter, Group toGrant) {
389 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("INSERT INTO user_groups SET user=?, permission=?, grantedby=?");
390 ps.setInt(1, getId());
391 ps.setString(2, toGrant.getDatabaseName());
392 ps.setInt(3, granter.getId());
396 public void revokeGroup(User revoker, Group toRevoke) {
397 groups.remove(toRevoke);
398 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE user_groups SET deleted=CURRENT_TIMESTAMP, revokedby=? WHERE deleted is NULL AND permission=? AND user=?");
399 ps.setInt(1, revoker.getId());
400 ps.setString(2, toRevoke.getDatabaseName());
401 ps.setInt(3, getId());
405 public List<Organisation> getOrganisations() {
406 List<Organisation> orgas = new ArrayList<>();
407 GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT orgid FROM org_admin WHERE `memid`=? AND deleted is NULL");
408 query.setInt(1, getId());
409 GigiResultSet res = query.executeQuery();
412 orgas.add(Organisation.getById(res.getInt(1)));
417 public static synchronized User getById(int id) {
418 CertificateOwner co = CertificateOwner.getById(id);
419 if (co instanceof User) {
425 public static User getByEmail(String mail) {
426 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT users.id FROM users inner join certOwners on certOwners.id=users.id WHERE email=? AND deleted is null");
427 ps.setString(1, mail);
428 GigiResultSet rs = ps.executeQuery();
432 return User.getById(rs.getInt(1));
435 public static User[] findByEmail(String mail) {
436 LinkedList<User> results = new LinkedList<User>();
437 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");
438 ps.setString(1, mail);
439 GigiResultSet rs = ps.executeQuery();
441 results.add(User.getById(rs.getInt(1)));
443 return results.toArray(new User[results.size()]);
446 public boolean canIssue(CertificateProfile p) {
447 switch (p.getCAId()) {
451 return getAssurancePoints() > 50;
453 return getAssurancePoints() > 50 && isInGroup(Group.getByString("codesigning"));
456 return getOrganisations().size() > 0;