update borrow module + fix invite

This commit is contained in:
2021-10-27 17:07:23 +02:00
parent 3c98987678
commit 0646a21d97
19 changed files with 363 additions and 299 deletions
@@ -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<Permission> 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<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()));
@@ -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<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.
*
* @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)) {
@@ -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;
@@ -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<PermissionMapping> 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<Permission> 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<QuotaMapping> getQuotaMappingsForToken(Long userId, String token) {
@@ -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));
@@ -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
*/
@@ -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.
*