Store an additional copy of all usernames in lowercase

This commit is contained in:
Colin McDonald 2016-08-17 21:08:37 -04:00
parent b3d8ab2fbd
commit 1689c3da67
3 changed files with 9 additions and 1 deletions

View File

@ -238,6 +238,7 @@ public final class APIv3 extends AbstractVerticle {
}); });
database.getCollection("users").createIndexes(ImmutableList.of( database.getCollection("users").createIndexes(ImmutableList.of(
new IndexModel(new Document("lastUsername", 1)), new IndexModel(new Document("lastUsername", 1)),
new IndexModel(new Document("lastUsernameLower", 1)),
new IndexModel(new Document("emailToken", 1)) new IndexModel(new Document("emailToken", 1))
), (a, b) -> { ), (a, b) -> {
}); });

View File

@ -41,6 +41,7 @@ public final class User {
@Getter @Id @JsonSerialize(using = UuidJsonSerializer.class) @JsonDeserialize(using = UuidJsonDeserializer.class) private UUID id; @Getter @Id @JsonSerialize(using = UuidJsonSerializer.class) @JsonDeserialize(using = UuidJsonDeserializer.class) private UUID id;
@Getter private String lastUsername; @Getter private String lastUsername;
@Getter private String lastUsernameLower;
@Getter @ExcludeFromReplies private Map<String, Instant> aliases = new HashMap<>(); @Getter @ExcludeFromReplies private Map<String, Instant> aliases = new HashMap<>();
@Getter @ExcludeFromReplies @Setter private String totpSecret; @Getter @ExcludeFromReplies @Setter private String totpSecret;
@Getter @ExcludeFromReplies private String password; @Getter @ExcludeFromReplies private String password;
@ -148,6 +149,10 @@ public final class User {
usersCollection.find(new Document("lastUsername", lastUsername)).first(SyncUtils.vertxWrap(callback)); usersCollection.find(new Document("lastUsername", lastUsername)).first(SyncUtils.vertxWrap(callback));
} }
public static void findByLastUsernameLower(String lastUsernameLower, SingleResultCallback<User> callback) {
usersCollection.find(new Document("lastUsernameLower", lastUsernameLower)).first(SyncUtils.vertxWrap(callback));
}
public static void findOrCreateByIdGrouped(Map<UUID, String> search, SingleResultCallback<Map<UUID, User>> callback) { public static void findOrCreateByIdGrouped(Map<UUID, String> search, SingleResultCallback<Map<UUID, User>> callback) {
usersCollection.find(new Document("_id", new Document("$in", search.keySet()))).into(new LinkedList<>(), SyncUtils.vertxWrap((users, error) -> { usersCollection.find(new Document("_id", new Document("$in", search.keySet()))).into(new LinkedList<>(), SyncUtils.vertxWrap((users, error) -> {
if (error != null) { if (error != null) {
@ -205,6 +210,7 @@ public final class User {
public User(UUID id, String lastUsername) { public User(UUID id, String lastUsername) {
this.id = id; this.id = id;
this.lastUsername = lastUsername; this.lastUsername = lastUsername;
this.lastUsernameLower = lastUsername.toLowerCase();
this.aliases = new HashMap<>(); this.aliases = new HashMap<>();
this.lastSeenAt = Instant.now(); this.lastSeenAt = Instant.now();
this.firstSeenAt = Instant.now(); this.firstSeenAt = Instant.now();
@ -215,6 +221,7 @@ public final class User {
public void updateUsername(String newUsername) { public void updateUsername(String newUsername) {
this.aliases.put(newUsername, Instant.now()); this.aliases.put(newUsername, Instant.now());
this.lastUsername = newUsername; this.lastUsername = newUsername;
this.lastUsernameLower = newUsername.toLowerCase();
} }
public void checkNameCollisions(SingleResultCallback<Void> callback) { public void checkNameCollisions(SingleResultCallback<Void> callback) {

View File

@ -22,7 +22,7 @@ public final class GETUsersIdVerifyPassword implements Handler<RoutingContext> {
User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v)); User user = SyncUtils.runBlocking(v -> User.findById(ctx.request().getParam("userId"), v));
if (user == null) { if (user == null) {
user = SyncUtils.runBlocking(v -> User.findByLastUsername(ctx.request().getParam("userId"), v)); user = SyncUtils.runBlocking(v -> User.findByLastUsernameLower(ctx.request().getParam("userId"), v));
} }
if (user == null) { if (user == null) {