Skip to content

Commit 3dc1fba

Browse files
committed
Change triggering to events and listeners
1 parent a96a525 commit 3dc1fba

File tree

11 files changed

+250
-36
lines changed

11 files changed

+250
-36
lines changed

README.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,36 @@ This package can be used to enhance the user security of Laravel projects.
1717

1818
## Installation
1919

20-
[PHP](https://php.net) 5.5+ or [HHVM](http://hhvm.com) 3.3+, and [Composer](https://getcomposer.org) are required.
20+
Requirements:
21+
- [PHP](https://php.net) 5.5+
22+
- [Composer](https://getcomposer.org)
2123

22-
To get the latest version of Laravel Security, simply add the following line to the require block of your `composer.json` file.
24+
To get the latest version of Laravel Security, simply run:
2325

2426
```
2527
composer require sicaboy/laravel-security
28+
```
29+
30+
Then do vendor publish:
2631

32+
```
2733
php artisan vendor:publish --provider="Sicaboy\LaravelSecurity\LaravelSecurityServiceProvider"
2834
```
2935

30-
- If you're on Laravel 5.5 or above, that's all you need to do! Check out the usage examples below.
3136
- If you're on Laravel < 5.5, you'll need to register the service provider. Open up `config/app.php` and add the following to the `providers` array:
3237

3338
```php
34-
Siaboy\LaravelSecurity\LaravelSecurityServiceProvider::class
39+
Siaboy\LaravelSecurity\LaravelSecurityServiceProvider::class,
40+
Siaboy\LaravelSecurity\Providers\EventSecurityServiceProvider::class,
3541
```
3642

43+
# Validators
44+
3745
## Available Rules
3846

39-
- [NotCommonPassword](src/Rules/NotCommonPassword.php)
47+
- [NotCommonPassword](src/Rules/NotCommonPassword.php) - Avoid user to use a common used password
4048

41-
- [NotAUsedPassword](src/Rules/NotAUsedPassword.php)
49+
- [NotAUsedPassword](src/Rules/NotAUsedPassword.php) - Avoid user to use a password which has been used before
4250

4351

4452
## Usage
@@ -61,17 +69,29 @@ public function rules()
6169
//...
6270
new \Sicaboy\LaravelSecurity\Rules\NotCommonPassword(),
6371
new \Sicaboy\LaravelSecurity\Rules\NotAUsedPassword(),
64-
// or only check used password for a specific user:
72+
// or only check used password for a specific user (e.g. on user password change):
6573
// new \Sicaboy\LaravelSecurity\Rules\NotAUsedPassword($userId),
66-
// Also you need to call handler function mentioned in the next section
74+
// Also you need to call event, examples in the next section
6775
],
6876
];
6977
}
7078
```
7179

72-
## Additional method you need to call when you use NotAUsedPassword
80+
## Event you need to call
81+
82+
There are events you should add to coresponding methods.
83+
84+
- If you use `NotAUsedPassword` validator, you need to call the following events:
85+
86+
```php
87+
// Call on user regration
88+
event(new \Sicaboy\LaravelSecurity\Events\UserRegistered($user, $newPlainPassword));
89+
90+
// Call on user password change
91+
event(new \Sicaboy\LaravelSecurity\Events\UserPasswordChanged($user, $newPlainPassword));
92+
```
7393

74-
You need to call `NotAUsedPasswordHandler::lodgePassword` when the user is created and changes the password. If you use `NotAUsedPassword` validator.
94+
Example:
7595

7696
```php
7797
protected function create(array $data)
@@ -81,8 +101,8 @@ You need to call `NotAUsedPasswordHandler::lodgePassword` when the user is creat
81101
'email' => $data['email'],
82102
'password' => Hash::make($data['password']),
83103
]);
84-
85-
\Sicaboy\LaravelSecurity\Handlers\NotAUsedPasswordHandler::lodgePassword($user->id, $data['password']);
104+
105+
event(new \Sicaboy\LaravelSecurity\Events\UserRegistered($user, $data['password']));
86106

87107
return $user;
88108
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
},
4545
"laravel": {
4646
"providers": [
47-
"Sicaboy\\LaravelSecurity\\LaravelSecurityServiceProvider"
47+
"Sicaboy\\LaravelSecurity\\LaravelSecurityServiceProvider",
48+
"Sicaboy\\LaravelSecurity\\Providers\\EventServiceProvider"
4849
]
4950
}
5051
}

config/laravel-security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
'database' => [
4848
'connection' => '',
49-
'user_security_table' => 'user_security',
49+
'user_security_table' => 'user_extend_security',
5050
'password_history_table' => 'password_history',
5151
'password_history_model' => Sicaboy\LaravelSecurity\Model\PasswordHistory::class,
5252
'user_model' => 'App\User',

routes/web.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Illuminate\Http\Middleware\CheckResponseForModifications;
4+
use Illuminate\Support\Facades\Route;
5+
6+
// Scripts & Styles...
7+
//Route::get('/scripts/{script}', 'ScriptController@show')->middleware(CheckResponseForModifications::class);
8+
//Route::get('/styles/{style}', 'StyleController@show')->middleware(CheckResponseForModifications::class);
9+
10+
Route::get('/mfa', function() {return 'hihihi';})->name('mfa');
11+
Route::post('/mfa', function() {return 'hihihi';});
12+
Route::post('/d', function() {return 'hihihi';});

src/Events/UserLoggedIn.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Sicaboy\LaravelSecurity\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
13+
class UserLoggedIn
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
/**
18+
* Create a new event instance.
19+
*
20+
* @return void
21+
*/
22+
public function __construct()
23+
{
24+
//
25+
}
26+
27+
/**
28+
* Get the channels the event should broadcast on.
29+
*
30+
* @return \Illuminate\Broadcasting\Channel|array
31+
*/
32+
public function broadcastOn()
33+
{
34+
return new PrivateChannel('channel-name');
35+
}
36+
}

src/Events/UserPasswordChanged.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Sicaboy\LaravelSecurity\Events;
4+
5+
use Illuminate\Broadcasting\Channel;
6+
use Illuminate\Queue\SerializesModels;
7+
use Illuminate\Broadcasting\PrivateChannel;
8+
use Illuminate\Broadcasting\PresenceChannel;
9+
use Illuminate\Foundation\Events\Dispatchable;
10+
use Illuminate\Broadcasting\InteractsWithSockets;
11+
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
12+
13+
class UserPasswordChanged
14+
{
15+
use Dispatchable, InteractsWithSockets, SerializesModels;
16+
17+
public $user;
18+
public $plainPassword;
19+
20+
/**
21+
* Create a new event instance.
22+
*
23+
* @return void
24+
*/
25+
public function __construct($user, $plainPassword)
26+
{
27+
$this->user = $user;
28+
$this->plainPassword = $plainPassword;
29+
}
30+
31+
/**
32+
* Get the channels the event should broadcast on.
33+
*
34+
* @return \Illuminate\Broadcasting\Channel|array
35+
*/
36+
/*public function broadcastOn()
37+
{
38+
return new PrivateChannel('channel-name');
39+
}*/
40+
}

src/Events/UserRegistered.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
namespace Sicaboy\LaravelSecurity\Events;
4+
5+
class UserRegistered extends UserPasswordChanged
6+
{
7+
}

src/Handlers/NotAUsedPasswordHandler.php

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

src/LaravelSecurityServiceProvider.php

Lines changed: 49 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
namespace Sicaboy\LaravelSecurity;
1313

14+
use Illuminate\Support\Facades\Route;
1415
use Illuminate\Support\ServiceProvider;
1516
use Illuminate\Support\Facades\Cache;
17+
use Sicaboy\LaravelSecurity\Providers\EventServiceProvider;
1618
use Validator;
1719

1820
class LaravelSecurityServiceProvider extends ServiceProvider
@@ -23,7 +25,23 @@ class LaravelSecurityServiceProvider extends ServiceProvider
2325
*/
2426
public function boot()
2527
{
28+
$this->app->register(EventServiceProvider::class);
29+
30+
$this->loadViewsFrom(__DIR__.'/../resources/views', 'laravel-security');
31+
32+
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'laravel-security');
33+
34+
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
35+
36+
$this->commands([
37+
// FooCommand::class,
38+
// BarCommand::class,
39+
]);
40+
41+
$this->registerRoutes();
42+
2643
$this->registerPublishing();
44+
2745
}
2846

2947

@@ -37,13 +55,40 @@ protected function registerPublishing()
3755
if ($this->app->runningInConsole()) {
3856
$this->publishes([
3957
__DIR__.'/../config' => config_path(),
40-
__DIR__.'/../database/migrations' => database_path('migrations'),
58+
// __DIR__.'/../database/migrations' => database_path('migrations'),
4159
__DIR__.'/../resources/lang' => resource_path('lang'),
42-
__DIR__.'/../resources/views' => resource_path('views/laravel-security')
60+
__DIR__.'/../resources/views' => resource_path('views/vendor/laravel-security'),
4361
], 'laravel-security');
44-
45-
$this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'security');
4662
}
4763
}
4864

65+
66+
/**
67+
* Register the package routes.
68+
*
69+
* @return void
70+
*/
71+
protected function registerRoutes()
72+
{
73+
Route::group($this->routeConfiguration(), function () {
74+
// $this->loadRoutesFrom(__DIR__.'/../routes/api.php');
75+
$this->loadRoutesFrom(__DIR__.'/../routes/web.php');
76+
});
77+
}
78+
79+
/**
80+
* Get the Nova route group configuration array.
81+
*
82+
* @return array
83+
*/
84+
protected function routeConfiguration()
85+
{
86+
return [
87+
'namespace' => 'Sicaboy\LaravelSecurity\Http\Controllers',
88+
'prefix' => 'security',
89+
'as' => 'security.',
90+
'middleware' => 'web',
91+
];
92+
}
93+
4994
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Sicaboy\LaravelSecurity\Listeners;
4+
5+
use Illuminate\Queue\InteractsWithQueue;
6+
use Illuminate\Contracts\Queue\ShouldQueue;
7+
8+
class InsertUsedPassword
9+
{
10+
/**
11+
* Create the event listener.
12+
*
13+
* @return void
14+
*/
15+
public function __construct()
16+
{
17+
//
18+
}
19+
20+
/**
21+
* Handle the event.
22+
*
23+
* @param object $event
24+
* @return void
25+
*/
26+
public function handle($event)
27+
{
28+
$userId = $event->user->id;
29+
$password = $event->plainPassword;
30+
$modelClassName = config('laravel-security.database.password_history_model');
31+
return $modelClassName::create([
32+
'user_id' => $userId,
33+
'password' => bcrypt($password)
34+
]);
35+
}
36+
}

0 commit comments

Comments
 (0)