-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPlugin.php
163 lines (140 loc) · 4.35 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
declare(strict_types=1);
namespace ReaZzon\JWTAuth;
use Event, Config;
use System\Classes\PluginBase;
use ReaZzon\JWTAuth\Classes\UserPluginResolver;
use ReaZzon\JWTAuth\Classes\Contracts\UserPluginResolver as UserPluginResolverContract;
use Illuminate\Auth\Access\Gate;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Foundation\AliasLoader;
use Illuminate\Contracts\Auth\Access\Gate as GateContract;
use ReaZzon\JWTAuth\Classes\Guards\JWTGuard;
use ReaZzon\JWTAuth\Classes\Providers\UserProvider;
use Lovata\Buddies\Models\User;
use ReaZzon\JWTAuth\Classes\Events\UserModelHandler;
use System\Classes\PluginManager;
use PHPOpenSourceSaver\JWTAuth\Providers\LaravelServiceProvider;
/**
* JWTAuth Plugin Information File
*/
class Plugin extends PluginBase
{
/**
* Returns information about this plugin.
*
* @return array
*/
public function pluginDetails()
{
return [
'name' => 'JWTAuth',
'description' => 'JWT authorization plugin',
'author' => 'ReaZzon, LeMaX10',
'icon' => 'icon-leaf'
];
}
/**
* Register method, called when the plugin is first registered.
*
* @return void
*/
public function register()
{
$this->checkRequiredPlugins();
$this->app->singleton(
UserPluginResolverContract::class,
static fn() => UserPluginResolver::instance()
);
$this->registerGates();
$this->registerJWT();
}
/**
* Boot method, called right before the request route.
*
* @return void
*/
public function boot()
{
$this->registerConfigs();
$this->addEventListeners();
}
protected function checkRequiredPlugins()
{
$plugins = ['RainLab.User', 'Lovata.Buddies'];
$pluginInstalled = false;
foreach ($plugins as $pluginName) {
if (PluginManager::instance()->hasPlugin($pluginName)) {
$pluginInstalled = true;
}
}
if (!$pluginInstalled) {
PluginManager::instance()->disablePlugin('ReaZzon.JWTAuth');
}
}
/**
*
*/
private function registerConfigs()
{
$pluginNamespace = str_replace('\\', '.', strtolower(__NAMESPACE__));
$packages = Config::get($pluginNamespace . '::packages');
foreach ($packages as $name => $options) {
if (!empty($options['config']) && !empty($options['config_namespace'])) {
Config::set($options['config_namespace'], $options['config']);
}
}
}
/**
*
*/
private function addEventListeners()
{
Event::subscribe(UserModelHandler::class);
}
/**
* Регистрация менеджера авторизации.
*/
private function registerGates(): void
{
$alias = AliasLoader::getInstance();
$alias->alias('Gate', \Illuminate\Support\Facades\Gate::class);
$this->app->singleton(GateContract::class, function ($app): Gate {
return new Gate($app, function () use ($app): ?User {
return app(UserPluginResolverContract::class)->getProvider()->user();
});
});
}
/**
* Регистрация JWT.
*/
private function registerJWT(): void
{
(new LaravelServiceProvider($this->app))->register();
$this->app->singleton('JWTGuard', static function ($app): Guard {
$guard = new JWTGuard(
$app['tymon.jwt'],
new UserProvider(app(UserPluginResolverContract::class)->getModel()),
$app['request'],
new \October\Rain\Events\Dispatcher()
);
$app->refresh('request', $guard, 'setRequest');
return $guard;
});
}
public function registerSettings(): array
{
return [
'settings' => [
'label' => 'Buddies Settings',
'description' => 'Manage user based settings.',
'category' => 'JWTAuth',
'icon' => 'icon-cog',
'class' => \ReaZzon\JWTAuth\Models\BuddiesSettings::class,
'order' => 500,
'keywords' => 'buddies users',
'permissions' => ['buddies-menu-*']
]
];
}
}