Paul Martin
2016-04-30 a502d96a860456ec5e8c96761db70f7cabb74751
src/main/java/com/gitblit/authority/UserCertificatePanel.java
@@ -46,13 +46,13 @@
public abstract class UserCertificatePanel extends JPanel {
   private static final long serialVersionUID = 1L;
   private Frame owner;
   private UserCertificateModel ucm;
   private UserOidsPanel oidsPanel;
   private CertificatesTableModel tableModel;
   private JButton saveUserButton;
@@ -60,27 +60,28 @@
   private JButton editUserButton;
   private JButton newCertificateButton;
   private JButton revokeCertificateButton;
   private JTable table;
   public UserCertificatePanel(Frame owner) {
      super(new BorderLayout());
      this.owner = owner;
      oidsPanel = new UserOidsPanel();
      JPanel fp = new JPanel(new BorderLayout(Utils.MARGIN, Utils.MARGIN));
      fp.add(oidsPanel, BorderLayout.NORTH);
      JPanel fieldsPanel = new JPanel(new BorderLayout());
      fieldsPanel.add(new HeaderPanel(Translation.get("gb.properties"), "vcard_16x16.png"), BorderLayout.NORTH);
      fieldsPanel.add(fp, BorderLayout.CENTER);
      saveUserButton = new JButton(Translation.get("gb.save"));
      saveUserButton.setEnabled(false);
      saveUserButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            setEditable(false);
            String username = ucm.user.username;
@@ -88,22 +89,23 @@
            saveUser(username, ucm);
         }
      });
      editUserButton = new JButton(Translation.get("gb.edit"));
      editUserButton.setEnabled(false);
      editUserButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            setEditable(true);
         }
      });
      JPanel userControls = new JPanel(new FlowLayout(FlowLayout.LEFT));
      userControls.add(editUserButton);
      userControls.add(saveUserButton);
      fieldsPanel.add(userControls, BorderLayout.SOUTH);
      JPanel certificatesPanel = new JPanel(new BorderLayout());
      certificatesPanel.add(new HeaderPanel(Translation.get("gb.certificates"), "rosette_16x16.png"), BorderLayout.NORTH);
      certificatesPanel.add(new HeaderPanel(Translation.get("gb.certificates"), "rosette_16x16.png"), BorderLayout.NORTH);
      tableModel = new CertificatesTableModel();
      table = Utils.newTable(tableModel, Utils.DATE_FORMAT);
      table.setRowSorter(new TableRowSorter<CertificatesTableModel>(tableModel));
@@ -125,21 +127,23 @@
         }
      });
      table.addMouseListener(new MouseAdapter() {
         @Override
         public void mouseClicked(MouseEvent e) {
            if (e.getClickCount() == 2) {
            if (e.getClickCount() == 2) {
               int row = table.rowAtPoint(e.getPoint());
               int modelIndex = table.convertRowIndexToModel(row);
               X509Certificate cert = tableModel.get(modelIndex);
               X509CertificateViewer viewer = new X509CertificateViewer(UserCertificatePanel.this.owner, cert);
               X509CertificateViewer viewer = new X509CertificateViewer(UserCertificatePanel.this.owner, cert);
               viewer.setVisible(true);
            }
         }
      });
      certificatesPanel.add(new JScrollPane(table), BorderLayout.CENTER);
      newCertificateButton = new JButton(Translation.get("gb.newCertificate"));
      newCertificateButton.setEnabled(false);
      newCertificateButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            try {
               if (saveUserButton.isEnabled()) {
@@ -149,7 +153,7 @@
                  oidsPanel.updateUser(ucm);
                  saveUser(username, ucm);
               }
               NewClientCertificateDialog dialog = new NewClientCertificateDialog(UserCertificatePanel.this.owner,
                     ucm.user.getDisplayName(), getDefaultExpiration(), isAllowEmail());
               dialog.setModal(true);
@@ -162,7 +166,7 @@
               final UserModel user = ucm.user;
               final X509Metadata metadata = new X509Metadata(user.username, dialog.getPassword());
               metadata.userDisplayname = user.getDisplayName();
               metadata.emailAddress = user.emailAddress;
               metadata.emailAddress = user.emailAddress;
               metadata.passwordHint = dialog.getPasswordHint();
               metadata.notAfter = dialog.getExpiration();
@@ -174,21 +178,22 @@
                  @Override
                  protected void onSuccess() {
                     JOptionPane.showMessageDialog(UserCertificatePanel.this.owner,
                     JOptionPane.showMessageDialog(UserCertificatePanel.this.owner,
                           MessageFormat.format(Translation.get("gb.clientCertificateGenerated"), user.getDisplayName()),
                           Translation.get("gb.newCertificate"), JOptionPane.INFORMATION_MESSAGE);
                  }
               };
               worker.execute();
               worker.execute();
            } catch (Exception x) {
               Utils.showException(UserCertificatePanel.this, x);
            }
         }
      });
      revokeCertificateButton = new JButton(Translation.get("gb.revokeCertificate"));
      revokeCertificateButton.setEnabled(false);
      revokeCertificateButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            try {
               int row = table.getSelectedRow();
@@ -197,12 +202,12 @@
               }
               int modelIndex = table.convertRowIndexToModel(row);
               final X509Certificate cert = tableModel.get(modelIndex);
               String [] choices = new String[RevocationReason.reasons.length];
               for (int i = 0; i < choices.length; i++) {
                  choices[i] = Translation.get("gb." + RevocationReason.reasons[i].name());
               }
               Object choice = JOptionPane.showInputDialog(UserCertificatePanel.this.owner,
                     Translation.get("gb.revokeCertificateReason"), Translation.get("gb.revokeCertificate"),
                     JOptionPane.PLAIN_MESSAGE, new ImageIcon(getClass().getResource("/rosette_32x32.png")), choices, Translation.get("gb.unspecified"));
@@ -224,7 +229,7 @@
                  } else {
                     // determine new expires date for user
                     Date newExpires = null;
                     for (X509Certificate c : ucm.certs) {
                     for (X509Certificate c : ucm.certs) {
                        if (!c.equals(cert)) {
                           if (!ucm.isRevoked(c.getSerialNumber())) {
                              if (newExpires == null || c.getNotAfter().after(newExpires)) {
@@ -235,7 +240,7 @@
                     }
                     ucm.expires = newExpires;
                  }
                  AuthorityWorker worker = new AuthorityWorker(UserCertificatePanel.this.owner) {
                     @Override
@@ -245,11 +250,11 @@
                     @Override
                     protected void onSuccess() {
                        JOptionPane.showMessageDialog(UserCertificatePanel.this.owner,
                        JOptionPane.showMessageDialog(UserCertificatePanel.this.owner,
                              MessageFormat.format(Translation.get("gb.certificateRevoked"), cert.getSerialNumber(), cert.getIssuerDN().getName()),
                              Translation.get("gb.revokeCertificate"), JOptionPane.INFORMATION_MESSAGE);
                     }
                  };
                  worker.execute();
               }
@@ -258,40 +263,40 @@
            }
         }
      });
      JPanel certificateControls = new JPanel(new FlowLayout(FlowLayout.LEFT));
      certificateControls.add(newCertificateButton);
      certificateControls.add(revokeCertificateButton);
      certificatesPanel.add(certificateControls, BorderLayout.SOUTH);
      add(fieldsPanel, BorderLayout.NORTH);
      add(certificatesPanel, BorderLayout.CENTER);
      setEditable(false);
   }
   public void setUserCertificateModel(UserCertificateModel ucm) {
      this.ucm = ucm;
      setEditable(false);
      oidsPanel.setUserCertificateModel(ucm);
      tableModel.setUserCertificateModel(ucm);
      tableModel.fireTableDataChanged();
      Utils.packColumns(table, Utils.MARGIN);
   }
   public void setEditable(boolean editable) {
      oidsPanel.setEditable(editable);
      editUserButton.setEnabled(!editable && ucm != null);
      saveUserButton.setEnabled(editable && ucm != null);
      newCertificateButton.setEnabled(ucm != null);
      revokeCertificateButton.setEnabled(false);
   }
   public abstract Date getDefaultExpiration();
   public abstract boolean isAllowEmail();
   public abstract boolean saveUser(String username, UserCertificateModel ucm);
   public abstract boolean newCertificate(UserCertificateModel ucm, X509Metadata metadata, boolean sendEmail);
   public abstract boolean revoke(UserCertificateModel ucm, X509Certificate cert, RevocationReason reason);