]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/User.java
FIX: Cleanup fields
[gigi.git] / src / org / cacert / gigi / User.java
index 949d77a4bb49dc2fbe609718ddf97d3741d14260..eebf9317e2b4839aadc894fd0f86363787e17ab2 100644 (file)
@@ -14,11 +14,11 @@ 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;
 
     public User(int id) {
         this.id = id;
@@ -241,7 +241,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 +265,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 +287,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 +364,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();
@@ -328,6 +375,6 @@ public class User {
                 return;
             }
         }
-        throw new GigiApiException("Email not one user's mail addresses.");
+        throw new GigiApiException("Email not one of user's email addresses.");
     }
 }