Skip to content

Commit 99ede38

Browse files
Add Custom User Provider (#9)
- Adds a new CoilpackUserProvider callback to Laravel's Authentication Provider. - Respects the Member model (default Coilpack Member model or one added in the config) - Uses EE's native authentication functions to create the session --------- Co-authored-by: Stephen Galbraith <109251940+nerdgency@users.noreply.github.com>
1 parent 5faebd6 commit 99ede38

File tree

6 files changed

+121
-6
lines changed

6 files changed

+121
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Ability to pass a 'tagdata' parameter to the ExpressionEngine Template library
88
- Support for the Member fieldtype introduced in ExpressionEngine 7.4
9+
- A custom user provider to enable the `coilpack` guard to be used for logging in ExpressionEngine members through Laravel's authentication manager.
910

1011
### Fixed
1112

src/Auth/CoilpackUserProvider.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
3+
namespace Expressionengine\Coilpack\Auth;
4+
5+
use Illuminate\Auth\EloquentUserProvider;
6+
use Illuminate\Contracts\Auth\Authenticatable;
7+
8+
class CoilpackUserProvider extends EloquentUserProvider
9+
{
10+
/**
11+
* Retrieve a user by the given credentials.
12+
*
13+
* @param array $credentials
14+
* @return \Illuminate\Contracts\Auth\Authenticatable|null
15+
*/
16+
public function retrieveByCredentials(array $credentials)
17+
{
18+
$key = array_key_exists('username', $credentials) ? 'username' : 'email';
19+
$value = $credentials[$key];
20+
21+
return $this->newModelQuery()
22+
->where('username', $value)
23+
->when(strpos($value, '@'), function ($query) use ($value) {
24+
$query->orWhere('email', $value);
25+
})
26+
->first();
27+
}
28+
29+
/**
30+
* Validate a user against the given credentials.
31+
*
32+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
33+
* @param array $credentials
34+
* @return bool
35+
*/
36+
public function validateCredentials(Authenticatable $user, array $credentials)
37+
{
38+
ee()->load->library('auth');
39+
40+
$result = false;
41+
42+
if (array_key_exists('username', $credentials)) {
43+
$result = ee()->auth->authenticate_username($credentials['username'], $credentials['password']);
44+
} elseif (array_key_exists('email', $credentials)) {
45+
$result = ee()->auth->authenticate_email($credentials['email'], $credentials['password']);
46+
}
47+
48+
if (! $result) {
49+
return false;
50+
}
51+
52+
$result->start_session();
53+
54+
return $result !== false;
55+
}
56+
57+
/**
58+
* Retrieve a user by their unique identifier and "remember me" token.
59+
*
60+
* @param mixed $identifier
61+
* @param string $token
62+
* @return \Illuminate\Contracts\Auth\Authenticatable|null
63+
*/
64+
public function retrieveByToken($identifier, $token)
65+
{
66+
if (! ee()->remember->exists()) {
67+
return null;
68+
}
69+
70+
$model = $this->createModel();
71+
72+
return $this->newModelQuery($model)->where(
73+
$model->getAuthIdentifierName(),
74+
ee()->remember->data('member_id')
75+
)->first();
76+
}
77+
78+
/**
79+
* Update the "remember me" token for the given user in storage.
80+
*
81+
* @param \Illuminate\Contracts\Auth\Authenticatable $user
82+
* @param string $token
83+
* @return void
84+
*/
85+
public function updateRememberToken(Authenticatable $user, $token)
86+
{
87+
ee()->remember->exists() ? ee()->remember->refresh() : ee()->remember->create();
88+
}
89+
}

src/Auth/SessionGuard.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ public function user()
4747
return $this->user;
4848
}
4949

50+
51+
/**
52+
* Remove the user data from the session and cookies.
53+
*
54+
* @return void
55+
*/
56+
protected function clearUserDataFromStorage()
57+
{
58+
$this->session->remove($this->getName());
59+
ee()->session->destroy();
60+
}
61+
5062
/**
5163
* Get a unique identifier for the auth session value.
5264
*

src/Bootstrap/ConfigureAuthProvider.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,27 @@ public function bootstrap(Application $app)
4949
$session->start(); // This is usually handled in middleware
5050
$provider = $app->make('auth')->createUserProvider($config['provider'] ?? null);
5151

52-
return new \Expressionengine\Coilpack\Auth\SessionGuard($name, $provider, $session);
52+
$guard = new \Expressionengine\Coilpack\Auth\SessionGuard($name, $provider, $session);
53+
$guard->setCookieJar($app['cookie']);
54+
55+
return $guard;
56+
});
57+
58+
app('auth')->provider('coilpack', function ($app, array $config) {
59+
return new \Expressionengine\Coilpack\Auth\CoilpackUserProvider(
60+
$app['hash'],
61+
$config['model']
62+
);
5363
});
5464

5565
// Configure our 'coilpack' guard which uses the 'members' provider below
5666
app('config')->set('auth.guards.coilpack', [
5767
'driver' => 'exp_sessions',
58-
'provider' => 'members',
68+
'provider' => 'coilpack',
5969
]);
6070

61-
app('config')->set('auth.providers.members', [
62-
'driver' => 'eloquent',
71+
app('config')->set('auth.providers.coilpack', [
72+
'driver' => 'coilpack',
6373
'model' => $this->getMemberModel(),
6474
]);
6575

src/CoilpackServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Expressionengine\Coilpack;
44

5+
use Expressionengine\Coilpack\Auth\CoilpackUserProvider;
56
use Expressionengine\Coilpack\Api\Graph\Support\FieldtypeRegistrar;
67
use Illuminate\Support\Facades\App;
78
use Illuminate\Support\Facades\Event;

src/Models/Member/Member.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
use Expressionengine\Coilpack\Models\FieldContent;
99
use Expressionengine\Coilpack\Models\Permission\Permission;
1010
use Expressionengine\Coilpack\Models\Role;
11+
use Illuminate\Auth\Authenticatable;
1112
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
13+
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
1214
use Illuminate\Foundation\Auth\Access\Authorizable;
1315

1416
/**
@@ -18,9 +20,9 @@
1820
* provided by the Member module. This is a single user of
1921
* the website.
2022
*/
21-
class Member extends Model implements AuthorizableContract
23+
class Member extends Model implements AuthorizableContract, AuthenticatableContract
2224
{
23-
use Authorizable;
25+
use Authorizable, Authenticatable;
2426

2527
protected $primaryKey = 'member_id';
2628

0 commit comments

Comments
 (0)