Skip to content

Commit 2adc33d

Browse files
committed
Added 5.8 Support
1 parent 4f372f1 commit 2adc33d

File tree

6 files changed

+154
-12
lines changed

6 files changed

+154
-12
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# CHANGELOG
22

3+
## 2.1.1
4+
- Added Laravel 5.8 support.
5+
- Extend config `wordpress-hash.php` with option to map email and password column if you've changed and default connection set to `wp-mysql`
6+
- Few spelling glitches.
7+
- Added Password accessor in WordpressUser Model
8+
- README.md and CHANGELOG.md Updated
9+
310
## 2.1.0
411
- Support added for seperate connection in `config/database.php`
512
- Default `wp_` prefix removed from `Models\WordpressUser.php`. It'll be handled by connection in `config/database.php`

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
| **Laravel** | **wordpress-auth-driver-laravel** |
66
|---|---|
77
| 5.2 to 5.5 | ^1.0 |
8-
| 5.6 to 5.7 | ^2.0 |
8+
| 5.6 to 5.8 | ^2.0 |
99

1010
## Installation
1111

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

1616
The best way to install this package is with the help of composer. Run
@@ -127,6 +127,9 @@ class CreatePasswordResetsTable extends Migration
127127
## Extension
128128
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`.
129129

130+
## Customization
131+
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.
132+
130133
## Usage
131134
You need to define `wordpress` **guard** explicitly to load the driver.
132135
### Examples
@@ -155,6 +158,30 @@ You may also change default guard in `config/auth.php` then your code will look
155158
Auth::loginUsingId(5);
156159
```
157160

161+
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
162+
163+
```php
164+
/**
165+
* Get the broker to be used during password reset.
166+
*
167+
* @return \Illuminate\Contracts\Auth\PasswordBroker
168+
*/
169+
public function broker()
170+
{
171+
return \Password::broker('wordpress');
172+
}
173+
174+
/**
175+
* Get the guard to be used during password reset.
176+
*
177+
* @return \Illuminate\Contracts\Auth\StatefulGuard
178+
*/
179+
protected function guard()
180+
{
181+
return \Auth::guard('wordpress');
182+
}
183+
```
184+
158185
## Changelog
159186

160187
[CHANGELOG](CHANGELOG.md)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"require": {
1515
"php": "^7.1",
1616
"hautelook/phpass": ">=0.3",
17-
"laravel/framework": ">=5.6 <5.8"
17+
"laravel/framework": ">=5.6 <=5.8"
1818
},
1919
"autoload": {
2020
"psr-4": {

config/wordpress-auth.php

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,59 @@
44

55
/*
66
|--------------------------------------------------------------------------
7-
| Seperate Database Connection
7+
| Separate Database Connection
88
|--------------------------------------------------------------------------
99
|
1010
| You can define your own connection other than the default one in
1111
| database.php to use multiple connections/database in one.
1212
|
1313
*/
14-
'connection' => 'mysql',
14+
'connection' => 'wp-mysql',
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| WordPress Customized Options
19+
|--------------------------------------------------------------------------
20+
|
21+
| Due to any reason if you plan to change your wordpress schema or
22+
| make usage of any additional column cab be defined here.
23+
|
24+
*/
25+
'options' => [
26+
27+
/*
28+
|--------------------------------------------------------------------------
29+
| WP Column Mapping
30+
|--------------------------------------------------------------------------
31+
|
32+
| Following option will help laravel auth scaffolding to work with
33+
| wordpress default (or customizeable) column names
34+
|
35+
*/
36+
'force_wp_email' => true,
37+
'force_wp_password' => true,
38+
39+
/*
40+
|--------------------------------------------------------------------------
41+
| WP Email Column
42+
|--------------------------------------------------------------------------
43+
|
44+
| If you're using any other column name other than default `user_email`
45+
|
46+
*/
47+
'email_column' => 'user_email',
48+
49+
/*
50+
|--------------------------------------------------------------------------
51+
| WP Password Column
52+
|--------------------------------------------------------------------------
53+
|
54+
| If you're using any other column name other than default `user_pass`
55+
|
56+
*/
57+
'password_column' => 'user_pass',
58+
59+
],
1560

1661
/*
1762
|--------------------------------------------------------------------------

src/Auth/EloquentWordpressUserProvider.php

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ class EloquentWordpressUserProvider extends EloquentUserProvider
1616
*/
1717
public function retrieveByCredentials(array $credentials)
1818
{
19-
if (empty($credentials)) {
19+
if (empty($credentials) ||
20+
(count($credentials) === 1 &&
21+
array_key_exists('password', $credentials))) {
2022
return;
2123
}
2224

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

30+
$credentials = $this->transformPayloadForWP($credentials);
31+
$password_column = config('wordpress-auth.options.password_column', 'user_pass');
2832
foreach ($credentials as $key => $value) {
29-
if (! Str::contains($key, 'user_pass')) {
30-
$query->where($key, $value);
33+
if (Str::contains($key, 'password') || $key == $password_column) {
34+
continue;
3135
}
36+
37+
$query->where($key, $value);
3238
}
3339

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

4855
return $this->hasher->check($plain, $user->getAuthPassword());
4956
}
57+
58+
private function transformPayloadForWP(array $credentials)
59+
{
60+
// Get password column name
61+
$password_column = config('wordpress-auth.options.password_column', 'user_pass');
62+
$email_column = config('wordpress-auth.options.email_column', 'user_email');
63+
64+
// This is required when used laravel auth scaffolding since wp follows
65+
// `user_email` and laravel follows `email` only
66+
// Same goes for `password` as `user_pass` by wordpress
67+
if (
68+
config('wordpress-auth.options.force_wp_email', false) &&
69+
array_key_exists('email', $credentials)
70+
) {
71+
$credentials[$email_column] = $credentials['email'];
72+
unset($credentials['email']);
73+
}
74+
75+
if (
76+
config('wordpress-auth.options.force_wp_password', false) &&
77+
array_key_exists('password', $credentials)
78+
) {
79+
$credentials[$password_column] = $credentials['password'];
80+
unset($credentials['password']);
81+
}
82+
83+
return $credentials;
84+
}
5085
}

src/Models/WordpressUser.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,18 +85,46 @@ public function __construct(array $attributes = [])
8585
{
8686
parent::__construct($attributes);
8787

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

92+
/**
93+
* Accessor to update password via Auth scaffolding
94+
*/
95+
public function setPasswordAttribute($value)
96+
{
97+
$this->attributes[$this->getPasswordColumnKey()] = $value;
98+
}
99+
100+
/**
101+
* Return the key used for email in wordpress schema
102+
*
103+
* @return string
104+
*/
105+
public function getEmailColumnKey()
106+
{
107+
return config('wordpress-auth.options.email_column', 'user_email');
108+
}
109+
110+
/**
111+
* Return the key used for password in wordpress schema
112+
*
113+
* @return string
114+
*/
115+
public function getPasswordColumnKey()
116+
{
117+
return config('wordpress-auth.options.password_column', 'user_pass');
118+
}
119+
92120
/**
93121
* Get the e-mail address where password reset links are sent.
94122
*
95123
* @return string
96124
*/
97125
public function getEmailForPasswordReset()
98126
{
99-
return $this->user_email;
127+
return $this->{$this->getEmailColumnKey()};
100128
}
101129

102130
/**
@@ -106,7 +134,7 @@ public function getEmailForPasswordReset()
106134
*/
107135
public function getAuthPassword()
108136
{
109-
return $this->user_pass;
137+
return $this->{$this->getPasswordColumnKey()};
110138
}
111139

112140
/**

0 commit comments

Comments
 (0)