]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/User.java
ADD: Get infomation about user's directory listing options
[gigi.git] / src / org / cacert / gigi / User.java
index f6d3c548ce51a7b608c508f4cb67425747820b7f..22f76c4c5f8347e968e6d6c65b54948d8e9e3c8b 100644 (file)
@@ -7,6 +7,8 @@ import java.sql.SQLException;
 import java.util.Calendar;
 
 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;
 
@@ -14,14 +16,20 @@ public class User {
 
     private int id;
 
-    Name name = new Name(null, null, null, null);
+    private Name name = new Name(null, null, null, null);
 
-    Date dob;
+    private Date dob;
 
-    String email;
+    private String email;
+
+    private Assurance[] receivedAssurances, madeAssurances;
 
     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=?");
             ps.setInt(1, id);
@@ -241,7 +249,7 @@ public class User {
 
     public EmailAddress[] getEmails() {
         try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM email WHERE memid=? AND deleted=0");
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted=0");
             ps.setInt(1, id);
             ResultSet rs = ps.executeQuery();
             rs.last();
@@ -265,7 +273,7 @@ public class User {
 
     public Domain[] getDomains() {
         try {
-            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domain WHERE memid=? AND deleted IS NULL");
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM domains WHERE memid=? AND deleted IS NULL");
             ps.setInt(1, id);
             ResultSet rs = ps.executeQuery();
             rs.last();
@@ -287,6 +295,53 @@ public class User {
         return null;
     }
 
+    public Certificate[] getCertificates() {
+        try {
+            PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT serial FROM certs WHERE memid=? AND revoked=0");
+            ps.setInt(1, id);
+            ResultSet rs = ps.executeQuery();
+            rs.last();
+            int count = rs.getRow();
+            Certificate[] data = new Certificate[count];
+            rs.beforeFirst();
+            for (int i = 0; i < data.length; i++) {
+                if ( !rs.next()) {
+                    throw new Error("Internal sql api violation.");
+                }
+                data[i] = Certificate.getBySerial(rs.getString(1));
+            }
+            rs.close();
+            return data;
+        } catch (SQLException e) {
+            e.printStackTrace();
+        }
+
+        return null;
+    }
+
+    public boolean isValidDomain(String domainname) {
+        for (Domain d : getDomains()) {
+            String sfx = d.getSuffix();
+            if (domainname.equals(sfx) || domainname.endsWith("." + sfx)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public boolean isValidEmail(String email) {
+        for (EmailAddress em : getEmails()) {
+            if (em.getAddress().equals(email)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    public boolean isValidName(String name) {
+        return getName().matches(name);
+    }
+
     public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
         try {
             EmailAddress[] adrs = getEmails();
@@ -317,7 +372,7 @@ public class User {
         for (int i = 0; i < emails.length; i++) {
             if (emails[i].getId() == mail.getId()) {
                 try {
-                    PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE email SET deleted=? WHERE id=?");
+                    PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
                     ps.setDate(1, new Date(System.currentTimeMillis()));
                     ps.setInt(2, mail.getId());
                     ps.execute();
@@ -330,4 +385,85 @@ 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) {
+                updateUserData();
+                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 Language getPrefferedLanguage() {
+        return Language.getInstance("de");
+    }
+
+    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");
+    }
 }