add owned organizations to member user

This commit is contained in:
ggmaleva 2021-05-13 17:30:14 +03:00
parent 2e4cb8827d
commit e0d475099d
No known key found for this signature in database
GPG Key ID: 0E412B78565B108F
5 changed files with 52 additions and 13 deletions

View File

@ -7,6 +7,7 @@ import com.rbkmoney.orgmanager.entity.OrganizationEntity;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
@ -18,7 +19,7 @@ public class BouncerContextConverter {
.setId(member.getId())
.setRealm(new Entity())
.setEmail(member.getEmail())
.setOrgs(CollectionUtils.isEmpty(member.getOrganizations()) ? null :
.setOrgs(CollectionUtils.isEmpty(member.getOrganizations()) ? new HashSet<>() :
member.getOrganizations()
.stream()
.map(organizationEntity -> this.toOrganization(organizationEntity, member.getRoles()))

View File

@ -17,9 +17,9 @@ public class AuthContextService implements AuthContextProviderSrv.Iface {
@Override
public ContextFragment getUserContext(String id) throws TException {
User user = userService.findById(id);
com.rbkmoney.bouncer.context.v1.ContextFragment contextFragment =
new com.rbkmoney.bouncer.context.v1.ContextFragment();
User user = userService.findById(id);
contextFragment.setUser(user);
TSerializer byteSerializer = new TSerializer();
return new ContextFragment()

View File

@ -3,6 +3,7 @@ package com.rbkmoney.orgmanager.service;
import com.rbkmoney.bouncer.context.v1.Organization;
import com.rbkmoney.bouncer.context.v1.User;
import com.rbkmoney.orgmanager.converter.BouncerContextConverter;
import com.rbkmoney.orgmanager.entity.MemberEntity;
import com.rbkmoney.orgmanager.entity.OrganizationEntity;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -27,20 +28,27 @@ public class UserServiceImpl implements UserService {
@Override
public User findById(String id) {
log.info("Find user with id {}", id);
AccessToken accessToken = keycloakService.getAccessToken();
Set<Organization> ownedOrganizations = findOwnedOrganizations(id);
return memberService.findById(id)
.map(bouncerContextConverter::toUser)
.orElseGet(this::buildCurrentUser);
.map(memberEntity -> buildMemberUser(ownedOrganizations, memberEntity))
.orElseGet(() ->
new User()
.setId(id)
.setEmail(accessToken.getEmail())
.setOrgs(ownedOrganizations));
}
private User buildCurrentUser() {
AccessToken accessToken = keycloakService.getAccessToken();
Set<OrganizationEntity> organizationEntities = organizationService.findByOwner(accessToken.getSubject());
Set<Organization> organizations = organizationEntities.stream()
private Set<Organization> findOwnedOrganizations(String owner) {
Set<OrganizationEntity> organizationEntities = organizationService.findByOwner(owner);
return organizationEntities.stream()
.map(organizationEntity -> bouncerContextConverter.toOrganization(organizationEntity, null))
.collect(Collectors.toSet());
return new User()
.setId(accessToken.getSubject())
.setEmail(accessToken.getEmail())
.setOrgs(organizations);
}
private User buildMemberUser(Set<Organization> organizations, MemberEntity memberEntity) {
User user = bouncerContextConverter.toUser(memberEntity);
user.getOrgs().addAll(organizations);
return user;
}
}

View File

@ -44,7 +44,7 @@ class BouncerContextConverterTest {
.setScope(new OrgRoleScope()
.setShop(new Entity().setId("resource")));
assertThat(role).isEqualToComparingFieldByField(expected);
assertThat(role).isEqualTo(expected);
}
@Test

View File

@ -1,5 +1,6 @@
package com.rbkmoney.orgmanager.service;
import com.rbkmoney.bouncer.context.v1.Organization;
import com.rbkmoney.bouncer.context.v1.User;
import com.rbkmoney.orgmanager.TestObjectFactory;
import com.rbkmoney.orgmanager.entity.MemberRoleEntity;
@ -11,9 +12,12 @@ import org.keycloak.representations.AccessToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;
class UserServiceImplTest extends AbstractRepositoryTest {
@ -67,6 +71,7 @@ class UserServiceImplTest extends AbstractRepositoryTest {
assertEquals(member.getId(), actualUser.getId());
assertEquals(member.getEmail(), actualUser.getEmail());
assertTrue(actualUser.getOrgs().isEmpty());
}
@Test
@ -89,4 +94,29 @@ class UserServiceImplTest extends AbstractRepositoryTest {
actualUser.getOrgs().iterator().next().getRoles().iterator().next().getId());
}
@Test
void findMemberUserWithMemberAndOwnedOrganizationsById() {
String memberId = TestObjectFactory.randomString();
var member = TestObjectFactory.testMemberEntity(memberId);
OrganizationEntity organization = TestObjectFactory.buildOrganization(member);
MemberRoleEntity memberRole = TestObjectFactory.buildMemberRole(RoleId.ACCOUNTANT, organization.getId());
OrganizationEntity ownedOrganization = TestObjectFactory.buildOrganization();
ownedOrganization.setOwner(memberId);
memberRoleRepository.save(memberRole);
member.setRoles(Set.of(memberRole));
memberRepository.save(member);
organizationRepository.saveAll(List.of(organization, ownedOrganization));
User actualUser = userService.findById(memberId);
assertEquals(member.getId(), actualUser.getId());
assertEquals(member.getEmail(), actualUser.getEmail());
List<String> actualOrgs = actualUser.getOrgs().stream().map(Organization::getId).collect(Collectors.toList());
assertTrue(actualOrgs.containsAll(List.of(organization.getId(), ownedOrganization.getId())));
Organization actualMemberOrg = actualUser.getOrgs().stream()
.filter(org -> org.getId().equals(organization.getId())).findFirst().get();
assertEquals(memberRole.getRoleId(),
actualMemberOrg.getRoles().iterator().next().getId());
}
}