argos/dmp-backend/core/src/main/java/eu/eudat/model/builder/UserBuilder.java

221 lines
12 KiB
Java
Raw Normal View History

2023-11-20 16:08:43 +01:00
package eu.eudat.model.builder;
import eu.eudat.authorization.AuthorizationFlags;
2024-04-16 10:02:17 +02:00
import eu.eudat.authorization.AuthorizationProperties;
2023-11-20 16:09:24 +01:00
import eu.eudat.commons.JsonHandlingService;
2024-04-16 10:02:17 +02:00
import eu.eudat.commons.scope.tenant.TenantScope;
2023-11-20 16:09:24 +01:00
import eu.eudat.commons.types.user.AdditionalInfoEntity;
2023-11-20 16:08:43 +01:00
import eu.eudat.convention.ConventionService;
2024-04-16 10:02:17 +02:00
import eu.eudat.data.TenantEntity;
import eu.eudat.data.TenantEntityManager;
2023-11-20 16:09:24 +01:00
import eu.eudat.data.UserEntity;
2023-11-20 16:08:43 +01:00
import eu.eudat.model.*;
2024-04-16 10:02:17 +02:00
import eu.eudat.query.*;
2023-11-20 16:08:43 +01:00
import gr.cite.tools.data.builder.BuilderFactory;
import gr.cite.tools.data.query.QueryFactory;
import gr.cite.tools.exception.MyApplicationException;
2024-04-16 10:02:17 +02:00
import gr.cite.tools.exception.MyForbiddenException;
import gr.cite.tools.exception.MyNotFoundException;
2023-11-20 16:08:43 +01:00
import gr.cite.tools.fieldset.BaseFieldSet;
import gr.cite.tools.fieldset.FieldSet;
import gr.cite.tools.logging.DataLogEntry;
import gr.cite.tools.logging.LoggerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2024-04-16 10:02:17 +02:00
import org.springframework.context.MessageSource;
2023-11-20 16:08:43 +01:00
import org.springframework.context.annotation.Scope;
2024-04-16 10:02:17 +02:00
import org.springframework.context.i18n.LocaleContextHolder;
2023-11-20 16:08:43 +01:00
import org.springframework.stereotype.Component;
2024-04-16 10:02:17 +02:00
import javax.management.InvalidApplicationException;
2023-11-20 16:08:43 +01:00
import java.util.*;
import java.util.stream.Collectors;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
2023-11-20 16:09:24 +01:00
public class UserBuilder extends BaseBuilder<User, UserEntity> {
2023-11-20 16:08:43 +01:00
private final QueryFactory queryFactory;
private final BuilderFactory builderFactory;
2023-11-20 16:09:24 +01:00
private final JsonHandlingService jsonHandlingService;
2024-04-16 10:02:17 +02:00
private final AuthorizationProperties authorizationProperties;
private final TenantEntityManager tenantEntityManager;
private final TenantScope tenantScope;
private final MessageSource messageSource;
2023-11-20 16:08:43 +01:00
private EnumSet<AuthorizationFlags> authorize = EnumSet.of(AuthorizationFlags.None);
@Autowired
2023-11-20 16:09:24 +01:00
public UserBuilder(ConventionService conventionService,
QueryFactory queryFactory,
2024-04-16 10:02:17 +02:00
BuilderFactory builderFactory, JsonHandlingService jsonHandlingService, AuthorizationProperties authorizationProperties, TenantEntityManager tenantEntityManager, TenantScope tenantScope, MessageSource messageSource) {
2023-11-20 16:09:24 +01:00
super(conventionService, new LoggerService(LoggerFactory.getLogger(UserBuilder.class)));
2023-11-20 16:08:43 +01:00
this.queryFactory = queryFactory;
this.builderFactory = builderFactory;
2023-11-20 16:09:24 +01:00
this.jsonHandlingService = jsonHandlingService;
2024-04-16 10:02:17 +02:00
this.authorizationProperties = authorizationProperties;
this.tenantEntityManager = tenantEntityManager;
this.tenantScope = tenantScope;
this.messageSource = messageSource;
2023-11-20 16:08:43 +01:00
}
2023-11-20 16:09:24 +01:00
public UserBuilder authorize(EnumSet<AuthorizationFlags> values) {
2023-11-20 16:08:43 +01:00
this.authorize = values;
return this;
}
@Override
2023-11-20 16:09:24 +01:00
public List<User> build(FieldSet fields, List<UserEntity> data) throws MyApplicationException {
2023-11-20 16:08:43 +01:00
this.logger.debug("building for {} items requesting {} fields", Optional.ofNullable(data).map(List::size).orElse(0), Optional.ofNullable(fields).map(FieldSet::getFields).map(Set::size).orElse(0));
this.logger.trace(new DataLogEntry("requested fields", fields));
if (fields == null || data == null || fields.isEmpty())
return new ArrayList<>();
2023-11-20 16:09:24 +01:00
List<User> models = new ArrayList<>();
FieldSet contactsFields = fields.extractPrefixed(this.asPrefix(User._contacts));
Map<UUID, List<UserContactInfo>> contactsMap = this.collectUserContactInfos(contactsFields, data);
2024-04-16 10:02:17 +02:00
FieldSet globalRolesFields = fields.extractPrefixed(this.asPrefix(User._globalRoles));
Map<UUID, List<UserRole>> globalRolesMap = this.collectUserGlobalRoles(globalRolesFields, data);
FieldSet tenantRolesFields = fields.extractPrefixed(this.asPrefix(User._tenantRoles));
Map<UUID, List<UserRole>> tenantRolesMap = this.collectUserTenantRoles(tenantRolesFields, data);
2023-11-20 16:09:24 +01:00
FieldSet credentialsFields = fields.extractPrefixed(this.asPrefix(User._credentials));
Map<UUID, List<UserCredential>> credentialsMap = this.collectUserCredentials(credentialsFields, data);
FieldSet additionalInfoFields = fields.extractPrefixed(this.asPrefix(User._additionalInfo));
2023-12-06 16:07:14 +01:00
FieldSet tenantUsersFields = fields.extractPrefixed(this.asPrefix(User._tenantUsers));
Map<UUID, List<TenantUser>> tenantUsersMap = this.collectTenantUsers(tenantUsersFields, data);
2023-11-20 16:09:24 +01:00
for (UserEntity d : data) {
User m = new User();
if (fields.hasField(this.asIndexer(User._id))) m.setId(d.getId());
if (fields.hasField(this.asIndexer(User._name))) m.setName(d.getName());
if (fields.hasField(this.asIndexer(User._createdAt))) m.setCreatedAt(d.getCreatedAt());
if (fields.hasField(this.asIndexer(User._updatedAt))) m.setUpdatedAt(d.getUpdatedAt());
if (fields.hasField(this.asIndexer(User._isActive))) m.setIsActive(d.getIsActive());
if (fields.hasField(this.asIndexer(User._hash))) m.setHash(this.hashValue(d.getUpdatedAt()));
if (contactsMap != null && !contactsFields.isEmpty() && contactsMap.containsKey(d.getId())) m.setContacts(contactsMap.get(d.getId()));
2024-04-16 10:02:17 +02:00
if (globalRolesMap != null && !globalRolesFields.isEmpty() && globalRolesMap.containsKey(d.getId())) m.setGlobalRoles(globalRolesMap.get(d.getId()));
if (tenantRolesMap != null && !tenantRolesFields.isEmpty() && tenantRolesMap.containsKey(d.getId())) m.setTenantRoles(tenantRolesMap.get(d.getId()));
2023-11-20 16:09:24 +01:00
if (credentialsMap != null && !credentialsFields.isEmpty() && credentialsMap.containsKey(d.getId())) m.setCredentials(credentialsMap.get(d.getId()));
if (!additionalInfoFields.isEmpty() && d.getAdditionalInfo() != null){
AdditionalInfoEntity definition = this.jsonHandlingService.fromJsonSafe(AdditionalInfoEntity.class, d.getAdditionalInfo());
m.setAdditionalInfo(this.builderFactory.builder(UserAdditionalInfoBuilder.class).authorize(this.authorize).build(additionalInfoFields, definition));
}
2023-12-06 16:07:14 +01:00
if (!tenantUsersFields.isEmpty() && tenantUsersMap != null && tenantUsersMap.containsKey(d.getId())) m.setTenantUsers(tenantUsersMap.get(d.getId()));
2023-11-20 16:08:43 +01:00
models.add(m);
}
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
return models;
}
2023-11-20 16:09:24 +01:00
private Map<UUID, List<UserContactInfo>> collectUserContactInfos(FieldSet fields, List<UserEntity> data) throws MyApplicationException {
2023-11-20 16:08:43 +01:00
if (fields.isEmpty() || data.isEmpty()) return null;
2023-11-20 16:09:24 +01:00
this.logger.debug("checking related - {}", UserContactInfo.class.getSimpleName());
2023-11-20 16:08:43 +01:00
2023-11-20 16:09:24 +01:00
Map<UUID, List<UserContactInfo>> itemMap;
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(UserContactInfo._user, User._id));
UserContactInfoQuery query = this.queryFactory.query(UserContactInfoQuery.class).authorize(this.authorize).userIds(data.stream().map(UserEntity::getId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(UserContactInfoBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getUser().getId());
2023-11-20 16:08:43 +01:00
2023-11-20 16:09:24 +01:00
if (!fields.hasField(this.asIndexer(UserContactInfo._user, User._id))) {
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getUser() != null).peek(x -> {
x.getUser().setId(null);
2023-11-20 16:08:43 +01:00
});
}
return itemMap;
}
2024-04-16 10:02:17 +02:00
private Map<UUID, List<UserRole>> collectUserGlobalRoles(FieldSet fields, List<UserEntity> data) throws MyApplicationException {
2023-11-20 16:09:24 +01:00
if (fields.isEmpty() || data.isEmpty()) return null;
this.logger.debug("checking related - {}", UserRole.class.getSimpleName());
Map<UUID, List<UserRole>> itemMap;
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(UserRole._user, User._id));
2024-04-16 10:02:17 +02:00
UserRoleQuery query = this.queryFactory.query(UserRoleQuery.class).authorize(this.authorize).tenantIsSet(false).roles(this.authorizationProperties.getAllowedGlobalRoles()).userIds(data.stream().map(UserEntity::getId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(UserRoleBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getUser().getId());
if (!fields.hasField(this.asIndexer(UserRole._user, User._id))) {
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getUser() != null).peek(x -> {
x.getUser().setId(null);
});
}
return itemMap;
}
private Map<UUID, List<UserRole>> collectUserTenantRoles(FieldSet fields, List<UserEntity> data) throws MyApplicationException {
if (fields.isEmpty() || data.isEmpty()) return null;
this.logger.debug("checking related - {}", UserRole.class.getSimpleName());
Map<UUID, List<UserRole>> itemMap;
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(UserRole._user, User._id));
if (!tenantScope.isSet()) throw new MyForbiddenException("tenant scope required");
UserRoleQuery query = this.queryFactory.query(UserRoleQuery.class).authorize(this.authorize).roles(this.authorizationProperties.getAllowedTenantRoles()).userIds(data.stream().map(UserEntity::getId).distinct().collect(Collectors.toList()));
if (tenantScope.isDefaultTenant()) query.tenantIsSet(false);
else {
try {
query.tenantIsSet(true).tenantIds(this.tenantScope.getTenant());
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
}
2023-11-20 16:09:24 +01:00
itemMap = this.builderFactory.builder(UserRoleBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getUser().getId());
if (!fields.hasField(this.asIndexer(UserRole._user, User._id))) {
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getUser() != null).peek(x -> {
x.getUser().setId(null);
});
}
return itemMap;
}
private Map<UUID, List<UserCredential>> collectUserCredentials(FieldSet fields, List<UserEntity> data) throws MyApplicationException {
if (fields.isEmpty() || data.isEmpty()) return null;
this.logger.debug("checking related - {}", UserCredential.class.getSimpleName());
2023-11-20 16:08:43 +01:00
2023-11-20 16:09:24 +01:00
Map<UUID, List<UserCredential>> itemMap;
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(UserCredential._user, User._id));
UserCredentialQuery query = this.queryFactory.query(UserCredentialQuery.class).authorize(this.authorize).userIds(data.stream().map(UserEntity::getId).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(UserCredentialBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getUser().getId());
2023-11-20 16:08:43 +01:00
2023-11-20 16:09:24 +01:00
if (!fields.hasField(this.asIndexer(UserCredential._user, User._id))) {
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getUser() != null).peek(x -> {
x.getUser().setId(null);
2023-11-20 16:08:43 +01:00
});
}
return itemMap;
}
2023-12-06 16:07:14 +01:00
private Map<UUID, List<TenantUser>> collectTenantUsers(FieldSet fields, List<UserEntity> datas) throws MyApplicationException {
if (fields.isEmpty() || datas.isEmpty()) return null;
this.logger.debug("checking related - {}", TenantUser.class.getSimpleName());
Map<UUID, List<TenantUser>> itemMap = null;
FieldSet clone = new BaseFieldSet(fields.getFields()).ensure(this.asIndexer(TenantUser._user, User._id));
TenantUserQuery query = this.queryFactory.query(TenantUserQuery.class).authorize(this.authorize).userIds(datas.stream().map(x -> x.getId()).distinct().collect(Collectors.toList()));
itemMap = this.builderFactory.builder(TenantUserBuilder.class).authorize(this.authorize).asMasterKey(query, clone, x -> x.getUser().getId());
if (!fields.hasField(this.asIndexer(TenantUser._user, User._id))) {
itemMap.values().stream().flatMap(List::stream).filter(x -> x != null && x.getUser() != null).map(x -> {
x.getUser().setId(null);
return x;
}).collect(Collectors.toList());
}
return itemMap;
}
2023-11-20 16:08:43 +01:00
}