UserProvider.php 4.63 KB
<?php

namespace FootyRoom\Services\Auth;

use FootyRoom\User\User;
use FootyRoom\Repositories\IUserRepository;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Contracts\Auth\UserProvider as UserProviderContract;
use Illuminate\Session\SessionManager;

class UserProvider implements UserProviderContract
{
    public function __construct(SessionManager $session, IUserRepository $repo)
    {
        $this->session = $session;
        $this->repo = $repo;
    }

    /**
     * Retrieve a user by their unique identifier.
     *
     * @param  mixed  $identifier
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveById($identifier)
    {
        return $this->repo->findById($identifier);
    }

    public function retrieveBySession($name)
    {
        $raw = $this->session->get($name);

        $user = new User($raw);

        return $user;
    }

    /**
     * Retrieve a user by by their unique identifier and "remember me" token.
     *
     * @param  mixed   $identifier
     * @param  string  $token
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByToken($identifier, $token)
    {
    }

    /**
     * Update the "remember me" token for the given user in storage.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  string  $token
     * @return void
     */
    public function updateRememberToken(Authenticatable $user, $token)
    {
    }

    /**
     * Retrieve a user by the given credentials.
     *
     * @param  array  $credentials
     * @return \Illuminate\Contracts\Auth\Authenticatable|null
     */
    public function retrieveByCredentials(array $credentials)
    {
        // First we will add each credential element to the query as a where clause.
        // Then we can execute the query and, if we found a user, return it in a
        // User "model" that will be utilized by the Guard instances.
        return $this->repo->findOne(function ($query) use ($credentials) {
            foreach ($credentials as $key => $value) {
                if (str_contains($key, 'password') || str_contains($key, 'passwordHash')) {
                    continue;
                }

                $query->where($key, $value);
            }
        });
    }

    /**
     * Validate a user against the given credentials.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable  $user
     * @param  array  $credentials
     * @return bool
     */
    public function validateCredentials(Authenticatable $user, array $credentials)
    {
        if (isset($credentials['password'])) {
            return $user->comparePassword($credentials['password']);
        }

        return $credentials['passwordHash'] === $user->getPassword();
    }

    /**
     * Records failed login attempt.
     *
     * @param  Authenticatable $user
     * @param  string          $ip
     * @return void
     */
    public function recordFailedAttempt(Authenticatable $user, $ip)
    {
        $this->repo->recordFailedLogin($this->getId($user), $ip);
        // $user->recordFailedLogin($ip);
    }

    /**
     * Clears number of successive failed logins.
     *
     * @param  Authenticatable $user
     * @return void
     */
    public function clearFailedAttempts(Authenticatable $user)
    {
        $this->repo->clearFailedLogins($this->getId($user));
        // $user->clearFailedLogins();
    }

    /**
     * Decides whether user's account is open.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     *
     * @return bool
     */
    public function accountOpen($user)
    {
        return $user->accountOpen();
    }

    /**
     * Returns number of failed attempts performed in a row.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     *
     * @return int|null
     */
    public function failedAttempts($user)
    {
        return $this->repo->failedAttempts($this->getId($user));
    }

    /**
     * Get user's password.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     *
     * @return string
     */
    public function getPasswordHash($user)
    {
        return $user->getPassword();
    }

    /**
     * Get user's id.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     *
     * @return int
     */
    public function getId($user)
    {
        return $user->getAuthIdentifier();
    }

    /**
     * Turns user data into array.
     *
     * @param  \Illuminate\Contracts\Auth\Authenticatable $user
     *
     * @return array
     */
    public function toArray($user)
    {
        return (array) $user;

        return $user->toArray();
    }
}