|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cortex\Auth\Providers; |
|
6
|
|
|
|
|
7
|
|
|
use Bouncer; |
|
8
|
|
|
use Cortex\Auth\Models\Role; |
|
9
|
|
|
use Illuminate\Http\Request; |
|
10
|
|
|
use Cortex\Auth\Models\Admin; |
|
11
|
|
|
use Cortex\Auth\Models\Member; |
|
12
|
|
|
use Illuminate\Routing\Router; |
|
13
|
|
|
use Cortex\Auth\Models\Ability; |
|
14
|
|
|
use Cortex\Auth\Models\Manager; |
|
15
|
|
|
use Cortex\Auth\Models\Session; |
|
16
|
|
|
use Cortex\Auth\Models\Guardian; |
|
17
|
|
|
use Cortex\Auth\Models\Socialite; |
|
18
|
|
|
use Illuminate\Support\ServiceProvider; |
|
19
|
|
|
use Rinvex\Support\Traits\ConsoleTools; |
|
20
|
|
|
use Cortex\Auth\Handlers\GenericHandler; |
|
21
|
|
|
use Cortex\Auth\Console\Commands\SeedCommand; |
|
22
|
|
|
use Cortex\Auth\Http\Middleware\Reauthenticate; |
|
23
|
|
|
use Cortex\Auth\Console\Commands\InstallCommand; |
|
24
|
|
|
use Cortex\Auth\Console\Commands\MigrateCommand; |
|
25
|
|
|
use Cortex\Auth\Console\Commands\PublishCommand; |
|
26
|
|
|
use Cortex\Auth\Console\Commands\RollbackCommand; |
|
27
|
|
|
use Cortex\Auth\Http\Middleware\UpdateLastActivity; |
|
28
|
|
|
use Cortex\Auth\Http\Middleware\AuthenticateSession; |
|
29
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
|
30
|
|
|
use Cortex\Auth\Http\Middleware\RedirectIfAuthenticated; |
|
31
|
|
|
|
|
32
|
|
|
class AuthServiceProvider extends ServiceProvider |
|
33
|
|
|
{ |
|
34
|
|
|
use ConsoleTools; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* The commands to be registered. |
|
38
|
|
|
* |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $commands = [ |
|
42
|
|
|
SeedCommand::class => 'command.cortex.auth.seed', |
|
43
|
|
|
InstallCommand::class => 'command.cortex.auth.install', |
|
44
|
|
|
MigrateCommand::class => 'command.cortex.auth.migrate', |
|
45
|
|
|
PublishCommand::class => 'command.cortex.auth.publish', |
|
46
|
|
|
RollbackCommand::class => 'command.cortex.auth.rollback', |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Register any application services. |
|
51
|
|
|
* |
|
52
|
|
|
* This service provider is a great spot to register your various container |
|
53
|
|
|
* bindings with the application. As you can see, we are registering our |
|
54
|
|
|
* "Registrar" implementation here. You can add your own bindings too! |
|
55
|
|
|
* |
|
56
|
|
|
* @return void |
|
57
|
|
|
*/ |
|
58
|
|
|
public function register(): void |
|
59
|
|
|
{ |
|
60
|
|
|
// Merge config |
|
61
|
|
|
$this->app['config']->set('auth.model', config('cortex.auth.models.member')); |
|
62
|
|
|
$this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'cortex.auth'); |
|
63
|
|
|
|
|
64
|
|
|
// Register console commands |
|
65
|
|
|
! $this->app->runningInConsole() || $this->registerCommands(); |
|
66
|
|
|
|
|
67
|
|
|
// Bind eloquent models to IoC container |
|
68
|
|
|
$this->app->singleton('cortex.auth.session', $sessionModel = $this->app['config']['cortex.auth.models.session']); |
|
69
|
|
|
$sessionModel === Session::class || $this->app->alias('cortex.auth.session', Session::class); |
|
70
|
|
|
|
|
71
|
|
|
$this->app->singleton('cortex.auth.socialite', $socialiteModel = $this->app['config']['cortex.auth.models.socialite']); |
|
72
|
|
|
$socialiteModel === Socialite::class || $this->app->alias('cortex.auth.socialite', Socialite::class); |
|
73
|
|
|
|
|
74
|
|
|
$this->app->singleton('cortex.auth.admin', $adminModel = $this->app['config']['cortex.auth.models.admin']); |
|
75
|
|
|
$adminModel === Admin::class || $this->app->alias('cortex.auth.admin', Admin::class); |
|
76
|
|
|
|
|
77
|
|
|
$this->app->singleton('cortex.auth.member', $memberModel = $this->app['config']['cortex.auth.models.member']); |
|
78
|
|
|
$memberModel === Member::class || $this->app->alias('cortex.auth.member', Member::class); |
|
79
|
|
|
|
|
80
|
|
|
$this->app->singleton('cortex.auth.manager', $managerModel = $this->app['config']['cortex.auth.models.manager']); |
|
81
|
|
|
$managerModel === Manager::class || $this->app->alias('cortex.auth.manager', Manager::class); |
|
82
|
|
|
|
|
83
|
|
|
$this->app->singleton('cortex.auth.guardian', $guardianModel = $this->app['config']['cortex.auth.models.guardian']); |
|
84
|
|
|
$guardianModel === Guardian::class || $this->app->alias('cortex.auth.guardian', Guardian::class); |
|
85
|
|
|
|
|
86
|
|
|
$this->app->singleton('cortex.auth.role', $roleModel = $this->app['config']['cortex.auth.models.role']); |
|
87
|
|
|
$roleModel === Role::class || $this->app->alias('cortex.auth.role', Role::class); |
|
88
|
|
|
|
|
89
|
|
|
$this->app->singleton('cortex.auth.ability', $abilityModel = $this->app['config']['cortex.auth.models.ability']); |
|
90
|
|
|
$abilityModel === Ability::class || $this->app->alias('cortex.auth.ability', Ability::class); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Bootstrap any application services. |
|
95
|
|
|
* |
|
96
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
|
97
|
|
|
* |
|
98
|
|
|
* @return void |
|
99
|
|
|
*/ |
|
100
|
|
|
public function boot(Router $router): void |
|
101
|
|
|
{ |
|
102
|
|
|
// Attach request macro |
|
103
|
|
|
$this->attachRequestMacro(); |
|
104
|
|
|
|
|
105
|
|
|
// Map bouncer models |
|
106
|
|
|
Bouncer::useRoleModel(config('cortex.auth.models.role')); |
|
107
|
|
|
Bouncer::useAbilityModel(config('cortex.auth.models.ability')); |
|
108
|
|
|
|
|
109
|
|
|
// Map bouncer tables (users, roles, abilities tables are set through their models) |
|
110
|
|
|
Bouncer::tables([ |
|
111
|
|
|
'permissions' => config('cortex.auth.tables.permissions'), |
|
112
|
|
|
'assigned_roles' => config('cortex.auth.tables.assigned_roles'), |
|
113
|
|
|
]); |
|
114
|
|
|
|
|
115
|
|
|
// Bind route models and constrains |
|
116
|
|
|
$router->pattern('role', '[a-zA-Z0-9-]+'); |
|
117
|
|
|
$router->pattern('ability', '[a-zA-Z0-9-]+'); |
|
118
|
|
|
$router->pattern('session', '[a-zA-Z0-9-]+'); |
|
119
|
|
|
$router->pattern('admin', '[a-zA-Z0-9-]+'); |
|
120
|
|
|
$router->pattern('member', '[a-zA-Z0-9-]+'); |
|
121
|
|
|
$router->pattern('manager', '[a-zA-Z0-9-]+'); |
|
122
|
|
|
$router->model('role', config('cortex.auth.models.role')); |
|
123
|
|
|
$router->model('admin', config('cortex.auth.models.admin')); |
|
124
|
|
|
$router->model('member', config('cortex.auth.models.member')); |
|
125
|
|
|
$router->model('manager', config('cortex.auth.models.manager')); |
|
126
|
|
|
$router->model('guardian', config('cortex.auth.models.guardian')); |
|
127
|
|
|
$router->model('ability', config('cortex.auth.models.ability')); |
|
128
|
|
|
$router->model('session', config('cortex.auth.models.session')); |
|
129
|
|
|
|
|
130
|
|
|
// Map relations |
|
131
|
|
|
Relation::morphMap([ |
|
132
|
|
|
'role' => config('cortex.auth.models.role'), |
|
133
|
|
|
'admin' => config('cortex.auth.models.admin'), |
|
134
|
|
|
'member' => config('cortex.auth.models.member'), |
|
135
|
|
|
'manager' => config('cortex.auth.models.manager'), |
|
136
|
|
|
'guardian' => config('cortex.auth.models.guardian'), |
|
137
|
|
|
'ability' => config('cortex.auth.models.ability'), |
|
138
|
|
|
]); |
|
139
|
|
|
|
|
140
|
|
|
// Load resources |
|
141
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/adminarea.php'); |
|
142
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/frontarea.php'); |
|
143
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/managerarea.php'); |
|
144
|
|
|
$this->loadRoutesFrom(__DIR__.'/../../routes/web/tenantarea.php'); |
|
145
|
|
|
$this->loadViewsFrom(__DIR__.'/../../resources/views', 'cortex/auth'); |
|
146
|
|
|
$this->loadTranslationsFrom(__DIR__.'/../../resources/lang', 'cortex/auth'); |
|
147
|
|
|
$this->app->runningInConsole() || $this->app->afterResolving('blade.compiler', function () { |
|
148
|
|
|
$accessarea = $this->app['request']->route('accessarea'); |
|
149
|
|
|
! file_exists($menus = __DIR__."/../../routes/menus/{$accessarea}.php") || require $menus; |
|
150
|
|
|
! file_exists($breadcrumbs = __DIR__."/../../routes/breadcrumbs/{$accessarea}.php") || require $breadcrumbs; |
|
151
|
|
|
}); |
|
152
|
|
|
|
|
153
|
|
|
// Publish Resources |
|
154
|
|
|
! $this->app->runningInConsole() || $this->publishesLang('cortex/auth', true); |
|
155
|
|
|
! $this->app->runningInConsole() || $this->publishesViews('cortex/auth', true); |
|
156
|
|
|
! $this->app->runningInConsole() || $this->publishesConfig('cortex/auth', true); |
|
157
|
|
|
! $this->app->runningInConsole() || $this->publishesMigrations('cortex/auth', true); |
|
158
|
|
|
|
|
159
|
|
|
// Register event handlers |
|
160
|
|
|
$this->app['events']->subscribe(GenericHandler::class); |
|
161
|
|
|
|
|
162
|
|
|
// Register attributes entities |
|
163
|
|
|
! app()->bound('rinvex.attributes.entities') || app('rinvex.attributes.entities')->push('admin'); |
|
164
|
|
|
! app()->bound('rinvex.attributes.entities') || app('rinvex.attributes.entities')->push('member'); |
|
165
|
|
|
! app()->bound('rinvex.attributes.entities') || app('rinvex.attributes.entities')->push('manager'); |
|
166
|
|
|
|
|
167
|
|
|
// Override middlware |
|
168
|
|
|
$this->overrideMiddleware($router); |
|
169
|
|
|
|
|
170
|
|
|
// Register menus |
|
171
|
|
|
$this->registerMenus(); |
|
172
|
|
|
|
|
173
|
|
|
// Share current user instance with all views |
|
174
|
|
|
$this->app['view']->composer('*', function ($view) { |
|
175
|
|
|
! config('rinvex.tenants.active') || $view->with('currentTenant', config('rinvex.tenants.active')); |
|
176
|
|
|
$view->with('currentUser', auth()->guard(request()->route('guard'))->user()); |
|
|
|
|
|
|
177
|
|
|
}); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Register console commands. |
|
182
|
|
|
* |
|
183
|
|
|
* @return void |
|
184
|
|
|
*/ |
|
185
|
|
|
protected function attachRequestMacro(): void |
|
186
|
|
|
{ |
|
187
|
|
|
Request::macro('attemptUser', function (string $guard = null) { |
|
188
|
|
|
$twofactor = $this->session()->get('cortex.auth.twofactor'); |
|
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
return auth()->guard($guard)->getProvider()->retrieveById($twofactor['user_id']); |
|
|
|
|
|
|
191
|
|
|
}); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Register menus. |
|
196
|
|
|
* |
|
197
|
|
|
* @return void |
|
198
|
|
|
*/ |
|
199
|
|
|
protected function registerMenus(): void |
|
200
|
|
|
{ |
|
201
|
|
|
$this->app['rinvex.menus.presenters']->put('account.sidebar', \Cortex\Auth\Presenters\AccountSidebarMenuPresenter::class); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Override middleware. |
|
206
|
|
|
* |
|
207
|
|
|
* @param \Illuminate\Routing\Router $router |
|
208
|
|
|
* |
|
209
|
|
|
* @return void |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function overrideMiddleware(Router $router): void |
|
212
|
|
|
{ |
|
213
|
|
|
// Append middleware to the 'web' middlware group |
|
214
|
|
|
$router->pushMiddlewareToGroup('web', AuthenticateSession::class); |
|
215
|
|
|
$router->pushMiddlewareToGroup('web', UpdateLastActivity::class); |
|
216
|
|
|
|
|
217
|
|
|
// Override route middleware on the fly |
|
218
|
|
|
$router->aliasMiddleware('reauthenticate', Reauthenticate::class); |
|
219
|
|
|
$router->aliasMiddleware('guest', RedirectIfAuthenticated::class); |
|
220
|
|
|
} |
|
221
|
|
|
} |
|
222
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: