<commit_before><?php

/*
 * This file is part of the CRUDlexUser package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CRUDlex;

use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
use CRUDlex\AbstractData;
use CRUDlex\Entity;

/**
 * This class setups CRUDlex with some events so the passwords get salted and
 * hashed properly.
 */
class UserSetup {

	/**
	 * The encoder to use.
	 */
	protected $encoder;

    /**
     * Gets a closure for possibly generating a password hash in the entity.
     *
     * @param AbstractData $data
     * the AbstractData instance managing the users
     *
     * @param string $passwordField
     * the Entity fieldname of the password hash
     *
     * @param string $saltField
     * the Entity fieldname of the password hash salt
     */
    protected function getPWHashFunction(AbstractData $data, $passwordField, $saltField) {
        $that = $this;
        return function(Entity $entity) use ($data, $passwordField, $saltField, $that) {
            $password = $entity->get($passwordField);

            if (!$password) {
                return true;
            }

            $salt = $entity->get($saltField);
            $newSalt = $that->possibleGenSalt($salt, $entity, $saltField);

            $passwordHash = $this->encoder->encodePassword($password, $salt);

            $doGenerateHash = $that->doGenerateHash($data, $entity, $passwordField, $password, $newSalt);

            if ($doGenerateHash) {
                $entity->set($passwordField, $passwordHash);
            }
            return true;
        };
    }
    
    /**
     * Constructor.
     *
     * @param PasswordEncoderInterface $encoder
     * the encoder to use, defaults to BCryptPasswordEncoder if null is given
     */
    public function __construct(PasswordEncoderInterface $encoder = null) {
    	$this->encoder = $passwordEncoder;
    	if ($this->encoder === null) {
    		$this->encoder = new BCryptPasswordEncoder(13);
    	}
    }

    /**
     * Generates a new salt if the given salt is null.
     *
     * @param string $salt
     * the salt to override if null
     * @param Entity
     * the entity getting the new salt
     * @param string $saltField
     * the field holding the salt in the entity
     *
     * @return boolean
     * true if a new salt was generated<commit_msg>fixed constructor<commit_after><?php

/*
 * This file is part of the CRUDlexUser package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace CRUDlex;

use Symfony\Component\Security\Core\Encoder\PasswordEncoderInterface;
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder;
use CRUDlex\AbstractData;
use CRUDlex\Entity;

/**
 * This class setups CRUDlex with some events so the passwords get salted and
 * hashed properly.
 */
class UserSetup {

	/**
	 * The encoder to use.
	 */
	protected $encoder;

    /**
     * Gets a closure for possibly generating a password hash in the entity.
     *
     * @param AbstractData $data
     * the AbstractData instance managing the users
     *
     * @param string $passwordField
     * the Entity fieldname of the password hash
     *
     * @param string $saltField
     * the Entity fieldname of the password hash salt
     */
    protected function getPWHashFunction(AbstractData $data, $passwordField, $saltField) {
        $that = $this;
        return function(Entity $entity) use ($data, $passwordField, $saltField, $that) {
            $password = $entity->get($passwordField);

            if (!$password) {
                return true;
            }

            $salt = $entity->get($saltField);
            $newSalt = $that->possibleGenSalt($salt, $entity, $saltField);

            $passwordHash = $this->encoder->encodePassword($password, $salt);

            $doGenerateHash = $that->doGenerateHash($data, $entity, $passwordField, $password, $newSalt);

            if ($doGenerateHash) {
                $entity->set($passwordField, $passwordHash);
            }
            return true;
        };
    }

    /**
     * Constructor.
     *
     * @param PasswordEncoderInterface $encoder
     * the encoder to use, defaults to BCryptPasswordEncoder if null is given
     */
    public function __construct(PasswordEncoderInterface $encoder = null) {
    	$this->encoder = $encoder;
    	if ($this->encoder === null) {
    		$this->encoder = new BCryptPasswordEncoder(13);
    	}
    }

    /**
     * Generates a new salt if the given salt is null.
     *
     * @param string $salt
     * the salt to override if null
     * @param Entity
     * the entity getting the new salt
     * @param string $saltField
     * the field holding the salt in the entity
     *
     * @return boolean
     * true if a new salt was generated
{'fraction_non_alphanumeric': 0.10170162247724575, 'fraction_numerical': 0.0007914523149980214, 'mean_word_length': 2.495850622406639, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 29, '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': '3069372', 'n_tokens_mistral': 1420, 'n_tokens_neox': 1362, 'n_words': 448}