1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Rojtjo\SentinelGuard; |
4
|
|
|
|
5
|
|
|
use Cartalyst\Sentinel\Sentinel; |
6
|
|
|
use Illuminate\Auth\AuthManager; |
7
|
|
|
use Illuminate\Support\ServiceProvider; |
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
9
|
|
|
|
10
|
|
|
class SentinelGuardServiceProvider extends ServiceProvider |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @param AuthManager $auth |
14
|
|
|
*/ |
15
|
|
|
public function boot(AuthManager $auth) |
16
|
|
|
{ |
17
|
|
|
$auth->extend('sentinel', function () { |
18
|
|
|
return new SentinelGuard( |
19
|
|
|
$this->app->make(GuardableSentinel::class) |
20
|
|
|
); |
21
|
|
|
}); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return void |
26
|
|
|
*/ |
27
|
|
|
public function register() |
28
|
|
|
{ |
29
|
|
|
$this->app->singleton('sentinel', function ($app) { |
30
|
|
|
$sentinel = new GuardableSentinel( |
31
|
|
|
$app['sentinel.persistence'], |
32
|
|
|
$app['sentinel.users'], |
33
|
|
|
$app['sentinel.roles'], |
34
|
|
|
$app['sentinel.activations'], |
35
|
|
|
$app['events'] |
36
|
|
|
); |
37
|
|
|
|
38
|
|
|
if (isset($app['sentinel.checkpoints'])) { |
39
|
|
|
foreach ($app['sentinel.checkpoints'] as $key => $checkpoint) { |
40
|
|
|
$sentinel->addCheckpoint($key, $checkpoint); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
$sentinel->setActivationRepository($app['sentinel.activations']); |
45
|
|
|
$sentinel->setReminderRepository($app['sentinel.reminders']); |
46
|
|
|
|
47
|
|
|
$sentinel->setRequestCredentials(function () use ($app) { |
48
|
|
|
$request = $app['request']; |
49
|
|
|
|
50
|
|
|
$login = $request->getUser(); |
51
|
|
|
$password = $request->getPassword(); |
52
|
|
|
|
53
|
|
|
if ($login === null && $password === null) { |
54
|
|
|
return; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return compact('login', 'password'); |
58
|
|
|
}); |
59
|
|
|
|
60
|
|
|
$sentinel->creatingBasicResponse(function () { |
61
|
|
|
$headers = ['WWW-Authenticate' => 'Basic']; |
62
|
|
|
|
63
|
|
|
return new Response('Invalid credentials.', 401, $headers); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
return $sentinel; |
67
|
|
|
}); |
68
|
|
|
|
69
|
|
|
$this->app->alias('sentinel', GuardableSentinel::class); |
70
|
|
|
$this->app->alias(Sentinel::class, GuardableSentinel::class); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|