<commit_before>package org.carlspring.strongbox.users.service.impl;

import org.carlspring.strongbox.data.CacheName;
import org.carlspring.strongbox.users.UsersFileManager;
import org.carlspring.strongbox.users.domain.User;
import org.carlspring.strongbox.users.domain.Users;
import org.carlspring.strongbox.users.dto.UserAccessModelDto;
import org.carlspring.strongbox.users.dto.UserDto;
import org.carlspring.strongbox.users.dto.UsersDto;
import org.carlspring.strongbox.users.security.AuthoritiesProvider;
import org.carlspring.strongbox.users.security.SecurityTokenProvider;
import org.carlspring.strongbox.users.service.UserService;

import javax.inject.Inject;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.jose4j.lang.JoseException;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author <NAME>
 * @author <NAME>
 */
@Service
public class UserServiceImpl
        implements UserService
{

    private final ReadWriteLock usersLock = new ReentrantReadWriteLock();

    @Inject
    private PasswordEncoder passwordEncoder;

    @Inject
    private SecurityTokenProvider tokenProvider;

    @Inject
    private UsersFileManager usersFileManager;

    @Inject
    private AuthoritiesProvider authoritiesProvider;

    /**
     * Yes, this is a state object.
     * It is protected by the {@link #usersLock} here
     * and should not be exposed to the world.
     */
    private UsersDto users;

    @Override
    public Users findAll()
    {
        final Lock readLock = usersLock.readLock();
        readLock.lock();

        try
        {
            return new Users(users);
        }
        finally
        {
            readLock.unlock();
        }
    }

    @Override
    @Cacheable(cacheNames = CacheName.User.USERS, key = "#p0")
    public User findByUserName(final String username)
    {
        final Lock readLock = usersLock.readLock();
        readLock.lock();

        try
        {
            Optional<UserDto> optionalUserDto = users.findByUserName(username);

            if (optionalUserDto.isPresent())
            {
                UserDto userDto = optionalUserDto.get();

                Set<String> authorities = userDto.getRoles()
                                                 .stream()
                                                 .map(this::getGrantedAuthorities)
                                                 .map(this::getAuthoritiesAsString)
                                                 .flatMap(Collection::stream)
                                                 .collect(Collectors.toCollection(HashSet::new));

                if(authorities.size() > 0) {<commit_msg>SB-1199 Cosmetic fixes.<commit_after>package org.carlspring.strongbox.users.service.impl;

import org.carlspring.strongbox.data.CacheName;
import org.carlspring.strongbox.users.UsersFileManager;
import org.carlspring.strongbox.users.domain.User;
import org.carlspring.strongbox.users.domain.Users;
import org.carlspring.strongbox.users.dto.UserAccessModelDto;
import org.carlspring.strongbox.users.dto.UserDto;
import org.carlspring.strongbox.users.dto.UsersDto;
import org.carlspring.strongbox.users.security.AuthoritiesProvider;
import org.carlspring.strongbox.users.security.SecurityTokenProvider;
import org.carlspring.strongbox.users.service.UserService;

import javax.inject.Inject;
import java.util.*;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.apache.commons.lang.StringUtils;
import org.jose4j.lang.JoseException;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

/**
 * @author <NAME>
 * @author <NAME>
 */
@Service
public class UserServiceImpl
        implements UserService
{

    private final ReadWriteLock usersLock = new ReentrantReadWriteLock();

    @Inject
    private PasswordEncoder passwordEncoder;

    @Inject
    private SecurityTokenProvider tokenProvider;

    @Inject
    private UsersFileManager usersFileManager;

    @Inject
    private AuthoritiesProvider authoritiesProvider;

    /**
     * Yes, this is a state object.
     * It is protected by the {@link #usersLock} here
     * and should not be exposed to the world.
     */
    private UsersDto users;

    @Override
    public Users findAll()
    {
        final Lock readLock = usersLock.readLock();
        readLock.lock();

        try
        {
            return new Users(users);
        }
        finally
        {
            readLock.unlock();
        }
    }

    @Override
    @Cacheable(cacheNames = CacheName.User.USERS, key = "#p0")
    public User findByUserName(final String username)
    {
        final Lock readLock = usersLock.readLock();
        readLock.lock();

        try
        {
            Optional<UserDto> optionalUserDto = users.findByUserName(username);

            if (optionalUserDto.isPresent())
            {
                UserDto userDto = optionalUserDto.get();

                Set<String> authorities = userDto.getRoles()
                                                 .stream()
                                                 .map(this::getGrantedAuthorities)
                                                 .map(this::getAuthoritiesAsString)
                                                 .flatMap(Collection::stream)
                                                 .collect(Collectors.toCollection(HashSet::new));

                if (authorities.size() > 0)
                {
{'fraction_non_alphanumeric': 0.09037202618553408, 'fraction_numerical': 0.001596678907871627, 'mean_word_length': 2.5033557046979866, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6547327', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1655, 'n_words': 315}