Skip to content

Instantly share code, notes, and snippets.

@mortenege
Created August 27, 2018 04:30

Revisions

  1. mortenege created this gist Aug 27, 2018.
    70 changes: 70 additions & 0 deletions WPUserProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,70 @@
    <?php

    namespace App\Extensions;

    use App\User;
    use Illuminate\Support\Str;
    use MikeMcLin\WpPassword\Facades\WpPassword;
    use Illuminate\Auth\EloquentUserProvider;
    use Illuminate\Contracts\Auth\Authenticatable;

    class WPUserProvider extends EloquentUserProvider {

    /**
    * 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){
    $plain = $credentials['user_pass'];
    return WpPassword::check($plain, $user->user_pass);
    // return $this->hasher->check($plain, $user->getAuthPassword());
    }

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

    // return $model->newQuery()->withoutGlobalScope('active')
    return $model->newQuery()
    ->where($model->getAuthIdentifierName(), $identifier)
    ->first();
    }

    /**
    * Retrieve a user by the given credentials.
    *
    * @param array $credentials
    * @return \Illuminate\Contracts\Auth\Authenticatable|null
    */
    public function retrieveByCredentials(array $credentials)
    {
    if (empty($credentials) ||
    (count($credentials) === 1 &&
    array_key_exists('user_pass', $credentials))) {
    return;
    }

    // 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
    // Eloquent User "model" that will be utilized by the Guard instances.
    // $query = $this->createModel()->newQuery()->withoutGlobalScope('active');
    $query = $this->createModel()->newQuery();

    foreach ($credentials as $key => $value) {
    if (! Str::contains($key, 'user_pass')) {
    $query->where($key, $value);
    }
    }

    return $query->first();
    }
    }