diff --git a/CHANGELOG.md b/CHANGELOG.md index 7812b08..d682e3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Ability to pass a 'tagdata' parameter to the ExpressionEngine Template library - Support for the Member fieldtype introduced in ExpressionEngine 7.4 +- A custom user provider to enable the `coilpack` guard to be used for logging in ExpressionEngine members through Laravel's authentication manager. ### Fixed diff --git a/src/Auth/CoilpackUserProvider.php b/src/Auth/CoilpackUserProvider.php new file mode 100644 index 0000000..91ef0b1 --- /dev/null +++ b/src/Auth/CoilpackUserProvider.php @@ -0,0 +1,89 @@ +newModelQuery() + ->where('username', $value) + ->when(strpos($value, '@'), function ($query) use ($value) { + $query->orWhere('email', $value); + }) + ->first(); + } + + /** + * 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) + { + ee()->load->library('auth'); + + $result = false; + + if (array_key_exists('username', $credentials)) { + $result = ee()->auth->authenticate_username($credentials['username'], $credentials['password']); + } elseif (array_key_exists('email', $credentials)) { + $result = ee()->auth->authenticate_email($credentials['email'], $credentials['password']); + } + + if (! $result) { + return false; + } + + $result->start_session(); + + return $result !== false; + } + + /** + * Retrieve a user by their unique identifier and "remember me" token. + * + * @param mixed $identifier + * @param string $token + * @return \Illuminate\Contracts\Auth\Authenticatable|null + */ + public function retrieveByToken($identifier, $token) + { + if (! ee()->remember->exists()) { + return null; + } + + $model = $this->createModel(); + + return $this->newModelQuery($model)->where( + $model->getAuthIdentifierName(), + ee()->remember->data('member_id') + )->first(); + } + + /** + * Update the "remember me" token for the given user in storage. + * + * @param \Illuminate\Contracts\Auth\Authenticatable $user + * @param string $token + * @return void + */ + public function updateRememberToken(Authenticatable $user, $token) + { + ee()->remember->exists() ? ee()->remember->refresh() : ee()->remember->create(); + } +} diff --git a/src/Auth/SessionGuard.php b/src/Auth/SessionGuard.php index 972f66f..3cabb3b 100644 --- a/src/Auth/SessionGuard.php +++ b/src/Auth/SessionGuard.php @@ -47,6 +47,18 @@ public function user() return $this->user; } + + /** + * Remove the user data from the session and cookies. + * + * @return void + */ + protected function clearUserDataFromStorage() + { + $this->session->remove($this->getName()); + ee()->session->destroy(); + } + /** * Get a unique identifier for the auth session value. * diff --git a/src/Bootstrap/ConfigureAuthProvider.php b/src/Bootstrap/ConfigureAuthProvider.php index ffc48ee..52009d7 100644 --- a/src/Bootstrap/ConfigureAuthProvider.php +++ b/src/Bootstrap/ConfigureAuthProvider.php @@ -49,17 +49,27 @@ public function bootstrap(Application $app) $session->start(); // This is usually handled in middleware $provider = $app->make('auth')->createUserProvider($config['provider'] ?? null); - return new \Expressionengine\Coilpack\Auth\SessionGuard($name, $provider, $session); + $guard = new \Expressionengine\Coilpack\Auth\SessionGuard($name, $provider, $session); + $guard->setCookieJar($app['cookie']); + + return $guard; + }); + + app('auth')->provider('coilpack', function ($app, array $config) { + return new \Expressionengine\Coilpack\Auth\CoilpackUserProvider( + $app['hash'], + $config['model'] + ); }); // Configure our 'coilpack' guard which uses the 'members' provider below app('config')->set('auth.guards.coilpack', [ 'driver' => 'exp_sessions', - 'provider' => 'members', + 'provider' => 'coilpack', ]); - app('config')->set('auth.providers.members', [ - 'driver' => 'eloquent', + app('config')->set('auth.providers.coilpack', [ + 'driver' => 'coilpack', 'model' => $this->getMemberModel(), ]); diff --git a/src/CoilpackServiceProvider.php b/src/CoilpackServiceProvider.php index 461f29d..7582683 100644 --- a/src/CoilpackServiceProvider.php +++ b/src/CoilpackServiceProvider.php @@ -2,6 +2,7 @@ namespace Expressionengine\Coilpack; +use Expressionengine\Coilpack\Auth\CoilpackUserProvider; use Expressionengine\Coilpack\Api\Graph\Support\FieldtypeRegistrar; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Event; diff --git a/src/Models/Member/Member.php b/src/Models/Member/Member.php index dc1bd13..e107d65 100644 --- a/src/Models/Member/Member.php +++ b/src/Models/Member/Member.php @@ -8,7 +8,9 @@ use Expressionengine\Coilpack\Models\FieldContent; use Expressionengine\Coilpack\Models\Permission\Permission; use Expressionengine\Coilpack\Models\Role; +use Illuminate\Auth\Authenticatable; use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract; +use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; use Illuminate\Foundation\Auth\Access\Authorizable; /** @@ -18,9 +20,9 @@ * provided by the Member module. This is a single user of * the website. */ -class Member extends Model implements AuthorizableContract +class Member extends Model implements AuthorizableContract, AuthenticatableContract { - use Authorizable; + use Authorizable, Authenticatable; protected $primaryKey = 'member_id';