Skip to content

Commit 4f372f1

Browse files
author
Ahsaan Muhammad Yousuf
committed
Support added for separate wordpress database via eloquent connection.
1 parent c3473e4 commit 4f372f1

File tree

6 files changed

+172
-53
lines changed

6 files changed

+172
-53
lines changed

CHANGELOG.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,22 @@
1-
# wordpress-auth-driver-laravel Changelog
1+
# CHANGELOG
2+
3+
## 2.1.0
4+
- Support added for seperate connection in `config/database.php`
5+
- Default `wp_` prefix removed from `Models\WordpressUser.php`. It'll be handled by connection in `config/database.php`
6+
- Added `user_registered` date mutators.
7+
- Config file renamed to `wordpress-auth`
8+
- README.md and CHANGELOG.md Updated
9+
10+
## 2.0.0
11+
- Added support for Laravel 5.6 & 5.7
12+
13+
## 1.1.0
14+
- Laravel 5.5 Support added
15+
16+
## 1.0.1 - 1.0.2
17+
- Package type changed to library
18+
- Added scrutinizer badges in README.md
19+
- Added README.md & CHANGELOG.md
220

321
## 1.0.0
422
- Initial release.

README.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
[![Latest Stable Version](https://poser.pugx.org/ahsankhatri/wordpress-auth-provider/v/stable)](https://packagist.org/packages/ahsankhatri/wordpress-auth-provider) [![Total Downloads](https://poser.pugx.org/ahsankhatri/wordpress-auth-provider/downloads)](https://packagist.org/packages/ahsankhatri/wordpress-auth-provider) [![Build Status](https://scrutinizer-ci.com/g/ahsankhatri/wordpress-auth-driver-laravel/badges/build.png?b=master)](https://scrutinizer-ci.com/g/ahsankhatri/wordpress-auth-driver-laravel/build-status/master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/ahsankhatri/wordpress-auth-driver-laravel/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/ahsankhatri/wordpress-auth-driver-laravel/?branch=master) [![Code Intelligence Status](https://scrutinizer-ci.com/g/ahsankhatri/wordpress-auth-driver-laravel/badges/code-intelligence.svg?b=master)](https://scrutinizer-ci.com/code-intelligence) [![License](https://poser.pugx.org/ahsankhatri/wordpress-auth-provider/license)](https://packagist.org/packages/ahsankhatri/wordpress-auth-provider)
44

5+
| **Laravel** | **wordpress-auth-driver-laravel** |
6+
|---|---|
7+
| 5.2 to 5.5 | ^1.0 |
8+
| 5.6 to 5.7 | ^2.0 |
9+
510
## Installation
611

712
To install this package you will need
@@ -55,6 +60,32 @@ To register authentication guard.
5560
],
5661
```
5762

63+
#### Publish config file (optional)
64+
```bash
65+
php artisan vendor:publish --provider="MrShan0\WordpressAuth\WordpressAuthServiceProvider"
66+
```
67+
68+
It will publish config file (`config/wordpress-auth.php`) where you can define your own connection type e.g `wp-mysql`. Make sure to fill `prefix` in `config/database.php` for `wp_` prefix in your tables if you're using prefix in wordpress tabels.
69+
70+
For example:
71+
```php
72+
'wp-mysql' => [
73+
'driver' => 'mysql',
74+
'host' => env('WP_DB_HOST', '127.0.0.1'),
75+
'port' => env('WP_DB_PORT', '3306'),
76+
'database' => env('WP_DB_DATABASE', 'forge'),
77+
'username' => env('WP_DB_USERNAME', 'forge'),
78+
'password' => env('WP_DB_PASSWORD', ''),
79+
'unix_socket' => env('WP_DB_SOCKET', ''),
80+
'charset' => 'utf8mb4',
81+
'collation' => 'utf8mb4_unicode_ci',
82+
'prefix' => 'wp_',
83+
'prefix_indexes' => true,
84+
'strict' => true,
85+
'engine' => null,
86+
],
87+
```
88+
5889
## Configuration
5990

6091
`password_resets` table (from Laravel default auth mechanism) is required to hold reset password token. If you do not have `password_resets` table then use this migration instead
@@ -93,6 +124,9 @@ class CreatePasswordResetsTable extends Migration
93124
}
94125
```
95126

127+
## Extension
128+
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`.
129+
96130
## Usage
97131
You need to define `wordpress` **guard** explicitly to load the driver.
98132
### Examples
@@ -108,6 +142,10 @@ Auth::guard('wordpress')->attempt([
108142
// get user object
109143
Auth::guard('wordpress')->user();
110144

145+
// Update wordpress compatible password
146+
$user->user_pass = app('wordpress-auth')->make('new_password');
147+
$user->save();
148+
111149
// logout
112150
Auth::guard('wordpress')->logout();
113151
```

config/hash.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

config/wordpress-auth.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
return [
4+
5+
/*
6+
|--------------------------------------------------------------------------
7+
| Seperate Database Connection
8+
|--------------------------------------------------------------------------
9+
|
10+
| You can define your own connection other than the default one in
11+
| database.php to use multiple connections/database in one.
12+
|
13+
*/
14+
'connection' => 'mysql',
15+
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Password hashing configuration for Wordpress authentication
19+
|--------------------------------------------------------------------------
20+
*/
21+
'hash' => [
22+
23+
/*
24+
|--------------------------------------------------------------------------
25+
| Iteration Count
26+
|--------------------------------------------------------------------------
27+
|
28+
| The number of iterations used to hash the password.
29+
| Minimum: 4, Maximum: 31
30+
|
31+
*/
32+
'iteration_count' => 8,
33+
34+
35+
/*
36+
|--------------------------------------------------------------------------
37+
| Portable Hashes
38+
|--------------------------------------------------------------------------
39+
|
40+
| Should we generate portable hashes? true or false
41+
|
42+
*/
43+
'portable_hashes' => true,
44+
45+
]
46+
47+
];

src/Models/WordpressUser.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,21 @@ class WordpressUser extends Authenticatable
1313

1414
/**
1515
* Explicitly define your table name
16-
*
16+
*
1717
* @var string
1818
*/
19-
protected $table = 'wp_users';
19+
protected $table = 'users';
2020

2121
/**
2222
* Disable timestamps
23-
*
23+
*
2424
* @var boolean
2525
*/
2626
public $timestamps = false;
2727

2828
/**
2929
* Define primary key
30-
*
30+
*
3131
* @var string
3232
*/
3333
protected $primaryKey = 'ID';
@@ -39,6 +39,15 @@ class WordpressUser extends Authenticatable
3939
*/
4040
protected $rememberTokenName = false;
4141

42+
/**
43+
* The attributes that should be mutated to dates.
44+
*
45+
* @var array
46+
*/
47+
protected $dates = [
48+
'user_registered',
49+
];
50+
4251
/**
4352
* The attributes that should be hidden for arrays.
4453
*
@@ -49,6 +58,37 @@ class WordpressUser extends Authenticatable
4958
'remember_token', // disabled via protected property
5059
];
5160

61+
/**
62+
* The attributes that are mass assignable.
63+
*
64+
* @var array
65+
*/
66+
protected $fillable = [
67+
'user_login',
68+
'user_pass',
69+
'user_nicename',
70+
'user_email',
71+
'user_url',
72+
'user_registered',
73+
'user_activation_key',
74+
'user_status',
75+
'display_name',
76+
];
77+
78+
/**
79+
* Create a new Eloquent model instance.
80+
*
81+
* @param array $attributes
82+
* @return void
83+
*/
84+
public function __construct(array $attributes = [])
85+
{
86+
parent::__construct($attributes);
87+
88+
// Set connection from config
89+
$this->setConnection(config('wordpress-auth.connection', 'mysql'));
90+
}
91+
5292
/**
5393
* Get the e-mail address where password reset links are sent.
5494
*
@@ -61,7 +101,7 @@ public function getEmailForPasswordReset()
61101

62102
/**
63103
* Return password value
64-
*
104+
*
65105
* @return string
66106
*/
67107
public function getAuthPassword()
@@ -71,7 +111,7 @@ public function getAuthPassword()
71111

72112
/**
73113
* Usage for notifiable for email
74-
*
114+
*
75115
* @return string
76116
*/
77117
public function routeNotificationForMail()

src/WordpressAuthServiceProvider.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
use Auth;
66
use Hautelook\Phpass\PasswordHash;
77
use Illuminate\Support\ServiceProvider;
8-
use MrShan0\WordpressAuth\Hashing\WordPressHasher;
98
use MrShan0\WordpressAuth\Auth\EloquentWordpressUserProvider;
109
use MrShan0\WordpressAuth\Guard\WordpressGuard;
10+
use MrShan0\WordpressAuth\Hashing\WordPressHasher;
1111

1212
class WordpressAuthServiceProvider extends ServiceProvider
1313
{
@@ -18,33 +18,44 @@ class WordpressAuthServiceProvider extends ServiceProvider
1818
*/
1919
protected $defer = false;
2020

21+
/**
22+
* Perform post-registration booting of services.
23+
*
24+
* @return void
25+
*/
26+
public function boot()
27+
{
28+
$this->publishes([
29+
__DIR__ . '/../config/wordpress-auth.php' => config_path('wordpress-auth.php'),
30+
]);
31+
}
32+
2133
/**
2234
* Register bindings in the container.
2335
*
2436
* @return void
2537
*/
2638
public function register()
2739
{
28-
$this->mergeConfigFrom(__DIR__ . '/../config/hash.php', 'wordpress-hash.hash');
40+
$this->mergeConfigFrom(__DIR__ . '/../config/wordpress-auth.php', 'wordpress-auth');
2941

30-
Auth::extend('wordpress', function($app) {
42+
Auth::extend('wordpress', function ($app) {
3143
return new WordpressGuard(
32-
new EloquentWordpressUserProvider($app['wordpress-hash'], $config['model'])
44+
new EloquentWordpressUserProvider($app['wordpress-auth'], $config['model'])
3345
);
3446
});
3547

36-
$this->app->singleton('wordpress-hash', function ($app)
37-
{
38-
$iteration_count = $app['config']->get('wordpress-hash.hash.iteration_count');
39-
$portable_hashes = $app['config']->get('wordpress-hash.hash.portable_hashes');
48+
$this->app->singleton('wordpress-auth', function ($app) {
49+
$iteration_count = $app['config']->get('wordpress-auth.hash.iteration_count');
50+
$portable_hashes = $app['config']->get('wordpress-auth.hash.portable_hashes');
4051

4152
$hasher = new PasswordHash($iteration_count, $portable_hashes);
4253

4354
return new WordPressHasher($hasher);
4455
});
4556

46-
Auth::provider('eloquent.wordpress', function($app, array $config) {
47-
return new EloquentWordpressUserProvider($app['wordpress-hash'], $config['model']);
57+
Auth::provider('eloquent.wordpress', function ($app, array $config) {
58+
return new EloquentWordpressUserProvider($app['wordpress-auth'], $config['model']);
4859
});
4960
}
5061

@@ -55,6 +66,6 @@ public function register()
5566
*/
5667
public function provides()
5768
{
58-
return ['wordpress-hash'];
69+
return ['wordpress-auth'];
5970
}
6071
}

0 commit comments

Comments
 (0)