update borrow module + fix invite
This commit is contained in:
parent
3c98987678
commit
0646a21d97
@ -23,9 +23,11 @@ import de.bstly.we.borrow.model.BorrowItemSlot;
|
||||
import de.bstly.we.borrow.model.QBorrowItem;
|
||||
import de.bstly.we.borrow.model.QBorrowItemManualSlot;
|
||||
import de.bstly.we.borrow.model.QBorrowItemPeriodSlot;
|
||||
import de.bstly.we.borrow.model.QBorrowRequest;
|
||||
import de.bstly.we.borrow.repository.BorrowItemManualSlotRepository;
|
||||
import de.bstly.we.borrow.repository.BorrowItemPeriodSlotRepository;
|
||||
import de.bstly.we.borrow.repository.BorrowItemRepository;
|
||||
import de.bstly.we.borrow.repository.BorrowRequestRepository;
|
||||
import de.bstly.we.businesslogic.UserManager;
|
||||
import de.bstly.we.email.businesslogic.EmailManager;
|
||||
import de.bstly.we.model.User;
|
||||
@ -43,6 +45,8 @@ public class BorrowItemManager {
|
||||
@Autowired
|
||||
private BorrowItemPeriodSlotRepository borrowItemPeriodSlotRepository;
|
||||
@Autowired
|
||||
private BorrowRequestRepository borrowRequestRepository;
|
||||
@Autowired
|
||||
private UserManager userManager;
|
||||
@Autowired
|
||||
private EmailManager emailManager;
|
||||
@ -50,6 +54,7 @@ public class BorrowItemManager {
|
||||
private QBorrowItem qBorrowItem = QBorrowItem.borrowItem;
|
||||
private QBorrowItemManualSlot qBorrowItemManualSlot = QBorrowItemManualSlot.borrowItemManualSlot;
|
||||
private QBorrowItemPeriodSlot qBorrowItemPeriodSlot = QBorrowItemPeriodSlot.borrowItemPeriodSlot;
|
||||
private QBorrowRequest qBorrowRequest = QBorrowRequest.borrowRequest;
|
||||
|
||||
/**
|
||||
* Exists.
|
||||
@ -207,6 +212,8 @@ public class BorrowItemManager {
|
||||
.findAll(qBorrowItemManualSlot.item.eq(borrowItem.getId())));
|
||||
borrowItemPeriodSlotRepository.deleteAll(borrowItemPeriodSlotRepository
|
||||
.findAll(qBorrowItemPeriodSlot.item.eq(borrowItem.getId())));
|
||||
borrowRequestRepository.deleteAll(
|
||||
borrowRequestRepository.findAll(qBorrowRequest.item.eq(borrowItem.getId())));
|
||||
|
||||
borrowItemRepository.delete(borrowItem);
|
||||
}
|
||||
@ -217,7 +224,10 @@ public class BorrowItemManager {
|
||||
* @param id the id
|
||||
*/
|
||||
public void delete(Long id) {
|
||||
borrowItemRepository.deleteById(id);
|
||||
BorrowItem borrowItem = get(id);
|
||||
Assert.notNull(borrowItem, "Invalid borrow item id: "
|
||||
+ id);
|
||||
delete(borrowItem);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -87,7 +87,7 @@ public class BorrowRequestManager {
|
||||
* @param descending the descending
|
||||
* @return the for user and stauts
|
||||
*/
|
||||
public Page<BorrowRequest> getForUserAndStauts(Long userId, BorrowRequestStatus status,
|
||||
public Page<BorrowRequest> getForUserAndStatus(Long userId, BorrowRequestStatus status,
|
||||
int page, int size, String sortBy, boolean descending) {
|
||||
return borrowRequestRepository.findAll(
|
||||
qBorrowRequest.user.eq(userId).and(qBorrowRequest.status.eq(status)),
|
||||
|
@ -59,55 +59,29 @@ public class BorrowItemController extends BaseController {
|
||||
@RequestParam("size") Optional<Integer> sizeParameter,
|
||||
@RequestParam("sort") Optional<String> sortParameter,
|
||||
@RequestParam("desc") Optional<Boolean> descParameter,
|
||||
@RequestParam("search") Optional<String> searchParameter) {
|
||||
@RequestParam("search") Optional<String> searchParameter,
|
||||
@RequestParam("owner") Optional<Boolean> ownerParameter) {
|
||||
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_REQUESTS)
|
||||
|| !permissionManager.isFullUser(getCurrentUserId())) {
|
||||
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
Page<BorrowItem> borrowItems = borrowItemManager.get(pageParameter.orElse(0),
|
||||
sizeParameter.orElse(10), sortParameter.orElse("id"), descParameter.orElse(false),
|
||||
searchParameter.orElse(null));
|
||||
|
||||
for (BorrowItem borrowItem : borrowItems.getContent()) {
|
||||
if (!borrowItem.getOwner().equals(getCurrentUserId())) {
|
||||
borrowItem.setEmail(null);
|
||||
borrowItem.setEmailNotification(null);
|
||||
}
|
||||
borrowItemManager.applySlots(borrowItem);
|
||||
}
|
||||
|
||||
return borrowItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the owner borrow items.
|
||||
*
|
||||
* @param pageParameter the page parameter
|
||||
* @param sizeParameter the size parameter
|
||||
* @param sortParameter the sort parameter
|
||||
* @param descParameter the desc parameter
|
||||
* @param searchParameter the search parameter
|
||||
* @return the owner borrow items
|
||||
*/
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping("/mine")
|
||||
public Page<BorrowItem> getOwnerBorrowItems(
|
||||
@RequestParam("page") Optional<Integer> pageParameter,
|
||||
@RequestParam("size") Optional<Integer> sizeParameter,
|
||||
@RequestParam("sort") Optional<String> sortParameter,
|
||||
@RequestParam("desc") Optional<Boolean> descParameter,
|
||||
@RequestParam("search") Optional<String> searchParameter) {
|
||||
Page<BorrowItem> borrowItems;
|
||||
|
||||
if (ownerParameter.isPresent() && ownerParameter.get().booleanValue()) {
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_ITEMS)
|
||||
|| !permissionManager.isFullUser(getCurrentUserId())) {
|
||||
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
Page<BorrowItem> borrowItems = borrowItemManager.getForUser(getCurrentUserId(),
|
||||
pageParameter.orElse(0), sizeParameter.orElse(10), sortParameter.orElse("id"),
|
||||
borrowItems = borrowItemManager.getForUser(getCurrentUserId(), pageParameter.orElse(0),
|
||||
sizeParameter.orElse(10), sortParameter.orElse("id"),
|
||||
descParameter.orElse(false), searchParameter.orElse(null));
|
||||
} else {
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(),
|
||||
BorrowPermissions.BORROW_REQUESTS)
|
||||
|| !permissionManager.isFullUser(getCurrentUserId())) {
|
||||
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
borrowItems = borrowItemManager.get(pageParameter.orElse(0), sizeParameter.orElse(10),
|
||||
sortParameter.orElse("id"), descParameter.orElse(false),
|
||||
searchParameter.orElse(null));
|
||||
}
|
||||
|
||||
for (BorrowItem borrowItem : borrowItems.getContent()) {
|
||||
if (!borrowItem.getOwner().equals(getCurrentUserId())) {
|
||||
@ -136,7 +110,7 @@ public class BorrowItemController extends BaseController {
|
||||
BorrowItem borrowItem = borrowItemManager.get(id);
|
||||
|
||||
if (borrowItem == null) {
|
||||
throw new EntityResponseStatusException(HttpStatus.NO_CONTENT);
|
||||
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
if (!borrowItem.getOwner().equals(getCurrentUserId())) {
|
||||
|
@ -78,52 +78,29 @@ public class BorrowRequestController extends BaseController {
|
||||
@RequestParam("page") Optional<Integer> pageParameter,
|
||||
@RequestParam("size") Optional<Integer> sizeParameter,
|
||||
@RequestParam("sort") Optional<String> sortParameter,
|
||||
@RequestParam("desc") Optional<Boolean> descParameter) {
|
||||
@RequestParam("desc") Optional<Boolean> descParameter,
|
||||
@RequestParam("owner") Optional<Boolean> ownerParameter) {
|
||||
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_REQUESTS)
|
||||
|| !permissionManager.isFullUser(getCurrentUserId())) {
|
||||
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
Page<BorrowRequest> borrowRequests = borrowRequestManager.getForUser(getCurrentUserId(),
|
||||
pageParameter.orElse(0), sizeParameter.orElse(10), sortParameter.orElse("id"),
|
||||
descParameter.orElse(false));
|
||||
|
||||
for (BorrowRequest borrowRequest : borrowRequests.getContent()) {
|
||||
BorrowItem borrowItem = borrowItemManager.get(borrowRequest.getItem());
|
||||
borrowItem.setEmail(null);
|
||||
borrowItem.setEmailNotification(null);
|
||||
borrowRequest.setBorrowItem(borrowItem);
|
||||
}
|
||||
|
||||
return borrowRequests;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the owner borrow requests.
|
||||
*
|
||||
* @param pageParameter the page parameter
|
||||
* @param sizeParameter the size parameter
|
||||
* @param sortParameter the sort parameter
|
||||
* @param descParameter the desc parameter
|
||||
* @return the owner borrow requests
|
||||
*/
|
||||
@PreAuthorize("isAuthenticated()")
|
||||
@GetMapping("/mine")
|
||||
public Page<BorrowRequest> getOwnerBorrowRequests(
|
||||
@RequestParam("page") Optional<Integer> pageParameter,
|
||||
@RequestParam("size") Optional<Integer> sizeParameter,
|
||||
@RequestParam("sort") Optional<String> sortParameter,
|
||||
@RequestParam("desc") Optional<Boolean> descParameter) {
|
||||
Page<BorrowRequest> borrowRequests;
|
||||
|
||||
if (ownerParameter.isPresent() && ownerParameter.get().booleanValue()) {
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_ITEMS)
|
||||
|| !permissionManager.isFullUser(getCurrentUserId())) {
|
||||
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
Page<BorrowRequest> borrowRequests = borrowRequestManager.getForOwner(getCurrentUserId(),
|
||||
borrowRequests = borrowRequestManager.getForOwner(getCurrentUserId(),
|
||||
pageParameter.orElse(0), sizeParameter.orElse(10), sortParameter.orElse("id"),
|
||||
descParameter.orElse(false));
|
||||
} else {
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(),
|
||||
BorrowPermissions.BORROW_REQUESTS)
|
||||
|| !permissionManager.isFullUser(getCurrentUserId())) {
|
||||
throw new EntityResponseStatusException(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
borrowRequests = borrowRequestManager.getForUser(getCurrentUserId(),
|
||||
pageParameter.orElse(0), sizeParameter.orElse(10), sortParameter.orElse("id"),
|
||||
descParameter.orElse(false));
|
||||
}
|
||||
|
||||
for (BorrowRequest borrowRequest : borrowRequests.getContent()) {
|
||||
BorrowItem borrowItem = borrowItemManager.get(borrowRequest.getItem());
|
||||
@ -294,7 +271,7 @@ public class BorrowRequestController extends BaseController {
|
||||
|
||||
return signedJwt.getJWTClaimsSet().getClaims();
|
||||
} catch (ParseException e) {
|
||||
throw new EntityResponseStatusException(HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
throw new EntityResponseStatusException(HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -70,14 +70,14 @@ public class BorrowJwtValidator implements Validator {
|
||||
errors.reject("INVALID");
|
||||
return;
|
||||
} else if (claims.getNotBeforeTime().after(new Date())) {
|
||||
errors.rejectValue("nbf", "UPCOMING");
|
||||
errors.rejectValue("nbf", "UPCOMING", claims.getNotBeforeTime().toInstant().toString());
|
||||
}
|
||||
|
||||
if (claims.getExpirationTime() == null) {
|
||||
errors.reject("INVALID");
|
||||
return;
|
||||
} else if (claims.getExpirationTime().before(new Date())) {
|
||||
errors.rejectValue("exp", "EXPIRED");
|
||||
errors.rejectValue("exp", "EXPIRED", claims.getExpirationTime().toInstant().toString());
|
||||
}
|
||||
|
||||
try {
|
||||
|
@ -4,7 +4,7 @@
|
||||
package de.bstly.we.borrow.controller.validation;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.LocalTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -130,7 +130,7 @@ public class BorrowRequestValidator implements Validator {
|
||||
BorrowItemPeriodSlot borrowItemPeriodSlot = (BorrowItemPeriodSlot) borrowItemSlot;
|
||||
if (borrowRequest.getStarts().atZone(ZoneOffset.UTC).getDayOfWeek()
|
||||
.compareTo(borrowItemPeriodSlot.getStartDay()) >= 0
|
||||
&& OffsetTime
|
||||
&& LocalTime
|
||||
.ofInstant(borrowRequest.getStarts(),
|
||||
ZoneOffset.UTC)
|
||||
.compareTo(
|
||||
@ -139,7 +139,7 @@ public class BorrowRequestValidator implements Validator {
|
||||
}
|
||||
if (borrowRequest.getEnds().atZone(ZoneOffset.UTC).getDayOfWeek()
|
||||
.compareTo(borrowItemPeriodSlot.getEndDay()) <= 0
|
||||
&& OffsetTime
|
||||
&& LocalTime
|
||||
.ofInstant(borrowRequest.getEnds(), ZoneOffset.UTC)
|
||||
.compareTo(
|
||||
borrowItemPeriodSlot.getEndTime()) <= 0) {
|
||||
|
@ -4,10 +4,12 @@
|
||||
package de.bstly.we.borrow.model;
|
||||
|
||||
import java.time.DayOfWeek;
|
||||
import java.time.OffsetTime;
|
||||
import java.time.LocalTime;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.EnumType;
|
||||
import javax.persistence.Enumerated;
|
||||
import javax.persistence.Table;
|
||||
|
||||
/**
|
||||
@ -18,13 +20,15 @@ import javax.persistence.Table;
|
||||
public class BorrowItemPeriodSlot extends BorrowItemSlot {
|
||||
|
||||
@Column(name = "start_day")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private DayOfWeek startDay;
|
||||
@Column(name = "end_day")
|
||||
private DayOfWeek endDay;
|
||||
@Column(name = "start_time")
|
||||
private OffsetTime startTime;
|
||||
private LocalTime startTime;
|
||||
@Column(name = "end_day")
|
||||
@Enumerated(EnumType.STRING)
|
||||
private DayOfWeek endDay;
|
||||
@Column(name = "end_time")
|
||||
private OffsetTime endTime;
|
||||
private LocalTime endTime;
|
||||
|
||||
/**
|
||||
* @return the startDay
|
||||
@ -40,6 +44,20 @@ public class BorrowItemPeriodSlot extends BorrowItemSlot {
|
||||
this.startDay = startDay;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the startTime
|
||||
*/
|
||||
public LocalTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param startTime the startTime to set
|
||||
*/
|
||||
public void setStartTime(LocalTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the endDay
|
||||
*/
|
||||
@ -54,31 +72,17 @@ public class BorrowItemPeriodSlot extends BorrowItemSlot {
|
||||
this.endDay = endDay;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the startTime
|
||||
*/
|
||||
public OffsetTime getStartTime() {
|
||||
return startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param startTime the startTime to set
|
||||
*/
|
||||
public void setStartTime(OffsetTime startTime) {
|
||||
this.startTime = startTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the endTime
|
||||
*/
|
||||
public OffsetTime getEndTime() {
|
||||
public LocalTime getEndTime() {
|
||||
return endTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param endTime the endTime to set
|
||||
*/
|
||||
public void setEndTime(OffsetTime endTime) {
|
||||
public void setEndTime(LocalTime endTime) {
|
||||
this.endTime = endTime;
|
||||
}
|
||||
|
||||
|
@ -170,9 +170,12 @@ public class PermissionManager implements UserDataProvider {
|
||||
*/
|
||||
public Permission update(Permission permission) {
|
||||
Assert.isTrue(
|
||||
permissionRepository.exists(qPermission.target.eq(permission.getTarget())
|
||||
.and(qPermission.name.eq(permission.getName()))),
|
||||
"Permission '" + permission.getName() + "' for target + '" + permission.getTarget()
|
||||
permissionRepository.exists(qPermission.target
|
||||
.eq(permission.getTarget()).and(qPermission.name.eq(permission.getName()))),
|
||||
"Permission '"
|
||||
+ permission.getName()
|
||||
+ "' for target + '"
|
||||
+ permission.getTarget()
|
||||
+ "' not exists!");
|
||||
Permission updatePermission = permissionRepository.findOne(qPermission.target
|
||||
.eq(permission.getTarget()).and(qPermission.name.eq(permission.getName()))).get();
|
||||
@ -215,7 +218,11 @@ public class PermissionManager implements UserDataProvider {
|
||||
Assert.isTrue(
|
||||
permissionRepository
|
||||
.exists(qPermission.target.eq(target).and(qPermission.name.eq(name))),
|
||||
"Permission '" + name + "' for target + '" + target + "' not exists!");
|
||||
"Permission '"
|
||||
+ name
|
||||
+ "' for target + '"
|
||||
+ target
|
||||
+ "' not exists!");
|
||||
Permission delete = permissionRepository
|
||||
.findOne(qPermission.target.eq(target).and(qPermission.name.eq(name))).get();
|
||||
permissionRepository.delete(delete);
|
||||
@ -247,8 +254,9 @@ public class PermissionManager implements UserDataProvider {
|
||||
* @param answers the answers
|
||||
* @param start the start
|
||||
*/
|
||||
public void applyItem(Long target, Integer item, JsonArray answers, Instant start) {
|
||||
for (Permission permission : getForItem(target, item, answers, start)) {
|
||||
public void applyItem(Long target, Integer item, JsonArray answers, Instant starts,
|
||||
Instant expires) {
|
||||
for (Permission permission : getForItem(target, item, answers, starts, expires)) {
|
||||
permissionRepository.save(permission);
|
||||
}
|
||||
}
|
||||
@ -259,22 +267,22 @@ public class PermissionManager implements UserDataProvider {
|
||||
* @param target the target
|
||||
* @param item the item
|
||||
* @param answers the answers
|
||||
* @param start the start
|
||||
* @param starts the start
|
||||
* @return the for item
|
||||
*/
|
||||
public List<Permission> getForItem(Long target, Integer item, JsonArray answers,
|
||||
Instant start) {
|
||||
public List<Permission> getForItem(Long target, Integer item, JsonArray answers, Instant starts,
|
||||
Instant expires) {
|
||||
List<Permission> permissions = Lists.newArrayList();
|
||||
|
||||
if (start == null) {
|
||||
start = Instant.now();
|
||||
}
|
||||
|
||||
for (PermissionMapping permissionMapping : permissionMappingManager.getAllByItem(item)) {
|
||||
for (String name : permissionMapping.getNames()) {
|
||||
Instant starts = null;
|
||||
Instant expires = InstantHelper.plus(start, permissionMapping.getLifetime(),
|
||||
permissionMapping.getLifetimeUnit());
|
||||
Instant permissionStarts = starts;
|
||||
Instant permissionsExpires = expires;
|
||||
|
||||
if (permissionsExpires == null) {
|
||||
permissionsExpires = InstantHelper.plus(
|
||||
permissionStarts == null ? Instant.now() : permissionStarts,
|
||||
permissionMapping.getLifetime(), permissionMapping.getLifetimeUnit());
|
||||
}
|
||||
|
||||
boolean additional = true;
|
||||
|
||||
@ -290,8 +298,8 @@ public class PermissionManager implements UserDataProvider {
|
||||
.getAsString();
|
||||
if (StringUtils.hasText(dateTimeString)) {
|
||||
dateTimeString = dateTimeString.replace(" ", "T");
|
||||
starts = OffsetDateTime.parse(dateTimeString).toInstant();
|
||||
expires = InstantHelper.plus(starts,
|
||||
permissionStarts = OffsetDateTime.parse(dateTimeString).toInstant();
|
||||
permissionsExpires = InstantHelper.plus(permissionStarts,
|
||||
permissionMapping.getLifetime(),
|
||||
permissionMapping.getLifetimeUnit());
|
||||
additional = false;
|
||||
@ -307,7 +315,7 @@ public class PermissionManager implements UserDataProvider {
|
||||
.getAsString();
|
||||
if (StringUtils.hasText(dateTimeString)) {
|
||||
dateTimeString = dateTimeString.replace(" ", "T");
|
||||
expires = InstantHelper.plus(
|
||||
permissionsExpires = InstantHelper.plus(
|
||||
OffsetDateTime.parse(dateTimeString).toInstant(),
|
||||
permissionMapping.getLifetime(),
|
||||
permissionMapping.getLifetimeUnit());
|
||||
@ -333,8 +341,8 @@ public class PermissionManager implements UserDataProvider {
|
||||
permission.setTarget(target);
|
||||
permission.setName(name);
|
||||
permission.setAddon(permissionMapping.isAddon());
|
||||
permission.setStarts(starts);
|
||||
permission.setExpires(expires);
|
||||
permission.setStarts(permissionStarts);
|
||||
permission.setExpires(permissionsExpires);
|
||||
} else {
|
||||
permission.setExpires(InstantHelper.plus(permission.getExpires(),
|
||||
permissionMapping.getLifetime(), permissionMapping.getLifetimeUnit()));
|
||||
|
@ -124,7 +124,10 @@ public class QuotaManager implements UserDataProvider {
|
||||
Assert.isTrue(
|
||||
quotaRepository.exists(
|
||||
qQuota.target.eq(quota.getTarget()).and(qQuota.name.eq(quota.getName()))),
|
||||
"Quota '" + quota.getName() + "' for target + '" + quota.getTarget()
|
||||
"Quota '"
|
||||
+ quota.getName()
|
||||
+ "' for target + '"
|
||||
+ quota.getTarget()
|
||||
+ "' not exists!");
|
||||
Quota updateQuota = quotaRepository
|
||||
.findOne(qQuota.target.eq(quota.getTarget()).and(qQuota.name.eq(quota.getName())))
|
||||
@ -165,7 +168,11 @@ public class QuotaManager implements UserDataProvider {
|
||||
*/
|
||||
public void delete(Long target, String name) {
|
||||
Assert.isTrue(quotaRepository.exists(qQuota.target.eq(target).and(qQuota.name.eq(name))),
|
||||
"Quota '" + name + "' for target + '" + target + "' not exists!");
|
||||
"Quota '"
|
||||
+ name
|
||||
+ "' for target + '"
|
||||
+ target
|
||||
+ "' not exists!");
|
||||
Quota delete = quotaRepository.findOne(qQuota.target.eq(target).and(qQuota.name.eq(name)))
|
||||
.get();
|
||||
quotaRepository.delete(delete);
|
||||
@ -189,6 +196,47 @@ public class QuotaManager implements UserDataProvider {
|
||||
quotaRepository.deleteAll(quotaRepository.findAll(qQuota.name.eq(name)));
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param target
|
||||
* @param item
|
||||
* @return
|
||||
*/
|
||||
public void addForItem(Long target, Integer item, List<Quota> quotas) {
|
||||
for (QuotaMapping quotaMapping : quotaMappingManager.getAllByItem(item)) {
|
||||
boolean added = false;
|
||||
for (Quota quota : quotas) {
|
||||
if (quota.getName().equals(quotaMapping.getName())) {
|
||||
quota.setValue(
|
||||
quotaMapping.isAppend() ? quota.getValue() + quotaMapping.getValue()
|
||||
: quotaMapping.getValue());
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!added) {
|
||||
if (target != null && hasQuota(target, quotaMapping.getName())) {
|
||||
Quota quota = get(target, quotaMapping.getName());
|
||||
|
||||
quota.setValue(
|
||||
quotaMapping.isAppend() ? quota.getValue() + quotaMapping.getValue()
|
||||
: quotaMapping.getValue());
|
||||
|
||||
quotas.add(quota);
|
||||
added = true;
|
||||
}
|
||||
if (!added) {
|
||||
Quota quota = new Quota();
|
||||
quota.setName(quotaMapping.getName());
|
||||
quota.setValue(quotaMapping.getValue());
|
||||
quota.setUnit(quotaMapping.getUnit());
|
||||
quota.setDisposable(quotaMapping.isDisposable());
|
||||
quotas.add(quota);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply item.
|
||||
*
|
||||
|
@ -17,7 +17,6 @@ import com.google.common.collect.Lists;
|
||||
import de.bstly.we.businesslogic.QuotaManager;
|
||||
import de.bstly.we.controller.support.TokenSessionManager;
|
||||
import de.bstly.we.model.Quota;
|
||||
import de.bstly.we.model.QuotaMapping;
|
||||
|
||||
/**
|
||||
* The Class QuotaController.
|
||||
@ -54,46 +53,12 @@ public class QuotaController extends BaseController {
|
||||
@GetMapping("/new")
|
||||
public List<Quota> getNewQuotas(HttpSession session) {
|
||||
List<Quota> quotas = Lists.newArrayList();
|
||||
|
||||
if (tokenSessionManager.getTokenFromSession(session).isEmpty()) {
|
||||
return quotas;
|
||||
}
|
||||
|
||||
for (String token : tokenSessionManager.getTokenFromSession(session)) {
|
||||
for (QuotaMapping quotaMapping : tokenSessionManager
|
||||
.getQuotaMappingsForToken(getCurrentUserId(), token)) {
|
||||
boolean added = false;
|
||||
for (Quota quota : quotas) {
|
||||
if (quota.getName().equals(quotaMapping.getName())) {
|
||||
quota.setValue(
|
||||
quotaMapping.isAppend() ? quota.getValue() + quotaMapping.getValue()
|
||||
: quotaMapping.getValue());
|
||||
added = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (!added) {
|
||||
if (quotaManager.hasQuota(getCurrentUserId(), quotaMapping.getName())) {
|
||||
Quota quota = quotaManager.get(getCurrentUserId(), quotaMapping.getName());
|
||||
|
||||
quota.setValue(
|
||||
quotaMapping.isAppend() ? quota.getValue() + quotaMapping.getValue()
|
||||
: quotaMapping.getValue());
|
||||
|
||||
quotas.add(quota);
|
||||
added = true;
|
||||
}
|
||||
if (!added) {
|
||||
Quota quota = new Quota();
|
||||
quota.setName(quotaMapping.getName());
|
||||
quota.setValue(quotaMapping.getValue());
|
||||
quota.setUnit(quotaMapping.getUnit());
|
||||
quota.setDisposable(quotaMapping.isDisposable());
|
||||
quotas.add(quota);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tokenSessionManager.addQuotasForToken(getCurrentUserId(), token, quotas);
|
||||
}
|
||||
|
||||
return quotas;
|
||||
|
@ -27,6 +27,7 @@ import de.bstly.we.businesslogic.QuotaMappingManager;
|
||||
import de.bstly.we.controller.model.ItemResultModel;
|
||||
import de.bstly.we.model.Permission;
|
||||
import de.bstly.we.model.PermissionMapping;
|
||||
import de.bstly.we.model.Quota;
|
||||
import de.bstly.we.model.QuotaMapping;
|
||||
import de.bstly.we.security.model.LocalUserDetails;
|
||||
import de.bstly.we.security.token.LocalAnonymousAuthenticationToken;
|
||||
@ -99,7 +100,7 @@ public class TokenSessionManager {
|
||||
}
|
||||
|
||||
permissions.addAll(permissionManager.getForItem(userId, item,
|
||||
orderPosition.get("answers").getAsJsonArray(), lastPaymentDate));
|
||||
orderPosition.get("answers").getAsJsonArray(), null, null));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
@ -132,6 +133,20 @@ public class TokenSessionManager {
|
||||
return quotaMappings;
|
||||
}
|
||||
|
||||
public void addQuotasForToken(Long userId, String token, List<Quota> quotas) {
|
||||
try {
|
||||
JsonObject result = pretixManager.getCheckInItemBySecret(token);
|
||||
if (result != null && result.get("secret").getAsString().equals(token)
|
||||
&& result.getAsJsonArray("checkins").size() < 1
|
||||
&& "p".equals(result.get("order__status").getAsString())) {
|
||||
int item = result.get("item").getAsInt();
|
||||
quotaManager.addForItem(userId, item, quotas);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply tokens.
|
||||
*
|
||||
@ -158,7 +173,7 @@ public class TokenSessionManager {
|
||||
}
|
||||
|
||||
permissionManager.applyItem(userId, item,
|
||||
position.get("answers").getAsJsonArray(), lastPaymentDate);
|
||||
position.get("answers").getAsJsonArray(), null, null);
|
||||
permissionMappings.addAll(permissionMappingManager.getAllByItem(item));
|
||||
quotaManager.applyItem(userId, item);
|
||||
quotaMappings.addAll(quotaMappingManager.getAllByItem(item));
|
||||
@ -214,7 +229,8 @@ public class TokenSessionManager {
|
||||
}
|
||||
|
||||
if (StringUtils.hasLength(tokens)) {
|
||||
tokens += "," + secret;
|
||||
tokens += ","
|
||||
+ secret;
|
||||
} else {
|
||||
tokens = secret;
|
||||
}
|
||||
@ -238,7 +254,8 @@ public class TokenSessionManager {
|
||||
for (String token : ((String) sessionAttribute).split(",")) {
|
||||
if (!token.equals(secret)) {
|
||||
if (StringUtils.hasLength(tokens)) {
|
||||
tokens += "," + secret;
|
||||
tokens += ","
|
||||
+ secret;
|
||||
} else {
|
||||
tokens = secret;
|
||||
}
|
||||
|
@ -35,8 +35,6 @@ public class User implements UserData {
|
||||
private boolean locked;
|
||||
@Column(name = "status", nullable = false)
|
||||
private UserStatus status;
|
||||
@Column(name = "secret")
|
||||
private String secret;
|
||||
@Column(name = "reset_token")
|
||||
private String resetToken;
|
||||
|
||||
@ -148,24 +146,6 @@ public class User implements UserData {
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the secret.
|
||||
*
|
||||
* @return the secret
|
||||
*/
|
||||
public String getSecret() {
|
||||
return secret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the secret.
|
||||
*
|
||||
* @param secret the new secret
|
||||
*/
|
||||
public void setSecret(String secret) {
|
||||
this.secret = secret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the reset token.
|
||||
*
|
||||
|
@ -99,7 +99,7 @@ public class InviteManager implements UserDataProvider {
|
||||
* @param redeemed the redeemed
|
||||
* @return the by owner
|
||||
*/
|
||||
public Page<Invite> getByOwner(Long owner, Integer item, int page, int size, String sortBy,
|
||||
public Page<Invite> getByOwner(Long owner, String quota, int page, int size, String sortBy,
|
||||
boolean descending, String search, String redeemed) {
|
||||
PageRequest pageRequest = PageRequest.of(page, size,
|
||||
descending ? Sort.by(sortBy).descending() : Sort.by(sortBy).ascending());
|
||||
@ -107,8 +107,8 @@ public class InviteManager implements UserDataProvider {
|
||||
BooleanBuilder query = new BooleanBuilder();
|
||||
query.and(qInvite.owner.eq(owner));
|
||||
|
||||
if (item != null) {
|
||||
query.and(qInvite.item.eq(item));
|
||||
if (StringUtils.hasText(quota)) {
|
||||
query.and(qInvite.quota.eq(quota));
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(search)) {
|
||||
@ -137,12 +137,12 @@ public class InviteManager implements UserDataProvider {
|
||||
* @param redeemed the redeemed
|
||||
* @return the others
|
||||
*/
|
||||
public Page<Invite> getOthers(Long owner, int item, int page, int size, String search,
|
||||
public Page<Invite> getOthers(Long owner, String quota, int page, int size, String search,
|
||||
String redeemed) {
|
||||
|
||||
BooleanBuilder query = new BooleanBuilder();
|
||||
query.and(qInvite.owner.ne(owner));
|
||||
query.and(qInvite.item.eq(item));
|
||||
query.and(qInvite.quota.eq(quota));
|
||||
|
||||
if (StringUtils.hasText(search)) {
|
||||
query.and(qInvite.note.containsIgnoreCase(search));
|
||||
@ -173,7 +173,8 @@ public class InviteManager implements UserDataProvider {
|
||||
}
|
||||
}
|
||||
|
||||
InviteMapping inviteMapping = inviteMappingManager.getByItem(invite.getItem());
|
||||
InviteMapping inviteMapping = inviteMappingManager.getByItemAndQuota(invite.getItem(),
|
||||
invite.getQuota());
|
||||
Assert.notNull(inviteMapping, "No mapping for item!");
|
||||
if (StringUtils.hasLength(inviteMapping.getCodeLink())) {
|
||||
invite.setCodeLink(String.format(inviteMapping.getCodeLink(), invite.getCode()));
|
||||
@ -181,6 +182,8 @@ public class InviteManager implements UserDataProvider {
|
||||
invite.setCodeLink(null);
|
||||
}
|
||||
|
||||
invite.setUrl(inviteMapping.getUrl());
|
||||
|
||||
return inviteRepository.save(invite);
|
||||
}
|
||||
|
||||
|
@ -49,8 +49,10 @@ public class InviteMappingManager {
|
||||
* @param item the item
|
||||
* @return the by item
|
||||
*/
|
||||
public InviteMapping getByItem(int item) {
|
||||
return inviteMappingRepository.findOne(qInviteMapping.item.eq(item)).orElse(null);
|
||||
public InviteMapping getByItemAndQuota(int item, String quota) {
|
||||
return inviteMappingRepository
|
||||
.findOne(qInviteMapping.item.eq(item).and(qInviteMapping.quota.eq(quota)))
|
||||
.orElse(null);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,6 +108,16 @@ public class InviteMappingManager {
|
||||
invite.setCodeLink(null);
|
||||
inviteRepository.save(invite);
|
||||
}
|
||||
|
||||
if (StringUtils.hasText(inviteMapping.getUrl())) {
|
||||
if (!inviteMapping.getUrl().equals(invite.getUrl())) {
|
||||
invite.setUrl(inviteMapping.getUrl());
|
||||
inviteRepository.save(invite);
|
||||
}
|
||||
} else if (StringUtils.hasText(invite.getUrl())) {
|
||||
invite.setUrl(null);
|
||||
inviteRepository.save(invite);
|
||||
}
|
||||
}
|
||||
|
||||
return inviteMappingRepository.save(inviteMapping);
|
||||
|
@ -4,6 +4,7 @@
|
||||
package de.bstly.we.invite.controller;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -93,24 +94,43 @@ public class InviteController extends BaseController {
|
||||
invite.setNote(null);
|
||||
invite.setOwner(null);
|
||||
} else if (!getCurrentUserId().equals(invite.getOwner())) {
|
||||
if (permissionManager.hasPermission(getCurrentUserId(), Permissions.ROLE_MEMBER)) {
|
||||
invite.setId(null);
|
||||
invite.setItem(null);
|
||||
if (!StringUtils.hasText(invite.getNote())) {
|
||||
invite.setNote("...");
|
||||
}
|
||||
invite.setCode(null);
|
||||
invite.setCodeLink(null);
|
||||
invite.setOwner(null);
|
||||
} else {
|
||||
invite.setId(null);
|
||||
invite.setItem(null);
|
||||
if (!permissionManager.hasPermission(getCurrentUserId(), Permissions.ROLE_MEMBER)) {
|
||||
invite.setNote(null);
|
||||
invite.setOwner(null);
|
||||
}
|
||||
}
|
||||
|
||||
return invite;
|
||||
}
|
||||
|
||||
@GetMapping("/{code}/permissions")
|
||||
public List<Permission> getPermissions(@PathVariable("code") String code) {
|
||||
Invite invite = inviteManager.getByCode(code);
|
||||
if (invite == null) {
|
||||
throw new EntityResponseStatusException(HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
|
||||
return permissionManager.getForItem(null, invite.getItem(), new JsonArray(),
|
||||
invite.getStarts(), invite.getExpires());
|
||||
}
|
||||
|
||||
@GetMapping("/{code}/quotas")
|
||||
public List<Quota> getQuotas(@PathVariable("code") String code) {
|
||||
Invite invite = inviteManager.getByCode(code);
|
||||
if (invite == null) {
|
||||
throw new EntityResponseStatusException(HttpStatus.NOT_ACCEPTABLE);
|
||||
}
|
||||
|
||||
List<Quota> quotas = Lists.newArrayList();
|
||||
|
||||
quotaManager.addForItem(null, invite.getItem(), quotas);
|
||||
|
||||
return quotas;
|
||||
}
|
||||
|
||||
/**
|
||||
* Register.
|
||||
*
|
||||
@ -119,6 +139,11 @@ public class InviteController extends BaseController {
|
||||
@PostMapping
|
||||
public void register(@RequestBody UserModel userModel) {
|
||||
Errors errors = new RequestBodyErrors(userModel);
|
||||
|
||||
if (!StringUtils.hasText(userModel.getToken())) {
|
||||
throw new EntityResponseStatusException(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
Invite invite = inviteManager.getByCode(userModel.getToken());
|
||||
|
||||
if (invite == null) {
|
||||
@ -133,7 +158,7 @@ public class InviteController extends BaseController {
|
||||
boolean register = false;
|
||||
|
||||
for (Permission permission : permissionManager.getForItem(null, invite.getItem(),
|
||||
new JsonArray(), invite.getStarts())) {
|
||||
new JsonArray(), invite.getStarts(), invite.getExpires())) {
|
||||
if (permission.getExpires().isAfter(Instant.now()) && !permission.isAddon()) {
|
||||
register = true;
|
||||
break;
|
||||
@ -180,7 +205,7 @@ public class InviteController extends BaseController {
|
||||
}
|
||||
|
||||
permissionManager.applyItem(user.getId(), invite.getItem(), new JsonArray(),
|
||||
invite.getStarts());
|
||||
invite.getStarts(), invite.getExpires());
|
||||
quotaManager.applyItem(user.getId(), invite.getItem());
|
||||
|
||||
invite.setRedeemed(true);
|
||||
@ -208,18 +233,11 @@ public class InviteController extends BaseController {
|
||||
@RequestParam("desc") Optional<Boolean> descParameter,
|
||||
@RequestParam("search") Optional<String> searchParameter,
|
||||
@RequestParam("redeemed") Optional<String> redeemedParameter) {
|
||||
Integer item = null;
|
||||
if (quotaParameter.isPresent() && StringUtils.hasText(quotaParameter.get())) {
|
||||
InviteMapping inviteMapping = inviteMappingManager.get(quotaParameter.get());
|
||||
if (inviteMapping == null) {
|
||||
throw new EntityResponseStatusException(HttpStatus.CONFLICT);
|
||||
}
|
||||
item = inviteMapping.getItem();
|
||||
}
|
||||
|
||||
return inviteManager.getByOwner(getCurrentUserId(), item, pageParameter.orElse(0),
|
||||
sizeParameter.orElse(10), sortParamater.orElse("id"), descParameter.orElse(false),
|
||||
searchParameter.orElse(null), redeemedParameter.orElse(null));
|
||||
return inviteManager.getByOwner(getCurrentUserId(), quotaParameter.orElse(""),
|
||||
pageParameter.orElse(0), sizeParameter.orElse(10), sortParamater.orElse("id"),
|
||||
descParameter.orElse(false), searchParameter.orElse(null),
|
||||
redeemedParameter.orElse(null));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -247,7 +265,7 @@ public class InviteController extends BaseController {
|
||||
throw new EntityResponseStatusException(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
Page<Invite> page = inviteManager.getOthers(getCurrentUserId(), inviteMapping.getItem(),
|
||||
Page<Invite> page = inviteManager.getOthers(getCurrentUserId(), quota,
|
||||
pageParameter.orElse(0), sizeParameter.orElse(10), searchParameter.orElse(null),
|
||||
redeemedParameter.orElse(null));
|
||||
for (Invite invite : page.getContent()) {
|
||||
@ -290,6 +308,7 @@ public class InviteController extends BaseController {
|
||||
invite.setExpires(inviteMapping.getExpires() != null ? inviteMapping.getExpires()
|
||||
: inviteModel.getExpires());
|
||||
invite.setItem(inviteMapping.getItem());
|
||||
invite.setQuota(inviteMapping.getQuota());
|
||||
|
||||
if (inviteMapping.getMessageLimit() != null && inviteMapping.getMessageLimit() > 0
|
||||
&& StringUtils.hasText(inviteModel.getMessage())
|
||||
|
@ -66,7 +66,8 @@ public class InviteManagingController extends BaseController {
|
||||
@PreAuthorize("hasRole('ROLE_ADMIN')")
|
||||
@PostMapping
|
||||
public Invite createOrUpdate(@RequestBody Invite invite) {
|
||||
if (invite.getItem() == null || inviteMappingManager.getByItem(invite.getItem()) == null) {
|
||||
if (invite.getItem() == null || inviteMappingManager.getByItemAndQuota(invite.getItem(),
|
||||
invite.getQuota()) == null) {
|
||||
throw new EntityResponseStatusException(HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
|
@ -29,6 +29,7 @@ public class Invite implements UserData {
|
||||
private Long owner;
|
||||
private String code;
|
||||
private Integer item;
|
||||
private String quota;
|
||||
private Instant starts;
|
||||
private Instant expires;
|
||||
@Lob
|
||||
@ -36,6 +37,7 @@ public class Invite implements UserData {
|
||||
private String note;
|
||||
private boolean redeemed;
|
||||
private String codeLink;
|
||||
private String url;
|
||||
|
||||
/**
|
||||
* Gets the id.
|
||||
@ -109,6 +111,20 @@ public class Invite implements UserData {
|
||||
this.item = item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the quota
|
||||
*/
|
||||
public String getQuota() {
|
||||
return quota;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param quota the quota to set
|
||||
*/
|
||||
public void setQuota(String quota) {
|
||||
this.quota = quota;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the starts.
|
||||
*
|
||||
@ -216,4 +232,19 @@ public class Invite implements UserData {
|
||||
public void setCodeLink(String codeLink) {
|
||||
this.codeLink = codeLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the url
|
||||
*/
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url the url to set
|
||||
*/
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ public class InviteMapping {
|
||||
@Column(name = "message_limit")
|
||||
private Integer messageLimit;
|
||||
private String codeLink;
|
||||
private String url;
|
||||
private String defaultMessage;
|
||||
|
||||
/**
|
||||
@ -137,6 +138,20 @@ public class InviteMapping {
|
||||
this.codeLink = codeLink;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the url
|
||||
*/
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param url the url to set
|
||||
*/
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the default message.
|
||||
*
|
||||
|
Loading…
Reference in New Issue
Block a user