2024-02-12 10:49:37 +01:00
|
|
|
package gr.cite.annotation.service.user;
|
2024-02-09 17:50:34 +01:00
|
|
|
|
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2024-02-12 10:49:37 +01:00
|
|
|
import gr.cite.annotation.authorization.AuthorizationFlags;
|
|
|
|
import gr.cite.annotation.authorization.Permission;
|
|
|
|
import gr.cite.annotation.common.JsonHandlingService;
|
|
|
|
import gr.cite.annotation.common.enums.IsActive;
|
2024-04-04 11:21:12 +02:00
|
|
|
import gr.cite.annotation.common.scope.tenant.TenantScope;
|
2024-02-12 10:49:37 +01:00
|
|
|
import gr.cite.annotation.convention.ConventionService;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.annotation.data.*;
|
2024-03-11 14:55:52 +01:00
|
|
|
import gr.cite.annotation.integrationevent.inbox.usertouch.UserTouchedIntegrationEvent;
|
2024-04-04 11:21:12 +02:00
|
|
|
import gr.cite.annotation.model.Tenant;
|
2024-02-12 10:49:37 +01:00
|
|
|
import gr.cite.annotation.model.User;
|
|
|
|
import gr.cite.annotation.model.builder.UserBuilder;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.annotation.model.deleter.TenantUserDeleter;
|
|
|
|
import gr.cite.annotation.model.deleter.UserCredentialDeleter;
|
2024-02-12 10:49:37 +01:00
|
|
|
import gr.cite.annotation.model.deleter.UserDeleter;
|
2024-04-04 11:21:12 +02:00
|
|
|
import gr.cite.annotation.query.TenantQuery;
|
2024-04-03 17:35:37 +02:00
|
|
|
import gr.cite.annotation.query.TenantUserQuery;
|
|
|
|
import gr.cite.annotation.query.UserCredentialQuery;
|
|
|
|
import gr.cite.commons.web.authz.service.AuthorizationService;
|
2024-02-09 17:50:34 +01:00
|
|
|
import gr.cite.tools.data.builder.BuilderFactory;
|
|
|
|
import gr.cite.tools.data.deleter.DeleterFactory;
|
|
|
|
import gr.cite.tools.data.query.QueryFactory;
|
|
|
|
import gr.cite.tools.exception.MyApplicationException;
|
|
|
|
import gr.cite.tools.exception.MyForbiddenException;
|
|
|
|
import gr.cite.tools.exception.MyNotFoundException;
|
|
|
|
import gr.cite.tools.exception.MyValidationException;
|
|
|
|
import gr.cite.tools.fieldset.BaseFieldSet;
|
|
|
|
import gr.cite.tools.fieldset.FieldSet;
|
|
|
|
import gr.cite.tools.logging.LoggerService;
|
|
|
|
import gr.cite.tools.logging.MapLogEntry;
|
|
|
|
import jakarta.transaction.Transactional;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2024-04-04 11:21:12 +02:00
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
2024-02-09 17:50:34 +01:00
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.management.InvalidApplicationException;
|
|
|
|
import java.time.Instant;
|
2024-04-03 17:35:37 +02:00
|
|
|
import java.util.ArrayList;
|
2024-02-09 17:50:34 +01:00
|
|
|
import java.util.EnumSet;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.UUID;
|
2024-04-03 17:35:37 +02:00
|
|
|
import java.util.stream.Collectors;
|
2024-02-09 17:50:34 +01:00
|
|
|
|
|
|
|
@Service
|
|
|
|
public class UserServiceImpl implements UserService {
|
|
|
|
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(UserServiceImpl.class));
|
|
|
|
|
|
|
|
private final AuthorizationService authorizationService;
|
|
|
|
|
|
|
|
private final DeleterFactory deleterFactory;
|
|
|
|
|
|
|
|
private final ConventionService conventionService;
|
|
|
|
|
2024-04-03 17:35:37 +02:00
|
|
|
private final TenantEntityManager entityManager;
|
2024-02-09 17:50:34 +01:00
|
|
|
|
|
|
|
private final BuilderFactory builderFactory;
|
|
|
|
|
|
|
|
private final QueryFactory queryFactory;
|
2024-04-04 11:21:12 +02:00
|
|
|
private final TenantScope tenantScope;
|
|
|
|
|
|
|
|
private final MessageSource messageSource;
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-04-03 17:35:37 +02:00
|
|
|
|
2024-02-09 17:50:34 +01:00
|
|
|
private final JsonHandlingService jsonHandlingService;
|
|
|
|
|
|
|
|
public UserServiceImpl(AuthorizationService authorizationService,
|
|
|
|
DeleterFactory deleterFactory,
|
|
|
|
ConventionService conventionService,
|
2024-04-03 17:35:37 +02:00
|
|
|
TenantEntityManager entityManager,
|
2024-04-04 11:21:12 +02:00
|
|
|
BuilderFactory builderFactory, QueryFactory queryFactory, TenantScope tenantScope, MessageSource messageSource,
|
2024-02-09 17:50:34 +01:00
|
|
|
JsonHandlingService jsonHandlingService) {
|
|
|
|
this.authorizationService = authorizationService;
|
|
|
|
this.deleterFactory = deleterFactory;
|
|
|
|
this.conventionService = conventionService;
|
|
|
|
this.entityManager = entityManager;
|
|
|
|
this.builderFactory = builderFactory;
|
2024-04-03 17:35:37 +02:00
|
|
|
this.queryFactory = queryFactory;
|
2024-04-04 11:21:12 +02:00
|
|
|
this.tenantScope = tenantScope;
|
|
|
|
this.messageSource = messageSource;
|
2024-04-03 17:35:37 +02:00
|
|
|
this.jsonHandlingService = jsonHandlingService;
|
2024-02-09 17:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@Transactional
|
|
|
|
public User persist(UserTouchedIntegrationEvent model, FieldSet fields) throws MyForbiddenException, MyValidationException, MyApplicationException, MyNotFoundException, InvalidApplicationException, JsonProcessingException {
|
|
|
|
logger.debug(new MapLogEntry("persisting user").And("model", model).And("fields", fields));
|
|
|
|
|
|
|
|
// this.authorizationService.authorizeAtLeastOneForce(model.getId() != null ? List.of(new OwnedResource(model.getId())) : null, Permission.EditUser);
|
|
|
|
|
|
|
|
Boolean isValid = this.conventionService.isValidGuid(model.getId());
|
|
|
|
|
|
|
|
UserEntity data;
|
|
|
|
if (isValid) {
|
|
|
|
data = this.entityManager.find(UserEntity.class, model.getId());
|
|
|
|
if (data == null) {
|
|
|
|
data = new UserEntity();
|
|
|
|
data.setId(model.getId());
|
|
|
|
data.setName(model.getName());
|
|
|
|
data.setAdditionalInfo(this.jsonHandlingService.toJson(model.getProfile()));
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.persist(data);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
data.setName(model.getName());
|
|
|
|
data.setAdditionalInfo(this.jsonHandlingService.toJson(model.getProfile()));
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
|
|
|
|
this.entityManager.merge(data);
|
|
|
|
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new MyApplicationException("Not valid user id");
|
|
|
|
}
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
2024-04-03 17:35:37 +02:00
|
|
|
this.persistUserCredential(model.getCredentials(), data.getId());
|
|
|
|
this.persistTenantUser(model.getTenantUsers(), data.getId());
|
|
|
|
|
|
|
|
this.entityManager.flush();
|
|
|
|
|
2024-02-09 17:50:34 +01:00
|
|
|
return this.builderFactory.builder(UserBuilder.class).authorize(EnumSet.of(AuthorizationFlags.None)).build(BaseFieldSet.build(fields, User._id), data);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void deleteAndSave(UUID id) throws MyForbiddenException, InvalidApplicationException {
|
|
|
|
logger.debug("deleting User: {}", id);
|
|
|
|
|
|
|
|
this.authorizationService.authorizeForce(Permission.DeleteUser);
|
|
|
|
|
|
|
|
this.deleterFactory.deleter(UserDeleter.class).deleteAndSaveByIds(List.of(id));
|
|
|
|
}
|
|
|
|
|
2024-04-03 17:35:37 +02:00
|
|
|
private void persistUserCredential(List<UserTouchedIntegrationEvent.UserCredential> models, UUID userId) throws InvalidApplicationException {
|
|
|
|
List<UserCredentialEntity> items = this.queryFactory.query(UserCredentialQuery.class)
|
|
|
|
.userIds(userId)
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collect();
|
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
|
|
|
if (models != null) {
|
|
|
|
for (UserTouchedIntegrationEvent.UserCredential model : models) {
|
|
|
|
UserCredentialEntity data = items.stream().filter(x -> x.getExternalId().equals(model.getSubjectId())).findFirst().orElse(null);
|
|
|
|
if (data == null) {
|
|
|
|
data = new UserCredentialEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setUserId(userId);
|
|
|
|
data.setExternalId(model.getSubjectId());
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
entityManager.persist(data);
|
|
|
|
}
|
|
|
|
updatedCreatedIds.add(data.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
List<UserCredentialEntity> toDelete = items.stream().filter(x -> updatedCreatedIds.stream().noneMatch(y -> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
deleterFactory.deleter(UserCredentialDeleter.class).delete(toDelete);
|
|
|
|
|
|
|
|
entityManager.flush();
|
2024-02-09 17:50:34 +01:00
|
|
|
}
|
|
|
|
|
2024-04-03 17:35:37 +02:00
|
|
|
private void persistTenantUser(List<UserTouchedIntegrationEvent.TenantUser> models, UUID userId) throws InvalidApplicationException {
|
|
|
|
List<TenantUserEntity> items = this.queryFactory.query(TenantUserQuery.class)
|
|
|
|
.userIds(userId)
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collect();
|
2024-04-04 11:21:12 +02:00
|
|
|
|
2024-04-03 17:35:37 +02:00
|
|
|
List<UUID> updatedCreatedIds = new ArrayList<>();
|
|
|
|
if (models != null) {
|
2024-04-04 11:21:12 +02:00
|
|
|
List<TenantEntity> tenantEntities = this.queryFactory.query(TenantQuery.class)
|
|
|
|
.ids(models.stream().map(UserTouchedIntegrationEvent.TenantUser::getTenant).toList())
|
|
|
|
.isActive(IsActive.Active)
|
|
|
|
.collectAs(new BaseFieldSet().ensure(Tenant._id).ensure(Tenant._code));
|
2024-04-03 17:35:37 +02:00
|
|
|
for (UserTouchedIntegrationEvent.TenantUser model : models) {
|
|
|
|
TenantUserEntity data = items.stream().filter(x -> x.getTenantId().equals(model.getTenant())).findFirst().orElse(null);
|
|
|
|
if (data == null) {
|
2024-04-04 11:21:12 +02:00
|
|
|
try {
|
|
|
|
TenantEntity tenant = tenantEntities.stream().filter(x -> x.getId().equals(model.getTenant())).findFirst().orElse(null);
|
|
|
|
if (tenant == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getTenant(), Tenant.class.getSimpleName()}, LocaleContextHolder.getLocale()));
|
|
|
|
this.tenantScope.setTempTenant(this.entityManager.getEntityManager(), tenant.getId(), tenant.getCode());
|
|
|
|
data = new TenantUserEntity();
|
|
|
|
data.setId(UUID.randomUUID());
|
|
|
|
data.setUserId(userId);
|
|
|
|
data.setTenantId(model.getTenant());
|
|
|
|
data.setCreatedAt(Instant.now());
|
|
|
|
data.setUpdatedAt(Instant.now());
|
|
|
|
data.setIsActive(IsActive.Active);
|
|
|
|
entityManager.persist(data);
|
|
|
|
} finally {
|
|
|
|
this.tenantScope.removeTempTenant(this.entityManager.getEntityManager());
|
|
|
|
}
|
2024-04-03 17:35:37 +02:00
|
|
|
}
|
|
|
|
updatedCreatedIds.add(data.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
List<TenantUserEntity> toDelete = items.stream().filter(x -> updatedCreatedIds.stream().noneMatch(y -> y.equals(x.getId()))).collect(Collectors.toList());
|
|
|
|
deleterFactory.deleter(TenantUserDeleter.class).delete(toDelete);
|
2024-02-09 17:50:34 +01:00
|
|
|
|
2024-04-03 17:35:37 +02:00
|
|
|
entityManager.flush();
|
2024-02-09 17:50:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|