]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/User.java
add: cert-rules closer to reality
[gigi.git] / src / org / cacert / gigi / dbObjects / User.java
index 1c9448b717b45487b23581f576a713237f8479c5..42457db543a625540be62b5deff109321fd72778 100644 (file)
@@ -27,7 +27,9 @@ public class User extends CertificateOwner {
 
     private String email;
 
-    private Assurance[] receivedAssurances, madeAssurances;
+    private Assurance[] receivedAssurances;
+
+    private Assurance[] madeAssurances;
 
     private Locale locale;
 
@@ -62,34 +64,10 @@ public class User extends CertificateOwner {
 
     public User() {}
 
-    public String getFName() {
-        return name.fname;
-    }
-
-    public String getLName() {
-        return name.lname;
-    }
-
-    public String getMName() {
-        return name.mname;
-    }
-
     public Name getName() {
         return name;
     }
 
-    public void setMName(String mname) {
-        this.name.mname = mname;
-    }
-
-    public String getSuffix() {
-        return name.suffix;
-    }
-
-    public void setSuffix(String suffix) {
-        this.name.suffix = suffix;
-    }
-
     public Date getDoB() {
         return dob;
     }
@@ -106,23 +84,15 @@ public class User extends CertificateOwner {
         this.email = email;
     }
 
-    public void setFName(String fname) {
-        this.name.fname = fname;
-    }
-
-    public void setLName(String lname) {
-        this.name.lname = lname;
-    }
-
     public void insert(String password) {
         int id = super.insert();
         GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("insert into `users` set `email`=?, `password`=?, " + "`fname`=?, `mname`=?, `lname`=?, " + "`suffix`=?, `dob`=?, `language`=?, id=?");
         query.setString(1, email);
         query.setString(2, PasswordHash.hash(password));
-        query.setString(3, name.fname);
-        query.setString(4, name.mname);
-        query.setString(5, name.lname);
-        query.setString(6, name.suffix);
+        query.setString(3, name.getFname());
+        query.setString(4, name.getMname());
+        query.setString(5, name.getLname());
+        query.setString(6, name.getSuffix());
         query.setDate(7, new java.sql.Date(dob.getTime()));
         query.setString(8, locale.toString());
         query.setInt(9, id);
@@ -255,33 +225,35 @@ public class User extends CertificateOwner {
     }
 
     public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
-        EmailAddress[] adrs = getEmails();
-        for (int i = 0; i < adrs.length; i++) {
-            if (adrs[i].getAddress().equals(newMail.getAddress())) {
-                if ( !adrs[i].isVerified()) {
+        for (EmailAddress email : getEmails()) {
+            if (email.getAddress().equals(newMail.getAddress())) {
+                if ( !email.isVerified()) {
                     throw new GigiApiException("Email not verified.");
                 }
+
                 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE users SET email=? WHERE id=?");
                 ps.setString(1, newMail.getAddress());
                 ps.setInt(2, getId());
                 ps.execute();
-                email = newMail.getAddress();
+
+                this.email = newMail.getAddress();
                 return;
             }
         }
+
         throw new GigiApiException("Given address not an address of the user.");
     }
 
-    public void deleteEmail(EmailAddress mail) throws GigiApiException {
-        if (getEmail().equals(mail.getAddress())) {
+    public void deleteEmail(EmailAddress delMail) throws GigiApiException {
+        if (getEmail().equals(delMail.getAddress())) {
             throw new GigiApiException("Can't delete user's default e-mail.");
         }
-        EmailAddress[] emails = getEmails();
-        for (int i = 0; i < emails.length; i++) {
-            if (emails[i].getId() == mail.getId()) {
+
+        for (EmailAddress email : getEmails()) {
+            if (email.getId() == delMail.getId()) {
                 GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE emails SET deleted=? WHERE id=?");
                 ps.setDate(1, new Date(System.currentTimeMillis()));
-                ps.setInt(2, mail.getId());
+                ps.setInt(2, delMail.getId());
                 ps.execute();
                 return;
             }
@@ -308,7 +280,7 @@ public class User extends CertificateOwner {
         return receivedAssurances;
     }
 
-    public Assurance[] getMadeAssurances() {
+    public synchronized Assurance[] getMadeAssurances() {
         if (madeAssurances == null) {
             GigiPreparedStatement query = DatabaseConnection.getInstance().prepare("SELECT * FROM notary WHERE `from`=? AND deleted is NULL");
             query.setInt(1, getId());
@@ -327,11 +299,11 @@ public class User extends CertificateOwner {
         return madeAssurances;
     }
 
-    public void invalidateMadeAssurances() {
+    public synchronized void invalidateMadeAssurances() {
         madeAssurances = null;
     }
 
-    public void invalidateReceivedAssurances() {
+    public synchronized void invalidateReceivedAssurances() {
         receivedAssurances = null;
     }
 
@@ -341,18 +313,21 @@ public class User extends CertificateOwner {
             if (getAssurancePoints() != 0) {
                 throw new GigiApiException("No change after assurance allowed.");
             }
-
-            GigiPreparedStatement 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();
+            rawUpdateUserData();
         }
     }
 
+    protected void rawUpdateUserData() {
+        GigiPreparedStatement update = DatabaseConnection.getInstance().prepare("UPDATE users SET fname=?, lname=?, mname=?, suffix=?, dob=? WHERE id=?");
+        update.setString(1, name.getFname());
+        update.setString(2, name.getLname());
+        update.setString(3, name.getMname());
+        update.setString(4, name.getSuffix());
+        update.setDate(5, getDoB());
+        update.setInt(6, getId());
+        update.executeUpdate();
+    }
+
     public Locale getPreferredLocale() {
         return locale;
     }
@@ -466,20 +441,30 @@ public class User extends CertificateOwner {
         }
     }
 
-    public boolean canIssue(CertificateProfile p) {
-        switch (p.getCAId()) {
-        case 0:
-            return true;
-        case 1:
-            return getAssurancePoints() > 50;
-        case 2:
-            return getAssurancePoints() > 50 && isInGroup(Group.getByString("codesigning"));
-        case 3:
-        case 4:
-            return getOrganisations().size() > 0;
-        default:
-            return false;
+    public EmailAddress[] getEmails() {
+        GigiPreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM emails WHERE memid=? AND deleted is NULL");
+        ps.setInt(1, getId());
+
+        try (GigiResultSet rs = ps.executeQuery()) {
+            LinkedList<EmailAddress> data = new LinkedList<EmailAddress>();
+
+            while (rs.next()) {
+                data.add(EmailAddress.getById(rs.getInt(1)));
+            }
+
+            return data.toArray(new EmailAddress[0]);
         }
     }
 
+    @Override
+    public boolean isValidEmail(String email) {
+        for (EmailAddress em : getEmails()) {
+            if (em.getAddress().equals(email)) {
+                return true;
+            }
+        }
+
+        return false;
+    }
+
 }