update invite, keep public key

This commit is contained in:
_Bastler 2022-02-05 10:37:49 +01:00
parent e3a437bd76
commit 1db9257889
12 changed files with 142 additions and 57 deletions

View File

@ -42,7 +42,7 @@ public class PermissionManager implements UserDataProvider {
* Gets the. * Gets the.
* *
* @param target the target * @param target the target
* @param name the name * @param name the name
* @return the list * @return the list
*/ */
public List<Permission> get(Long target, String name) { public List<Permission> get(Long target, String name) {
@ -126,7 +126,7 @@ public class PermissionManager implements UserDataProvider {
* Checks for permission. * Checks for permission.
* *
* @param target the target * @param target the target
* @param name the name * @param name the name
* @return true, if successful * @return true, if successful
*/ */
public boolean hasPermission(Long target, String name) { public boolean hasPermission(Long target, String name) {
@ -143,10 +143,10 @@ public class PermissionManager implements UserDataProvider {
/** /**
* Creates the. * Creates the.
* *
* @param target the target * @param target the target
* @param name the name * @param name the name
* @param addon the addon * @param addon the addon
* @param starts the starts * @param starts the starts
* @param expires the expires * @param expires the expires
* @return the permission * @return the permission
*/ */
@ -188,7 +188,7 @@ public class PermissionManager implements UserDataProvider {
/** /**
* Clone. * Clone.
* *
* @param name the name * @param name the name
* @param clone the clone * @param clone the clone
* @return the list * @return the list
*/ */
@ -212,7 +212,7 @@ public class PermissionManager implements UserDataProvider {
* Delete. * Delete.
* *
* @param target the target * @param target the target
* @param name the name * @param name the name
*/ */
public void delete(Long target, String name) { public void delete(Long target, String name) {
Assert.isTrue( Assert.isTrue(
@ -249,10 +249,10 @@ public class PermissionManager implements UserDataProvider {
/** /**
* Apply item. * Apply item.
* *
* @param target the target * @param target the target
* @param item the item * @param item the item
* @param answers the answers * @param answers the answers
* @param starts the starts * @param starts the starts
* @param expires the expires * @param expires the expires
*/ */
public void applyItem(Long target, Integer item, JsonArray answers, Instant starts, public void applyItem(Long target, Integer item, JsonArray answers, Instant starts,
@ -265,10 +265,10 @@ public class PermissionManager implements UserDataProvider {
/** /**
* Gets the for item. * Gets the for item.
* *
* @param target the target * @param target the target
* @param item the item * @param item the item
* @param answers the answers * @param answers the answers
* @param starts the starts * @param starts the starts
* @param expires the expires * @param expires the expires
* @return the for item * @return the for item
*/ */

View File

@ -78,14 +78,16 @@ public class PermissionMappingManager {
/** /**
* Creates the. * Creates the.
* *
* @param item the item * @param item the item
* @param names the names * @param names the names
* @param lifetime the lifetime * @param lifetime the lifetime
* @param lifetimeUnit the lifetime unit * @param lifetimeUnit the lifetime unit
* @param lifetimeRound the lifetime round * @param lifetimeRound the lifetime round
* @param addon the addon * @param addon the addon
* @param product the product * @param product the product
* @param startsQuestion the starts question * @param starts the starts
* @param expires the expires
* @param startsQuestion the starts question
* @param expiresQuestion the expires question * @param expiresQuestion the expires question
* @return the permission mapping * @return the permission mapping
*/ */
@ -140,9 +142,9 @@ public class PermissionMappingManager {
/** /**
* Gets the. * Gets the.
* *
* @param page the page * @param page the page
* @param size the size * @param size the size
* @param sortBy the sort by * @param sortBy the sort by
* @param descending the descending * @param descending the descending
* @return the page * @return the page
*/ */

View File

@ -42,7 +42,7 @@ public class SystemPropertyManager {
/** /**
* Gets the. * Gets the.
* *
* @param key the key * @param key the key
* @param defaultValue the default value * @param defaultValue the default value
* @return the string * @return the string
*/ */
@ -64,7 +64,7 @@ public class SystemPropertyManager {
/** /**
* Gets the boolean. * Gets the boolean.
* *
* @param key the key * @param key the key
* @param defaultValue the default value * @param defaultValue the default value
* @return the boolean * @return the boolean
*/ */
@ -85,7 +85,7 @@ public class SystemPropertyManager {
/** /**
* Gets the integer. * Gets the integer.
* *
* @param key the key * @param key the key
* @param defaultValue the default value * @param defaultValue the default value
* @return the integer * @return the integer
*/ */
@ -106,7 +106,7 @@ public class SystemPropertyManager {
/** /**
* Gets the long. * Gets the long.
* *
* @param key the key * @param key the key
* @param defaultValue the default value * @param defaultValue the default value
* @return the long * @return the long
*/ */
@ -117,7 +117,7 @@ public class SystemPropertyManager {
/** /**
* Adds the. * Adds the.
* *
* @param key the key * @param key the key
* @param value the value * @param value the value
*/ */
public void add(String key, String value) { public void add(String key, String value) {
@ -129,7 +129,7 @@ public class SystemPropertyManager {
/** /**
* Update. * Update.
* *
* @param key the key * @param key the key
* @param value the value * @param value the value
*/ */
public void update(String key, String value) { public void update(String key, String value) {
@ -140,6 +140,12 @@ public class SystemPropertyManager {
systemPropertyRepository.save(systemProperty); systemPropertyRepository.save(systemProperty);
} }
/**
* Sets the.
*
* @param key the key
* @param value the value
*/
public void set(String key, String value) { public void set(String key, String value) {
if (systemPropertyRepository.existsById(key)) { if (systemPropertyRepository.existsById(key)) {
update(key, value); update(key, value);

View File

@ -106,7 +106,7 @@ public class UserManager implements UserDataProvider {
/** /**
* Sets the password. * Sets the password.
* *
* @param id the id * @param id the id
* @param password the password * @param password the password
* @return the user * @return the user
*/ */
@ -124,7 +124,7 @@ public class UserManager implements UserDataProvider {
* *
* @param username the username * @param username the username
* @param password the password * @param password the password
* @param status the status * @param status the status
* @return the user * @return the user
*/ */
public User create(String username, String password, UserStatus status) { public User create(String username, String password, UserStatus status) {
@ -148,9 +148,9 @@ public class UserManager implements UserDataProvider {
/** /**
* Gets the. * Gets the.
* *
* @param page the page * @param page the page
* @param size the size * @param size the size
* @param sortBy the sort by * @param sortBy the sort by
* @param descending the descending * @param descending the descending
* @return the page * @return the page
*/ */
@ -218,7 +218,7 @@ public class UserManager implements UserDataProvider {
/** /**
* Write public key. * Write public key.
* *
* @param username the username * @param username the username
* @param publicKey the public key * @param publicKey the public key
*/ */
public void writePublicKey(String username, String publicKey) { public void writePublicKey(String username, String publicKey) {
@ -263,10 +263,11 @@ public class UserManager implements UserDataProvider {
/** /**
* Password reset. * Password reset.
* *
* @param user the user * @param user the user
* @param outputStream the output stream * @param outputStream the output stream
*/ */
public void passwordReset(User user, ServletOutputStream outputStream) { public void passwordReset(User user, ServletOutputStream outputStream) {
// TODO: change to public key profile field
String resetToken = RandomStringUtils.random(64, true, true); String resetToken = RandomStringUtils.random(64, true, true);
String command = "echo \"" String command = "echo \""
+ resetToken + resetToken

View File

@ -100,7 +100,11 @@ public class UserProfileFieldManager implements UserDataProvider {
Assert.isTrue( Assert.isTrue(
userProfileFieldRepository.exists( userProfileFieldRepository.exists(
qUserProfileField.target.eq(target).and(qUserProfileField.name.eq(name))), qUserProfileField.target.eq(target).and(qUserProfileField.name.eq(name))),
"ProfileField '" + name + "' for target + '" + target + "' not exists!"); "ProfileField '"
+ name
+ "' for target + '"
+ target
+ "' not exists!");
UserProfileField delete = userProfileFieldRepository UserProfileField delete = userProfileFieldRepository
.findOne(qUserProfileField.target.eq(target).and(qUserProfileField.name.eq(name))) .findOne(qUserProfileField.target.eq(target).and(qUserProfileField.name.eq(name)))
.get(); .get();
@ -143,7 +147,9 @@ public class UserProfileFieldManager implements UserDataProvider {
@Override @Override
public void purgeUserData(Long userId) { public void purgeUserData(Long userId) {
for (UserProfileField userProfileField : getAllByTarget(userId)) { for (UserProfileField userProfileField : getAllByTarget(userId)) {
userProfileFieldRepository.delete(userProfileField); if (!userProfileField.getName().equals(UserProfileFields.PROFILE_FIELD_PUBLIC_KEY)) {
userProfileFieldRepository.delete(userProfileField);
}
} }
} }

View File

@ -9,6 +9,7 @@ package de.bstly.we.businesslogic;
public interface UserProfileFields { public interface UserProfileFields {
public static final String PROFILE_FIELD_EMAIL = "email"; public static final String PROFILE_FIELD_EMAIL = "email";
public static final String PROFILE_FIELD_PUBLIC_KEY = "publicKey";
public static final String PROFILE_FIELD_EMAIL_PRIMARY = "primaryEmail"; public static final String PROFILE_FIELD_EMAIL_PRIMARY = "primaryEmail";
public static final String PROFILE_FIELD_LOCALE = "locale"; public static final String PROFILE_FIELD_LOCALE = "locale";
} }

View File

@ -103,10 +103,10 @@ public class PermissionManagementController extends BaseController {
} }
/** /**
* Update permission. * Update permissions.
* *
* @param permission the permission * @param permissions the permissions
* @return the permission * @return the list
*/ */
@PreAuthorize("hasRole('ROLE_ADMIN')") @PreAuthorize("hasRole('ROLE_ADMIN')")
@PatchMapping("list") @PatchMapping("list")
@ -125,7 +125,7 @@ public class PermissionManagementController extends BaseController {
/** /**
* Clone. * Clone.
* *
* @param name the name * @param name the name
* @param clone the clone * @param clone the clone
* @return the list * @return the list
*/ */

View File

@ -206,6 +206,8 @@ public class PermissionMapping {
} }
/** /**
* Gets the starts.
*
* @return the starts * @return the starts
*/ */
public Instant getStarts() { public Instant getStarts() {
@ -213,13 +215,17 @@ public class PermissionMapping {
} }
/** /**
* @param starts the starts to set * Sets the starts.
*
* @param starts the new starts
*/ */
public void setStarts(Instant starts) { public void setStarts(Instant starts) {
this.starts = starts; this.starts = starts;
} }
/** /**
* Gets the expires.
*
* @return the expires * @return the expires
*/ */
public Instant getExpires() { public Instant getExpires() {
@ -227,7 +233,9 @@ public class PermissionMapping {
} }
/** /**
* @param expires the expires to set * Sets the expires.
*
* @param expires the new expires
*/ */
public void setExpires(Instant expires) { public void setExpires(Instant expires) {
this.expires = expires; this.expires = expires;
@ -275,6 +283,9 @@ public class PermissionMapping {
@Converter @Converter
public static class ChronoUnitConverter implements AttributeConverter<ChronoUnit, String> { public static class ChronoUnitConverter implements AttributeConverter<ChronoUnit, String> {
/*
* @see javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.Object)
*/
/* /*
* @see javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang. * @see javax.persistence.AttributeConverter#convertToDatabaseColumn(java.lang.
* Object) * Object)
@ -284,6 +295,9 @@ public class PermissionMapping {
return chronoUnit.name(); return chronoUnit.name();
} }
/*
* @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.Object)
*/
/* /*
* @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang. * @see javax.persistence.AttributeConverter#convertToEntityAttribute(java.lang.
* Object) * Object)

View File

@ -46,7 +46,7 @@ public class InviteMappingManager {
/** /**
* Gets the by item and quota. * Gets the by item and quota.
* *
* @param item the item * @param item the item
* @param quota the quota * @param quota the quota
* @return the by item and quota * @return the by item and quota
*/ */
@ -59,9 +59,9 @@ public class InviteMappingManager {
/** /**
* Gets the. * Gets the.
* *
* @param page the page * @param page the page
* @param size the size * @param size the size
* @param sortBy the sort by * @param sortBy the sort by
* @param descending the descending * @param descending the descending
* @return the page * @return the page
*/ */
@ -73,9 +73,9 @@ public class InviteMappingManager {
/** /**
* Creates the. * Creates the.
* *
* @param quota the quota * @param quota the quota
* @param item the item * @param item the item
* @param starts the starts * @param starts the starts
* @param expires the expires * @param expires the expires
* @return the invite mapping * @return the invite mapping
*/ */

View File

@ -92,10 +92,12 @@ public class InviteController extends BaseController {
invite.setItem(null); invite.setItem(null);
invite.setNote(null); invite.setNote(null);
invite.setOwner(null); invite.setOwner(null);
invite.setRedeemedBy(null);
} else if (!getCurrentUserId().equals(invite.getOwner())) { } else if (!getCurrentUserId().equals(invite.getOwner())) {
invite.setCode(null); invite.setCode(null);
invite.setCodeLink(null); invite.setCodeLink(null);
invite.setOwner(null); invite.setOwner(null);
invite.setRedeemedBy(null);
if (!permissionManager.hasPermission(getCurrentUserId(), Permissions.ROLE_MEMBER)) { if (!permissionManager.hasPermission(getCurrentUserId(), Permissions.ROLE_MEMBER)) {
invite.setNote(null); invite.setNote(null);
} }
@ -201,6 +203,8 @@ public class InviteController extends BaseController {
User user = userManager.create(userModel.getUsername(), userModel.getPassword(), User user = userManager.create(userModel.getUsername(), userModel.getPassword(),
userModel.getStatus()); userModel.getStatus());
Long userId = user.getId();
for (UserProfileField userProfileField : userModel.getProfileFields()) { for (UserProfileField userProfileField : userModel.getProfileFields()) {
userProfileField.setTarget(user.getId()); userProfileField.setTarget(user.getId());
if (userProfileField.getType() == null) { if (userProfileField.getType() == null) {
@ -214,11 +218,42 @@ public class InviteController extends BaseController {
userProfileField = userProfileFieldManager.save(userProfileField); userProfileField = userProfileFieldManager.save(userProfileField);
} }
permissionManager.applyItem(user.getId(), invite.getItem(), new JsonArray(), permissionManager.applyItem(userId, invite.getItem(), new JsonArray(), invite.getStarts(),
invite.getStarts(), invite.getExpires()); invite.getExpires());
quotaManager.applyItem(user.getId(), invite.getItem()); quotaManager.applyItem(userId, invite.getItem());
invite.setRedeemed(true); invite.setRedeemed(true);
invite.setRedeemedBy(userId);
inviteManager.save(invite);
}
/**
* Redeem.
*
* @param code the code
*/
@PreAuthorize("isAuthenticated()")
@PostMapping("redeem")
public void redeem(@RequestBody String code) {
Invite invite = inviteManager.getByCode(code);
if (invite == null) {
throw new EntityResponseStatusException(HttpStatus.NOT_ACCEPTABLE);
}
if (invite.isRedeemed()
|| invite.getExpires() != null && invite.getExpires().isBefore(Instant.now())) {
throw new EntityResponseStatusException(HttpStatus.GONE);
}
Long userId = getCurrentUserId();
permissionManager.applyItem(userId, invite.getItem(), new JsonArray(), invite.getStarts(),
invite.getExpires());
quotaManager.applyItem(userId, invite.getItem());
invite.setRedeemed(true);
invite.setRedeemedBy(userId);
inviteManager.save(invite); inviteManager.save(invite);
} }
@ -283,6 +318,7 @@ public class InviteController extends BaseController {
invite.setCodeLink(null); invite.setCodeLink(null);
invite.setMessage(null); invite.setMessage(null);
invite.setOwner(null); invite.setOwner(null);
invite.setRedeemedBy(null);
} }
return page; return page;

View File

@ -36,6 +36,7 @@ public class Invite implements UserData {
private String message; private String message;
private String note; private String note;
private boolean redeemed; private boolean redeemed;
private Long redeemedBy;
private String codeLink; private String codeLink;
private String url; private String url;
@ -219,6 +220,24 @@ public class Invite implements UserData {
this.redeemed = redeemed; this.redeemed = redeemed;
} }
/**
* Gets the redeemed by.
*
* @return the redeemed by
*/
public Long getRedeemedBy() {
return redeemedBy;
}
/**
* Sets the redeemed by.
*
* @param redeemedBy the new redeemed by
*/
public void setRedeemedBy(Long redeemedBy) {
this.redeemedBy = redeemedBy;
}
/** /**
* Gets the code link. * Gets the code link.
* *

View File

@ -13,7 +13,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>11</java.version> <java.version>11</java.version>
<log4j2.version>2.17.1</log4j2.version> <log4j2.version>2.17.1</log4j2.version>
<revision>1.6.0-SNAPSHOT</revision> <revision>1.6.1-SNAPSHOT</revision>
</properties> </properties>
<parent> <parent>