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.Bookmarks;
import de.bstly.board.model.QBookmarks; import de.bstly.board.model.QBookmarks;
import de.bstly.board.model.QLocalUser;
import de.bstly.board.repository.BookmarksRepository; import de.bstly.board.repository.BookmarksRepository;
import de.bstly.board.repository.EntryRepository; import de.bstly.board.repository.EntryRepository;
import de.bstly.board.repository.LocalUserRepository; import de.bstly.board.repository.LocalUserRepository;
@ -27,6 +28,7 @@ public class BookmarksManager {
private EntryRepository entryRepository; private EntryRepository entryRepository;
@Autowired @Autowired
private LocalUserRepository localUserRepository; private LocalUserRepository localUserRepository;
private QLocalUser qLocalUser = QLocalUser.localUser;
private QBookmarks qBookmarks = QBookmarks.bookmarks; private QBookmarks qBookmarks = QBookmarks.bookmarks;
/** /**
@ -36,7 +38,8 @@ public class BookmarksManager {
* @return the bookmarks * @return the bookmarks
*/ */
public Bookmarks get(String username) { 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 * @return true, if successful
*/ */
public boolean hasEntry(String username, Long entryId) { public boolean hasEntry(String username, Long entryId) {
return bookmarksRepository return bookmarksRepository.exists(qBookmarks.username.equalsIgnoreCase(username)
.exists(qBookmarks.username.eq(username).and(qBookmarks.entries.contains(entryId))); .and(qBookmarks.entries.contains(entryId)));
} }
/** /**
@ -59,7 +62,8 @@ public class BookmarksManager {
*/ */
public void addEntry(String username, Long entryId) { public void addEntry(String username, Long entryId) {
Assert.isTrue(entryRepository.existsById(entryId), "Invalid 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); Bookmarks bookmarks = get(username);
if (bookmarks.getEntries() == null) { if (bookmarks.getEntries() == null) {
@ -80,7 +84,8 @@ public class BookmarksManager {
*/ */
public void removeEntry(String username, Long entryId) { public void removeEntry(String username, Long entryId) {
Assert.isTrue(entryRepository.existsById(entryId), "Invalid 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); 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, public Page<Comment> fetchByUsername(String username, Long orElse, Instant date, int page,
int size, boolean asc) { int size, boolean asc) {
Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created")); Sort sort = Sort.by(asc ? Order.asc("created") : Order.desc("created"));
return commentRepository.findAll(qComment.author.eq(username).and(qComment.parent.isNull()) return commentRepository
.and(qComment.created.before(date)), PageRequest.of(page, size, sort)); .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", comment.getMetadata().put("upvoted",
voteRepository.exists(qVote.target.eq(comment.getId()) voteRepository.exists(qVote.target.eq(comment.getId())
.and(qVote.targetType.eq(Types.comment)).and(qVote.type.eq(VoteType.up)) .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")) { if (!ignore.contains("downvoted")) {
comment.getMetadata().put("downvoted", comment.getMetadata().put("downvoted",
voteRepository.exists(qVote.target.eq(comment.getId()) voteRepository.exists(
.and(qVote.targetType.eq(Types.comment)) qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment))
.and(qVote.type.eq(VoteType.down)).and(qVote.author.eq(username)))); .and(qVote.type.eq(VoteType.down))
.and(qVote.author.equalsIgnoreCase(username))));
} }
if (!ignore.contains("unvote")) { if (!ignore.contains("unvote")) {
comment.getMetadata().put("unvote", comment.getMetadata().put("unvote",
voteRepository.exists( voteRepository.exists(
qVote.target.eq(comment.getId()).and(qVote.targetType.eq(Types.comment)) 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")) { 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) { 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")); 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)); PageRequest.of(page, size, sort));
} }
@ -135,19 +135,19 @@ public class EntryManager {
entry.getMetadata().put("upvoted", entry.getMetadata().put("upvoted",
voteRepository.exists(qVote.target.eq(entry.getId()) voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.up)) .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")) { if (!entry.getMetadata().containsKey("downvoted")) {
entry.getMetadata().put("downvoted", entry.getMetadata().put("downvoted",
voteRepository.exists(qVote.target.eq(entry.getId()) voteRepository.exists(qVote.target.eq(entry.getId())
.and(qVote.targetType.eq(Types.entry)).and(qVote.type.eq(VoteType.down)) .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")) { if (!entry.getMetadata().containsKey("unvote")) {
entry.getMetadata().put("unvote", voteRepository.exists(qVote.target.eq(entry.getId()) 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) { public long getUserPoints(Long entryId, String username) {
long upvotes = voteRepository.count(qVote.targetType.eq(Types.entry) 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))); .and(qVote.target.eq(entryId)));
long downvotes = voteRepository.count(qVote.targetType.eq(Types.entry) 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))); .and(qVote.target.eq(entryId)));
return upvotes - downvotes; return upvotes - downvotes;
} }

View File

@ -144,7 +144,8 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @return the by username * @return the by username
*/ */
public LocalUser getByUsername(String 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; int count = 1;
String username = tmpUsername; String username = tmpUsername;
while (localUserRepository.existsById(username)) { while (localUserRepository
.exists(qLocalUser.username.equalsIgnoreCase(username))) {
username = tmpUsername username = tmpUsername
+ "-" + "-"
+ count; + count;
@ -226,13 +228,14 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @param user the user * @param user the user
*/ */
public void applyMetadata(String username, LocalUser 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); user.getMetadata().put("self", true);
} }
if (!user.getMetadata().containsKey("points")) { if (!user.getMetadata().containsKey("points")) {
long points = 0; 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()); points += entryManager.getUserPoints(entry.getId(), user.getUsername());
} }
user.getMetadata().put("points", points); user.getMetadata().put("points", points);
@ -256,7 +259,7 @@ public class UserManager implements UserDetailsService, SmartInitializingSinglet
* @return true, if successful * @return true, if successful
*/ */
public boolean exists(String username) { public boolean exists(String username) {
return localUserRepository.existsById(username); return localUserRepository.exists(qLocalUser.username.equalsIgnoreCase(username));
} }
} }