mirror of
https://github.com/valitydev/org-manager.git
synced 2024-11-06 08:25:21 +00:00
add owned organizations to member user
This commit is contained in:
parent
2e4cb8827d
commit
e0d475099d
@ -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()))
|
||||
|
@ -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()
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ class BouncerContextConverterTest {
|
||||
.setScope(new OrgRoleScope()
|
||||
.setShop(new Entity().setId("resource")));
|
||||
|
||||
assertThat(role).isEqualToComparingFieldByField(expected);
|
||||
assertThat(role).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user