Skip to content

Instantly share code, notes, and snippets.

@mortenege
Created August 27, 2018 04:30
Show Gist options
  • Save mortenege/c29bc7204bad0d7cfe5cd09eb82d6acb to your computer and use it in GitHub Desktop.
Save mortenege/c29bc7204bad0d7cfe5cd09eb82d6acb to your computer and use it in GitHub Desktop.
Use a Wordpress databse structure with Laravel
<?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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment