draft for borrow, update partey tags, added jwt

This commit is contained in:
2021-10-22 10:56:20 +02:00
parent 442bdb4996
commit a24f0650d1
41 changed files with 3415 additions and 162 deletions
@@ -80,17 +80,19 @@ public class RoomController extends DebugLogger {
memberData.setTags(Lists.newArrayList());
try {
Long userId = Long.parseLong(userIdentifier);
if (StringUtils.hasText(visitCardUrlFormat)) {
User user = userManager.get(userId);
memberData.setVisitCardUrl(String.format(visitCardUrlFormat, user.getUsername()));
}
if (permissionManager.isFullUser(userId)
User user = userManager.get(userId);
if (user != null && permissionManager.isFullUser(userId)
&& permissionManager.hasPermission(userId, ParteyPermissions.PARTEY)) {
if (StringUtils.hasText(visitCardUrlFormat)) {
memberData
.setVisitCardUrl(String.format(visitCardUrlFormat, user.getUsername()));
}
memberData.setAnonymous(false);
for (ParteyUserTag parteyUserTag : parteyUserTagManager.getForTarget(userId)) {
for (ParteyUserTag parteyUserTag : parteyUserTagManager
.getNonExpiredForUsername(user.getUsername())) {
memberData.getTags().add(parteyUserTag.getTag());
}
}
@@ -40,7 +40,7 @@ public class ParteyMapManager {
private String internalMapUri;
private Pattern internalMapUriPattern = Pattern.compile("\\/@\\/(.+)");
private Pattern externalMapUriPattern = Pattern.compile("\\/_\\/(.+)");
private Pattern externalMapUriPattern = Pattern.compile("\\/_\\/[^/]+\\/(.+)");
/**
* Gets the.
@@ -3,6 +3,7 @@
*/
package de.bstly.we.partey.businesslogic;
import java.time.Instant;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
@@ -10,10 +11,15 @@ import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
import de.bstly.we.businesslogic.UserManager;
import de.bstly.we.model.User;
import de.bstly.we.partey.model.ParteyUserTag;
import de.bstly.we.partey.model.ParteyUserTag.ParteyUserTagId;
import de.bstly.we.partey.model.QParteyUserTag;
import de.bstly.we.partey.repository.ParteyUserTagRepository;
@@ -25,18 +31,20 @@ public class ParteyUserTagManager {
@Autowired
private ParteyUserTagRepository parteyUserTagRepository;
@Autowired
private UserManager userManager;
private QParteyUserTag qParteyUserTag = QParteyUserTag.parteyUserTag;
/**
* Gets the.
* Gets the all.
*
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param page the page
* @param size the size
* @param sortBy the sort by
* @param descending the descending
* @return the page
* @return the all
*/
public Page<ParteyUserTag> get(int page, int size, String sortBy, boolean descending) {
public Page<ParteyUserTag> getAll(int page, int size, String sortBy, boolean descending) {
PageRequest pageRequest = PageRequest.of(page, size,
descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending());
@@ -44,14 +52,85 @@ public class ParteyUserTagManager {
}
/**
* Gets the for target.
* Gets the all by user id.
*
* @param target the target
* @return the for target
* @param userId the user id
* @return the all by user id
*/
public List<ParteyUserTag> getForTarget(long target) {
return Lists
.newArrayList(parteyUserTagRepository.findAll(qParteyUserTag.target.eq(target)));
public List<ParteyUserTag> getAllByUserId(Long userId) {
User user = userManager.get(userId);
Assert.notNull(user, "invalid userId: '"
+ userId
+ "'!");
return getAllForUsername(user.getUsername());
}
/**
* Gets the non expired by user id.
*
* @param userId the user id
* @return the non expired by user id
*/
public List<ParteyUserTag> getNonExpiredByUserId(Long userId) {
return getNonExpiredByUserId(userId, false);
}
/**
* Gets the non expired by user id.
*
* @param userId the user id
* @param upcoming the upcoming
* @return the non expired by user id
*/
public List<ParteyUserTag> getNonExpiredByUserId(Long userId, boolean upcoming) {
User user = userManager.get(userId);
Assert.notNull(user, "invalid userId: '"
+ userId
+ "'!");
return getNonExpiredForUsername(user.getUsername(), upcoming);
}
/**
* Gets the all for username.
*
* @param username the username
* @return the all for username
*/
public List<ParteyUserTag> getAllForUsername(String username) {
return Lists.newArrayList(
parteyUserTagRepository.findAll(qParteyUserTag.username.eq(username)));
}
/**
* Gets the non expired for username.
*
* @param username the username
* @return the non expired for username
*/
public List<ParteyUserTag> getNonExpiredForUsername(String username) {
return getNonExpiredForUsername(username, false);
}
/**
* Gets the non expired for username.
*
* @param username the username
* @param upcoming the upcoming
* @return the non expired for username
*/
public List<ParteyUserTag> getNonExpiredForUsername(String username, boolean upcoming) {
BooleanBuilder query = new BooleanBuilder();
query.and(qParteyUserTag.username.eq(username));
query.and(qParteyUserTag.expires.isNull().or(qParteyUserTag.expires.after(Instant.now())));
if (!upcoming) {
query.and(
qParteyUserTag.starts.isNull().or(qParteyUserTag.starts.before(Instant.now())));
}
return Lists.newArrayList(parteyUserTagRepository.findAll(query.getValue()));
}
/**
@@ -67,20 +146,21 @@ public class ParteyUserTagManager {
/**
* Delete.
*
* @param id the id
* @param parteyUserTag the partey user tag
*/
public void delete(Long id) {
parteyUserTagRepository.deleteById(id);
public void delete(ParteyUserTag parteyUserTag) {
parteyUserTagRepository.deleteById(
new ParteyUserTagId(parteyUserTag.getUsername(), parteyUserTag.getTag()));
}
/**
* Delete all for target.
*
* @param target the target
* @param username the username
*/
public void deleteAllForTarget(Long target) {
public void deleteAllForTarget(String username) {
parteyUserTagRepository
.deleteAll(parteyUserTagRepository.findAll(qParteyUserTag.target.eq(target)));
.deleteAll(parteyUserTagRepository.findAll(qParteyUserTag.username.eq(username)));
}
}
@@ -33,7 +33,7 @@ public class UserTagController extends BaseController {
@PreAuthorize("isAuthenticated()")
@GetMapping
public List<ParteyUserTag> getParteyUserTagsForTarget() {
return parteyUserTagManager.getForTarget(getCurrentUserId());
return parteyUserTagManager.getNonExpiredByUserId(getCurrentUserId(), true);
}
}
@@ -55,8 +55,8 @@ public class UserTagManagementController extends BaseController {
@RequestParam("sort") Optional<String> sortParameter,
@RequestParam("desc") Optional<Boolean> descParameter) {
Page<ParteyUserTag> page = parteyUserTagManager.get(pageParameter.orElse(0),
sizeParameter.orElse(10), sortParameter.orElse("id"), descParameter.orElse(false));
Page<ParteyUserTag> page = parteyUserTagManager.getAll(pageParameter.orElse(0),
sizeParameter.orElse(10), sortParameter.orElse("username"), descParameter.orElse(false));
return page;
}
@@ -77,7 +77,45 @@ public class UserTagManagementController extends BaseController {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
return parteyUserTagManager.getForTarget(user.getId());
return parteyUserTagManager.getAllForUsername(username);
}
/**
* Gets the non expired partey user tags for target.
*
* @param username the username
* @return the non expired partey user tags for target
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("{username}/active")
public List<ParteyUserTag> getNonExpiredParteyUserTagsForTarget(
@PathVariable("username") String username) {
User user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
return parteyUserTagManager.getNonExpiredForUsername(username);
}
/**
* Gets the upcoming partey user tags for target.
*
* @param username the username
* @return the upcoming partey user tags for target
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@GetMapping("{username}/upcoming")
public List<ParteyUserTag> getUpcomingParteyUserTagsForTarget(
@PathVariable("username") String username) {
User user = userManager.getByUsername(username);
if (user == null) {
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
}
return parteyUserTagManager.getNonExpiredForUsername(username, true);
}
/**
@@ -95,12 +133,12 @@ public class UserTagManagementController extends BaseController {
/**
* Delete partey user tag.
*
* @param id the id
* @param parteyUserTag the partey user tag
*/
@PreAuthorize("hasRole('ROLE_ADMIN')")
@DeleteMapping("{id}")
public void deleteParteyUserTag(@PathVariable("id") Long id) {
parteyUserTagManager.delete(id);
@DeleteMapping
public void deleteParteyUserTag(@RequestBody ParteyUserTag parteyUserTag) {
parteyUserTagManager.delete(parteyUserTag);
}
}
@@ -3,59 +3,50 @@
*/
package de.bstly.we.partey.model;
import java.io.Serializable;
import java.time.Instant;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.Table;
import de.bstly.we.partey.model.ParteyUserTag.ParteyUserTagId;
/**
* The Class ParteyUserTag.
*/
@Entity
@IdClass(ParteyUserTagId.class)
@Table(name = "partey_user_tags")
public class ParteyUserTag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
private Long target;
private String username;
@Id
private String tag;
@Column(name = "starts", nullable = true)
private Instant starts;
@Column(name = "expires", nullable = true)
private Instant expires;
/**
* Gets the id.
* Gets the username.
*
* @return the id
* @return the username
*/
public Long getId() {
return id;
public String getUsername() {
return username;
}
/**
* Sets the id.
* Sets the username.
*
* @param id the new id
* @param username the new username
*/
public void setId(Long id) {
this.id = id;
}
/**
* Gets the target.
*
* @return the target
*/
public Long getTarget() {
return target;
}
/**
* Sets the target.
*
* @param target the new target
*/
public void setTarget(Long target) {
this.target = target;
public void setUsername(String username) {
this.username = username;
}
/**
@@ -76,4 +67,99 @@ public class ParteyUserTag {
this.tag = tag;
}
/**
* Gets the starts.
*
* @return the starts
*/
public Instant getStarts() {
return starts;
}
/**
* Sets the starts.
*
* @param starts the new starts
*/
public void setStarts(Instant starts) {
this.starts = starts;
}
/**
* Gets the expires.
*
* @return the expires
*/
public Instant getExpires() {
return expires;
}
/**
* Sets the expires.
*
* @param expires the new expires
*/
public void setExpires(Instant expires) {
this.expires = expires;
}
public static class ParteyUserTagId implements Serializable {
private static final long serialVersionUID = 1L;
private String username;
private String tag;
public ParteyUserTagId() {
super();
}
/**
* @param username
* @param tag
*/
public ParteyUserTagId(String username, String tag) {
super();
this.username = username;
this.tag = tag;
}
/**
* Gets the username.
*
* @return the username
*/
public String getUsername() {
return username;
}
/**
* Sets the username.
*
* @param username the new username
*/
public void setUsername(String username) {
this.username = username;
}
/**
* Gets the tag.
*
* @return the tag
*/
public String getTag() {
return tag;
}
/**
* Sets the tag.
*
* @param tag the new tag
*/
public void setTag(String tag) {
this.tag = tag;
}
}
}
@@ -8,11 +8,12 @@ import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;
import de.bstly.we.partey.model.ParteyUserTag;
import de.bstly.we.partey.model.ParteyUserTag.ParteyUserTagId;
/**
* The Interface ParteyUserTagRepository.
*/
@Repository
public interface ParteyUserTagRepository
extends JpaRepository<ParteyUserTag, Long>, QuerydslPredicateExecutor<ParteyUserTag> {
public interface ParteyUserTagRepository extends JpaRepository<ParteyUserTag, ParteyUserTagId>,
QuerydslPredicateExecutor<ParteyUserTag> {
}