<commit_before>package de.ciber.comics;

import java.util.Collection;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Component;

import de.ciber.comics.db.UserRepository;
import de.ciber.comics.db.entity.Address;
import de.ciber.comics.db.entity.ContactDetails;
import de.ciber.comics.db.entity.RegistrationDetails;
import de.ciber.comics.db.entity.User;
import de.ciber.comics.db.entity.UserRole;
import de.ciber.comics.db.entity.UserStatus;
import de.ciber.comics.view.model.UserSession;

@Component
public class RegistrationService {
	
	private static final Logger logger = LoggerFactory.getLogger(RegistrationService.class);
	
	@Autowired
	UserRepository repository;
	
	@Autowired
	private JavaMailSender mailer;
	
	@Autowired
	private SimpleMailMessage templateMsg;
	
	@Autowired
	ReloadableResourceBundleMessageSource props;
	
	private static Md5PasswordEncoder pwdEncoder;
	
	static {
		pwdEncoder = new Md5PasswordEncoder();
		pwdEncoder.setEncodeHashAsBase64(true);
	}
	
	public Collection<User> getRegisteredUsers() {
		return repository.findAll();
	}
	
	public void registerUser(RegistrationInfo registrationInfo, String clientIpAddress) {
		User newUser = new User();
		
		// copy data from 'registrationInfo' into 'newUser'
		newUser.setFirstname(registrationInfo.getFirstname());
		newUser.setLastname(registrationInfo.getLastname());
		newUser.setTitle(registrationInfo.getTitle());
		newUser.setPassword(pwdEncoder.encodePassword(registrationInfo.getPassword(), registrationInfo.getUsername()));
		newUser.setRole(UserRole.ROLE_USER);
		newUser.setUsername(registrationInfo.getUsername());
		
		if (registrationInfo.hasAvatarImage()) {
			newUser.setAvatarImage(registrationInfo.getAvatarImage());
		}

		// user has to opt-in via a link to complete registration
		newUser.setStatus(UserStatus.REGISTRATION_NOT_COMPLETE);
		String activationKey = pwdEncoder.encodePassword(registrationInfo.getEmail(), 42).replace('=', 'x');
				
		Address newAddress = new Address();
		newAddress.setStreet(registrationInfo.getStreet());
		newAddress.setHouseNumber(registrationInfo.getHousenumber());
		newAddress.setPostalCode(registrationInfo.getPostalcode());
		newAddress.setTown(registrationInfo.getTown());
		newAddress.setCareOf(registrationInfo.getCareof());
		newAddress.setCompany(registrationInfo.getCompany());
		
		ContactDetails newContactDetails = new ContactDetails();
		
		RegistrationDetails newRegistrationDetails = new RegistrationDetails();
		newRegistrationDetails.setDate(System.currentTimeMillis());
		newRegistrationDetails.setIp(clientIpAddress);
		newRegistrationDetails.setActivationKey(activationKey);
		
		newUser.setAddress(newAddress);
		newUser.setContactDetails(newContactDetails);
		newUser.setRegistrationDetails(newRegistrationDetails);
		
		newUser = repository.save(newUser);
		
		// send confirmation link via email
		String confirmationLink = UserSession.SYSTEM_BASE_URL + "activateAccount.html?key=" + activationKey
				+ "&id=" + newUser.getId();
		String mailText = props.getMessage("email.confirmaccount.text", new Object[]{
				registrationInfo.getFirstname(),
				registrationInfo.getLastname(),
				confirmationLink}, Locale.getDefault());
		String mailSubject = props.getMessage("email.confirmaccount.subject", null, Locale.getDefault());
		
		SimpleMailMessage message = new SimpleMailMessage(templateMsg);
		message.setTo(registrationInfo.getEmail());
		message.setText(mailText);
		message.setSubject(mailSubject);<commit_msg>Added URL encoding for activation key<commit_after>package de.ciber.comics;

import java.net.URLEncoder;
import java.util.Collection;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Component;

import de.ciber.comics.db.UserRepository;
import de.ciber.comics.db.entity.Address;
import de.ciber.comics.db.entity.ContactDetails;
import de.ciber.comics.db.entity.RegistrationDetails;
import de.ciber.comics.db.entity.User;
import de.ciber.comics.db.entity.UserRole;
import de.ciber.comics.db.entity.UserStatus;
import de.ciber.comics.view.model.UserSession;

@Component
public class RegistrationService {
	
	private static final Logger logger = LoggerFactory.getLogger(RegistrationService.class);
	
	@Autowired
	UserRepository repository;
	
	@Autowired
	private JavaMailSender mailer;
	
	@Autowired
	private SimpleMailMessage templateMsg;
	
	@Autowired
	ReloadableResourceBundleMessageSource props;
	
	private static Md5PasswordEncoder pwdEncoder;
	
	static {
		pwdEncoder = new Md5PasswordEncoder();
		pwdEncoder.setEncodeHashAsBase64(true);
	}
	
	public Collection<User> getRegisteredUsers() {
		return repository.findAll();
	}
	
	public void registerUser(RegistrationInfo registrationInfo, String clientIpAddress) {
		User newUser = new User();
		
		// copy data from 'registrationInfo' into 'newUser'
		newUser.setFirstname(registrationInfo.getFirstname());
		newUser.setLastname(registrationInfo.getLastname());
		newUser.setTitle(registrationInfo.getTitle());
		newUser.setPassword(<PASSWORD>Encoder.encodePassword(registrationInfo.getPassword(), registrationInfo.getUsername()));
		newUser.setRole(UserRole.ROLE_USER);
		newUser.setUsername(registrationInfo.getUsername());
		
		if (registrationInfo.hasAvatarImage()) {
			newUser.setAvatarImage(registrationInfo.getAvatarImage());
		}

		// user has to opt-in via a link to complete registration
		newUser.setStatus(UserStatus.REGISTRATION_NOT_COMPLETE);
		String activationKey = pwdEncoder.encodePassword(registrationInfo.getEmail(), 42).replace('=', 'x');
				
		Address newAddress = new Address();
		newAddress.setStreet(registrationInfo.getStreet());
		newAddress.setHouseNumber(registrationInfo.getHousenumber());
		newAddress.setPostalCode(registrationInfo.getPostalcode());
		newAddress.setTown(registrationInfo.getTown());
		newAddress.setCareOf(registrationInfo.getCareof());
		newAddress.setCompany(registrationInfo.getCompany());
		
		ContactDetails newContactDetails = new ContactDetails();
		
		RegistrationDetails newRegistrationDetails = new RegistrationDetails();
		newRegistrationDetails.setDate(System.currentTimeMillis());
		newRegistrationDetails.setIp(clientIpAddress);
		newRegistrationDetails.setActivationKey(activationKey);
		
		newUser.setAddress(newAddress);
		newUser.setContactDetails(newContactDetails);
		newUser.setRegistrationDetails(newRegistrationDetails);
		
		newUser = repository.save(newUser);
		
		// send confirmation link via email
		String confirmationLink = UserSession.SYSTEM_BASE_URL + "activateAccount.html?key=" + URLEncoder.encode(activationKey)
				+ "&id=" + newUser.getId();

		String mailText = props.getMessage("email.confirmaccount.text", new Object[]{
				registrationInfo.getFirstname(),
				registrationInfo.getLastname(),
				confirmationLink}, Locale.getDefault());
		String mailSubject = props.getMessage("email.confirmaccount.subject", null, Locale.getDefault());
		
		SimpleMailMessage message = new SimpleMailMessage(templateMsg);
		message.setTo(registrationInfo.getEmail());
		message.setText(mailText);
		message.setSubject(mailSubject);
{'fraction_non_alphanumeric': 0.10245437382001259, 'fraction_numerical': 0.002265575833857772, 'mean_word_length': 6.596558317399618, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6100018', 'n_tokens_mistral': 2444, 'n_tokens_neox': 2203, 'n_words': 364}