use of com.epam.ta.reportportal.entity.user.User in project service-authorization by reportportal.
the class GitHubUserReplicator method synchronizeUser.
public User synchronizeUser(String accessToken) {
GitHubClient gitHubClient = GitHubClient.withAccessToken(accessToken);
UserResource userResource = gitHubClient.getUser();
Optional<User> userOptional = userRepository.findByLogin(normalizeId(userResource.getLogin()));
BusinessRule.expect(userOptional, Optional::isPresent).verify(ErrorType.USER_NOT_FOUND, userResource.getLogin());
User user = userOptional.get();
BusinessRule.expect(user.getUserType(), userType -> Objects.equals(userType, UserType.GITHUB)).verify(ErrorType.INCORRECT_AUTHENTICATION_TYPE, "User '" + userResource.getLogin() + "' is not GitHUB user");
if (StringUtils.isNotBlank(userResource.getName())) {
user.setFullName(userResource.getName());
}
String email = userResource.getEmail();
if (Strings.isNullOrEmpty(email)) {
email = retrieveEmail(gitHubClient).orElseThrow(() -> new UserSynchronizationException("User 'email' has not been provided"));
}
email = normalizeId(email);
if (!user.getEmail().equals(email)) {
checkEmail(email);
user.setEmail(email);
}
Metadata metadata = ofNullable(user.getMetadata()).orElse(new Metadata(Maps.newHashMap()));
metadata.getMetadata().put("synchronizationDate", Date.from(ZonedDateTime.now(ZoneOffset.UTC).toInstant()));
user.setMetadata(metadata);
uploadAvatar(gitHubClient, user, userResource.getAvatarUrl());
userRepository.save(user);
return user;
}
use of com.epam.ta.reportportal.entity.user.User in project service-authorization by reportportal.
the class UiAuthenticationSuccessEventHandler method onApplicationEvent.
@EventListener
@Transactional
public void onApplicationEvent(UiUserSignedInEvent event) {
String username = event.getAuthentication().getName();
userRepository.updateLastLoginDate(LocalDateTime.ofInstant(Instant.ofEpochMilli(event.getTimestamp()), ZoneOffset.UTC), username);
if (MapUtils.isEmpty(acquireUser(event.getAuthentication()).getProjectDetails())) {
User user = userRepository.findByLogin(username).orElseThrow(() -> new ReportPortalException(ErrorType.USER_NOT_FOUND, username));
Project project = personalProjectService.generatePersonalProject(user);
user.getProjects().addAll(project.getUsers());
}
}
use of com.epam.ta.reportportal.entity.user.User in project service-authorization by reportportal.
the class LdapUserReplicator method replicateUser.
/**
* Replicates LDAP user to internal database (if does NOT exist). Creates personal project for that user
*
* @param name Username
* @param ctx LDAP context
* @param syncAttributes Synchronization Attributes
* @return Internal User representation
*/
@Transactional
public User replicateUser(String name, DirContextOperations ctx, Map<String, String> syncAttributes) {
String emailAttribute = ofNullable(syncAttributes.get(LdapParameter.EMAIL_ATTRIBUTE.getParameterName())).orElseThrow(() -> new UserSynchronizationException("Email attribute not provided"));
String email = (String) ctx.getObjectAttribute(emailAttribute);
if (isNullOrEmpty(email)) {
throw new UserSynchronizationException("Email not provided");
}
email = normalizeId(email);
String login = CROP_DOMAIN.apply(name);
Optional<User> userOptional = userRepository.findByLogin(login);
if (userOptional.isEmpty()) {
User newUser = new User();
newUser.setLogin(login);
ofNullable(syncAttributes.get(LdapParameter.FULL_NAME_ATTRIBUTE.getParameterName())).filter(StringUtils::isNotBlank).flatMap(it -> ofNullable(ctx.getStringAttribute(it))).ifPresent(newUser::setFullName);
ofNullable(syncAttributes.get(LdapParameter.PHOTO_ATTRIBUTE.getParameterName())).filter(StringUtils::isNotBlank).flatMap(it -> ofNullable(ctx.getObjectAttribute(it)));
checkEmail(email);
newUser.setEmail(email);
newUser.setMetadata(defaultMetaData());
newUser.setUserType(UserType.LDAP);
newUser.setRole(UserRole.USER);
newUser.setExpired(false);
final Project project = generatePersonalProject(newUser);
newUser.getProjects().add(project.getUsers().iterator().next());
userRepository.save(newUser);
return newUser;
} else if (!UserType.LDAP.equals(userOptional.get().getUserType())) {
// if user with such login exists, but it's not GitHub user than throw an exception
throw new UserSynchronizationException("User with login '" + userOptional.get().getLogin() + "' already exists");
}
return userOptional.get();
}
use of com.epam.ta.reportportal.entity.user.User in project service-authorization by reportportal.
the class SamlUserReplicator method replicateUser.
public User replicateUser(ReportPortalSamlAuthentication samlAuthentication) {
String userName = CROP_DOMAIN.apply(samlAuthentication.getPrincipal());
Optional<User> userOptional = userRepository.findByLogin(userName);
if (userOptional.isPresent()) {
return userOptional.get();
}
IntegrationType samlIntegrationType = integrationTypeRepository.findByName(AuthIntegrationType.SAML.getName()).orElseThrow(() -> new ReportPortalException(ErrorType.AUTH_INTEGRATION_NOT_FOUND, AuthIntegrationType.SAML.getName()));
List<Integration> providers = integrationRepository.findAllGlobalByType(samlIntegrationType);
Optional<Integration> samlProvider = providers.stream().filter(provider -> {
Optional<String> idpUrlOptional = SamlParameter.IDP_URL.getParameter(provider);
return idpUrlOptional.isPresent() && idpUrlOptional.get().equalsIgnoreCase(samlAuthentication.getIssuer());
}).findFirst();
User user = new User();
user.setLogin(userName);
List<Attribute> details = samlAuthentication.getDetails();
if (samlProvider.isPresent()) {
populateUserDetailsIfSettingsArePresent(user, samlProvider.get(), details);
} else {
populateUserDetails(user, details);
}
user.setUserType(UserType.SAML);
user.setRole(UserRole.USER);
user.setExpired(false);
Project project = generatePersonalProject(user);
user.getProjects().add(project.getUsers().iterator().next());
user.setMetadata(defaultMetaData());
userRepository.save(user);
return user;
}
use of com.epam.ta.reportportal.entity.user.User in project service-authorization by reportportal.
the class GitHubUserReplicator method createUser.
private User createUser(UserResource userResource, GitHubClient gitHubClient) {
User user = new User();
String login = normalizeId(userResource.getLogin());
user.setLogin(login);
updateUser(user, userResource, gitHubClient);
user.setUserType(UserType.GITHUB);
user.setRole(UserRole.USER);
user.setExpired(false);
final Project project = generatePersonalProject(user);
user.getProjects().addAll(project.getUsers());
return user;
}
Aggregations