-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PTV-1911: Fix user search bugs re underscores + mongo errors #439
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package us.kbase.auth2.lib; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static us.kbase.auth2.lib.Utils.checkStringNoCheckedException; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
import us.kbase.auth2.lib.exceptions.ErrorType; | ||
import us.kbase.auth2.lib.exceptions.IllegalParameterException; | ||
|
@@ -35,8 +39,8 @@ public class UserName extends Name { | |
} | ||
} | ||
|
||
private static final String INVALID_CHARS_REGEX = "[^a-z\\d_]+"; | ||
private final static Pattern INVALID_CHARS = Pattern.compile(INVALID_CHARS_REGEX); | ||
private static final Pattern FORCE_ALPHA_FIRST_CHAR = Pattern.compile("^[^a-z]+"); | ||
private final static Pattern INVALID_CHARS = Pattern.compile("[^a-z\\d_]+"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That would work, but my personal preference is to use the more explicit form where you don't have to look up what |
||
public final static int MAX_NAME_LENGTH = 100; | ||
|
||
/** Create a new user name. | ||
|
@@ -75,15 +79,37 @@ public boolean isRoot() { | |
*/ | ||
public static Optional<UserName> sanitizeName(final String suggestedUserName) { | ||
requireNonNull(suggestedUserName, "suggestedUserName"); | ||
final String s = suggestedUserName.toLowerCase().replaceAll(INVALID_CHARS_REGEX, "") | ||
.replaceAll("^[^a-z]+", ""); | ||
final String s = cleanUserName(suggestedUserName); | ||
try { | ||
return s.isEmpty() ? Optional.empty() : Optional.of(new UserName(s)); | ||
} catch (IllegalParameterException | MissingParameterException e) { | ||
throw new RuntimeException("This should be impossible", e); | ||
} | ||
} | ||
|
||
private static String cleanUserName(final String putativeName) { | ||
return FORCE_ALPHA_FIRST_CHAR.matcher( | ||
INVALID_CHARS.matcher( | ||
putativeName.toLowerCase()) | ||
.replaceAll("")) | ||
.replaceAll(""); | ||
} | ||
|
||
/** Given a string, splits the string by whitespace, strips all illegal | ||
* characters from the tokens, and returns the resulting strings, | ||
* discarding repeats. | ||
* @param names the names string to process. | ||
* @return the list of canonicalized names. | ||
*/ | ||
public static List<String> getCanonicalNames(final String names) { | ||
checkStringNoCheckedException(names, "names"); | ||
return Arrays.asList(names.toLowerCase().split("\\s+")).stream() | ||
.map(u -> cleanUserName(u)) | ||
.filter(u -> !u.isEmpty()) | ||
.distinct() | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder builder = new StringBuilder(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I hope 2pac never made a KBase account!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure how deep KBase has penetrated into the gangsta rap community
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something to check with KBase outreach about.