Skip to content

Commit 3977f4b

Browse files
authored
Merge pull request #3 from lbausch/develop
Version 0.2.0
2 parents d0cd12e + 5ec9daa commit 3977f4b

File tree

9 files changed

+212
-90
lines changed

9 files changed

+212
-90
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ jobs:
2222
strategy:
2323
fail-fast: true
2424
matrix:
25-
php: [7.2, 7.3, 7.4]
25+
php: [7.3, 7.4]
2626
stability: [prefer-lowest, prefer-stable]
2727

2828
name: PHP ${{ matrix.php }} - ${{ matrix.stability }}

README.md

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
![tests](https://github.com/lbausch/flarum-laravel-session/workflows/tests/badge.svg) [![codecov](https://codecov.io/gh/lbausch/flarum-laravel-session/branch/master/graph/badge.svg)](https://codecov.io/gh/lbausch/flarum-laravel-session)
44

5-
**Disclaimer**: This package is still a proof of concept.
6-
75
- [What It Does](#what-it-does)
86
- [Requirements](#requirements)
97
- [Installation and Configuration](#installation-and-configuration)
@@ -14,17 +12,17 @@
1412
- [Disable Cookie Encryption](#disable-cookie-encryption)
1513
- [Usage](#usage)
1614
- [Setup Middleware](#setup-middleware)
17-
- [Updating Attributes](#updating-attributes)
18-
- [Accessing Session Cookie From Different Domain](#accessing-session-cookie-from-different-domain)
15+
- [Handle An Identified User](#handle-an-identified-user)
16+
- [Accessing Flarum Session Cookie From Different Domain](#accessing-flarum-session-cookie-from-different-domain)
1917

2018
## What It Does
2119
This package allows to use the session of [Flarum](https://flarum.org/) for authentication within a Laravel application.
2220
It accesses Flarum's session cookie and reads the session data from the session storage.
23-
Based on the user information in the Flarum database an user in the Laravel application is created / updated and logged in.
21+
Based on the user information in the Flarum user database an user in the Laravel application is created / updated and logged in.
2422

2523
## Requirements
26-
+ PHP 7.2+
27-
+ Laravel 7
24+
+ PHP 7.3+
25+
+ Laravel 8
2826
+ Working installation of Flarum in the same filesystem as the Laravel application, so Flarum's session files can be read
2927
+ Flarum and Laravel need to share the same domain / subdomain, so Flarum's session cookie can be accessed
3028

@@ -37,7 +35,7 @@ composer require lbausch/flarum-laravel-session
3735
```
3836

3937
### Register Middleware
40-
Register the middleware in `app/Http/Kernel.php`:
38+
Register the `\Bausch\FlarumLaravelSession\FlarumSessionMiddleware` middleware in `app/Http/Kernel.php`:
4139
```php
4240
/**
4341
* The application's route middleware.
@@ -104,13 +102,48 @@ Route::middleware(['flarum'])->group(function () {
104102
});
105103
});
106104
```
105+
All requests to the `/` route will then be checked by the middleware.
106+
107+
### Handle An Identified User
108+
Once the middleware successfully identified an user, it executes the default handler `\Bausch\FlarumLaravelSession\Actions\HandleIdentifiedUser`. You may configure a different handler by calling `FlarumLaravelSession::handleIdentifiedUser()` in a service provider. This is a perfect place to update attributes or execute further actions, just remember to implement the `\Bausch\FlarumLaravelSession\Contracts\FlarumUserIdentified` interface.
109+
Have a look at the default handler for a reference implementation.
107110

108-
### Updating Attributes
109-
Attributes which are updated upon a successful authentication can be specified by modifying the array `update_attributes` in `config/flarum.php`.
110-
To track the relationship between your local user and the Flarum user, you may add a column `flarum_id` to your users table.
111+
```php
112+
<?php
113+
114+
namespace App\Providers;
115+
116+
use Illuminate\Support\ServiceProvider;
117+
use Bausch\FlarumLaravelSession\FlarumLaravelSession;
118+
use App\Handlers\YourCustomHandler;
119+
120+
class AppServiceProvider extends ServiceProvider
121+
{
122+
/**
123+
* Register any application services.
124+
*
125+
* @return void
126+
*/
127+
public function register()
128+
{
129+
//
130+
}
131+
132+
/**
133+
* Bootstrap any application services.
134+
*
135+
* @return void
136+
*/
137+
public function boot()
138+
{
139+
FlarumLaravelSession::handleIdentifiedUser(YourCustomHandler::class);
140+
}
141+
}
142+
```
111143

144+
If you need to use a different user model than `App\Models\User`, you may call `FlarumLaravelSession::useUserModel(YourUser::class);` in your service provider.
112145

113-
## Accessing Session Cookie From Different Domain
146+
## Accessing Flarum Session Cookie From Different Domain
114147
If Flarum is running on domain.tld and Laravel on sub.domain.tld you need to configure Flarum (`config.php`), so the session cookie can be accessed on the subdomain:
115148
```php
116149
// Note the dot before domain.tld

composer.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,16 @@
2222
}
2323
],
2424
"require": {
25-
"php": "^7.2",
26-
"illuminate/auth": "^7.0",
27-
"illuminate/filesystem": "^7.0",
28-
"illuminate/http": "^7.0",
29-
"illuminate/session": "^7.0",
30-
"illuminate/support": "^7.0"
25+
"php": "^7.3",
26+
"illuminate/auth": "^8.0",
27+
"illuminate/filesystem": "^8.0",
28+
"illuminate/http": "^8.0",
29+
"illuminate/session": "^8.0",
30+
"illuminate/support": "^8.0"
3131
},
3232
"require-dev": {
33-
"orchestra/testbench": "^5.3",
34-
"phpunit/phpunit": "8.*"
33+
"orchestra/testbench": "^6.0",
34+
"phpunit/phpunit": "9.*"
3535
},
3636
"autoload": {
3737
"psr-4": {

config/flarum.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
*/
77
'url' => env('FLARUM_URL'),
88

9-
/*
10-
* Model which is authenticatable
11-
*/
12-
'model' => config('auth.providers.users.model', App\User::class),
13-
149
/*
1510
* Flarum session configuration
1611
*/
@@ -30,13 +25,4 @@
3025
* Flarum database connection as defined in config/database.php
3126
*/
3227
'db_connection' => env('FLARUM_DB_CONNECTION', 'flarum'),
33-
34-
/*
35-
* Attributes to update upon successful authentication: Flarum user => local user
36-
*/
37-
'update_attributes' => [
38-
'username' => 'username',
39-
'id' => 'flarum_id',
40-
'email' => 'email',
41-
],
4228
];

src/Actions/HandleIdentifiedUser.php

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?php
2+
3+
namespace Bausch\FlarumLaravelSession\Actions;
4+
5+
use Bausch\FlarumLaravelSession\Contracts\FlarumUserIdentified;
6+
use Bausch\FlarumLaravelSession\FlarumLaravelSession;
7+
use Closure;
8+
use Illuminate\Container\Container;
9+
use Illuminate\Foundation\Auth\User;
10+
use Illuminate\Http\Request;
11+
use Illuminate\Support\Facades\Auth;
12+
use Illuminate\Support\Str;
13+
14+
class HandleIdentifiedUser implements FlarumUserIdentified
15+
{
16+
public function __invoke(object $flarum_user, Request $request, Closure $next)
17+
{
18+
// Find the corresponding local user
19+
$user = $this->getUser()->where('flarum_id', $flarum_user->id)->first();
20+
21+
// Create or update the corresponding local user
22+
$user = $this->createOrUpdateUser($user, $flarum_user);
23+
24+
// Login the local user and remember him
25+
Auth::login($user, $remember = true);
26+
27+
return $next($request);
28+
}
29+
30+
/**
31+
* Create or update User.
32+
*
33+
* @param \Illuminate\Foundation\Auth\User $user
34+
*/
35+
protected function createOrUpdateUser(?User $user, object $flarum_user): User
36+
{
37+
// Get a user instance
38+
if (null === $user) {
39+
$user = $this->getUser();
40+
}
41+
42+
$user = $this->updateAttributes($user, $flarum_user);
43+
44+
// Save user
45+
if ($user->isDirty()) {
46+
$user->save();
47+
}
48+
49+
// Return user
50+
return $user;
51+
}
52+
53+
/**
54+
* Update attributes.
55+
*
56+
* @param \Illuminate\Foundation\Auth\User $user
57+
*/
58+
protected function updateAttributes(?User $user, object $flarum_user): User
59+
{
60+
// Attributes to update: Flarum user => local user
61+
$update_attributes = [
62+
'username' => 'username',
63+
'id' => 'flarum_id',
64+
'email' => 'email',
65+
];
66+
67+
// Update attributes
68+
foreach ($update_attributes as $flarum_attribute => $local_attribute) {
69+
$user->{$local_attribute} = $flarum_user->{$flarum_attribute};
70+
}
71+
72+
if ($user->isDirty()) {
73+
// Set a random password
74+
$user->password = bcrypt(Str::random(30));
75+
}
76+
77+
return $user;
78+
}
79+
80+
/**
81+
* Get user instance.
82+
*/
83+
protected function getUser(): User
84+
{
85+
return Container::getInstance()->make(FlarumLaravelSession::userModel());
86+
}
87+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bausch\FlarumLaravelSession\Contracts;
4+
5+
use Closure;
6+
use Illuminate\Http\Request;
7+
8+
interface FlarumUserIdentified
9+
{
10+
/**
11+
* Handle an authenticated Flarum user.
12+
*
13+
* @return mixed
14+
*/
15+
public function __invoke(object $flarum_user, Request $request, Closure $next);
16+
}

src/FlarumLaravelSession.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Bausch\FlarumLaravelSession;
4+
5+
use Bausch\FlarumLaravelSession\Contracts\FlarumUserIdentified;
6+
7+
class FlarumLaravelSession
8+
{
9+
/**
10+
* The user model that should be used by FlarumLaravelSession.
11+
*
12+
* @var string
13+
*/
14+
public static $userModel = 'App\\Models\\User';
15+
16+
/**
17+
* Register class that handles actions once a Flarum user has been identified.
18+
*/
19+
public static function handleIdentifiedUser(string $class)
20+
{
21+
return app()->singleton(FlarumUserIdentified::class, $class);
22+
}
23+
24+
/**
25+
* Get the name of the user model used by the application.
26+
*
27+
* @return string
28+
*/
29+
public static function userModel()
30+
{
31+
return static::$userModel;
32+
}
33+
34+
/**
35+
* Specify the user model that should be used by FlarumLaravelSession.
36+
*
37+
* @return static
38+
*/
39+
public static function useUserModel(string $model)
40+
{
41+
static::$userModel = $model;
42+
43+
return new static();
44+
}
45+
}

src/FlarumSessionMiddleware.php

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22

33
namespace Bausch\FlarumLaravelSession;
44

5+
use Bausch\FlarumLaravelSession\Contracts\FlarumUserIdentified;
56
use Closure;
67
use Illuminate\Container\Container;
78
use Illuminate\Filesystem\Filesystem;
8-
use Illuminate\Foundation\Auth\User;
99
use Illuminate\Http\Request;
1010
use Illuminate\Session\FileSessionHandler;
1111
use Illuminate\Session\Store;
1212
use Illuminate\Support\Facades\Auth;
1313
use Illuminate\Support\Facades\Config;
1414
use Illuminate\Support\Facades\DB;
15-
use Illuminate\Support\Str;
1615

1716
class FlarumSessionMiddleware
1817
{
@@ -58,16 +57,11 @@ public function handle(Request $request, Closure $next)
5857
abort(403);
5958
}
6059

61-
// Find the corresponding local user
62-
$user = $this->getUser()->where('flarum_id', $flarum_user->id)->first();
60+
// Get Handler for handling Flarum user
61+
$handler = Container::getInstance()->make(FlarumUserIdentified::class);
6362

64-
// Create or update the corresponding local user
65-
$user = $this->createOrUpdateUser($user, $flarum_user);
66-
67-
// Login the local user and remember him
68-
Auth::login($user, true);
69-
70-
return $next($request);
63+
// Execute handler
64+
return $handler($flarum_user, $request, $next);
7165
}
7266

7367
/**
@@ -90,42 +84,4 @@ protected function getFileSessionHandler(): FileSessionHandler
9084
$lifetime_minutes
9185
);
9286
}
93-
94-
/**
95-
* Create or update User.
96-
*/
97-
protected function createOrUpdateUser(?User $user, object $flarum_user): User
98-
{
99-
// Get a user instance
100-
if (null === $user) {
101-
$user = $this->getUser();
102-
}
103-
104-
// Attributes to update: Flarum user => local user
105-
$update_attributes = Config::get('flarum.update_attributes', []);
106-
107-
// Update attributes
108-
foreach ($update_attributes as $flarum_attribute => $local_attribute) {
109-
$user->{$local_attribute} = $flarum_user->{$flarum_attribute};
110-
}
111-
112-
// Set a random password
113-
$user->password = bcrypt(Str::random(30));
114-
115-
// Save user
116-
if ($user->isDirty()) {
117-
$user->save();
118-
}
119-
120-
// Return user
121-
return $user;
122-
}
123-
124-
/**
125-
* Get User instance.
126-
*/
127-
protected function getUser(): User
128-
{
129-
return Container::getInstance()->make(Config::get('flarum.model'));
130-
}
13187
}

0 commit comments

Comments
 (0)