caseinsensitive usernames

This commit is contained in:
_Bastler 2021-10-04 19:55:53 +02:00
parent 1a9f582967
commit 14d810a7d0
4 changed files with 41 additions and 29 deletions

View File

@ -11,6 +11,7 @@ import com.google.common.collect.Lists;
import de.bstly.board.model.Bookmarks;
import de.bstly.board.model.QBookmarks;
import de.bstly.board.model.QLocalUser;
import de.bstly.board.repository.BookmarksRepository;
import de.bstly.board.repository.EntryRepository;
import de.bstly.board.repository.LocalUserRepository;
@ -27,6 +28,7 @@ public class BookmarksManager {
private EntryRepository entryRepository;
@Autowired
private LocalUserRepository localUserRepository;
private QLocalUser qLocalUser = QLocalUser.localUser;
private QBookmarks qBookmarks = QBookmarks.bookmarks;
/**
@ -36,7 +38,8 @@ public class BookmarksManager {
* @return the bookmarks
*/
public Bookmarks get(String username) {
return bookmarksRepository.findById(username).orElse(new Bookmarks(username));
return bookmarksRepository.findOne(qBookmarks.username.equalsIgnoreCase(username))
.orElse(new Bookmarks(username));
}
/**
@ -47,8 +50,8 @@ public class BookmarksManager {
* @return true, if successful
*/
public boolean hasEntry(String username, Long entryId) {
return bookmarksRepository
.exists(qBookmarks.username.eq(username).and(qBookmarks.entries.contains(entryId)));
return bookmarksRepository.exists(qBookmarks.username.equalsIgnoreCase(username)
.and(qBookmarks.entries.contains(entryId)));
}
/**
@ -59,7 +62,8 @@ public class BookmarksManager {
*/
public void addEntry(String username, Long entryId) {
Assert.isTrue(entryRepository.existsById(entryId), "Invalid entryid");
Assert.isTrue(localUserRepository.existsById(username), "Invalid username");
Assert.isTrue(localUserRepository.exists(qLocalUser.username.equalsIgnoreCase(username)),
"Invalid username");
Bookmarks bookmarks = get(username);
if (bookmarks.getEntries() == null) {
@ -80,7 +84,8 @@ public class BookmarksManager {
*/
public void removeEntry(String username, Long entryId) {
Assert.isTrue(entryRepository.existsById(entryId), "Invalid entryid");
Assert.isTrue(localUserRepository.existsById(username), "Invalid username");
Assert.isTrue(localUserRepository.exists(qLocalUser.username.equalsIgnoreCase(username)),
"Invalid username");
Bookmarks bookmarks = get(username);

View File

@ -105,8 +105,11 @@ public class CommentManager {
public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page,
int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return commentRepository.findAll(qComment.author.eq(username).and(qComment.parent.isNull())
.and(qComment.created.before(date)), PageRequest.of(page, size, sort));
return commentRepository
.findAll(
qComment.author.equalsIgnoreCase(username).and(qComment.parent.isNull())
.and(qComment.created.before(date)),
PageRequest.of(page, size, sort));
}
/**
@ -156,21 +159,22 @@ public class CommentManager {
comment.getMetadata().put("upvoted",
voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment)).and(qVote.type.eq(VoteType.up))
.and(qVote.author.eq(username))));
.and(qVote.author.equalsIgnoreCase(username))));
}
if (!ignore.contains("downvoted")) {
comment.getMetadata().put("downvoted",
voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment))
.and(qVote.type.eq(VoteType.down)).and(qVote.author.eq(username))));
voteRepository.exists(
qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment))
.and(qVote.type.eq(VoteType.down))
.and(qVote.author.equalsIgnoreCase(username))));
}
if (!ignore.contains("unvote")) {
comment.getMetadata().put("unvote",
voteRepository.exists(
qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment))
.and(qVote.author.eq(username))));
.and(qVote.author.equalsIgnoreCase(username))));
}
if (!ignore.contains("entry")) {

View File

@ -88,7 +88,7 @@ public class EntryManager {
*/
public Page<Entry> fetchByUser(String username, Instant date, int page, int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return entryRepository.findAll(qEntry.author.eq(username).and(qEntry.created.before(date)),
return entryRepository.findAll(qEntry.author.equalsIgnoreCase(username).and(qEntry.created.before(date)),
PageRequest.of(page, size, sort));
}
@ -135,19 +135,19 @@ public class EntryManager {
entry.getMetadata().put("upvoted",
voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.up))
.and(qVote.author.eq(username))));
.and(qVote.author.equalsIgnoreCase(username))));
}
if (!entry.getMetadata().containsKey("downvoted")) {
entry.getMetadata().put("downvoted",
voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.down))
.and(qVote.author.eq(username))));
.and(qVote.author.equalsIgnoreCase(username))));
}
if (!entry.getMetadata().containsKey("unvote")) {
entry.getMetadata().put("unvote", voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.author.eq(username))));
.and(qVote.targetType.eq(Types.entry)).and(qVote.author.equalsIgnoreCase(username))));
}
}
@ -227,10 +227,10 @@ public class EntryManager {
*/
public long getUserPoints(Long entryId, String username) {
long upvotes = voteRepository.count(qVote.targetType.eq(Types.entry)
.and(qVote.type.eq(VoteType.up).and(qVote.author.ne(username)))
.and(qVote.type.eq(VoteType.up).and(qVote.author.notEqualsIgnoreCase(username)))
.and(qVote.target.eq(entryId)));
long downvotes = voteRepository.count(qVote.targetType.eq(Types.entry)
.and(qVote.type.eq(VoteType.down).and(qVote.author.ne(username)))
.and(qVote.type.eq(VoteType.down).and(qVote.author.notEqualsIgnoreCase(username)))
.and(qVote.target.eq(entryId)));
return upvotes - downvotes;
}

View File

@ -144,7 +144,8 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @return the by username
*/
public LocalUser getByUsername(String username) {
return localUserRepository.findById(username).orElse(null);
return localUserRepository.findOne(qLocalUser.username.equalsIgnoreCase(username))
.orElse(null);
}
/**
@ -189,7 +190,8 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
}
int count = 1;
String username = tmpUsername;
while (localUserRepository.existsById(username)) {
while (localUserRepository
.exists(qLocalUser.username.equalsIgnoreCase(username))) {
username = tmpUsername
+ "-"
+ count;
@ -226,13 +228,14 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @param user the user
*/
public void applyMetadata(String username, LocalUser user) {
if (user.getUsername().equals(username) && !user.getMetadata().containsKey("self")) {
if (user.getUsername().equalsIgnoreCase(username)
&& !user.getMetadata().containsKey("self")) {
user.getMetadata().put("self", true);
}
if (!user.getMetadata().containsKey("points")) {
long points = 0;
for (Entry entry : entryRepository.findAll(qEntry.author.eq(username))) {
for (Entry entry : entryRepository.findAll(qEntry.author.equalsIgnoreCase(username))) {
points += entryManager.getUserPoints(entry.getId(), user.getUsername());
}
user.getMetadata().put("points", points);
@ -256,7 +259,7 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @return true, if successful
*/
public boolean exists(String username) {
return localUserRepository.existsById(username);
return localUserRepository.exists(qLocalUser.username.equalsIgnoreCase(username));
}
}