<commit_before><?php
/**
 * This file is part of the EnriseUserAdLdap Module (https://github.com/RobQuistNL/EnriseUserAdLdap)
 *
 * Copyright (c) 2013 <NAME> (https://github.com/RobQuistNL)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.txt that was distributed with this source code.
 */
namespace EnriseUserAdLdap\Authentication\Adapter;

use EnriseUserAdLdap\Mapper\User as UserMapperInterface;
use EnriseUserAdLdap\Entity\User as UserEntity;
use Zend\Authentication\Storage;
use Zend\Authentication\Result as AuthenticationResult;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Options\AuthenticationOptionsInterface;
use ZfcUser\Authentication\Adapter\ChainableAdapter as AdapterChain;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;

class Ldap implements AdapterChain, ServiceManagerAwareInterface {

    /**
     * @var UserMapperInterface
     */
    protected $mapper;

    /**
     * @var closure / invokable object
     */
    protected $credentialPreprocessor;

    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * @var AuthenticationOptionsInterface
     */
    protected $options;

    /**
     * @var Storage\StorageInterface
     */
    protected $storage;
    
    /**
     * Initiate our own UserMapper
     * @return \EnriseUserAdLdap\Mapper\User
     */
    private function createMapper() 
    {
        $config = $this->getServiceManager()->get('Config');
        $mapper = new \EnriseUserAdLdap\Mapper\User(
                $this->getServiceManager()->get('ldap_interface'), $this->getServiceManager()->get('zfcuser_module_options'), $config['ldap']
        );
        
        $this->setMapper($mapper);
        
        return $this->getMapper();
    }
    
    /**
     * Create a ZfcUser user entity with values from an EnriseUserAdLdap entity
     * @param EnriseUserAdLdap\Entity\User $userEntity
     * @return \ZfcUser\Entity\User
     */
    private function populateUserDbObject(UserEntity $userEntity) 
    {
        
        $userDbObject = new \ZfcUser\Entity\User();
        
        $userDbObject->setUsername($userEntity->getUsername());
        $userDbObject->setEmail($userEntity->getEmail());
        $userDbObject->setDisplayName($userEntity->getDisplayName());
        $userDbObject->setPassword(''); //Otherwise the query won't work.
        
        return $userDbObject;
    }
    
    /**
     * authenticate Function.
     * Fired by ChainableAdapter.
     * 
     * Checks the user login via LDAP, and adds user into DB if he exists.
     * 
     * @see \ZfcUser\Authentication\Adapter\ChainableAdapter::authenticate()
     */
    public function authenticate(AuthEvent $e) 
    {

        $this->createMapper();

        $identity = $e->getRequest()->getPost()->get('identity');
        $credential = $e->getRequest()->getPost()->get('credential');
        
        $userObject = $this->getMapper()->authenticate($identity, $credential);
        
        if ($userObject === false) {
            // Password does not match
            $e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID)
                    ->setMessages(array($userObject));
            $this->setSatisfied(false);
            return false;
        }
        
        $userEntity = $userObject->getEntity();
        $e->setIdentity($userEntity);
        
        $userDbMapper = $this->serviceManager->get('zfcuser_user_db_mapper');<commit_msg>Fixed user ID bug<commit_after><?php
/**
 * This file is part of the EnriseUserAdLdap Module (https://github.com/RobQuistNL/EnriseUserAdLdap)
 *
 * Copyright (c) 2013 <NAME> (https://github.com/RobQuistNL)
 *
 * For the full copyright and license information, please view
 * the file LICENSE.txt that was distributed with this source code.
 */
namespace EnriseUserAdLdap\Authentication\Adapter;

use EnriseUserAdLdap\Mapper\User as UserMapperInterface;
use EnriseUserAdLdap\Entity\User as UserEntity;
use Zend\Authentication\Storage;
use Zend\Authentication\Result as AuthenticationResult;
use Zend\ServiceManager\ServiceManagerAwareInterface;
use Zend\ServiceManager\ServiceManager;
use ZfcUser\Options\AuthenticationOptionsInterface;
use ZfcUser\Authentication\Adapter\ChainableAdapter as AdapterChain;
use ZfcUser\Authentication\Adapter\AdapterChainEvent as AuthEvent;

class Ldap implements AdapterChain, ServiceManagerAwareInterface {

    /**
     * @var UserMapperInterface
     */
    protected $mapper;

    /**
     * @var closure / invokable object
     */
    protected $credentialPreprocessor;

    /**
     * @var ServiceManager
     */
    protected $serviceManager;

    /**
     * @var AuthenticationOptionsInterface
     */
    protected $options;

    /**
     * @var Storage\StorageInterface
     */
    protected $storage;
    
    /**
     * Initiate our own UserMapper
     * @return \EnriseUserAdLdap\Mapper\User
     */
    private function createMapper() 
    {
        $config = $this->getServiceManager()->get('Config');
        $mapper = new \EnriseUserAdLdap\Mapper\User(
                $this->getServiceManager()->get('ldap_interface'), $this->getServiceManager()->get('zfcuser_module_options'), $config['ldap']
        );
        
        $this->setMapper($mapper);
        
        return $this->getMapper();
    }
    
    /**
     * Create a ZfcUser user entity with values from an EnriseUserAdLdap entity
     * @param EnriseUserAdLdap\Entity\User $userEntity
     * @return \ZfcUser\Entity\User
     */
    private function populateUserDbObject(UserEntity $userEntity) 
    {
        
        $userDbObject = new \ZfcUser\Entity\User();
        
        $userDbObject->setUsername($userEntity->getUsername());
        $userDbObject->setEmail($userEntity->getEmail());
        $userDbObject->setDisplayName($userEntity->getDisplayName());
        $userDbObject->setPassword(''); //Otherwise the query won't work.
        
        return $userDbObject;
    }
    
    /**
     * authenticate Function.
     * Fired by ChainableAdapter.
     * 
     * Checks the user login via LDAP, and adds user into DB if he exists.
     * 
     * @see \ZfcUser\Authentication\Adapter\ChainableAdapter::authenticate()
     */
    public function authenticate(AuthEvent $e) 
    {

        $this->createMapper();

        $identity = $e->getRequest()->getPost()->get('identity');
        $credential = $e->getRequest()->getPost()->get('credential');
        
        $userObject = $this->getMapper()->authenticate($identity, $credential);
        if ($userObject === false) {
            // Password does not match
            $e->setCode(AuthenticationResult::FAILURE_CREDENTIAL_INVALID)
                    ->setMessages(array($userObject));
            $this->setSatisfied(false);
            return false;
        }

        $userEntity = $userObject->getEntity();
        $e->setIdentity($userEntity);
        
        $userDbMapper = $this->serviceManager->get('zfcuser_user_db_mapper');
{'fraction_non_alphanumeric': 0.11873313449794064, 'fraction_numerical': 0.001136202243999432, 'mean_word_length': 2.81680216802168, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 67, 'https://': 4, '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': '7386617', 'n_tokens_mistral': 2001, 'n_tokens_neox': 1961, 'n_words': 392}