<commit_before>package apolo.web.config;


import apolo.business.service.UserService;
import apolo.common.config.model.ApplicationProperties;
import apolo.common.exception.AccessDeniedException;
import apolo.common.util.MessageBundle;
import apolo.common.util.ThreadLocalContextUtil;
import apolo.data.enums.Status;
import apolo.data.model.User;
import apolo.security.CurrentUser;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

@Component
public class UserAuthenticationProvider implements AuthenticationProvider {

	@Autowired
	private UserService userService;
	
	@Autowired
	private ApplicationProperties applicationProperties;
	
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		User user = userService.loadByUsernameAndPassword(
				authentication.getName(), 
				authentication.getCredentials().toString() 
			);
		
		String tenant = ThreadLocalContextUtil.getTenantId();
		
		if (tenant == null || !tenant.isEmpty()) {
			ThreadLocalContextUtil.setTenantId(applicationProperties.getDefaultTenant());
		}

		if (user != null) {
			if (user.getTenant() != null
					&& user.getTenant().getStatus().equals(Status.LOCKED)) {
				String message = MessageBundle.getMessageBundle("error.403.0");
				throw new BadCredentialsException(message, new AccessDeniedException(0, message));
			}
			
			// Set permission to see only the code screen.
			Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
			
			authorities = userService.loadUserAuthorities(user);
			
			ThreadLocalContextUtil.setTenantId(user.getTenant().getUrl());

			return new CurrentUser(
					user.getId(), 
					user.getEmail(), 
					user.getPassword().toLowerCase(), 
					user, 
					authorities
				);
		} else {
			throw new BadCredentialsException(MessageBundle.getMessageBundle("error.403.1"));
		}

	}

	@SuppressWarnings("rawtypes")
	public boolean supports(Class authentication) {
		return (UsernamePasswordAuthenticationToken.class
				.isAssignableFrom(authentication));
	}
}
<commit_msg>bugfix: usuários bloqueados estavam conseguindo autenticar.<commit_after>package apolo.web.config;


import apolo.business.service.UserService;
import apolo.common.config.model.ApplicationProperties;
import apolo.common.exception.AccessDeniedException;
import apolo.common.util.MessageBundle;
import apolo.common.util.ThreadLocalContextUtil;
import apolo.data.enums.Status;
import apolo.data.enums.UserStatus;
import apolo.data.model.User;
import apolo.security.CurrentUser;

import java.util.ArrayList;
import java.util.Collection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Component;

@Component
public class UserAuthenticationProvider implements AuthenticationProvider {

	@Autowired
	private UserService userService;
	
	@Autowired
	private ApplicationProperties applicationProperties;
	
	public Authentication authenticate(Authentication authentication) throws AuthenticationException {
		User user = userService.loadByUsernameAndPassword(
				authentication.getName(), 
				authentication.getCredentials().toString() 
			);
		
		String tenant = ThreadLocalContextUtil.getTenantId();
		
		if (tenant == null || !tenant.isEmpty()) {
			ThreadLocalContextUtil.setTenantId(applicationProperties.getDefaultTenant());
		}

		if (user != null) {
			if (UserStatus.LOCKED.equals(user.getStatus())) {
				String message = MessageBundle.getMessageBundle("error.403.2");
				throw new BadCredentialsException(message, new AccessDeniedException(0, message));
			} else if (user.getTenant() != null
					&& Status.LOCKED.equals(user.getTenant().getStatus())) {
				String message = MessageBundle.getMessageBundle("error.403.0");
				throw new BadCredentialsException(message, new AccessDeniedException(0, message));
			}
			
			// Set permission to see only the code screen.
			Collection<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
			
			authorities = userService.loadUserAuthorities(user);
			
			ThreadLocalContextUtil.setTenantId(user.getTenant().getUrl());

			return new CurrentUser(
					user.getId(), 
					user.getEmail(), 
					user.getPassword().toLowerCase(), 
					user, 
					authorities
				);
		} else {
			throw new BadCredentialsException(MessageBundle.getMessageBundle("error.403.1"));
		}

	}

	@SuppressWarnings("rawtypes")
	public boolean supports(Class authentication) {
		return (UsernamePasswordAuthenticationToken.class
				.isAssignableFrom(authentication));
	}
}

{'fraction_non_alphanumeric': 0.09728052564336558, 'fraction_numerical': 0.004197846322321591, 'mean_word_length': 5.650485436893204, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7061090', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1447, 'n_words': 263}