]> WPIA git - gigi.git/blobdiff - src/org/cacert/gigi/dbObjects/SupportedUser.java
upd: transform existing mails into mail templates
[gigi.git] / src / org / cacert / gigi / dbObjects / SupportedUser.java
index 58755059e27cddf15342b2513d3018881befd25d..975ef9b51e02055fb444819a4330d43a42cadbab 100644 (file)
@@ -1,14 +1,23 @@
 package org.cacert.gigi.dbObjects;
 
-import java.sql.Date;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Locale;
 
 import org.cacert.gigi.GigiApiException;
-import org.cacert.gigi.database.DatabaseConnection;
 import org.cacert.gigi.database.GigiPreparedStatement;
+import org.cacert.gigi.dbObjects.Certificate.CertificateStatus;
+import org.cacert.gigi.localisation.Language;
+import org.cacert.gigi.output.template.MailTemplate;
+import org.cacert.gigi.output.template.Outputable;
+import org.cacert.gigi.util.DayDate;
+import org.cacert.gigi.util.ServerConstants;
 
 public class SupportedUser {
 
-    private User target, supporter;
+    private User target;
+
+    private User supporter;
 
     private String ticket;
 
@@ -18,29 +27,23 @@ public class SupportedUser {
         this.ticket = ticket;
     }
 
-    public boolean setName(Name newName) throws GigiApiException {
-        if (newName.equals(target.getName())) {
-            return false;
-        }
-        writeSELog("SE Name change");
-        target.setName(newName);
-        return true;
-    }
-
-    public boolean setDob(Date dob) throws GigiApiException {
-        if (dob.toString().equals(target.getDoB().toString())) {
+    public boolean setDob(DayDate dob) throws GigiApiException {
+        if (dob.equals(target.getDoB())) {
             return false;
         }
         writeSELog("SE dob change");
-        target.setDoB(dob);
+        target.setDoBAsSupport(dob);
         return true;
     }
 
     public void revokeAllCertificates() throws GigiApiException {
         writeSELog("SE Revoke certificates");
         Certificate[] certs = target.getCertificates(false);
+        // TODO Check for open jobs!
         for (int i = 0; i < certs.length; i++) {
-            certs[i].revoke();
+            if (certs[i].getStatus() == CertificateStatus.ISSUED) {
+                certs[i].revoke();
+            }
         }
     }
 
@@ -48,12 +51,13 @@ public class SupportedUser {
         if (ticket == null) {
             throw new GigiApiException("No ticket set!");
         }
-        GigiPreparedStatement prep = DatabaseConnection.getInstance().prepare("INSERT INTO adminLog SET uid=?, admin=?, type=?, information=?");
-        prep.setInt(1, target.getId());
-        prep.setInt(2, supporter.getId());
-        prep.setString(3, type);
-        prep.setString(4, ticket);
-        prep.executeUpdate();
+        try (GigiPreparedStatement prep = new GigiPreparedStatement("INSERT INTO `adminLog` SET uid=?, admin=?, type=?, information=?")) {
+            prep.setInt(1, target.getId());
+            prep.setInt(2, supporter.getId());
+            prep.setString(3, type);
+            prep.setString(4, ticket);
+            prep.executeUpdate();
+        }
     }
 
     public int getId() {
@@ -72,8 +76,28 @@ public class SupportedUser {
         return target;
     }
 
-    public void submitSupportAction() throws GigiApiException {
-        target.rawUpdateUserData();
+    public void grant(Group toMod) {
+        target.grantGroup(supporter, toMod);
     }
 
+    public void revoke(Group toMod) {
+        target.revokeGroup(supporter, toMod);
+    }
+
+    private static final MailTemplate supportNotification = new MailTemplate(SupportedUser.class.getResource("SupportNotificationMail.templ"));
+
+    public void sendSupportNotification(String subject, Outputable message) {
+        try {
+            HashMap<String, Object> vars = new HashMap<>();
+            vars.put("supporter", supporter.getPreferredName().toString());
+            vars.put("action", message);
+            vars.put("ticket", this.getTicket());
+            vars.put("subject", subject);
+
+            String supportemailaddress = "support@" + ServerConstants.getWwwHostName().replaceFirst("^www\\.", "");
+            supportNotification.sendMail(Language.getInstance(Locale.ENGLISH), vars, supportemailaddress);
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
 }