Search in sources :

Example 16 with UserEntity

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

the class GitHubAuthenticationResource method processUser.

private Response processUser(final Map<String, Object> userInfo) {
    String username = (String) userInfo.get("email");
    if (username == null) {
        throw new BadRequestException("No public email linked to your GitHub account");
    }
    // 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.GITHUB.getName());
        newUser.setSourceId(userInfo.get("id").toString());
        String[] partNames = userInfo.get("name").toString().split(" ");
        newUser.setLastname(partNames[0]);
        newUser.setFirstname(partNames[1]);
        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("avatar_url").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) BadRequestException(javax.ws.rs.BadRequestException) 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 17 with UserEntity

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

the class TicketServiceImpl method create.

@Override
public void create(final String userId, final NewTicketEntity ticketEntity) {
    if (!enabled) {
        throw new SupportUnavailableException();
    }
    LOGGER.info("Creating a support ticket: {}", ticketEntity);
    final Map<String, Object> parameters = new HashMap<>();
    final UserEntity user = userService.findById(userId);
    if (user.getEmail() == null) {
        throw new EmailRequiredException(userId);
    }
    parameters.put("user", user);
    final String emailTo;
    if (ticketEntity.getApi() == null) {
        final MetadataEntity emailMetadata = metadataService.findDefaultByKey(METADATA_EMAIL_SUPPORT_KEY);
        if (emailMetadata == null) {
            throw new IllegalStateException("The support email metadata has not been found");
        }
        emailTo = emailMetadata.getValue();
    } else {
        final ApiModelEntity api = apiService.findByIdForTemplates(ticketEntity.getApi());
        final String apiMetadataEmailSupport = api.getMetadata().get(METADATA_EMAIL_SUPPORT_KEY);
        if (apiMetadataEmailSupport == null) {
            throw new IllegalStateException("The support email API metadata has not been found");
        }
        emailTo = apiMetadataEmailSupport;
        parameters.put("api", api);
    }
    if (DEFAULT_METADATA_EMAIL_SUPPORT.equals(emailTo)) {
        throw new IllegalStateException("The support email API metadata has not been changed");
    }
    if (ticketEntity.getApplication() != null) {
        parameters.put("application", applicationService.findById(ticketEntity.getApplication()));
    }
    parameters.put("content", ticketEntity.getContent().replaceAll("(\r\n|\n)", "<br />"));
    emailService.sendEmailNotification(new EmailNotificationBuilder().from(user.getEmail()).fromName(user.getFirstname() + ' ' + user.getLastname()).to(emailTo).subject(ticketEntity.getSubject()).copyToSender(ticketEntity.isCopyToSender()).template(SUPPORT_TICKET).params(parameters).build());
}
Also used : MetadataEntity(io.gravitee.management.model.MetadataEntity) HashMap(java.util.HashMap) ApiModelEntity(io.gravitee.management.model.ApiModelEntity) EmailRequiredException(io.gravitee.management.service.exceptions.EmailRequiredException) EmailNotificationBuilder(io.gravitee.management.service.builder.EmailNotificationBuilder) UserEntity(io.gravitee.management.model.UserEntity) SupportUnavailableException(io.gravitee.management.service.exceptions.SupportUnavailableException)

Example 18 with UserEntity

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

the class ApiService_CreateTest method shouldCreateForUser.

@Test
public void shouldCreateForUser() throws TechnicalException {
    when(api.getId()).thenReturn(API_ID);
    when(api.getName()).thenReturn(API_NAME);
    when(api.getVisibility()).thenReturn(Visibility.PRIVATE);
    when(api.getLifecycleState()).thenReturn(LifecycleState.STARTED);
    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");
    when(userService.findById(USER_NAME)).thenReturn(new UserEntity());
    when(groupService.findByEvent(any())).thenReturn(Collections.emptySet());
    final ApiEntity apiEntity = apiService.create(newApi, USER_NAME);
    assertNotNull(apiEntity);
    assertEquals(API_NAME, apiEntity.getName());
}
Also used : ApiEntity(io.gravitee.management.model.ApiEntity) NewApiEntity(io.gravitee.management.model.NewApiEntity) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 19 with UserEntity

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

the class ApplicationService_FindByUserTest method shouldFindByUser.

@Test
public void shouldFindByUser() throws Exception {
    when(appMembership.getReferenceId()).thenReturn(APPLICATION_ID);
    when(application.getId()).thenReturn(APPLICATION_ID);
    when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.APPLICATION)).thenReturn(Collections.singleton(appMembership));
    when(applicationRepository.findByIds(Collections.singletonList(APPLICATION_ID))).thenReturn(Collections.singleton(application));
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.GROUP)).thenReturn(Collections.emptySet());
    when(applicationRepository.findByGroups(Collections.emptyList(), ApplicationStatus.ACTIVE)).thenReturn(Collections.emptySet());
    Membership po = new Membership(USERNAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
    when(userService.findByUsername(USERNAME, false)).thenReturn(new UserEntity());
    Set<ApplicationEntity> apps = applicationService.findByUser(USERNAME);
    Assert.assertNotNull(apps);
    Assert.assertFalse("should find app", apps.isEmpty());
    Assert.assertEquals(APPLICATION_ID, apps.iterator().next().getId());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Membership(io.gravitee.repository.management.model.Membership) UserEntity(io.gravitee.management.model.UserEntity) Test(org.junit.Test)

Example 20 with UserEntity

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

the class ApplicationService_FindByUserTest method shouldNotFindByUserBecauseOfArchived.

@Test
public void shouldNotFindByUserBecauseOfArchived() throws Exception {
    when(appMembership.getReferenceId()).thenReturn(APPLICATION_ID);
    when(application.getId()).thenReturn(APPLICATION_ID);
    when(application.getStatus()).thenReturn(ApplicationStatus.ARCHIVED);
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.APPLICATION)).thenReturn(Collections.singleton(appMembership));
    when(applicationRepository.findByIds(Collections.singletonList(APPLICATION_ID))).thenReturn(Collections.singleton(application));
    when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.GROUP)).thenReturn(Collections.emptySet());
    when(applicationRepository.findByGroups(Collections.emptyList(), ApplicationStatus.ACTIVE)).thenReturn(Collections.emptySet());
    Membership po = new Membership(USERNAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
    po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
    when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
    when(userService.findByUsername(USERNAME, false)).thenReturn(new UserEntity());
    Set<ApplicationEntity> apps = applicationService.findByUser(USERNAME);
    Assert.assertNotNull(apps);
    Assert.assertTrue("should not find app", apps.isEmpty());
}
Also used : ApplicationEntity(io.gravitee.management.model.ApplicationEntity) Membership(io.gravitee.repository.management.model.Membership) 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