<commit_before><?php

namespace Laravolt\Auth\Services;

use Adldap\AdldapInterface;
use App\User;

class LdapService
{
    protected $ldap;

    /**
     * LdapService constructor.
     */
    public function __construct(AdldapInterface $ldap)
    {
        $this->ldap = $ldap;
    }

    public function getUser($data)
    {
        $username = array_get($data, config('laravolt.auth.identifier'));
        $password = array_get($data, 'password');
        $dn = sprintf("uid=%s,%s", $username, env('LDAP_BASE_DN'));

        $loginPassed = $this->ldap->auth()->attempt($dn, $password);

        if (!$loginPassed) {
            throw new \Exception('LDAP authentication failed');
        }

        $ldapUser = $this->ldap->search()->where(config('ldap_auth.usernames.ldap.authenticate'), '=', $username)->first();
        if (!$ldapUser) {
            throw new \Exception('Cannot find LDAP user with uid = '.$username);
        }

        $localUser = User::where(config('ldap_auth.usernames.eloquent'), '=', $username)->first();
        if (!$localUser) {
            throw new \Exception('LDAP user exists, but does not have a corresponding local account');
        }

        return $localUser;
    }
}<commit_msg>Flexible auth format<commit_after><?php

namespace Laravolt\Auth\Services;

use Adldap\AdldapInterface;
use Adldap\Query\Builder;
use App\User;

class LdapService
{
    protected $ldap;

    /**
     * LdapService constructor.
     */
    public function __construct(AdldapInterface $ldap)
    {
        $this->ldap = $ldap;
    }

    public function getUser($data)
    {
        $username = array_get($data, config('laravolt.auth.identifier'));
        $password = array_get($data, 'password');
        $dn = sprintf(env('LDAP_AUTH_FORMAT'), $username);

        $loginPassed = $this->ldap->auth()->attempt($dn, $password);

        if (!$loginPassed) {
            throw new \Exception('LDAP authentication failed');
        }

        $discover = config('ldap_auth.usernames.ldap.discover');
        $ldapUser = $this->ldap->search()->orFilter(
            function (Builder $builder) use ($discover, $username, $dn) {
                $builder
                    ->where($discover, '=', $username)
                    ->where($discover, '=', $dn);
            }
        )->first();

        if (!$ldapUser) {
            throw new \Exception(sprintf('Cannot find LDAP user with %s = %s or %s', $discover, $username, $dn));
        }

        $localUser = User::where(config('ldap_auth.usernames.eloquent'), '=', $username)->first();
        if (!$localUser) {
            throw new \Exception('LDAP user exists, but does not have a corresponding eloquent record');
        }

        return $localUser;
    }
}
{'fraction_non_alphanumeric': 0.15412710007304603, 'fraction_numerical': 0.0, 'mean_word_length': 2.507042253521127, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, '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': '7433596', 'n_tokens_mistral': 845, 'n_tokens_neox': 832, 'n_words': 174}