Skip to content
This repository was archived by the owner on Nov 6, 2019. It is now read-only.

Commit f429bf2

Browse files
committed
✨ init commit.
0 parents  commit f429bf2

13 files changed

+316
-0
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
root = true
2+
3+
[*]
4+
indent_style = space
5+
indent_size = 4
6+
end_of_line = lf
7+
charset = utf-8
8+
trim_trailing_whitespace = true
9+
insert_final_newline = false
10+
11+
[*.{vue,js,scss}]
12+
charset = utf-8
13+
indent_style = space
14+
indent_size = 2
15+
end_of_line = lf
16+
insert_final_newline = true
17+
trim_trailing_whitespace = true
18+
19+
[*.md]
20+
trim_trailing_whitespace = false

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
* text=auto
2+
3+
/tests export-ignore
4+
.gitattributes export-ignore
5+
.gitignore export-ignore
6+
.scrutinizer.yml export-ignore
7+
.travis.yml export-ignore
8+
phpunit.php export-ignore
9+
phpunit.xml.dist export-ignore
10+
phpunit.xml export-ignore
11+
.php_cs export-ignore

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
*.DS_Store
2+
/vendor
3+
/coverage
4+
sftp-config.json
5+
composer.lock
6+
.subsplit
7+
.php_cs.cache

.php_cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
$header = <<<EOF
3+
This file is part of the overtrue/laravel-single-session.
4+
5+
(c) overtrue <i@overtrue.me>
6+
7+
This source file is subject to the MIT license that is bundled
8+
with this source code in the file LICENSE.
9+
EOF;
10+
11+
return PhpCsFixer\Config::create()
12+
->setRiskyAllowed(true)
13+
->setRules(array(
14+
'@Symfony' => true,
15+
'header_comment' => array('header' => $header),
16+
'array_syntax' => array('syntax' => 'short'),
17+
'ordered_imports' => true,
18+
'no_useless_else' => true,
19+
'no_useless_return' => true,
20+
'php_unit_construct' => true,
21+
'php_unit_strict' => true,
22+
))
23+
->setFinder(
24+
PhpCsFixer\Finder::create()
25+
->exclude('vendor')
26+
->in(__DIR__)
27+
)
28+
;

README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Laravel-single-session
2+
3+
A plugin provide single session authentication for Laravel 5.
4+
5+
## Installing
6+
7+
```shell
8+
$ composer require overtrue/laravel-single-session -vvv
9+
```
10+
11+
## Usage
12+
13+
1. Register provider(if you disabled Laravel auto discovery)
14+
15+
```php
16+
'providers' => [
17+
Overtrue\LaravelSingleSession\SingleSessionServiceProvider::class
18+
],
19+
```
20+
21+
2. (Optional) Config the session:
22+
23+
```php
24+
// config/session.php
25+
// The storage for store the last session id.
26+
'last_session_storage' => 'cache', // cache(default)/database
27+
28+
// The field name of last session id.
29+
'last_session_field' => 'last_session_id',
30+
31+
// The path of redirect when logout after session changed.
32+
'redirect_path' => '/',
33+
```
34+
35+
3. it works.
36+
37+
38+
### Events
39+
40+
If the session changed, the following event will be triggered:
41+
42+
```phl
43+
Overtrue\LaravelSingleSession\Events\SessionExpired
44+
```
45+
46+
## License
47+
48+
MIT

composer.json

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "overtrue/laravel-single-session",
3+
"description": "A plugin provide single session authentication for Laravel 5.",
4+
"type": "library",
5+
"require": {
6+
"laravel/framework": "~5.5"
7+
},
8+
"license": "MIT",
9+
"authors": [
10+
{
11+
"name": "overtrue",
12+
"email": "anzhengchao@gmail.com"
13+
}
14+
],
15+
"autoload": {
16+
"psr-4":{
17+
"Overtrue\\LaravelSingleSession\\": "./src"
18+
}
19+
},
20+
"extra": {
21+
"laravel": {
22+
"aliases": {
23+
"LaravelSingleSessionMiddleware": "Overtrue\\LaravelSingleSession\\Middleware\\SingleSession"
24+
},
25+
"providers": ["Overtrue\\LaravelSingleSession\\SingleSessionServiceProvider"]
26+
}
27+
}
28+
}

phpunit.xml.dist

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
<testsuites>
13+
<testsuite name="Application Test Suite">
14+
<directory>./tests/</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/.gitkeep

Whitespace-only changes.

src/Events/SessionDestroyed.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-single-session.
5+
*
6+
* (c) overtrue <i@overtrue.me>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Overtrue\LaravelSingleSession\Events;
13+
14+
use Illuminate\Broadcasting\InteractsWithSockets;
15+
use Illuminate\Foundation\Events\Dispatchable;
16+
use Illuminate\Queue\SerializesModels;
17+
18+
class SessionDestroyed
19+
{
20+
use Dispatchable, InteractsWithSockets, SerializesModels;
21+
22+
public $user;
23+
public $sessionId;
24+
25+
/**
26+
* Create a new event instance.
27+
*/
28+
public function __construct($user, $sessionId)
29+
{
30+
$this->user = $user;
31+
$this->sessionId = $sessionId;
32+
}
33+
}

src/Listeners/ClearLastSession.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-single-session.
5+
*
6+
* (c) overtrue <i@overtrue.me>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Overtrue\LaravelSingleSession\Listeners;
13+
14+
use Illuminate\Support\Facades\Cache;
15+
use Illuminate\Support\Facades\Event;
16+
use Illuminate\Support\Facades\Session;
17+
use Overtrue\LaravelSingleSession\Events\SessionDestroyed;
18+
19+
/**
20+
* Class ClearLastSession.
21+
*
22+
* @author overtrue <i@overtrue.me>
23+
*/
24+
class ClearLastSession
25+
{
26+
/**
27+
* Handle the event.
28+
*
29+
* @param object $event
30+
*/
31+
public function handle($event)
32+
{
33+
$lastSessionField = config('last_session_field', 'last_session_id');
34+
35+
if (config('session.last_session_storage', 'cache')) {
36+
$previousSessionId = Cache::pull($lastSessionField.'.'.$event->user->id);
37+
Session::getHandler()->destroy($previousSessionId);
38+
Event::fire(new SessionDestroyed($event->user, $previousSessionId));
39+
} else {
40+
$event->user->update([$lastSessionField => null]);
41+
}
42+
}
43+
}

src/Listeners/StoreLastSession.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-single-session.
5+
*
6+
* (c) overtrue <i@overtrue.me>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Overtrue\LaravelSingleSession\Listeners;
13+
14+
use Illuminate\Support\Facades\Cache;
15+
use Illuminate\Support\Facades\Session;
16+
17+
/**
18+
* Class StoreLastSession.
19+
*
20+
* @author overtrue <i@overtrue.me>
21+
*/
22+
class StoreLastSession
23+
{
24+
/**
25+
* Handle the event.
26+
*
27+
* @param object $event
28+
*/
29+
public function handle($event)
30+
{
31+
$currentSessionId = Session::getId();
32+
$lastSessionField = config('last_session_field', 'last_session_id');
33+
34+
if (config('session.last_session_storage', 'cache')) {
35+
Cache::forever($lastSessionField.'.'.$event->user->id, $currentSessionId);
36+
} else {
37+
$event->user->update([$lastSessionField => $currentSessionId]);
38+
}
39+
}
40+
}

src/SingleSessionServiceProvider.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the overtrue/laravel-single-session.
5+
*
6+
* (c) overtrue <i@overtrue.me>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Overtrue\LaravelSingleSession;
13+
14+
use Illuminate\Support\Facades\Event;
15+
use Illuminate\Support\ServiceProvider;
16+
use Overtrue\LaravelSingleSession\Listeners\ClearLastSession;
17+
use Overtrue\LaravelSingleSession\Listeners\StoreLastSession;
18+
19+
/**
20+
* Class SingleSessionServiceProvider.
21+
*
22+
* @author overtrue <i@overtrue.me>
23+
*/
24+
class SingleSessionServiceProvider extends ServiceProvider
25+
{
26+
public function boot()
27+
{
28+
Event::listen(\Illuminate\Auth\Events\Login::class, ClearLastSession::class);
29+
Event::listen(\Illuminate\Auth\Events\Authenticated::class, StoreLastSession::class);
30+
}
31+
32+
public function register()
33+
{
34+
// do nothing
35+
}
36+
}

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)