use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class UserCommandHandler method handle.
@Override
public Single<UserReply> handle(UserCommand command) {
UserPayload userPayload = command.getPayload();
GraviteeContext.setCurrentOrganization(userPayload.getOrganizationId());
try {
final UserEntity existingUser = userService.findBySource(COCKPIT_SOURCE, userPayload.getId(), false);
UpdateUserEntity updatedUser = new UpdateUserEntity();
updatedUser.setFirstname(userPayload.getFirstName());
updatedUser.setLastname(userPayload.getLastName());
updatedUser.setEmail(userPayload.getEmail());
updatedUser.setPicture(userPayload.getPicture());
updatedUser.setCustomFields(new HashMap<>());
if (userPayload.getAdditionalInformation() != null) {
updatedUser.getCustomFields().putAll(userPayload.getAdditionalInformation());
}
updatedUser.getCustomFields().computeIfAbsent(PICTURE, k -> userPayload.getPicture());
updatedUser.getCustomFields().computeIfAbsent(SUB, k -> userPayload.getUsername());
UserEntity cockpitUserEntity = userService.update(existingUser.getId(), updatedUser);
logger.info("User [{}] with APIM id [{}] updated.", userPayload.getUsername(), cockpitUserEntity.getId());
return Single.just(new UserReply(command.getId(), CommandStatus.SUCCEEDED));
} catch (UserNotFoundException unfe) {
NewExternalUserEntity newUser = new NewExternalUserEntity();
newUser.setSourceId(userPayload.getId());
newUser.setFirstname(userPayload.getFirstName());
newUser.setLastname(userPayload.getLastName());
newUser.setEmail(userPayload.getEmail());
newUser.setPicture(userPayload.getPicture());
newUser.setSource(COCKPIT_SOURCE);
newUser.setCustomFields(new HashMap<>());
if (userPayload.getAdditionalInformation() != null) {
newUser.getCustomFields().putAll(userPayload.getAdditionalInformation());
}
newUser.getCustomFields().computeIfAbsent(PICTURE, k -> userPayload.getPicture());
newUser.getCustomFields().computeIfAbsent(SUB, k -> userPayload.getUsername());
try {
UserEntity cockpitUserEntity = userService.create(newUser, false);
logger.info("User [{}] created with APIM id [{}].", userPayload.getUsername(), cockpitUserEntity.getId());
return Single.just(new UserReply(command.getId(), CommandStatus.SUCCEEDED));
} catch (Exception e) {
logger.info("Error occurred when creating user [{}] for organization [{}].", userPayload.getUsername(), userPayload.getOrganizationId(), e);
return Single.just(new UserReply(command.getId(), CommandStatus.ERROR));
}
} catch (Exception e) {
logger.info("Error occurred when updating user [{}] for organization [{}].", userPayload.getUsername(), userPayload.getOrganizationId(), e);
return Single.just(new UserReply(command.getId(), CommandStatus.ERROR));
} finally {
GraviteeContext.cleanContext();
}
}
use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_GetMemberPermissionsTest method shouldGetPermissionsIfMemberOfApi.
@Test
public void shouldGetPermissionsIfMemberOfApi() throws Exception {
ApiEntity api = mock(ApiEntity.class);
doReturn(API_ID).when(api).getId();
doReturn(Collections.emptySet()).when(api).getGroups();
doReturn(api).when(apiService).findById(API_ID);
Membership membership = mock(Membership.class);
doReturn("API_" + ROLENAME).when(membership).getRoleId();
doReturn(new HashSet<>(asList(membership))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
UserEntity userEntity = mock(UserEntity.class);
doReturn(userEntity).when(userService).findById(USERNAME);
RoleEntity roleEntity = mock(RoleEntity.class);
Map<String, char[]> rolePerms = new HashMap<>();
rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
doReturn(rolePerms).when(roleEntity).getPermissions();
doReturn(roleEntity).when(roleService).findById("API_" + ROLENAME);
Map<String, char[]> permissions = membershipService.getUserMemberPermissions(api, USERNAME);
assertNotNull(permissions);
assertPermissions(rolePerms, permissions);
verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
verify(membershipRepository, never()).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
verify(apiService, times(1)).findById(API_ID);
verify(userService, times(1)).findById(USERNAME);
verify(roleService, times(1)).findById("API_" + ROLENAME);
}
use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_GetMemberPermissionsTest method shouldGetMergedPermissionsIfMemberOfApiAndApiGroup.
@Test
public void shouldGetMergedPermissionsIfMemberOfApiAndApiGroup() throws Exception {
ApiEntity api = mock(ApiEntity.class);
doReturn(API_ID).when(api).getId();
doReturn(Collections.singleton(GROUP_ID1)).when(api).getGroups();
doReturn(api).when(apiService).findById(API_ID);
Membership membershipUser = mock(Membership.class);
doReturn("API_" + ROLENAME).when(membershipUser).getRoleId();
doReturn(new HashSet<>(asList(membershipUser))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
Membership membershipGroup = mock(Membership.class);
doReturn("API_" + ROLENAME2).when(membershipGroup).getRoleId();
doReturn(new HashSet<>(asList(membershipGroup))).when(membershipRepository).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
UserEntity userEntity = mock(UserEntity.class);
doReturn(userEntity).when(userService).findById(USERNAME);
RoleEntity roleEntity = mock(RoleEntity.class);
Map<String, char[]> rolePerms = new HashMap<>();
rolePerms.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.UPDATE.getId(), RolePermissionAction.CREATE.getId() });
doReturn(rolePerms).when(roleEntity).getPermissions();
doReturn(roleEntity).when(roleService).findById("API_" + ROLENAME);
RoleEntity roleEntity2 = mock(RoleEntity.class);
Map<String, char[]> rolePerms2 = new HashMap<>();
rolePerms2.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.READ.getId(), RolePermissionAction.DELETE.getId() });
rolePerms2.put(ApiPermission.PLAN.getName(), new char[] { RolePermissionAction.READ.getId() });
doReturn(rolePerms2).when(roleEntity2).getPermissions();
doReturn(RoleScope.API).when(roleEntity2).getScope();
doReturn(roleEntity2).when(roleService).findById("API_" + ROLENAME2);
Map<String, char[]> permissions = membershipService.getUserMemberPermissions(api, USERNAME);
assertNotNull(permissions);
Map<String, char[]> expectedPermissions = new HashMap<>();
expectedPermissions.put(ApiPermission.DOCUMENTATION.getName(), new char[] { RolePermissionAction.CREATE.getId(), RolePermissionAction.READ.getId(), RolePermissionAction.UPDATE.getId(), RolePermissionAction.DELETE.getId() });
expectedPermissions.put(ApiPermission.PLAN.getName(), new char[] { RolePermissionAction.READ.getId() });
assertPermissions(expectedPermissions, permissions);
verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.API, API_ID);
verify(membershipRepository, times(1)).findByMemberIdAndMemberTypeAndReferenceTypeAndReferenceId(USERNAME, MembershipMemberType.USER, MembershipReferenceType.GROUP, GROUP_ID1);
verify(apiService, times(1)).findById(API_ID);
verify(userService, times(1)).findById(USERNAME);
verify(roleService, times(1)).findById("API_" + ROLENAME);
verify(roleService, times(1)).findById("API_" + ROLENAME2);
}
use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationMapperTest method init.
@Before
public void init() {
now = Instant.now();
Date nowDate = Date.from(now);
applicationEntity = new ApplicationEntity();
applicationListItem = new ApplicationListItem();
// init
reset(groupService);
reset(userService);
reset(userMapper);
GroupEntity grpEntity = new GroupEntity();
grpEntity.setId(APPLICATION_GROUP_ID);
grpEntity.setName(APPLICATION_GROUP_NAME);
when(groupService.findById(APPLICATION_GROUP_ID)).thenReturn(grpEntity);
UserEntity userEntity = Mockito.mock(UserEntity.class);
when(userEntity.getDisplayName()).thenReturn(APPLICATION_USER_DISPLAYNAME);
when(userEntity.getEmail()).thenReturn(APPLICATION_USER_EMAIL);
when(userEntity.getId()).thenReturn(APPLICATION_USER_ID);
when(userService.findById(APPLICATION_USER_ID)).thenReturn(userEntity);
when(userMapper.convert(userEntity)).thenCallRealMethod();
when(userMapper.computeUserLinks(anyString(), any())).thenCallRealMethod();
PrimaryOwnerEntity primaryOwner = new PrimaryOwnerEntity(userEntity);
when(uriInfo.getBaseUriBuilder()).thenReturn(UriBuilder.fromPath(""));
applicationEntity.setCreatedAt(nowDate);
applicationEntity.setDescription(APPLICATION_DESCRIPTION);
applicationEntity.setGroups(new HashSet<String>(Arrays.asList(APPLICATION_GROUP_ID)));
applicationEntity.setId(APPLICATION_ID);
applicationEntity.setName(APPLICATION_NAME);
applicationEntity.setPrimaryOwner(primaryOwner);
applicationEntity.setStatus(APPLICATION_STATUS);
applicationEntity.setType(APPLICATION_TYPE);
applicationEntity.setUpdatedAt(nowDate);
applicationListItem.setCreatedAt(nowDate);
applicationListItem.setDescription(APPLICATION_DESCRIPTION);
applicationListItem.setGroups(new HashSet<String>(Arrays.asList(APPLICATION_GROUP_ID)));
applicationListItem.setId(APPLICATION_ID);
applicationListItem.setName(APPLICATION_NAME);
applicationListItem.setPrimaryOwner(primaryOwner);
applicationListItem.setStatus(APPLICATION_STATUS);
applicationListItem.setType(APPLICATION_TYPE);
applicationListItem.setUpdatedAt(nowDate);
}
use of io.gravitee.rest.api.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class MemberMapperTest method testConvert.
@Test
public void testConvert() {
Instant now = Instant.now();
Date nowDate = Date.from(now);
// init
RoleEntity ownerRoleEntity = new RoleEntity();
ownerRoleEntity.setName("OWNER");
memberEntity = new MemberEntity();
memberEntity.setCreatedAt(nowDate);
memberEntity.setDisplayName(MEMBER_DISPLAYNAME);
memberEntity.setEmail(MEMBER_EMAIL);
memberEntity.setId(MEMBER_ID);
memberEntity.setRoles(Arrays.asList(ownerRoleEntity));
memberEntity.setUpdatedAt(nowDate);
UserEntity userEntity = Mockito.mock(UserEntity.class);
when(userEntity.getDisplayName()).thenReturn(MEMBER_DISPLAYNAME);
when(userEntity.getEmail()).thenReturn(MEMBER_EMAIL);
when(userEntity.getId()).thenReturn(MEMBER_ID);
when(uriInfo.getBaseUriBuilder()).thenReturn(UriBuilder.fromPath(""));
when(userService.findById(MEMBER_ID)).thenReturn(userEntity);
when(userMapper.convert(userEntity)).thenCallRealMethod();
when(userMapper.computeUserLinks(anyString(), any())).thenCallRealMethod();
// Test
Member responseMember = memberMapper.convert(memberEntity, uriInfo);
assertNotNull(responseMember);
assertEquals(now.toEpochMilli(), responseMember.getCreatedAt().toInstant().toEpochMilli());
assertNull(responseMember.getId());
assertEquals("OWNER", responseMember.getRole());
assertEquals(now.toEpochMilli(), responseMember.getUpdatedAt().toInstant().toEpochMilli());
User user = responseMember.getUser();
assertNotNull(user);
assertEquals(MEMBER_DISPLAYNAME, user.getDisplayName());
assertEquals(MEMBER_EMAIL, user.getEmail());
assertEquals(MEMBER_ID, user.getId());
assertEquals("environments/DEFAULT/users/" + MEMBER_ID + "/avatar?", user.getLinks().getAvatar());
}
Aggregations