<commit_before>package ua.com.fielden.platform.security.user;

import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.from;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.select;

import java.security.SignatureException;
import java.util.Collection;
import java.util.Optional;

import org.apache.log4j.Logger;

import com.google.inject.Inject;

import ua.com.fielden.platform.cypher.SessionIdentifierGenerator;
import ua.com.fielden.platform.dao.CommonEntityDao;
import ua.com.fielden.platform.dao.QueryExecutionModel;
import ua.com.fielden.platform.dao.annotations.SessionRequired;
import ua.com.fielden.platform.entity.annotation.EntityType;
import ua.com.fielden.platform.entity.fetch.IFetchProvider;
import ua.com.fielden.platform.entity.query.IFilter;
import ua.com.fielden.platform.entity.query.model.EntityResultQueryModel;
import ua.com.fielden.platform.security.exceptions.SecurityException;

/**
 * DAO implementation of {@link IUserSecret}.
 *
 * @author TG Team
 *
 */
@EntityType(UserSecret.class)
public class UserSecretDao extends CommonEntityDao<UserSecret> implements IUserSecret {

    private final Logger logger = Logger.getLogger(getClass());
    private final SessionIdentifierGenerator crypto;

    @Inject
    public UserSecretDao(
            final SessionIdentifierGenerator crypto,
            final IFilter filter) {
        super(filter);
        this.crypto = crypto;
    }

    @Override
    public final String hashPasswd(final String passwd, final String salt) {
        try {
            return crypto.calculatePBKDF2WithHmacSHA1(passwd, salt);
        } catch (final SignatureException ex) {
            logger.error(ex);
            throw new SecurityException("Could not hash user password.", ex);
        }
    }
    
    @Override
    protected IFetchProvider<UserSecret> createFetchProvider() {
        return super.createFetchProvider().with("key.email", "salt", "password", "resetUuid");
    }

    @SessionRequired
    @Override
    public Optional<UserSecret> findByUsername(String username) {
        final EntityResultQueryModel<UserSecret> query = select(UserSecret.class).where().prop("key.key").eq().val(username).model();
        final QueryExecutionModel<UserSecret, EntityResultQueryModel<UserSecret>> qem = from(query).with(createFetchProvider().fetchModel()).model();
        return getEntityOptional(qem);<commit_msg>#1120, #1118 Minor adjustment to the fetch model in UserSecretDao.<commit_after>package ua.com.fielden.platform.security.user;

import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.from;
import static ua.com.fielden.platform.entity.query.fluent.EntityQueryUtils.select;

import java.security.SignatureException;
import java.util.Collection;
import java.util.Optional;

import org.apache.log4j.Logger;

import com.google.inject.Inject;

import ua.com.fielden.platform.cypher.SessionIdentifierGenerator;
import ua.com.fielden.platform.dao.CommonEntityDao;
import ua.com.fielden.platform.dao.QueryExecutionModel;
import ua.com.fielden.platform.dao.annotations.SessionRequired;
import ua.com.fielden.platform.entity.annotation.EntityType;
import ua.com.fielden.platform.entity.fetch.IFetchProvider;
import ua.com.fielden.platform.entity.query.IFilter;
import ua.com.fielden.platform.entity.query.model.EntityResultQueryModel;
import ua.com.fielden.platform.security.exceptions.SecurityException;

/**
 * DAO implementation of {@link IUserSecret}.
 *
 * @author <NAME>
 *
 */
@EntityType(UserSecret.class)
public class UserSecretDao extends CommonEntityDao<UserSecret> implements IUserSecret {

    private final Logger logger = Logger.getLogger(getClass());
    private final SessionIdentifierGenerator crypto;

    @Inject
    public UserSecretDao(
            final SessionIdentifierGenerator crypto,
            final IFilter filter) {
        super(filter);
        this.crypto = crypto;
    }

    @Override
    public final String hashPasswd(final String passwd, final String salt) {
        try {
            return crypto.calculatePBKDF2WithHmacSHA1(passwd, salt);
        } catch (final SignatureException ex) {
            logger.error(ex);
            throw new SecurityException("Could not hash user password.", ex);
        }
    }
    
    @Override
    protected IFetchProvider<UserSecret> createFetchProvider() {
        return super.createFetchProvider().with("key.email", "key.base", "salt", "password", "resetUuid");
    }

    @SessionRequired
    @Override
    public Optional<UserSecret> findByUsername(String username) {
        final EntityResultQueryModel<UserSecret> query = select(UserSecret.class).where().prop("key.key").eq().val(username).model();
        final QueryExecutionModel<UserSecret, EntityResultQueryModel<UserSecret>> qem = from(query).with(createFetchProvider().fetchModel()).model();
        return getEntityOptional(qem);
{'fraction_non_alphanumeric': 0.10603588907014681, 'fraction_numerical': 0.0028548123980424145, 'mean_word_length': 4.240384615384615, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, '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': '6003645', 'n_tokens_mistral': 1387, 'n_tokens_neox': 1387, 'n_words': 260}