use of com.epam.ta.reportportal.entity.project.Project in project commons-dao by reportportal.
the class UserRepositoryTest method createUserTest.
@SuppressWarnings("OptionalGetWithoutIsPresent")
@Test
void createUserTest() {
User reg = new User();
reg.setEmail("email.com");
reg.setFullName("test");
reg.setLogin("created");
reg.setPassword("new");
reg.setUserType(UserType.INTERNAL);
reg.setRole(UserRole.USER);
Map<String, Object> map = new HashMap<>();
map.put("last_login", new Date());
reg.setMetadata(new Metadata(map));
Project defaultProject = projectRepository.findByName("superadmin_personal").get();
Set<ProjectUser> projectUsers = defaultProject.getUsers();
projectUsers.add(new ProjectUser().withProjectRole(ProjectRole.CUSTOMER).withUser(reg).withProject(defaultProject));
defaultProject.setUsers(projectUsers);
userRepository.save(reg);
final Optional<User> created = userRepository.findByLogin("created");
assertTrue(created.isPresent());
}
use of com.epam.ta.reportportal.entity.project.Project in project commons-dao by reportportal.
the class PersonalProjectService method generatePersonalProject.
/**
* Generates personal project for provided user
*
* @param user User project should be created for
* @return Built Project object
*/
public Project generatePersonalProject(User user) {
Project project = new Project();
project.setName(generatePersonalProjectName(user.getLogin()));
project.setCreationDate(Date.from(ZonedDateTime.now().toInstant()));
project.setProjectType(ProjectType.PERSONAL);
ProjectUser projectUser = new ProjectUser().withUser(user).withProjectRole(ProjectRole.PROJECT_MANAGER).withProject(project);
project.setUsers(Sets.newHashSet(projectUser));
project.setMetadata(new Metadata(Collections.singletonMap("additional_info", "Personal project of " + (isNullOrEmpty(user.getFullName()) ? user.getLogin() : user.getFullName()))));
project.setProjectAttributes(defaultProjectAttributes(project, attributeRepository.getDefaultProjectAttributes()));
project.setProjectIssueTypes(defaultIssueTypes(project, issueTypeRepository.getDefaultIssueTypes()));
return project;
}
use of com.epam.ta.reportportal.entity.project.Project in project service-authorization by reportportal.
the class GitHubUserReplicator method replicateUser.
/**
* Replicates GitHub user to internal database (if does NOT exist). Updates if exist. Creates personal project for that user
*
* @param userResource GitHub user to be replicated
* @param gitHubClient Configured github client
* @return Internal User representation
*/
@Transactional
public ReportPortalUser replicateUser(UserResource userResource, GitHubClient gitHubClient) {
String login = normalizeId(userResource.getLogin());
User user = userRepository.findByLogin(login).map(u -> {
if (UserType.GITHUB.equals(u.getUserType())) {
updateUser(u, userResource, gitHubClient);
} else {
// if user with such login exists, but it's not GitHub user than throw an exception
throw new UserSynchronizationException("User with login '" + u.getLogin() + "' already exists");
}
return u;
}).orElseGet(() -> userRepository.save(createUser(userResource, gitHubClient)));
return ReportPortalUser.userBuilder().fromUser(user);
}
use of com.epam.ta.reportportal.entity.project.Project 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.project.Project 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();
}
Aggregations