<commit_before>package at.fhj.swd13.pse.domain.user;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;

import org.jboss.logging.Logger;

import at.fhj.swd13.pse.db.DbContext;
import at.fhj.swd13.pse.db.entity.Document;
import at.fhj.swd13.pse.db.entity.Person;
import at.fhj.swd13.pse.db.entity.PersonRelation;
import at.fhj.swd13.pse.db.entity.PersonTag;
import at.fhj.swd13.pse.db.entity.Tag;
import at.fhj.swd13.pse.domain.ServiceException;
import at.fhj.swd13.pse.domain.document.DocumentService;
import at.fhj.swd13.pse.domain.tag.TagService;
import at.fhj.swd13.pse.plumbing.MailService;
import at.fhj.swd13.pse.service.ServiceBase;

/**
 * User Service, object that provides all higher level logic for managing users
 * 
 */
@Stateless
@Local(UserService.class)
public class UserServiceImpl extends ServiceBase implements UserService {

	@Inject
	private PasswordStrengthValidator passwordStrengthValidator;

	@Inject
	private TagService tagService;

	@Inject
	private Logger logger;

	@Inject
	private PasswordCreator passwordCreator;

	@Inject
	private MailService mailService;
	
	@Inject
	private DocumentService documentService;

	/**
	 * Create an instance of the user service
	 * 
	 * @param dbContext
	 *            the connection to the persistent storage with which to work
	 */
	public UserServiceImpl() {
		super();
	}

	public UserServiceImpl(DbContext dbContext) {
		super(dbContext);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.fhj.swd13.pse.domain.user.UserService#loginUser(java.lang.String, java.lang.String)
	 */
	@Override
	public Person loginUser(final String username, final String plainPassword, String sessionId) {
		try {
			Person p = dbContext.getPersonDAO().getByUsername(username);
	
			
			if (p != null && p.getIsLoginAllowed() && p.getIsActive() && p.isMatchingPassword(plainPassword)) {
				p.setIsOnline(true);
				p.setCurrentSessionId(sessionId);
				return p;
			}
		} catch (Throwable ex) {
			logger.info("[UserService] loginUser failed for " + username + " : " + ex.getMessage(), ex);
			throw new ServiceException(ex);
		}
		return null;
	}

	@Override
	public void logoutUser(String username) {
		try {
			Person p = dbContext.getPersonDAO().getByUsername(username);
			p.setIsOnline(false);
			p.setCurrentSessionId(null);
		} catch (Throwable ex) {
			logger.info("[UserService] logoutUser failed for " + username + " : " + ex.getMessage(), ex);
			throw new ServiceException(ex);<commit_msg>fixe npe while logging out unknown user<commit_after>package at.fhj.swd13.pse.domain.user;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.inject.Inject;

import org.jboss.logging.Logger;

import at.fhj.swd13.pse.db.DbContext;
import at.fhj.swd13.pse.db.entity.Document;
import at.fhj.swd13.pse.db.entity.Person;
import at.fhj.swd13.pse.db.entity.PersonRelation;
import at.fhj.swd13.pse.db.entity.PersonTag;
import at.fhj.swd13.pse.db.entity.Tag;
import at.fhj.swd13.pse.domain.ServiceException;
import at.fhj.swd13.pse.domain.document.DocumentService;
import at.fhj.swd13.pse.domain.tag.TagService;
import at.fhj.swd13.pse.plumbing.MailService;
import at.fhj.swd13.pse.service.ServiceBase;

/**
 * User Service, object that provides all higher level logic for managing users
 * 
 */
@Stateless
@Local(UserService.class)
public class UserServiceImpl extends ServiceBase implements UserService {

	@Inject
	private PasswordStrengthValidator passwordStrengthValidator;

	@Inject
	private TagService tagService;

	@Inject
	private Logger logger;

	@Inject
	private PasswordCreator passwordCreator;

	@Inject
	private MailService mailService;
	
	@Inject
	private DocumentService documentService;

	/**
	 * Create an instance of the user service
	 * 
	 * @param dbContext
	 *            the connection to the persistent storage with which to work
	 */
	public UserServiceImpl() {
		super();
	}

	public UserServiceImpl(DbContext dbContext) {
		super(dbContext);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see at.fhj.swd13.pse.domain.user.UserService#loginUser(java.lang.String, java.lang.String)
	 */
	@Override
	public Person loginUser(final String username, final String plainPassword, String sessionId) {
		try {
			Person p = dbContext.getPersonDAO().getByUsername(username);
	
			
			if (p != null && p.getIsLoginAllowed() && p.getIsActive() && p.isMatchingPassword(plainPassword)) {
				p.setIsOnline(true);
				p.setCurrentSessionId(sessionId);
				return p;
			}
		} catch (Throwable ex) {
			logger.info("[UserService] loginUser failed for " + username + " : " + ex.getMessage(), ex);
			throw new ServiceException(ex);
		}
		return null;
	}

	@Override
	public void logoutUser(String username) {
		try {
			Person p = dbContext.getPersonDAO().getByUsername(username);
			if ( p != null ) {
				p.setIsOnline(false);
				p.setCurrentSessionId(null);
			} else {
				logger.warn("[UserService] cannot logout unkown user " + username );
			}
		} catch (Throwable ex) {
			logger.info("[UserService] logoutUser failed for " + username + " : " + ex.getMessage(), ex);
			throw new ServiceException(ex);
{'fraction_non_alphanumeric': 0.11695568400770713, 'fraction_numerical': 0.01001926782273603, 'mean_word_length': 4.206619859578736, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7590365', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1738, 'n_words': 366}