X-Git-Url: https://code.wpia.club/?a=blobdiff_plain;f=src%2Forg%2Fcacert%2Fgigi%2FUser.java;h=41396ae102e53ea121ad67364d764f78f3af6469;hb=231b0e94c013f955d3ab3b6c2ecab6908b2b9ca8;hp=eebf9317e2b4839aadc894fd0f86363787e17ab2;hpb=d895448cb685adc4c2bfac8d92759252d2ce8c36;p=gigi.git diff --git a/src/org/cacert/gigi/User.java b/src/org/cacert/gigi/User.java index eebf9317..41396ae1 100644 --- a/src/org/cacert/gigi/User.java +++ b/src/org/cacert/gigi/User.java @@ -5,12 +5,15 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Calendar; +import java.util.Locale; import org.cacert.gigi.database.DatabaseConnection; +import org.cacert.gigi.localisation.Language; +import org.cacert.gigi.util.Notary; import org.cacert.gigi.util.PasswordHash; import org.cacert.gigi.util.PasswordStrengthChecker; -public class User { +public class User implements IdCachable { private int id; @@ -20,16 +23,30 @@ public class User { private String email; + private Assurance[] receivedAssurances, madeAssurances; + + private Locale locale; + public User(int id) { this.id = id; + updateName(id); + } + + private void updateName(int id) { try { - PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email` FROM `users` WHERE id=?"); + PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT `fname`, `lname`,`mname`, `suffix`, `dob`, `email`, `language` FROM `users` WHERE id=?"); ps.setInt(1, id); ResultSet rs = ps.executeQuery(); if (rs.next()) { name = new Name(rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4)); dob = rs.getDate(5); email = rs.getString(6); + String localeStr = rs.getString(7); + if (localeStr == null || localeStr.equals("")) { + locale = Locale.getDefault(); + } else { + locale = Language.getLocaleFromString(localeStr); + } } rs.close(); } catch (SQLException e) { @@ -103,7 +120,7 @@ public class User { if (id != 0) { throw new Error("refusing to insert"); } - PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0"); + PreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `created`=NOW(), locked=0, `language`=?"); query.setString(1, email); query.setString(2, PasswordHash.hash(password)); query.setString(3, name.fname); @@ -111,8 +128,12 @@ public class User { query.setString(5, name.lname); query.setString(6, name.suffix); query.setDate(7, new java.sql.Date(dob.getTime())); - query.execute(); - id = DatabaseConnection.lastInsertId(query); + query.setString(8, locale.toString()); + synchronized (User.class) { + query.execute(); + id = DatabaseConnection.lastInsertId(query); + myCache.put(this); + } } public void changePassword(String oldPass, String newPass) throws GigiApiException { @@ -235,10 +256,6 @@ public class User { return points; } - public static User getById(int id) { - return new User(id); - } - public EmailAddress[] getEmails() { try { PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0"); @@ -377,4 +394,115 @@ public class User { } throw new GigiApiException("Email not one of user's email addresses."); } + + public Assurance[] getReceivedAssurances() throws SQLException { + if (receivedAssurances == null) { + PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `to`=? AND deleted=0"); + query.setInt(1, getId()); + ResultSet res = query.executeQuery(); + res.last(); + Assurance[] assurances = new Assurance[res.getRow()]; + res.beforeFirst(); + for (int i = 0; i < assurances.length; i++) { + res.next(); + assurances[i] = new Assurance(res); + } + this.receivedAssurances = assurances; + return assurances; + } + return receivedAssurances; + } + + public Assurance[] getMadeAssurances() throws SQLException { + if (madeAssurances == null) { + PreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted=0"); + query.setInt(1, getId()); + ResultSet res = query.executeQuery(); + res.last(); + Assurance[] assurances = new Assurance[res.getRow()]; + res.beforeFirst(); + for (int i = 0; i < assurances.length; i++) { + res.next(); + assurances[i] = new Assurance(res); + } + this.madeAssurances = assurances; + return assurances; + } + return madeAssurances; + } + + public void invalidateMadeAssurances() { + madeAssurances = null; + } + + public void invalidateReceivedAssurances() { + receivedAssurances = null; + } + + public void updateUserData() throws SQLException, GigiApiException { + synchronized (Notary.class) { + if (getAssurancePoints() != 0) { + throw new GigiApiException("No change after assurance allowed."); + } + PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?"); + update.setString(1, getFname()); + update.setString(2, getLname()); + update.setString(3, getMname()); + update.setString(4, getSuffix()); + update.setDate(5, getDob()); + update.setInt(6, getId()); + update.executeUpdate(); + } + } + + public Locale getPreferredLocale() { + return locale; + } + + public void setPreferredLocale(Locale locale) { + this.locale = locale; + + } + + public boolean wantsDirectoryListing() throws SQLException { + PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT listme FROM users WHERE id=?"); + get.setInt(1, getId()); + ResultSet exec = get.executeQuery(); + exec.next(); + return exec.getBoolean("listme"); + } + + public String getContactInformation() throws SQLException { + PreparedStatement get = DatabaseConnection.getInstance().prepare("SELECT contactinfo FROM users WHERE id=?"); + get.setInt(1, getId()); + ResultSet exec = get.executeQuery(); + exec.next(); + return exec.getString("contactinfo"); + } + + public void setDirectoryListing(boolean on) throws SQLException { + PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET listme = ? WHERE id = ?"); + update.setBoolean(1, on); + update.setInt(2, getId()); + update.executeUpdate(); + } + + public void setContactInformation(String contactInfo) throws SQLException { + PreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET contactinfo = ? WHERE id = ?"); + update.setString(1, contactInfo); + update.setInt(2, getId()); + update.executeUpdate(); + } + + private static ObjectCache myCache = new ObjectCache<>(); + + public static User getById(int id) { + User u = myCache.get(id); + if (u == null) { + synchronized (User.class) { + myCache.put(u = new User(id)); + } + } + return u; + } }