add tenant users on User model

This commit is contained in:
amentis 2023-12-06 17:07:14 +02:00
parent 37c170ea54
commit 5825aa642a
3 changed files with 37 additions and 3 deletions

View File

@ -47,6 +47,9 @@ public class User {
public static final String _credentials = "credentials"; public static final String _credentials = "credentials";
public final static String _tenantUsers = "tenantUsers";
private List<TenantUser> tenantUsers;
public UUID getId() { public UUID getId() {
return id; return id;
} }
@ -126,4 +129,12 @@ public class User {
public void setCredentials(List<UserCredential> credentials) { public void setCredentials(List<UserCredential> credentials) {
this.credentials = credentials; this.credentials = credentials;
} }
public List<TenantUser> getTenantUsers() {
return tenantUsers;
}
public void setTenantUsers(List<TenantUser> tenantUsers) {
this.tenantUsers = tenantUsers;
}
} }

View File

@ -6,6 +6,7 @@ import eu.eudat.commons.types.user.AdditionalInfoEntity;
import eu.eudat.convention.ConventionService; import eu.eudat.convention.ConventionService;
import eu.eudat.data.UserEntity; import eu.eudat.data.UserEntity;
import eu.eudat.model.*; import eu.eudat.model.*;
import eu.eudat.query.TenantUserQuery;
import eu.eudat.query.UserContactInfoQuery; import eu.eudat.query.UserContactInfoQuery;
import eu.eudat.query.UserCredentialQuery; import eu.eudat.query.UserCredentialQuery;
import eu.eudat.query.UserRoleQuery; import eu.eudat.query.UserRoleQuery;
@ -70,6 +71,9 @@ public class UserBuilder extends BaseBuilder<User, UserEntity> {
Map<UUID, List<UserCredential>> credentialsMap = this.collectUserCredentials(credentialsFields, data); Map<UUID, List<UserCredential>> credentialsMap = this.collectUserCredentials(credentialsFields, data);
FieldSet additionalInfoFields = fields.extractPrefixed(this.asPrefix(User._additionalInfo)); FieldSet additionalInfoFields = fields.extractPrefixed(this.asPrefix(User._additionalInfo));
FieldSet tenantUsersFields = fields.extractPrefixed(this.asPrefix(User._tenantUsers));
Map<UUID, List<TenantUser>> tenantUsersMap = this.collectTenantUsers(tenantUsersFields, data);
for (UserEntity d : data) { for (UserEntity d : data) {
User m = new User(); User m = new User();
if (fields.hasField(this.asIndexer(User._id))) m.setId(d.getId()); if (fields.hasField(this.asIndexer(User._id))) m.setId(d.getId());
@ -85,6 +89,7 @@ public class UserBuilder extends BaseBuilder<User, UserEntity> {
AdditionalInfoEntity definition = this.jsonHandlingService.fromJsonSafe(AdditionalInfoEntity.class, d.getAdditionalInfo()); AdditionalInfoEntity definition = this.jsonHandlingService.fromJsonSafe(AdditionalInfoEntity.class, d.getAdditionalInfo());
m.setAdditionalInfo(this.builderFactory.builder(UserAdditionalInfoBuilder.class).authorize(this.authorize).build(additionalInfoFields, definition)); 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); models.add(m);
} }
this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0)); this.logger.debug("build {} items", Optional.of(models).map(List::size).orElse(0));
@ -146,4 +151,22 @@ public class UserBuilder extends BaseBuilder<User, UserEntity> {
return itemMap; return itemMap;
} }
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;
}
} }

View File

@ -3,10 +3,7 @@ package eu.eudat.model.censorship;
import eu.eudat.authorization.OwnedResource; import eu.eudat.authorization.OwnedResource;
import eu.eudat.authorization.Permission; import eu.eudat.authorization.Permission;
import eu.eudat.convention.ConventionService; import eu.eudat.convention.ConventionService;
import eu.eudat.model.Description;
import eu.eudat.model.User; 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.commons.web.authz.service.AuthorizationService;
import gr.cite.tools.data.censor.CensorFactory; import gr.cite.tools.data.censor.CensorFactory;
import gr.cite.tools.fieldset.FieldSet; import gr.cite.tools.fieldset.FieldSet;
@ -53,6 +50,9 @@ public class UserCensor extends BaseCensor {
FieldSet additionalInfoFields = fields.extractPrefixed(this.asIndexerPrefix(User._additionalInfo)); FieldSet additionalInfoFields = fields.extractPrefixed(this.asIndexerPrefix(User._additionalInfo));
this.censorFactory.censor(UserAdditionalInfoCensor.class).censor(additionalInfoFields, userId); this.censorFactory.censor(UserAdditionalInfoCensor.class).censor(additionalInfoFields, userId);
FieldSet tenantUsersFields = fields.extractPrefixed(this.asIndexerPrefix(User._tenantUsers));
this.censorFactory.censor(TenantUserCensor.class).censor(tenantUsersFields, userId);
} }
} }