From a098da747052eb8d9f612f78dece8ac027a72caa Mon Sep 17 00:00:00 2001 From: James Moger Date: Thu, 12 May 2011 18:18:20 -0400 Subject: [PATCH] Improvements to create/edit user. --- distrib/gitblit.properties | 4 +++ src/com/gitblit/JettyLoginService.java | 5 ++- .../wicket/pages/EditRepositoryPage.java | 2 +- .../gitblit/wicket/pages/EditUserPage.java | 31 ++++++++++++++++++- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/distrib/gitblit.properties b/distrib/gitblit.properties index 6bf33f13..bcbd3db4 100644 --- a/distrib/gitblit.properties +++ b/distrib/gitblit.properties @@ -37,6 +37,10 @@ realm.realmFile = users.properties # Valid values are plain, md5 or crypt (unix style). Default is md5. realm.passwordStorage = md5 +# Minimum valid length for a plain text password. +# Default value is 5. Absolute minimum is 4. +realm.minPasswordLength = 5 + # # Git:Blit Web Settings # diff --git a/src/com/gitblit/JettyLoginService.java b/src/com/gitblit/JettyLoginService.java index fb510ee6..c191f0f0 100644 --- a/src/com/gitblit/JettyLoginService.java +++ b/src/com/gitblit/JettyLoginService.java @@ -69,8 +69,11 @@ public class JettyLoginService extends MappedLoginService implements ILoginServi @Override public UserModel getUserModel(String username) { - UserModel model = new UserModel(username); UserIdentity identity = _users.get(username); + if (identity == null) { + return null; + } + UserModel model = new UserModel(username); Subject subject = identity.getSubject(); for (Principal principal : subject.getPrincipals()) { if (principal instanceof RolePrincipal) { diff --git a/src/com/gitblit/wicket/pages/EditRepositoryPage.java b/src/com/gitblit/wicket/pages/EditRepositoryPage.java index 8eed0059..88202901 100644 --- a/src/com/gitblit/wicket/pages/EditRepositoryPage.java +++ b/src/com/gitblit/wicket/pages/EditRepositoryPage.java @@ -116,7 +116,7 @@ public class EditRepositoryPage extends BasePage { error(e.getMessage()); return; } - setRedirect(true); + setRedirect(false); setResponsePage(RepositoriesPage.class); } }; diff --git a/src/com/gitblit/wicket/pages/EditUserPage.java b/src/com/gitblit/wicket/pages/EditUserPage.java index d1faa782..7522f3ed 100644 --- a/src/com/gitblit/wicket/pages/EditUserPage.java +++ b/src/com/gitblit/wicket/pages/EditUserPage.java @@ -1,5 +1,6 @@ package com.gitblit.wicket.pages; +import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @@ -71,8 +72,25 @@ public class EditUserPage extends BasePage { private static final long serialVersionUID = 1L; + /* + * (non-Javadoc) + * + * @see org.apache.wicket.markup.html.form.Form#onSubmit() + */ @Override protected void onSubmit() { + String username = userModel.getUsername(); + if (StringUtils.isEmpty(username)) { + error("Please enter a username!"); + return; + } + if (isCreate) { + UserModel model = GitBlit.self().getUserModel(username); + if (model != null) { + error(MessageFormat.format("Username {0} is unavailable.", username)); + return; + } + } if (!userModel.getPassword().equals(confirmPassword.getObject())) { error("Passwords do not match!"); return; @@ -80,6 +98,16 @@ public class EditUserPage extends BasePage { String password = userModel.getPassword(); if (!password.toUpperCase().startsWith(Crypt.__TYPE) && !password.toUpperCase().startsWith(MD5.__TYPE)) { // This is a plain text password. + // Check length. + int minLength = GitBlit.self().settings().getInteger(Keys.realm.minPasswordLength, 5); + if (minLength < 4) { + minLength = 4; + } + if (password.trim().length() < minLength) { + error(MessageFormat.format("Password is too short. Minimum length is {0} characters.", minLength)); + return; + } + // Optionally encrypt/obfuscate the password. String type = GitBlit.self().settings().getString(Keys.realm.passwordStorage, "md5"); if (type.equalsIgnoreCase("md5")) { @@ -103,9 +131,10 @@ public class EditUserPage extends BasePage { error(e.getMessage()); return; } - setRedirect(true); + setRedirect(false); if (isCreate) { // create another user + info(MessageFormat.format("New user {0} successfully created.", userModel.getUsername())); setResponsePage(EditUserPage.class); } else { // back to home -- 2.39.5