James Moger
2011-10-14 bcc616b8e425f73e7abc6799f23445c1e411463d
src/com/gitblit/client/EditUserDialog.java
@@ -26,7 +26,9 @@
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.swing.ImageIcon;
import javax.swing.JButton;
@@ -54,6 +56,8 @@
   private final IStoredSettings settings;
   private boolean isCreate;
   private boolean canceled = true;
   private JTextField usernameField;
@@ -68,18 +72,23 @@
   private JPalette<String> repositoryPalette;
   private Set<String> usernames;
   public EditUserDialog(IStoredSettings settings) {
      this(new UserModel(""), settings);
      setTitle("Create User");
      this.isCreate = true;
      setTitle(Translation.get("gb.newUser"));
   }
   public EditUserDialog(UserModel anUser, IStoredSettings settings) {
      super();
      this.user = new UserModel("");
      this.settings = settings;
      this.usernames = new HashSet<String>();
      this.isCreate = false;
      initialize(anUser);
      setModal(true);
      setTitle("Edit User: " + anUser.username);
      setTitle(Translation.get("gb.edit") + ": " + anUser.username);
      setIconImage(new ImageIcon(getClass().getResource("/gitblt-favicon.png")).getImage());
   }
@@ -88,24 +97,26 @@
      passwordField = new JPasswordField(anUser.password == null ? "" : anUser.password, 25);
      confirmPasswordField = new JPasswordField(anUser.password == null ? "" : anUser.password,
            25);
      canAdminCheckbox = new JCheckBox("can administer Gitblit server", anUser.canAdmin);
      canAdminCheckbox = new JCheckBox(Translation.get("gb.canAdminDescription"), anUser.canAdmin);
      notFederatedCheckbox = new JCheckBox(
            "block federated Gitblit instances from pulling this account",
            Translation.get("gb.excludeFromFederationDescription"),
            anUser.excludeFromFederation);
      JPanel fieldsPanel = new JPanel(new GridLayout(0, 1));
      fieldsPanel.add(newFieldPanel("username", usernameField));
      fieldsPanel.add(newFieldPanel("password", passwordField));
      fieldsPanel.add(newFieldPanel("confirm password", confirmPasswordField));
      fieldsPanel.add(newFieldPanel("can admin", canAdminCheckbox));
      fieldsPanel.add(newFieldPanel("exclude from federation", notFederatedCheckbox));
      fieldsPanel.add(newFieldPanel(Translation.get("gb.username"), usernameField));
      fieldsPanel.add(newFieldPanel(Translation.get("gb.password"), passwordField));
      fieldsPanel.add(newFieldPanel(Translation.get("gb.confirmPassword"), confirmPasswordField));
      fieldsPanel.add(newFieldPanel(Translation.get("gb.canAdmin"), canAdminCheckbox));
      fieldsPanel.add(newFieldPanel(Translation.get("gb.excludeFromFederation"),
            notFederatedCheckbox));
      repositoryPalette = new JPalette<String>();
      JPanel panel = new JPanel(new BorderLayout());
      panel.add(fieldsPanel, BorderLayout.NORTH);
      panel.add(newFieldPanel("restricted repositories", repositoryPalette), BorderLayout.CENTER);
      panel.add(newFieldPanel(Translation.get("gb.restrictedRepositories"), repositoryPalette),
            BorderLayout.CENTER);
      JButton createButton = new JButton("Save");
      JButton createButton = new JButton(Translation.get("gb.save"));
      createButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            if (validateFields()) {
@@ -115,7 +126,7 @@
         }
      });
      JButton cancelButton = new JButton("Cancel");
      JButton cancelButton = new JButton(Translation.get("gb.cancel"));
      cancelButton.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent event) {
            canceled = true;
@@ -159,20 +170,17 @@
   private boolean validateFields() {
      String uname = usernameField.getText();
      if (StringUtils.isEmpty(uname)) {
         showValidationError("Please enter a username!");
         error("Please enter a username!");
         return false;
      }
      // TODO verify username uniqueness on create
      // if (isCreate) {
      // UserModel model = GitBlit.self().getUserModel(username);
      // if (model != null) {
      // error(MessageFormat.format("Username ''{0}'' is unavailable.",
      // username));
      // return;
      // }
      // }
      // verify username uniqueness on create
      if (isCreate) {
         if (usernames.contains(uname.toLowerCase())) {
            error(MessageFormat.format("Username ''{0}'' is unavailable.", uname));
            return false;
         }
      }
      int minLength = settings.getInteger(Keys.realm.minPasswordLength, 5);
      if (minLength < 4) {
@@ -180,17 +188,17 @@
      }
      char[] pw = passwordField.getPassword();
      if (pw == null || pw.length < minLength) {
         showValidationError(MessageFormat.format(
         error(MessageFormat.format(
               "Password is too short. Minimum length is {0} characters.", minLength));
         return false;
      }
      char[] cpw = confirmPasswordField.getPassword();
      if (cpw == null || cpw.length != pw.length) {
         showValidationError("Please confirm the password!");
         error("Please confirm the password!");
         return false;
      }
      if (!Arrays.equals(pw, cpw)) {
         showValidationError("Passwords do not match!");
         error("Passwords do not match!");
         return false;
      }
      user.username = uname;
@@ -209,11 +217,18 @@
      return true;
   }
   private void showValidationError(String message) {
      JOptionPane.showMessageDialog(EditUserDialog.this, message, "Validation Error",
   private void error(String message) {
      JOptionPane.showMessageDialog(EditUserDialog.this, message, Translation.get("gb.error"),
            JOptionPane.ERROR_MESSAGE);
   }
   public void setUsers(List<UserModel> users) {
      usernames.clear();
      for (UserModel user : users) {
         usernames.add(user.username.toLowerCase());
      }
   }
   public void setRepositories(List<RepositoryModel> repositories, List<String> selected) {
      List<String> restricted = new ArrayList<String>();
      for (RepositoryModel repo : repositories) {