Search in sources :

Example 6 with UserEntity

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();
    }
}
Also used : UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) Logger(org.slf4j.Logger) PICTURE(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.PICTURE) SUB(io.gravitee.rest.api.model.configuration.identity.SocialIdentityProviderEntity.UserProfile.SUB) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) LoggerFactory(org.slf4j.LoggerFactory) GraviteeContext(io.gravitee.rest.api.service.common.GraviteeContext) HashMap(java.util.HashMap) Single(io.reactivex.Single) Command(io.gravitee.cockpit.api.command.Command) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply) Component(org.springframework.stereotype.Component) UserService(io.gravitee.rest.api.service.UserService) CommandHandler(io.gravitee.cockpit.api.command.CommandHandler) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) CommandStatus(io.gravitee.cockpit.api.command.CommandStatus) UserCommand(io.gravitee.cockpit.api.command.user.UserCommand) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) UserEntity(io.gravitee.rest.api.model.UserEntity) UserPayload(io.gravitee.cockpit.api.command.user.UserPayload) HashMap(java.util.HashMap) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) UpdateUserEntity(io.gravitee.rest.api.model.UpdateUserEntity) NewExternalUserEntity(io.gravitee.rest.api.model.NewExternalUserEntity) UserEntity(io.gravitee.rest.api.model.UserEntity) UserNotFoundException(io.gravitee.rest.api.service.exceptions.UserNotFoundException) UserReply(io.gravitee.cockpit.api.command.user.UserReply)

Example 7 with UserEntity

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);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 8 with UserEntity

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);
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) ApiEntity(io.gravitee.rest.api.model.api.ApiEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Example 9 with UserEntity

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);
}
Also used : ApplicationListItem(io.gravitee.rest.api.model.application.ApplicationListItem) ApplicationEntity(io.gravitee.rest.api.model.ApplicationEntity) GroupEntity(io.gravitee.rest.api.model.GroupEntity) PrimaryOwnerEntity(io.gravitee.rest.api.model.PrimaryOwnerEntity) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Before(org.junit.Before)

Example 10 with UserEntity

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());
}
Also used : RoleEntity(io.gravitee.rest.api.model.RoleEntity) User(io.gravitee.rest.api.portal.rest.model.User) Instant(java.time.Instant) MemberEntity(io.gravitee.rest.api.model.MemberEntity) Member(io.gravitee.rest.api.portal.rest.model.Member) Date(java.util.Date) UserEntity(io.gravitee.rest.api.model.UserEntity) Test(org.junit.Test)

Aggregations

UserEntity (io.gravitee.rest.api.model.UserEntity)57 Test (org.junit.Test)36 User (io.gravitee.rest.api.portal.rest.model.User)14 Response (javax.ws.rs.core.Response)14 RoleEntity (io.gravitee.rest.api.model.RoleEntity)13 UpdateUserEntity (io.gravitee.rest.api.model.UpdateUserEntity)12 ApiEntity (io.gravitee.rest.api.model.api.ApiEntity)12 Date (java.util.Date)10 PrimaryOwnerEntity (io.gravitee.rest.api.model.PrimaryOwnerEntity)9 UserService (io.gravitee.rest.api.service.UserService)9 NewExternalUserEntity (io.gravitee.rest.api.model.NewExternalUserEntity)8 List (java.util.List)8 Instant (java.time.Instant)7 UserDetails (io.gravitee.rest.api.idp.api.authentication.UserDetails)6 ApplicationEntity (io.gravitee.rest.api.model.ApplicationEntity)6 MembershipService (io.gravitee.rest.api.service.MembershipService)6 HashMap (java.util.HashMap)6 Collectors (java.util.stream.Collectors)6 MembershipPayload (io.gravitee.cockpit.api.command.membership.MembershipPayload)5 MembershipReply (io.gravitee.cockpit.api.command.membership.MembershipReply)5