<commit_before><?php declare(strict_types = 1);

namespace Dms\Web\Laravel\Auth;

use Dms\Core\Auth\AdminBannedException;
use Dms\Core\Auth\AdminForbiddenException;
use Dms\Core\Auth\IAdmin;
use Dms\Core\Auth\IAdminRepository;
use Dms\Core\Auth\AuthSystem;
use Dms\Core\Auth\InvalidCredentialsException;
use Dms\Core\Auth\IPermission;
use Dms\Core\Auth\IRoleRepository;
use Dms\Core\Auth\NotAuthenticatedException;
use Dms\Core\Event\IEventDispatcher;
use Dms\Core\Exception\InvalidArgumentException;
use Dms\Core\Ioc\IIocContainer;
use Dms\Web\Laravel\Auth\Password\IPasswordHasherFactory;
use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Auth\StatefulGuard;

/**
 * The auth system implementation using the laravel auth component.
 *
 * @author <NAME> <<EMAIL>>
 */
class LaravelAuthSystem extends AuthSystem
{
    /**
     * @var StatefulGuard
     */
    protected $laravelAuth;

    /**
     * @var IAdminRepository
     */
    protected $userRepository;

    /**
     * @var IRoleRepository
     */
    protected $roleRepository;

    /**
     * @var IPasswordHasherFactory
     */
    protected $passwordHasherFactory;

    /**
     * @var IPermission[]|null
     */
    protected $currentUsersPermissions;

    /**
     * @var IIocContainer
     */
    protected $iocContainer;

    /**
     * LaravelAuthSystem constructor.
     *
     * @param AuthManager            $laravelAuth
     * @param IAdminRepository       $userRepository
     * @param IRoleRepository        $roleRepository
     * @param IPasswordHasherFactory $passwordHasherFactory
     * @param IIocContainer          $iocContainer
     */
    public function __construct(
        AuthManager $laravelAuth,
        IAdminRepository $userRepository,
        IRoleRepository $roleRepository,
        IPasswordHasherFactory $passwordHasherFactory,
        IIocContainer $iocContainer
    ) {
        $this->laravelAuth           = $laravelAuth->guard('dms');
        $this->userRepository        = $userRepository;
        $this->passwordHasherFactory = $passwordHasherFactory;
        $this->roleRepository        = $roleRepository;
        $this->iocContainer          = $iocContainer;
    }

    /**
     * @param string $username
     * @param string $password
     *
     * @return IAdmin
     * @throws InvalidCredentialsException
     * @throws AdminBannedException
     */
    protected function loadByCredentials(string $username, string $password) : IAdmin
    {
        /** @var IAdmin $user */
        $users = $this->userRepository->matching(
            $this->userRepository->criteria()
                ->where(Admin::USERNAME, '=', $username)
        );

        if (count($users) !== 1) {
            throw InvalidCredentialsException::defaultMessage($username);
        }

        $user           = $users[0];
        $passwordHasher = $this->passwordHasherFactory->buildFor($user->getPassword());

        if (!$passwordHasher->verify($password, $user->getPassword())) {
            throw InvalidCredentialsException::defaultMessage($username);
        }

        if ($user->isBanned()) {
            throw AdminBannedException::defaultMessage($user);
        }

        return $user;
    }

    /**
     * Attempts to login with the supplied credentials.
     *
     * @param string $username
     * @param string $password
     *
     * @return void<commit_msg>Fix issue with attempting to login to oauth account via username and password<commit_after><?php declare(strict_types = 1);

namespace Dms\Web\Laravel\Auth;

use Dms\Core\Auth\AdminBannedException;
use Dms\Core\Auth\AdminForbiddenException;
use Dms\Core\Auth\IAdmin;
use Dms\Core\Auth\IAdminRepository;
use Dms\Core\Auth\AuthSystem;
use Dms\Core\Auth\InvalidCredentialsException;
use Dms\Core\Auth\IPermission;
use Dms\Core\Auth\IRoleRepository;
use Dms\Core\Auth\NotAuthenticatedException;
use Dms\Core\Event\IEventDispatcher;
use Dms\Core\Exception\InvalidArgumentException;
use Dms\Core\Ioc\IIocContainer;
use Dms\Web\Laravel\Auth\Password\IPasswordHasherFactory;
use Illuminate\Auth\AuthManager;
use Illuminate\Contracts\Auth\StatefulGuard;

/**
 * The auth system implementation using the laravel auth component.
 *
 * @author <NAME> <<EMAIL>>
 */
class LaravelAuthSystem extends AuthSystem
{
    /**
     * @var StatefulGuard
     */
    protected $laravelAuth;

    /**
     * @var IAdminRepository
     */
    protected $userRepository;

    /**
     * @var IRoleRepository
     */
    protected $roleRepository;

    /**
     * @var IPasswordHasherFactory
     */
    protected $passwordHasherFactory;

    /**
     * @var IPermission[]|null
     */
    protected $currentUsersPermissions;

    /**
     * @var IIocContainer
     */
    protected $iocContainer;

    /**
     * LaravelAuthSystem constructor.
     *
     * @param AuthManager            $laravelAuth
     * @param IAdminRepository       $userRepository
     * @param IRoleRepository        $roleRepository
     * @param IPasswordHasherFactory $passwordHasherFactory
     * @param IIocContainer          $iocContainer
     */
    public function __construct(
        AuthManager $laravelAuth,
        IAdminRepository $userRepository,
        IRoleRepository $roleRepository,
        IPasswordHasherFactory $passwordHasherFactory,
        IIocContainer $iocContainer
    ) {
        $this->laravelAuth           = $laravelAuth->guard('dms');
        $this->userRepository        = $userRepository;
        $this->passwordHasherFactory = $passwordHasherFactory;
        $this->roleRepository        = $roleRepository;
        $this->iocContainer          = $iocContainer;
    }

    /**
     * @param string $username
     * @param string $password
     *
     * @return IAdmin
     * @throws InvalidCredentialsException
     * @throws AdminBannedException
     */
    protected function loadByCredentials(string $username, string $password) : IAdmin
    {
        $users = $this->userRepository->matching(
            $this->userRepository->criteria()
                ->whereInstanceOf(LocalAdmin::class)
                ->where(Admin::USERNAME, '=', $username)
        );

        if (count($users) !== 1) {
            throw InvalidCredentialsException::defaultMessage($username);
        }

        /** @var LocalAdmin $user */
        $user           = $users[0];
        $passwordHasher = $this->passwordHasherFactory->buildFor($user->getPassword());

        if (!$passwordHasher->verify($password, $user->getPassword())) {
            throw InvalidCredentialsException::defaultMessage($username);
        }

        if ($user->isBanned()) {
            throw AdminBannedException::defaultMessage($user);
        }

        return $user;
    }

    /**
     * Attempts to login with the supplied credentials.
     *
     * @param string $username
     * @param string $password
     *
     * @return void
{'fraction_non_alphanumeric': 0.11012122097268877, 'fraction_numerical': 0.0008762961881115817, 'mean_word_length': 2.640616693248272, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 44, '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': '2205831', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1917, 'n_words': 366}