From d65f712ea3d8941f4b9145c0630c30c20af80d13 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Fri, 11 Nov 2011 17:22:21 -0500 Subject: [PATCH] Documentation. Add javadoc and source jars to the gbapi download. --- src/com/gitblit/client/EditUserDialog.java | 160 ++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 110 insertions(+), 50 deletions(-) diff --git a/src/com/gitblit/client/EditUserDialog.java b/src/com/gitblit/client/EditUserDialog.java index 5fce8c5..246a077 100644 --- a/src/com/gitblit/client/EditUserDialog.java +++ b/src/com/gitblit/client/EditUserDialog.java @@ -23,10 +23,12 @@ import java.awt.Insets; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.awt.event.KeyEvent; 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; @@ -37,12 +39,14 @@ import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JPasswordField; +import javax.swing.JRootPane; import javax.swing.JTextField; +import javax.swing.KeyStroke; import com.gitblit.Constants.AccessRestrictionType; -import com.gitblit.IStoredSettings; import com.gitblit.Keys; import com.gitblit.models.RepositoryModel; +import com.gitblit.models.ServerSettings; import com.gitblit.models.UserModel; import com.gitblit.utils.StringUtils; @@ -50,9 +54,13 @@ private static final long serialVersionUID = 1L; + private final String username; + private final UserModel user; - private final IStoredSettings settings; + private final ServerSettings settings; + + private boolean isCreate; private boolean canceled = true; @@ -68,19 +76,37 @@ private JPalette<String> repositoryPalette; - public EditUserDialog(IStoredSettings settings) { + private Set<String> usernames; + + public EditUserDialog(ServerSettings settings) { this(new UserModel(""), settings); - setTitle("Create User"); + this.isCreate = true; + setTitle(Translation.get("gb.newUser")); } - public EditUserDialog(UserModel anUser, IStoredSettings settings) { + public EditUserDialog(UserModel anUser, ServerSettings settings) { super(); + this.username = anUser.username; 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()); + } + + @Override + protected JRootPane createRootPane() { + KeyStroke stroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0); + JRootPane rootPane = new JRootPane(); + rootPane.registerKeyboardAction(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + setVisible(false); + } + }, stroke, JComponent.WHEN_IN_FOCUSED_WINDOW); + return rootPane; } private void initialize(UserModel anUser) { @@ -88,24 +114,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 +143,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; @@ -143,7 +171,6 @@ getContentPane().setLayout(new BorderLayout(5, 5)); getContentPane().add(centerPanel, BorderLayout.CENTER); pack(); - setLocationRelativeTo(null); } private JPanel newFieldPanel(String label, JComponent comp) { @@ -159,48 +186,74 @@ 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; - // } - // } + boolean rename = false; + // verify username uniqueness on create + if (isCreate) { + if (usernames.contains(uname.toLowerCase())) { + error(MessageFormat.format("Username ''{0}'' is unavailable.", uname)); + return false; + } + } else { + // check rename collision + rename = !StringUtils.isEmpty(username) && !username.equalsIgnoreCase(uname); + if (rename) { + if (usernames.contains(uname.toLowerCase())) { + error(MessageFormat.format( + "Failed to rename ''{0}'' because ''{1}'' already exists.", username, + uname)); + return false; + } + } + } + user.username = uname; - int minLength = settings.getInteger(Keys.realm.minPasswordLength, 5); + int minLength = settings.get(Keys.realm.minPasswordLength).getInteger(5); if (minLength < 4) { minLength = 4; } - char[] pw = passwordField.getPassword(); - if (pw == null || pw.length < minLength) { - showValidationError(MessageFormat.format( - "Password is too short. Minimum length is {0} characters.", minLength)); + + String password = new String(passwordField.getPassword()); + if (StringUtils.isEmpty(password) || password.length() < minLength) { + 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!"); + if (!password.toUpperCase().startsWith(StringUtils.MD5_TYPE) + && !password.toUpperCase().startsWith(StringUtils.COMBINED_MD5_TYPE)) { + String cpw = new String(confirmPasswordField.getPassword()); + if (cpw == null || cpw.length() != password.length()) { + error("Please confirm the password!"); + return false; + } + if (!password.equals(cpw)) { + error("Passwords do not match!"); + return false; + } + + String type = settings.get(Keys.realm.passwordStorage).getString("md5"); + if (type.equalsIgnoreCase("md5")) { + // store MD5 digest of password + user.password = StringUtils.MD5_TYPE + StringUtils.getMD5(password); + } else if (type.equalsIgnoreCase("combined-md5")) { + // store MD5 digest of username+password + user.password = StringUtils.COMBINED_MD5_TYPE + + StringUtils.getMD5(username.toLowerCase() + password); + } else { + // plain-text password + user.password = password; + } + } else if (rename && password.toUpperCase().startsWith(StringUtils.COMBINED_MD5_TYPE)) { + error("Gitblit is configured for combined-md5 password hashing. You must enter a new password on account rename."); return false; - } - if (!Arrays.equals(pw, cpw)) { - showValidationError("Passwords do not match!"); - return false; - } - user.username = uname; - String type = settings.getString(Keys.realm.passwordStorage, "md5"); - if (type.equalsIgnoreCase("md5")) { - // store MD5 digest of password - user.password = StringUtils.MD5_TYPE + StringUtils.getMD5(new String(pw)); } else { - user.password = new String(pw); + // no change in password + user.password = password; } + user.canAdmin = canAdminCheckbox.isSelected(); user.excludeFromFederation = notFederatedCheckbox.isSelected(); @@ -209,11 +262,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) { -- Gitblit v1.9.1