backend tenant specific invite and confirm invite changes

This commit is contained in:
amentis 2024-06-21 14:51:10 +03:00
parent 7a9a338062
commit dfe7681d62
3 changed files with 53 additions and 26 deletions

View File

@ -21,7 +21,8 @@ public class NotificationProperties {
private UUID descriptionTemplateInvitationType; private UUID descriptionTemplateInvitationType;
private UUID contactSupportType; private UUID contactSupportType;
private UUID publicContactSupportType; private UUID publicContactSupportType;
private UUID tenantSpecificInvitationUserType; private UUID tenantSpecificInvitationExternalUserType;
private UUID tenantSpecificInvitationExistingUserType;
private int emailExpirationTimeSeconds; private int emailExpirationTimeSeconds;
private String contactSupportEmail; private String contactSupportEmail;
@ -153,11 +154,19 @@ public class NotificationProperties {
this.descriptionAnnotationCreated = descriptionAnnotationCreated; this.descriptionAnnotationCreated = descriptionAnnotationCreated;
} }
public UUID getTenantSpecificInvitationUserType() { public UUID getTenantSpecificInvitationExternalUserType() {
return tenantSpecificInvitationUserType; return tenantSpecificInvitationExternalUserType;
} }
public void setTenantSpecificInvitationUserType(UUID tenantSpecificInvitationUserType) { public void setTenantSpecificInvitationExternalUserType(UUID tenantSpecificInvitationExternalUserType) {
this.tenantSpecificInvitationUserType = tenantSpecificInvitationUserType; this.tenantSpecificInvitationExternalUserType = tenantSpecificInvitationExternalUserType;
}
public UUID getTenantSpecificInvitationExistingUserType() {
return tenantSpecificInvitationExistingUserType;
}
public void setTenantSpecificInvitationExistingUserType(UUID tenantSpecificInvitationExistingUserType) {
this.tenantSpecificInvitationExistingUserType = tenantSpecificInvitationExistingUserType;
} }
} }

View File

@ -914,19 +914,23 @@ public class UserServiceImpl implements UserService {
public void sendUserToTenantInvitation(UserTenantUsersInviteRequest users) throws InvalidApplicationException, JAXBException { public void sendUserToTenantInvitation(UserTenantUsersInviteRequest users) throws InvalidApplicationException, JAXBException {
String tenantName = null; String tenantName = null;
String tenantCode = null; String tenantCode;
if (this.tenantScope.getTenantCode() != null && !this.tenantScope.getTenantCode().equals(this.tenantScope.getDefaultTenantCode())) { if (this.tenantScope.getTenantCode() != null && !this.tenantScope.getTenantCode().equals(this.tenantScope.getDefaultTenantCode())) {
TenantEntity tenantEntity = this.queryFactory.query(TenantQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).codes(this.tenantScope.getTenantCode()).isActive(IsActive.Active).first(); TenantEntity tenantEntity = this.queryFactory.query(TenantQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).codes(this.tenantScope.getTenantCode()).isActive(IsActive.Active).first();
if (tenantEntity == null) throw new MyApplicationException("Tenant not found"); if (tenantEntity == null) throw new MyApplicationException("Tenant not found");
tenantName = tenantEntity.getName(); tenantName = tenantEntity.getName();
tenantCode = tenantEntity.getCode(); tenantCode = tenantEntity.getCode();
} else { } else {
tenantName = "OpenCDMP";
tenantCode = this.tenantScope.getDefaultTenantCode(); tenantCode = this.tenantScope.getDefaultTenantCode();
} }
for (UserInviteToTenantRequestPersist user: users.getUsers()) { for (UserInviteToTenantRequestPersist user: users.getUsers()) {
String token = this.createUserInviteToTenantConfirmation(user, tenantCode); String token = this.createUserInviteToTenantConfirmation(user, tenantCode);
this.createTenantSpecificInvitationUserNotificationEvent(token, user.getEmail(), tenantName); UserContactInfoEntity contactInfoEntity = this.queryFactory.query(UserContactInfoQuery.class).disableTracking().values(user.getEmail()).types(ContactInfoType.Email).first();
if (contactInfoEntity != null){
this.createTenantSpecificInvitationUserNotificationEvent(token, user.getEmail(), tenantName, contactInfoEntity.getUserId());
} else {
this.createTenantSpecificInvitationUserNotificationEvent(token, user.getEmail(), tenantName, null);
}
} }
} }
@ -952,12 +956,13 @@ public class UserServiceImpl implements UserService {
return persist.getToken(); return persist.getToken();
} }
private void createTenantSpecificInvitationUserNotificationEvent(String token, String email, String tenantName) throws InvalidApplicationException { private void createTenantSpecificInvitationUserNotificationEvent(String token, String email, String tenantName, UUID existingRecipient) throws InvalidApplicationException {
UserEntity currentUser = this.entityManager.find(UserEntity.class, this.userScope.getUserIdSafe()); UserEntity sender = this.entityManager.find(UserEntity.class, this.userScope.getUserIdSafe());
if (currentUser == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{ this.userScope.getUserIdSafe(), User.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (sender == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{ this.userScope.getUserIdSafe(), User.class.getSimpleName()}, LocaleContextHolder.getLocale()));
NotifyIntegrationEvent event = new NotifyIntegrationEvent(); NotifyIntegrationEvent event = new NotifyIntegrationEvent();
if (existingRecipient == null) {
List<ContactPair> contactPairs = new ArrayList<>(); List<ContactPair> contactPairs = new ArrayList<>();
contactPairs.add(new ContactPair(ContactInfoType.Email, email)); contactPairs.add(new ContactPair(ContactInfoType.Email, email));
@ -965,13 +970,18 @@ public class UserServiceImpl implements UserService {
event.setContactHint(this.jsonHandlingService.toJsonSafe(contactData)); event.setContactHint(this.jsonHandlingService.toJsonSafe(contactData));
event.setContactTypeHint(NotificationContactType.EMAIL); event.setContactTypeHint(NotificationContactType.EMAIL);
event.setNotificationType(this.notificationProperties.getTenantSpecificInvitationUserType()); event.setNotificationType(this.notificationProperties.getTenantSpecificInvitationExternalUserType());
} else {
event.setUserId(existingRecipient);
event.setNotificationType(this.notificationProperties.getTenantSpecificInvitationExistingUserType());
}
NotificationFieldData data = new NotificationFieldData(); NotificationFieldData data = new NotificationFieldData();
List<FieldInfo> fieldInfoList = new ArrayList<>(); List<FieldInfo> fieldInfoList = new ArrayList<>();
fieldInfoList.add(new FieldInfo("{userName}", DataType.String, currentUser.getName())); fieldInfoList.add(new FieldInfo("{userName}", DataType.String, sender.getName()));
fieldInfoList.add(new FieldInfo("{confirmationToken}", DataType.String, token)); fieldInfoList.add(new FieldInfo("{confirmationToken}", DataType.String, token));
fieldInfoList.add(new FieldInfo("{expiration_time}", DataType.String, this.secondsToTime(this.notificationProperties.getEmailExpirationTimeSeconds()))); fieldInfoList.add(new FieldInfo("{expiration_time}", DataType.String, this.secondsToTime(this.notificationProperties.getEmailExpirationTimeSeconds())));
fieldInfoList.add(new FieldInfo("{tenantName}", DataType.String, tenantName)); if (!this.conventionService.isNullOrEmpty(tenantName)) fieldInfoList.add(new FieldInfo("{tenantName}", DataType.String, tenantName));
data.setFields(fieldInfoList); data.setFields(fieldInfoList);
event.setData(this.jsonHandlingService.toJsonSafe(data)); event.setData(this.jsonHandlingService.toJsonSafe(data));
@ -991,10 +1001,14 @@ public class UserServiceImpl implements UserService {
this.checkActionState(action); this.checkActionState(action);
UserInviteToTenantRequestEntity userInviteToTenantRequest = this.xmlHandlingService.fromXmlSafe(UserInviteToTenantRequestEntity.class, action.getData()); UserInviteToTenantRequestEntity userInviteToTenantRequest = this.xmlHandlingService.fromXmlSafe(UserInviteToTenantRequestEntity.class, action.getData());
if (userInviteToTenantRequest == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{action.getId(), UserInviteToTenantRequestEntity.class.getSimpleName()}, LocaleContextHolder.getLocale())); if (userInviteToTenantRequest == null)
throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{action.getId(), UserInviteToTenantRequestEntity.class.getSimpleName()}, LocaleContextHolder.getLocale()));
TenantEntity tenantEntity = this.queryFactory.query(TenantQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).codes(userInviteToTenantRequest.getTenantCode()).isActive(IsActive.Active).first(); TenantEntity tenantEntity = null;
if (!userInviteToTenantRequest.getTenantCode().equals(this.tenantScope.getTenantCode())) {
tenantEntity = this.queryFactory.query(TenantQuery.class).disableTracking().authorize(AuthorizationFlags.AllExceptPublic).codes(userInviteToTenantRequest.getTenantCode()).isActive(IsActive.Active).first();
if (tenantEntity == null) throw new MyApplicationException("Tenant not found"); if (tenantEntity == null) throw new MyApplicationException("Tenant not found");
}
this.addUserToTenant(tenantEntity, userInviteToTenantRequest); this.addUserToTenant(tenantEntity, userInviteToTenantRequest);
} }
@ -1018,7 +1032,8 @@ public class UserServiceImpl implements UserService {
tenantUserEntity.setId(UUID.randomUUID()); tenantUserEntity.setId(UUID.randomUUID());
tenantUserEntity.setUserId(userId); tenantUserEntity.setUserId(userId);
tenantUserEntity.setIsActive(IsActive.Active); tenantUserEntity.setIsActive(IsActive.Active);
tenantUserEntity.setTenantId(tenant.getId()); if (tenant != null) tenantUserEntity.setTenantId(tenant.getId());
else tenantUserEntity.setTenantId(null);
tenantUserEntity.setCreatedAt(Instant.now()); tenantUserEntity.setCreatedAt(Instant.now());
tenantUserEntity.setUpdatedAt(Instant.now()); tenantUserEntity.setUpdatedAt(Instant.now());
this.entityManager.persist(tenantUserEntity); this.entityManager.persist(tenantUserEntity);
@ -1028,7 +1043,8 @@ public class UserServiceImpl implements UserService {
UserRoleEntity item = new UserRoleEntity(); UserRoleEntity item = new UserRoleEntity();
item.setId(UUID.randomUUID()); item.setId(UUID.randomUUID());
item.setUserId(userId); item.setUserId(userId);
item.setTenantId(tenant.getId()); if (tenant != null) item.setTenantId(tenant.getId());
else item.setTenantId(null);
item.setRole(role); item.setRole(role);
item.setCreatedAt(Instant.now()); item.setCreatedAt(Instant.now());
this.entityManager.persist(item); this.entityManager.persist(item);
@ -1043,7 +1059,8 @@ public class UserServiceImpl implements UserService {
this.entityManager.flush(); this.entityManager.flush();
for (String role: userInviteToTenantRequest.getRoles()) { for (String role: userInviteToTenantRequest.getRoles()) {
this.keycloakService.addUserToTenantRoleGroup(userCredential.getExternalId(), tenant.getCode(), role); if (tenant != null && !this.conventionService.isNullOrEmpty(tenant.getCode())) this.keycloakService.addUserToTenantRoleGroup(userCredential.getExternalId(), tenant.getCode(), role);
else this.keycloakService.addUserToTenantRoleGroup(userCredential.getExternalId(), tenantScope.getDefaultTenantCode(), role);
} }
} }

View File

@ -13,5 +13,6 @@ notification:
descriptionTemplateInvitationType: 223BB607-EFA1-4CE7-99EC-4BEABFEF9A8B descriptionTemplateInvitationType: 223BB607-EFA1-4CE7-99EC-4BEABFEF9A8B
contactSupportType: 5B1D6C52-88F9-418B-9B8A-6F1F963D9EAD contactSupportType: 5B1D6C52-88F9-418B-9B8A-6F1F963D9EAD
publicContactSupportType: B542B606-ACC6-4629-ADEF-4D8EE2F01222 publicContactSupportType: B542B606-ACC6-4629-ADEF-4D8EE2F01222
tenantSpecificInvitationUserType: 497dada5-eccc-4bc0-9e0b-63e22b4eb0be tenantSpecificInvitationExternalUserType: 497dada5-eccc-4bc0-9e0b-63e22b4eb0be
tenantSpecificInvitationExistingUserType: b3809c17-d1e4-420a-919c-828564114191
contactSupportEmail: support@dmp.com contactSupportEmail: support@dmp.com