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());
}
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());
}
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());
}
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());
}
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());
}
Aggregations