Jeroen Baten
2013-01-04 9af47c10c6a268877c1d232c8d71ee6df4f8a7ab
src/com/gitblit/authority/UserCertificatePanel.java
@@ -16,27 +16,23 @@
package com.gitblit.authority;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.security.cert.X509Certificate;
import java.text.MessageFormat;
import java.util.Date;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.TableRowSorter;
@@ -55,15 +51,8 @@
   
   private UserCertificateModel ucm;
   
   private JTextField displayname;
   private JTextField username;
   private JTextField emailAddress;
   private JTextField organizationalUnit;
   private JTextField organization;
   private JTextField locality;
   private JTextField stateProvince;
   private JTextField countryCode;
   private UserOidsPanel oidsPanel;
   private CertificatesTableModel tableModel;
   private JButton saveUserButton;
@@ -80,29 +69,10 @@
      super(new BorderLayout());
      
      this.owner = owner;
      oidsPanel = new UserOidsPanel();
      
      displayname = new JTextField(20);
      username = new JTextField(20);
      username.setEditable(false);
      emailAddress = new JTextField(20);
      organizationalUnit = new JTextField(20);
      organization = new JTextField(20);
      locality = new JTextField(20);
      stateProvince = new JTextField(20);
      countryCode = new JTextField(20);
      JPanel fields = new JPanel(new GridLayout(0, 1, 5, 5));
      fields.add(newFieldPanel(Translation.get("gb.displayName"), displayname));
      fields.add(newFieldPanel(Translation.get("gb.username") + " (CN)", username));
      fields.add(newFieldPanel(Translation.get("gb.emailAddress") + " (E)", emailAddress));
      fields.add(newFieldPanel(Translation.get("gb.organizationalUnit") + " (OU)", organizationalUnit));
      fields.add(newFieldPanel(Translation.get("gb.organization") + " (O)", organization));
      fields.add(newFieldPanel(Translation.get("gb.locality") + " (L)", locality));
      fields.add(newFieldPanel(Translation.get("gb.stateProvince") + " (ST)", stateProvince));
      fields.add(newFieldPanel(Translation.get("gb.countryCode") + " (C)", countryCode));
      JPanel fp = new JPanel(new BorderLayout(5, 5));
      fp.add(fields, BorderLayout.NORTH);
      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);
@@ -114,7 +84,7 @@
         public void actionPerformed(ActionEvent e) {
            setEditable(false);
            String username = ucm.user.username;
            updateUser();
            oidsPanel.updateUser(ucm);
            saveUser(username, ucm);
         }
      });
@@ -176,31 +146,42 @@
                  // save changes
                  String username = ucm.user.username;
                  setEditable(false);
                  updateUser();
                  oidsPanel.updateUser(ucm);
                  saveUser(username, ucm);
               }
               
               NewClientCertificateDialog dialog = new NewClientCertificateDialog(UserCertificatePanel.this.owner,
                     ucm.user.getDisplayName(), getDefaultExpiration());
                     ucm.user.getDisplayName(), getDefaultExpiration(), isAllowEmail());
               dialog.setModal(true);
               dialog.setVisible(true);
               if (dialog.isCanceled()) {
                  return;
               }
               setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
               UserModel user = ucm.user;
               X509Metadata metadata = new X509Metadata(user.username, dialog.getPassword());
               final boolean sendEmail = dialog.sendEmail();
               final UserModel user = ucm.user;
               final X509Metadata metadata = new X509Metadata(user.username, dialog.getPassword());
               metadata.userDisplayname = user.getDisplayName();
               metadata.emailAddress = user.emailAddress;            
               metadata.passwordHint = dialog.getPasswordHint();
               metadata.notAfter = dialog.getExpiration();
               newCertificate(ucm, metadata, dialog.sendEmail());
               AuthorityWorker worker = new AuthorityWorker(UserCertificatePanel.this.owner) {
                  @Override
                  protected Boolean doRequest() throws IOException {
                     return newCertificate(ucm, metadata, sendEmail);
                  }
                  @Override
                  protected void onSuccess() {
                     JOptionPane.showMessageDialog(UserCertificatePanel.this.owner,
                           MessageFormat.format(Translation.get("gb.clientCertificateGenerated"), user.getDisplayName()),
                           Translation.get("gb.newCertificate"), JOptionPane.INFORMATION_MESSAGE);
                  }
               };
               worker.execute();
            } catch (Exception x) {
               Utils.showException(UserCertificatePanel.this, x);
            } finally {
               setCursor(Cursor.getDefaultCursor());
            }
         }
      });
@@ -215,7 +196,7 @@
                  return;
               }
               int modelIndex = table.convertRowIndexToModel(row);
               X509Certificate cert = tableModel.get(modelIndex);
               final X509Certificate cert = tableModel.get(modelIndex);
               
               String [] choices = new String[RevocationReason.reasons.length];
               for (int i = 0; i < choices.length; i++) {
@@ -224,17 +205,18 @@
               
               Object choice = JOptionPane.showInputDialog(UserCertificatePanel.this.owner,
                     Translation.get("gb.revokeCertificateReason"), Translation.get("gb.revokeCertificate"),
                     JOptionPane.PLAIN_MESSAGE, new ImageIcon(getClass().getResource("/rosette_16x16.png")), choices, Translation.get("gb.unspecified"));
                     JOptionPane.PLAIN_MESSAGE, new ImageIcon(getClass().getResource("/rosette_32x32.png")), choices, Translation.get("gb.unspecified"));
               if (choice == null) {
                  return;
               }
               RevocationReason reason = RevocationReason.unspecified;
               RevocationReason selection = RevocationReason.unspecified;
               for (int i = 0 ; i < choices.length; i++) {
                  if (choices[i].equals(choice)) {
                     reason = RevocationReason.reasons[i];
                     selection = RevocationReason.reasons[i];
                     break;
                  }
               }
               final RevocationReason reason = selection;
               if (!ucm.isRevoked(cert.getSerialNumber())) {
                  if (ucm.certs.size() == 1) {
                     // no other certificates
@@ -253,12 +235,26 @@
                     }
                     ucm.expires = newExpires;
                  }
                  revoke(ucm, cert, reason);
                  AuthorityWorker worker = new AuthorityWorker(UserCertificatePanel.this.owner) {
                     @Override
                     protected Boolean doRequest() throws IOException {
                        return revoke(ucm, cert, reason);
                     }
                     @Override
                     protected void onSuccess() {
                        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();
               }
            } catch (Exception x) {
               Utils.showException(UserCertificatePanel.this, x);
            } finally {
               setCursor(Cursor.getDefaultCursor());
            }
         }
      });
@@ -273,40 +269,18 @@
      setEditable(false);
   }
   
   private JPanel newFieldPanel(String label, Component c) {
      JLabel jlabel = new JLabel(label);
      jlabel.setPreferredSize(new Dimension(150, 20));
      JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
      panel.add(jlabel);
      panel.add(c);
      return panel;
   }
   public void setUserCertificateModel(UserCertificateModel ucm) {
      this.ucm = ucm;
      setEditable(false);
      displayname.setText(ucm.user.getDisplayName());
      username.setText(ucm.user.username);
      emailAddress.setText(ucm.user.emailAddress);
      organizationalUnit.setText(ucm.user.organizationalUnit);
      organization.setText(ucm.user.organization);
      locality.setText(ucm.user.locality);
      stateProvince.setText(ucm.user.stateProvince);
      countryCode.setText(ucm.user.countryCode);
      oidsPanel.setUserCertificateModel(ucm);
      
      tableModel.setUserCertificateModel(ucm);
      tableModel.fireTableDataChanged();
      Utils.packColumns(table, Utils.MARGIN);
   }
   
   public void setEditable(boolean editable) {
      displayname.setEditable(editable);
//      username.setEditable(editable);
      emailAddress.setEditable(editable);
      organizationalUnit.setEditable(editable);
      organization.setEditable(editable);
      locality.setEditable(editable);
      stateProvince.setEditable(editable);
      countryCode.setEditable(editable);
      oidsPanel.setEditable(editable);
      
      editUserButton.setEnabled(!editable && ucm != null);
      saveUserButton.setEnabled(editable && ucm != null);
@@ -315,20 +289,10 @@
      revokeCertificateButton.setEnabled(false);
   }
   
   private void updateUser() {
      ucm.user.displayName = displayname.getText();
      ucm.user.username = username.getText();
      ucm.user.emailAddress = emailAddress.getText();
      ucm.user.organizationalUnit = organizationalUnit.getText();
      ucm.user.organization = organization.getText();
      ucm.user.locality = locality.getText();
      ucm.user.stateProvince = stateProvince.getText();
      ucm.user.countryCode = countryCode.getText();
   }
   public abstract Date getDefaultExpiration();
   public abstract boolean isAllowEmail();
   
   public abstract void saveUser(String username, UserCertificateModel ucm);
   public abstract void newCertificate(UserCertificateModel ucm, X509Metadata metadata, boolean sendEmail);
   public abstract void revoke(UserCertificateModel ucm, X509Certificate cert, RevocationReason reason);
   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);
}