src/main/java/com/epam/reportportal/auth/integration/ldap/LdapUserReplicator.java
@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();
	}
/**
	 * 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
	 */
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
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())) {
			throw new UserSynchronizationException("User with login '" + userOptional.get().getLogin() + "' already exists");
		}
		return userOptional.get();
	}
@ 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 ( ) ) ) { throw new UserSynchronizationException ( "User with login '" + userOptional . get ( ) . getLogin ( ) + "' already exists" ) ; } return userOptional . get ( ) ; }
Replicates LDAP user to internal database (if does NOT exist).
Replicates LDAP user to internal database ( if does NOT exist ) .
//if user with such login exists, but it's not GitHub user than throw an exception