diff --git a/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowItemManager.java b/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowItemManager.java index d27db38..2f49ab7 100644 --- a/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowItemManager.java +++ b/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowItemManager.java @@ -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); } /** diff --git a/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowRequestManager.java b/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowRequestManager.java index 7938261..c48eab8 100644 --- a/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowRequestManager.java +++ b/borrow/src/main/java/de/bstly/we/borrow/businesslogic/BorrowRequestManager.java @@ -87,7 +87,7 @@ public class BorrowRequestManager { * @param descending the descending * @return the for user and stauts */ - public Page getForUserAndStauts(Long userId, BorrowRequestStatus status, + public Page 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)), diff --git a/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowItemController.java b/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowItemController.java index 34f3583..26ed15d 100644 --- a/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowItemController.java +++ b/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowItemController.java @@ -59,56 +59,30 @@ public class BorrowItemController extends BaseController { @RequestParam("size") Optional sizeParameter, @RequestParam("sort") Optional sortParameter, @RequestParam("desc") Optional descParameter, - @RequestParam("search") Optional searchParameter) { + @RequestParam("search") Optional searchParameter, + @RequestParam("owner") Optional ownerParameter) { - if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_REQUESTS) - || !permissionManager.isFullUser(getCurrentUserId())) { - throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); - } + Page borrowItems; - Page 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); + if (ownerParameter.isPresent() && ownerParameter.get().booleanValue()) { + if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_ITEMS) + || !permissionManager.isFullUser(getCurrentUserId())) { + throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); } - borrowItemManager.applySlots(borrowItem); + 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)); } - 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 getOwnerBorrowItems( - @RequestParam("page") Optional pageParameter, - @RequestParam("size") Optional sizeParameter, - @RequestParam("sort") Optional sortParameter, - @RequestParam("desc") Optional descParameter, - @RequestParam("search") Optional searchParameter) { - - if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_ITEMS) - || !permissionManager.isFullUser(getCurrentUserId())) { - throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); - } - - Page borrowItems = borrowItemManager.getForUser(getCurrentUserId(), - 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); @@ -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())) { diff --git a/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowRequestController.java b/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowRequestController.java index a2ae5da..f762639 100644 --- a/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowRequestController.java +++ b/borrow/src/main/java/de/bstly/we/borrow/controller/BorrowRequestController.java @@ -78,53 +78,30 @@ public class BorrowRequestController extends BaseController { @RequestParam("page") Optional pageParameter, @RequestParam("size") Optional sizeParameter, @RequestParam("sort") Optional sortParameter, - @RequestParam("desc") Optional descParameter) { + @RequestParam("desc") Optional descParameter, + @RequestParam("owner") Optional ownerParameter) { - if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_REQUESTS) - || !permissionManager.isFullUser(getCurrentUserId())) { - throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); + Page borrowRequests; + + if (ownerParameter.isPresent() && ownerParameter.get().booleanValue()) { + if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_ITEMS) + || !permissionManager.isFullUser(getCurrentUserId())) { + throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); + } + 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)); } - Page 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 getOwnerBorrowRequests( - @RequestParam("page") Optional pageParameter, - @RequestParam("size") Optional sizeParameter, - @RequestParam("sort") Optional sortParameter, - @RequestParam("desc") Optional descParameter) { - - if (!permissionManager.hasPermission(getCurrentUserId(), BorrowPermissions.BORROW_ITEMS) - || !permissionManager.isFullUser(getCurrentUserId())) { - throw new EntityResponseStatusException(HttpStatus.FORBIDDEN); - } - - Page borrowRequests = borrowRequestManager.getForOwner(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); @@ -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); } } } diff --git a/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowJwtValidator.java b/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowJwtValidator.java index 395403e..c28edef 100644 --- a/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowJwtValidator.java +++ b/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowJwtValidator.java @@ -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 { diff --git a/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowRequestValidator.java b/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowRequestValidator.java index 37d6f37..730242b 100644 --- a/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowRequestValidator.java +++ b/borrow/src/main/java/de/bstly/we/borrow/controller/validation/BorrowRequestValidator.java @@ -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) { diff --git a/borrow/src/main/java/de/bstly/we/borrow/model/BorrowItemPeriodSlot.java b/borrow/src/main/java/de/bstly/we/borrow/model/BorrowItemPeriodSlot.java index fa00a96..cdcd093 100644 --- a/borrow/src/main/java/de/bstly/we/borrow/model/BorrowItemPeriodSlot.java +++ b/borrow/src/main/java/de/bstly/we/borrow/model/BorrowItemPeriodSlot.java @@ -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; } diff --git a/core/src/main/java/de/bstly/we/businesslogic/PermissionManager.java b/core/src/main/java/de/bstly/we/businesslogic/PermissionManager.java index d6a4c7f..86bdc3c 100755 --- a/core/src/main/java/de/bstly/we/businesslogic/PermissionManager.java +++ b/core/src/main/java/de/bstly/we/businesslogic/PermissionManager.java @@ -42,7 +42,7 @@ public class PermissionManager implements UserDataProvider { * Gets the. * * @param target the target - * @param name the name + * @param name the name * @return the list */ public List get(Long target, String name) { @@ -126,7 +126,7 @@ public class PermissionManager implements UserDataProvider { * Checks for permission. * * @param target the target - * @param name the name + * @param name the name * @return true, if successful */ public boolean hasPermission(Long target, String name) { @@ -143,10 +143,10 @@ public class PermissionManager implements UserDataProvider { /** * Creates the. * - * @param target the target - * @param name the name - * @param addon the addon - * @param starts the starts + * @param target the target + * @param name the name + * @param addon the addon + * @param starts the starts * @param expires the expires * @return the permission */ @@ -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(); @@ -185,7 +188,7 @@ public class PermissionManager implements UserDataProvider { /** * Clone. * - * @param name the name + * @param name the name * @param clone the clone * @return the list */ @@ -209,13 +212,17 @@ public class PermissionManager implements UserDataProvider { * Delete. * * @param target the target - * @param name the name + * @param name the name */ public void delete(Long target, String name) { 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); @@ -242,13 +249,14 @@ public class PermissionManager implements UserDataProvider { /** * Apply item. * - * @param target the target - * @param item the item + * @param target the target + * @param item the item * @param answers the answers - * @param start the start + * @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); } } @@ -256,25 +264,25 @@ public class PermissionManager implements UserDataProvider { /** * Gets the for item. * - * @param target the target - * @param item the item + * @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 getForItem(Long target, Integer item, JsonArray answers, - Instant start) { + public List getForItem(Long target, Integer item, JsonArray answers, Instant starts, + Instant expires) { List 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())); diff --git a/core/src/main/java/de/bstly/we/businesslogic/QuotaManager.java b/core/src/main/java/de/bstly/we/businesslogic/QuotaManager.java index b807938..a90ff6c 100644 --- a/core/src/main/java/de/bstly/we/businesslogic/QuotaManager.java +++ b/core/src/main/java/de/bstly/we/businesslogic/QuotaManager.java @@ -33,7 +33,7 @@ public class QuotaManager implements UserDataProvider { * Gets the. * * @param target the target - * @param name the name + * @param name the name * @return the quota */ public Quota get(Long target, String name) { @@ -85,7 +85,7 @@ public class QuotaManager implements UserDataProvider { * Checks for quota. * * @param target the target - * @param name the name + * @param name the name * @return true, if successful */ public boolean hasQuota(Long target, String name) { @@ -96,10 +96,10 @@ public class QuotaManager implements UserDataProvider { /** * Creates the. * - * @param target the target - * @param name the name - * @param value the value - * @param unit the unit + * @param target the target + * @param name the name + * @param value the value + * @param unit the unit * @param disposable the disposable * @return the quota */ @@ -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()))) @@ -138,7 +141,7 @@ public class QuotaManager implements UserDataProvider { /** * Clone. * - * @param name the name + * @param name the name * @param clone the clone * @param value the value * @return the list @@ -161,11 +164,15 @@ public class QuotaManager implements UserDataProvider { * Delete. * * @param target the target - * @param name the name + * @param name the name */ 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,11 +196,52 @@ 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 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. * * @param target the target - * @param item the item + * @param item the item */ public void applyItem(Long target, Integer item) { for (QuotaMapping quotaMapping : quotaMappingManager.getAllByItem(item)) { diff --git a/core/src/main/java/de/bstly/we/controller/QuotaController.java b/core/src/main/java/de/bstly/we/controller/QuotaController.java index 85c1b96..8e93b23 100644 --- a/core/src/main/java/de/bstly/we/controller/QuotaController.java +++ b/core/src/main/java/de/bstly/we/controller/QuotaController.java @@ -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 getNewQuotas(HttpSession session) { List 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; diff --git a/core/src/main/java/de/bstly/we/controller/support/TokenSessionManager.java b/core/src/main/java/de/bstly/we/controller/support/TokenSessionManager.java index 29ee8fa..bda0f2f 100644 --- a/core/src/main/java/de/bstly/we/controller/support/TokenSessionManager.java +++ b/core/src/main/java/de/bstly/we/controller/support/TokenSessionManager.java @@ -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; @@ -54,7 +55,7 @@ public class TokenSessionManager { * Gets the permission mappings for token. * * @param userId the user id - * @param token the token + * @param token the token * @return the permission mappings for token */ public List getPermissionMappingsForToken(Long userId, String token) { @@ -78,7 +79,7 @@ public class TokenSessionManager { * Gets the permissions for token. * * @param userId the user id - * @param token the token + * @param token the token * @return the permissions for token */ public List getPermissionsForToken(Long userId, String token) { @@ -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 @@ -112,7 +113,7 @@ public class TokenSessionManager { * Gets the quota mappings for token. * * @param userId the user id - * @param token the token + * @param token the token * @return the quota mappings for token */ public List getQuotaMappingsForToken(Long userId, String token) { @@ -132,6 +133,20 @@ public class TokenSessionManager { return quotaMappings; } + public void addQuotasForToken(Long userId, String token, List 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)); @@ -200,7 +215,7 @@ public class TokenSessionManager { /** * Adds the token to session. * - * @param secret the secret + * @param secret the secret * @param session the session */ public void addTokenToSession(String secret, HttpSession session) { @@ -214,7 +229,8 @@ public class TokenSessionManager { } if (StringUtils.hasLength(tokens)) { - tokens += "," + secret; + tokens += "," + + secret; } else { tokens = secret; } @@ -226,7 +242,7 @@ public class TokenSessionManager { /** * Removes the token from session. * - * @param secret the secret + * @param secret the secret * @param session the session */ public void removeTokenFromSession(String secret, HttpSession session) { @@ -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; } @@ -261,7 +278,7 @@ public class TokenSessionManager { /** * Creates the new auth. * - * @param auth the auth + * @param auth the auth * @param details the details * @return the authentication */ diff --git a/core/src/main/java/de/bstly/we/model/User.java b/core/src/main/java/de/bstly/we/model/User.java index 97cda11..dbc0324 100755 --- a/core/src/main/java/de/bstly/we/model/User.java +++ b/core/src/main/java/de/bstly/we/model/User.java @@ -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. * diff --git a/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteManager.java b/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteManager.java index 7ec16e0..3eacb93 100644 --- a/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteManager.java +++ b/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteManager.java @@ -63,8 +63,8 @@ public class InviteManager implements UserDataProvider { /** * Gets the. * - * @param page the page - * @param size the size + * @param page the page + * @param size the size * @param search the search * @return the page */ @@ -89,17 +89,17 @@ public class InviteManager implements UserDataProvider { /** * Gets the by owner. * - * @param owner the owner - * @param item the item - * @param page the page - * @param size the size - * @param sortBy the sort by + * @param owner the owner + * @param item the item + * @param page the page + * @param size the size + * @param sortBy the sort by * @param descending the descending - * @param search the search - * @param redeemed the redeemed + * @param search the search + * @param redeemed the redeemed * @return the by owner */ - public Page getByOwner(Long owner, Integer item, int page, int size, String sortBy, + public Page 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)) { @@ -129,20 +129,20 @@ public class InviteManager implements UserDataProvider { /** * Gets the others. * - * @param owner the owner - * @param item the item - * @param page the page - * @param size the size - * @param search the search + * @param owner the owner + * @param item the item + * @param page the page + * @param size the size + * @param search the search * @param redeemed the redeemed * @return the others */ - public Page getOthers(Long owner, int item, int page, int size, String search, + public Page 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); } diff --git a/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteMappingManager.java b/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteMappingManager.java index 7e258de..0fff1cb 100644 --- a/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteMappingManager.java +++ b/invite/src/main/java/de/bstly/we/invite/businesslogic/InviteMappingManager.java @@ -49,16 +49,18 @@ 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); } /** * Gets the. * - * @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 */ @@ -70,9 +72,9 @@ public class InviteMappingManager { /** * Creates the. * - * @param quota the quota - * @param item the item - * @param starts the starts + * @param quota the quota + * @param item the item + * @param starts the starts * @param expires the expires * @return the invite mapping */ @@ -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); diff --git a/invite/src/main/java/de/bstly/we/invite/controller/InviteController.java b/invite/src/main/java/de/bstly/we/invite/controller/InviteController.java index 645a37d..dc79b90 100644 --- a/invite/src/main/java/de/bstly/we/invite/controller/InviteController.java +++ b/invite/src/main/java/de/bstly/we/invite/controller/InviteController.java @@ -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.setOwner(null); - } else { - invite.setId(null); - invite.setItem(null); + invite.setId(null); + invite.setCode(null); + invite.setCodeLink(null); + invite.setOwner(null); + if (!permissionManager.hasPermission(getCurrentUserId(), Permissions.ROLE_MEMBER)) { invite.setNote(null); - invite.setOwner(null); } } return invite; } + @GetMapping("/{code}/permissions") + public List 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 getQuotas(@PathVariable("code") String code) { + Invite invite = inviteManager.getByCode(code); + if (invite == null) { + throw new EntityResponseStatusException(HttpStatus.NOT_ACCEPTABLE); + } + + List 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); @@ -190,12 +215,12 @@ public class InviteController extends BaseController { /** * Gets the invites. * - * @param quotaParameter the quota parameter - * @param pageParameter the page parameter - * @param sizeParameter the size parameter - * @param sortParamater the sort paramater - * @param descParameter the desc parameter - * @param searchParameter the search parameter + * @param quotaParameter the quota parameter + * @param pageParameter the page parameter + * @param sizeParameter the size parameter + * @param sortParamater the sort paramater + * @param descParameter the desc parameter + * @param searchParameter the search parameter * @param redeemedParameter the redeemed parameter * @return the invites */ @@ -208,27 +233,20 @@ public class InviteController extends BaseController { @RequestParam("desc") Optional descParameter, @RequestParam("search") Optional searchParameter, @RequestParam("redeemed") Optional 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)); } /** * Gets the other invites. * - * @param quota the quota - * @param pageParameter the page parameter - * @param sizeParameter the size parameter - * @param searchParameter the search parameter + * @param quota the quota + * @param pageParameter the page parameter + * @param sizeParameter the size parameter + * @param searchParameter the search parameter * @param redeemedParameter the redeemed parameter * @return the other invites */ @@ -247,7 +265,7 @@ public class InviteController extends BaseController { throw new EntityResponseStatusException(HttpStatus.CONFLICT); } - Page page = inviteManager.getOthers(getCurrentUserId(), inviteMapping.getItem(), + Page page = inviteManager.getOthers(getCurrentUserId(), quota, pageParameter.orElse(0), sizeParameter.orElse(10), searchParameter.orElse(null), redeemedParameter.orElse(null)); for (Invite invite : page.getContent()) { @@ -264,7 +282,7 @@ public class InviteController extends BaseController { /** * Creates the invite. * - * @param quota the quota + * @param quota the quota * @param inviteModel the invite model * @return the invite */ @@ -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()) diff --git a/invite/src/main/java/de/bstly/we/invite/controller/InviteManagingController.java b/invite/src/main/java/de/bstly/we/invite/controller/InviteManagingController.java index cdd5d55..8d3232a 100644 --- a/invite/src/main/java/de/bstly/we/invite/controller/InviteManagingController.java +++ b/invite/src/main/java/de/bstly/we/invite/controller/InviteManagingController.java @@ -44,8 +44,8 @@ public class InviteManagingController extends BaseController { /** * Gets the invites. * - * @param page the page - * @param size the size + * @param page the page + * @param size the size * @param search the search * @return the invites */ @@ -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); } diff --git a/invite/src/main/java/de/bstly/we/invite/model/Invite.java b/invite/src/main/java/de/bstly/we/invite/model/Invite.java index 7b0bfda..7bf7bb6 100644 --- a/invite/src/main/java/de/bstly/we/invite/model/Invite.java +++ b/invite/src/main/java/de/bstly/we/invite/model/Invite.java @@ -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; + } + } diff --git a/invite/src/main/java/de/bstly/we/invite/model/InviteMapping.java b/invite/src/main/java/de/bstly/we/invite/model/InviteMapping.java index 3423200..8a12ca1 100644 --- a/invite/src/main/java/de/bstly/we/invite/model/InviteMapping.java +++ b/invite/src/main/java/de/bstly/we/invite/model/InviteMapping.java @@ -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. * diff --git a/pom.xml b/pom.xml index fdf1bad..63af358 100755 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ UTF-8 11 - 1.2.0-SNAPSHOT + 1.3.0-SNAPSHOT