From 5825aa642a4d9c8a8c1a2ecd8836c82934f97066 Mon Sep 17 00:00:00 2001 From: amentis Date: Wed, 6 Dec 2023 17:07:14 +0200 Subject: [PATCH] add tenant users on User model --- .../src/main/java/eu/eudat/model/User.java | 11 +++++++++ .../eu/eudat/model/builder/UserBuilder.java | 23 +++++++++++++++++++ .../eu/eudat/model/censorship/UserCensor.java | 6 ++--- 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/User.java b/dmp-backend/core/src/main/java/eu/eudat/model/User.java index 6fd190877..d9e775a60 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/User.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/User.java @@ -47,6 +47,9 @@ public class User { public static final String _credentials = "credentials"; + public final static String _tenantUsers = "tenantUsers"; + private List tenantUsers; + public UUID getId() { return id; } @@ -126,4 +129,12 @@ public class User { public void setCredentials(List credentials) { this.credentials = credentials; } + + public List getTenantUsers() { + return tenantUsers; + } + + public void setTenantUsers(List tenantUsers) { + this.tenantUsers = tenantUsers; + } } diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/builder/UserBuilder.java b/dmp-backend/core/src/main/java/eu/eudat/model/builder/UserBuilder.java index cbfa0f136..46d613232 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/builder/UserBuilder.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/builder/UserBuilder.java @@ -6,6 +6,7 @@ import eu.eudat.commons.types.user.AdditionalInfoEntity; import eu.eudat.convention.ConventionService; import eu.eudat.data.UserEntity; import eu.eudat.model.*; +import eu.eudat.query.TenantUserQuery; import eu.eudat.query.UserContactInfoQuery; import eu.eudat.query.UserCredentialQuery; import eu.eudat.query.UserRoleQuery; @@ -70,6 +71,9 @@ public class UserBuilder extends BaseBuilder { Map> credentialsMap = this.collectUserCredentials(credentialsFields, data); FieldSet additionalInfoFields = fields.extractPrefixed(this.asPrefix(User._additionalInfo)); + + FieldSet tenantUsersFields = fields.extractPrefixed(this.asPrefix(User._tenantUsers)); + Map> tenantUsersMap = this.collectTenantUsers(tenantUsersFields, data); for (UserEntity d : data) { User m = new User(); if (fields.hasField(this.asIndexer(User._id))) m.setId(d.getId()); @@ -85,6 +89,7 @@ public class UserBuilder extends BaseBuilder { AdditionalInfoEntity definition = this.jsonHandlingService.fromJsonSafe(AdditionalInfoEntity.class, d.getAdditionalInfo()); m.setAdditionalInfo(this.builderFactory.builder(UserAdditionalInfoBuilder.class).authorize(this.authorize).build(additionalInfoFields, definition)); } + if (!tenantUsersFields.isEmpty() && tenantUsersMap != null && tenantUsersMap.containsKey(d.getId())) m.setTenantUsers(tenantUsersMap.get(d.getId())); models.add(m); } this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); @@ -146,4 +151,22 @@ public class UserBuilder extends BaseBuilder { return itemMap; } + private Map> collectTenantUsers(FieldSet fields, List datas) throws MyApplicationException { + if (fields.isEmpty() || datas.isEmpty()) return null; + this.logger.debug("checking related - {}", TenantUser.class.getSimpleName()); + + Map> 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; + } + } diff --git a/dmp-backend/core/src/main/java/eu/eudat/model/censorship/UserCensor.java b/dmp-backend/core/src/main/java/eu/eudat/model/censorship/UserCensor.java index f5c5ea43f..86038e84f 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/model/censorship/UserCensor.java +++ b/dmp-backend/core/src/main/java/eu/eudat/model/censorship/UserCensor.java @@ -3,10 +3,7 @@ package eu.eudat.model.censorship; import eu.eudat.authorization.OwnedResource; import eu.eudat.authorization.Permission; import eu.eudat.convention.ConventionService; -import eu.eudat.model.Description; import eu.eudat.model.User; -import eu.eudat.model.UserContactInfo; -import eu.eudat.model.censorship.descriptionproperties.PropertyDefinitionCensor; import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.tools.data.censor.CensorFactory; import gr.cite.tools.fieldset.FieldSet; @@ -53,6 +50,9 @@ public class UserCensor extends BaseCensor { FieldSet additionalInfoFields = fields.extractPrefixed(this.asIndexerPrefix(User._additionalInfo)); this.censorFactory.censor(UserAdditionalInfoCensor.class).censor(additionalInfoFields, userId); + + FieldSet tenantUsersFields = fields.extractPrefixed(this.asIndexerPrefix(User._tenantUsers)); + this.censorFactory.censor(TenantUserCensor.class).censor(tenantUsersFields, userId); } }