]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/User.java
FIX: Additional check before maildeletion
[gigi.git] / src / org / cacert / gigi / User.java
index 70b48d246660e7c96e59b8c09c1b1d36b7e01c1d..010df4a9773cd48ee705d79d4a50c0f6491c16f8 100644 (file)
@@ -246,7 +246,8 @@ public class User {
 
        public EmailAddress[] getEmails() {
                try {
-                       PreparedStatement ps = DatabaseConnection.getInstance().prepare("SELECT id FROM email WHERE memid=?");
+                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                               "SELECT id FROM email WHERE memid=? AND deleted=0");
                        ps.setInt(1, id);
                        ResultSet rs = ps.executeQuery();
                        rs.last();
@@ -268,6 +269,31 @@ public class User {
                return null;
        }
 
+       public Domain[] getDomains() {
+               try {
+                       PreparedStatement ps = DatabaseConnection.getInstance().prepare(
+                               "SELECT id FROM domain WHERE memid=? AND deleted IS NULL");
+                       ps.setInt(1, id);
+                       ResultSet rs = ps.executeQuery();
+                       rs.last();
+                       int count = rs.getRow();
+                       Domain[] data = new Domain[count];
+                       rs.beforeFirst();
+                       for (int i = 0; i < data.length; i++) {
+                               if (!rs.next()) {
+                                       throw new Error("Internal sql api violation.");
+                               }
+                               data[i] = Domain.getById(rs.getInt(1));
+                       }
+                       rs.close();
+                       return data;
+               } catch (SQLException e) {
+                       e.printStackTrace();
+               }
+
+               return null;
+       }
+
        public void updateDefaultEmail(EmailAddress newMail) throws GigiApiException {
                try {
                        EmailAddress[] adrs = getEmails();
@@ -295,12 +321,22 @@ public class User {
                if (getEmail().equals(mail.getAddress())) {
                        throw new GigiApiException("Can't delete user's default e-mail.");
                }
-               try {
-                       PreparedStatement ps = DatabaseConnection.getInstance().prepare("UPDATE email SET deleted=1 WHERE id=?");
-                       ps.setInt(1, mail.getId());
-                       ps.execute();
-               } catch (SQLException e) {
-                       throw new GigiApiException(e);
+               EmailAddress[] emails = getEmails();
+               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=?");
+                                       ps.setDate(1, new Date(System.currentTimeMillis()));
+                                       ps.setInt(2, mail.getId());
+                                       ps.execute();
+                               } catch (SQLException e) {
+                                       e.printStackTrace();
+                                       throw new GigiApiException(e);
+                               }
+                               return;
+                       }
                }
+               throw new GigiApiException("Email not one user's mail addresses.");
        }
 }