Skip to content

Commit

Permalink
Added 5.8 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsankhatri committed Apr 21, 2019
1 parent 4f372f1 commit 2adc33d
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 12 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# CHANGELOG

## 2.1.1
- Added Laravel 5.8 support.
- Extend config `wordpress-hash.php` with option to map email and password column if you've changed and default connection set to `wp-mysql`
- Few spelling glitches.
- Added Password accessor in WordpressUser Model
- README.md and CHANGELOG.md Updated

## 2.1.0
- Support added for seperate connection in `config/database.php`
- Default `wp_` prefix removed from `Models\WordpressUser.php`. It'll be handled by connection in `config/database.php`
Expand Down
31 changes: 29 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
| **Laravel** | **wordpress-auth-driver-laravel** |
|---|---|
| 5.2 to 5.5 | ^1.0 |
| 5.6 to 5.7 | ^2.0 |
| 5.6 to 5.8 | ^2.0 |

## Installation

To install this package you will need
- Laravel 5.6|5.7 ([for older versions of laravel](https://github.com/ahsankhatri/wordpress-auth-driver-laravel/tree/v1))
- Laravel 5.6|5.7|5.8 ([for older versions of laravel](https://github.com/ahsankhatri/wordpress-auth-driver-laravel/tree/v1))
- PHP 7.1

The best way to install this package is with the help of composer. Run
Expand Down Expand Up @@ -127,6 +127,9 @@ class CreatePasswordResetsTable extends Migration
## Extension
Alternatively, if you want to use a custom user model, you should have it extend `MrShan0\WordpressAuth\Models\WordpressUser` and specify the name of your model in `config/auth.php` under `providers` -> `wordpress` -> `model`.

## Customization
If you've renamed your `user_email` column of wordpress database, you need to first publish configurations of this package if you've not already, extend the model as mentioned above and make sure you've override your changes in your `$fillable` property and `config/wordpress-auth.php` config file which is being used for authentication scaffolding and sending notifications.

## Usage
You need to define `wordpress` **guard** explicitly to load the driver.
### Examples
Expand Down Expand Up @@ -155,6 +158,30 @@ You may also change default guard in `config/auth.php` then your code will look
Auth::loginUsingId(5);
```

If you haven't set default guard and wanted to take advantage of **Password Resets** (Auth Scaffolding) in laravel. You may need to define `guard` and `broker` explicitly in `Auth/ForgotPasswordController.php` and `Auth/ResetPasswordController.php` as

```php
/**
* Get the broker to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\PasswordBroker
*/
public function broker()
{
return \Password::broker('wordpress');
}

/**
* Get the guard to be used during password reset.
*
* @return \Illuminate\Contracts\Auth\StatefulGuard
*/
protected function guard()
{
return \Auth::guard('wordpress');
}
```

## Changelog

[CHANGELOG](CHANGELOG.md)
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": "^7.1",
"hautelook/phpass": ">=0.3",
"laravel/framework": ">=5.6 <5.8"
"laravel/framework": ">=5.6 <=5.8"
},
"autoload": {
"psr-4": {
Expand Down
49 changes: 47 additions & 2 deletions config/wordpress-auth.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,59 @@

/*
|--------------------------------------------------------------------------
| Seperate Database Connection
| Separate Database Connection
|--------------------------------------------------------------------------
|
| You can define your own connection other than the default one in
| database.php to use multiple connections/database in one.
|
*/
'connection' => 'mysql',
'connection' => 'wp-mysql',

/*
|--------------------------------------------------------------------------
| WordPress Customized Options
|--------------------------------------------------------------------------
|
| Due to any reason if you plan to change your wordpress schema or
| make usage of any additional column cab be defined here.
|
*/
'options' => [

/*
|--------------------------------------------------------------------------
| WP Column Mapping
|--------------------------------------------------------------------------
|
| Following option will help laravel auth scaffolding to work with
| wordpress default (or customizeable) column names
|
*/
'force_wp_email' => true,
'force_wp_password' => true,

/*
|--------------------------------------------------------------------------
| WP Email Column
|--------------------------------------------------------------------------
|
| If you're using any other column name other than default `user_email`
|
*/
'email_column' => 'user_email',

/*
|--------------------------------------------------------------------------
| WP Password Column
|--------------------------------------------------------------------------
|
| If you're using any other column name other than default `user_pass`
|
*/
'password_column' => 'user_pass',

],

/*
|--------------------------------------------------------------------------
Expand Down
43 changes: 39 additions & 4 deletions src/Auth/EloquentWordpressUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class EloquentWordpressUserProvider extends EloquentUserProvider
*/
public function retrieveByCredentials(array $credentials)
{
if (empty($credentials)) {
if (empty($credentials) ||
(count($credentials) === 1 &&
array_key_exists('password', $credentials))) {
return;
}

Expand All @@ -25,10 +27,14 @@ public function retrieveByCredentials(array $credentials)
// Eloquent User "model" that will be utilized by the Guard instances.
$query = $this->createModel()->newQuery();

$credentials = $this->transformPayloadForWP($credentials);
$password_column = config('wordpress-auth.options.password_column', 'user_pass');
foreach ($credentials as $key => $value) {
if (! Str::contains($key, 'user_pass')) {
$query->where($key, $value);
if (Str::contains($key, 'password') || $key == $password_column) {
continue;
}

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

return $query->first();
Expand All @@ -43,8 +49,37 @@ public function retrieveByCredentials(array $credentials)
*/
public function validateCredentials(UserContract $user, array $credentials)
{
$plain = $credentials['user_pass'];
$credentials = $this->transformPayloadForWP($credentials);
$plain = $credentials[config('wordpress-auth.options.password_column', 'user_pass')];

return $this->hasher->check($plain, $user->getAuthPassword());
}

private function transformPayloadForWP(array $credentials)
{
// Get password column name
$password_column = config('wordpress-auth.options.password_column', 'user_pass');
$email_column = config('wordpress-auth.options.email_column', 'user_email');

// This is required when used laravel auth scaffolding since wp follows
// `user_email` and laravel follows `email` only
// Same goes for `password` as `user_pass` by wordpress
if (
config('wordpress-auth.options.force_wp_email', false) &&
array_key_exists('email', $credentials)
) {
$credentials[$email_column] = $credentials['email'];
unset($credentials['email']);
}

if (
config('wordpress-auth.options.force_wp_password', false) &&
array_key_exists('password', $credentials)
) {
$credentials[$password_column] = $credentials['password'];
unset($credentials['password']);
}

return $credentials;
}
}
34 changes: 31 additions & 3 deletions src/Models/WordpressUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,46 @@ public function __construct(array $attributes = [])
{
parent::__construct($attributes);

// Set connection from config
// Set connection via config explicitly.
$this->setConnection(config('wordpress-auth.connection', 'mysql'));
}

/**
* Accessor to update password via Auth scaffolding
*/
public function setPasswordAttribute($value)
{
$this->attributes[$this->getPasswordColumnKey()] = $value;
}

/**
* Return the key used for email in wordpress schema
*
* @return string
*/
public function getEmailColumnKey()
{
return config('wordpress-auth.options.email_column', 'user_email');
}

/**
* Return the key used for password in wordpress schema
*
* @return string
*/
public function getPasswordColumnKey()
{
return config('wordpress-auth.options.password_column', 'user_pass');
}

/**
* Get the e-mail address where password reset links are sent.
*
* @return string
*/
public function getEmailForPasswordReset()
{
return $this->user_email;
return $this->{$this->getEmailColumnKey()};
}

/**
Expand All @@ -106,7 +134,7 @@ public function getEmailForPasswordReset()
*/
public function getAuthPassword()
{
return $this->user_pass;
return $this->{$this->getPasswordColumnKey()};
}

/**
Expand Down

0 comments on commit 2adc33d

Please sign in to comment.