Search in sources :

Example 6 with UserEntity

use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class AuthenticationSuccessListenerTest method shouldConnectFoundUser.

@Test
public void shouldConnectFoundUser() {
    when(eventMock.getAuthentication()).thenReturn(authenticationMock);
    when(authenticationMock.getPrincipal()).thenReturn(userDetailsMock);
    when(userDetailsMock.getUsername()).thenReturn(USERNAME);
    when(userServiceMock.create(any())).thenReturn(userEntity);
    when(userServiceMock.findByUsername(userDetailsMock.getUsername(), false)).thenReturn(new UserEntity());
    listener.onApplicationEvent(eventMock);
    verify(userServiceMock, times(1)).findByUsername(userDetailsMock.getUsername(), false);
    verify(userServiceMock, never()).create(any(NewExternalUserEntity.class), anyBoolean());
    verify(userServiceMock, times(1)).connect(userDetailsMock.getUsername());
}
Also used : NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 7 with UserEntity

use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class GoogleAuthenticationResource method processUser.

private Response processUser(final Map<String, Object> userInfo) {
    String username = (String) userInfo.get("email");
    // set user to Authentication Context
    UserDetails userDetails = new UserDetails(username, "", Collections.emptyList());
    userDetails.setEmail(username);
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities()));
    try {
        UserEntity registeredUser = userService.findByUsername(username, false);
        userDetails.setUsername(registeredUser.getId());
    } catch (UserNotFoundException unfe) {
        final NewExternalUserEntity newUser = new NewExternalUserEntity();
        newUser.setUsername(username);
        newUser.setSource(AuthenticationSource.GOOGLE.getName());
        newUser.setSourceId(userInfo.get("sub").toString());
        newUser.setFirstname(userInfo.get("given_name").toString());
        newUser.setLastname(userInfo.get("family_name").toString());
        newUser.setEmail(username);
        UserEntity createdUser = userService.create(newUser, true);
        userDetails.setUsername(createdUser.getId());
    }
    // User refresh
    UpdateUserEntity user = new UpdateUserEntity();
    user.setUsername(username);
    user.setPicture(userInfo.get("picture").toString());
    userService.update(user);
    return connectUser(userDetails.getUsername());
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) UserDetails(io.gravitee.management.idp.api.authentication.UserDetails) NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) UpdateUserEntity(io.gravitee.management.model.UpdateUserEntity) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) UserEntity(io.gravitee.management.model.UserEntity) NewExternalUserEntity(io.gravitee.management.model.NewExternalUserEntity) UpdateUserEntity(io.gravitee.management.model.UpdateUserEntity)

Example 8 with UserEntity

use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class EventServiceImpl method convert.

private EventEntity convert(Event event) {
    EventEntity eventEntity = new EventEntity();
    eventEntity.setId(event.getId());
    eventEntity.setType(io.gravitee.management.model.EventType.valueOf(event.getType().toString()));
    eventEntity.setPayload(event.getPayload());
    eventEntity.setParentId(event.getParentId());
    eventEntity.setProperties(event.getProperties());
    eventEntity.setCreatedAt(event.getCreatedAt());
    eventEntity.setUpdatedAt(event.getUpdatedAt());
    if (event.getProperties() != null) {
        final String userId = event.getProperties().get(Event.EventProperties.USER.getValue());
        if (userId != null && !userId.isEmpty()) {
            try {
                eventEntity.setUser(userService.findById(userId));
            } catch (UserNotFoundException unfe) {
                UserEntity user = new UserEntity();
                user.setSource("system");
                user.setId(userId);
                user.setUsername("system");
                eventEntity.setUser(user);
            }
        }
    }
    return eventEntity;
}
Also used : UserNotFoundException(io.gravitee.management.service.exceptions.UserNotFoundException) EventEntity(io.gravitee.management.model.EventEntity) NewEventEntity(io.gravitee.management.model.NewEventEntity) UserEntity(io.gravitee.management.model.UserEntity)

Example 9 with UserEntity

use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiService_CreateTest method shouldNotCreateForUserBecauseTechnicalException.

@Test(expected = TechnicalManagementException.class)
public void shouldNotCreateForUserBecauseTechnicalException() throws TechnicalException {
    when(apiRepository.findById(anyString())).thenThrow(TechnicalException.class);
    when(newApi.getName()).thenReturn(API_NAME);
    when(newApi.getVersion()).thenReturn("v1");
    when(newApi.getDescription()).thenReturn("Ma description");
    when(userService.findByUsername(USER_NAME, false)).thenReturn(new UserEntity());
    apiService.create(newApi, USER_NAME);
}
Also used : UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 10 with UserEntity

use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.

the class ApiService_CreateTest method shouldCreateWithDefaultPath.

@Test
public void shouldCreateWithDefaultPath() throws TechnicalException {
    when(api.getId()).thenReturn(API_ID);
    when(api.getName()).thenReturn(API_NAME);
    when(api.getVisibility()).thenReturn(Visibility.PRIVATE);
    when(apiRepository.findById(anyString())).thenReturn(Optional.empty());
    when(apiRepository.create(any())).thenReturn(api);
    when(newApi.getName()).thenReturn(API_NAME);
    when(newApi.getVersion()).thenReturn("v1");
    when(newApi.getDescription()).thenReturn("Ma description");
    when(newApi.getContextPath()).thenReturn("/context");
    UserEntity admin = new UserEntity();
    admin.setUsername(USER_NAME);
    when(userService.findById(admin.getUsername())).thenReturn(admin);
    final ApiEntity apiEntity = apiService.create(newApi, USER_NAME);
    assertNotNull(apiEntity);
    assertEquals(API_NAME, apiEntity.getName());
    assertNotNull(apiEntity.getPaths());
/*assertTrue("paths not empty", !apiEntity.getPaths().isEmpty());
        assertEquals("paths.size == 1", apiEntity.getPaths().size(), 1);
        assertEquals("path == /* ", apiEntity.getPaths().get(0).getPath(), "/*");*/
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) NewApiEntity(io.gravitee.management.model.NewApiEntity) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Aggregations

UserEntity (io.gravitee.management.model.UserEntity)21 Test (org.junit.Test)14 Membership (io.gravitee.repository.management.model.Membership)8 NewExternalUserEntity (io.gravitee.management.model.NewExternalUserEntity)6 RoleEntity (io.gravitee.management.model.RoleEntity)6 UserDetails (io.gravitee.management.idp.api.authentication.UserDetails)4 ApplicationEntity (io.gravitee.management.model.ApplicationEntity)4 UserNotFoundException (io.gravitee.management.service.exceptions.UserNotFoundException)4 MemberEntity (io.gravitee.management.model.MemberEntity)3 HashMap (java.util.HashMap)3 ApiEntity (io.gravitee.management.model.ApiEntity)2 GroupEntity (io.gravitee.management.model.GroupEntity)2 NewApiEntity (io.gravitee.management.model.NewApiEntity)2 UpdateUserEntity (io.gravitee.management.model.UpdateUserEntity)2 Date (java.util.Date)2 HashSet (java.util.HashSet)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 JWTSigner (com.auth0.jwt.JWTSigner)1 ApiModelEntity (io.gravitee.management.model.ApiModelEntity)1 EventEntity (io.gravitee.management.model.EventEntity)1