]> BookStack Code Mirror - bookstack/blob - app/App/Providers/AuthServiceProvider.php
Comments: Added HTML filter on load, tinymce elem filtering
[bookstack] / app / App / Providers / AuthServiceProvider.php
1 <?php
2
3 namespace BookStack\App\Providers;
4
5 use BookStack\Access\ExternalBaseUserProvider;
6 use BookStack\Access\Guards\AsyncExternalBaseSessionGuard;
7 use BookStack\Access\Guards\LdapSessionGuard;
8 use BookStack\Access\LdapService;
9 use BookStack\Access\LoginService;
10 use BookStack\Access\RegistrationService;
11 use BookStack\Api\ApiTokenGuard;
12 use BookStack\Users\Models\User;
13 use Illuminate\Support\Facades\Auth;
14 use Illuminate\Support\ServiceProvider;
15 use Illuminate\Validation\Rules\Password;
16
17 class AuthServiceProvider extends ServiceProvider
18 {
19     /**
20      * Bootstrap the application services.
21      *
22      * @return void
23      */
24     public function boot()
25     {
26         // Password Configuration
27         // Changes here must be reflected in ApiDocsGenerate@getValidationAsString.
28         Password::defaults(fn () => Password::min(8));
29
30         // Custom guards
31         Auth::extend('api-token', function ($app, $name, array $config) {
32             return new ApiTokenGuard($app['request'], $app->make(LoginService::class));
33         });
34
35         Auth::extend('ldap-session', function ($app, $name, array $config) {
36             $provider = Auth::createUserProvider($config['provider']);
37
38             return new LdapSessionGuard(
39                 $name,
40                 $provider,
41                 $app['session.store'],
42                 $app[LdapService::class],
43                 $app[RegistrationService::class]
44             );
45         });
46
47         Auth::extend('async-external-session', function ($app, $name, array $config) {
48             $provider = Auth::createUserProvider($config['provider']);
49
50             return new AsyncExternalBaseSessionGuard(
51                 $name,
52                 $provider,
53                 $app['session.store'],
54                 $app[RegistrationService::class]
55             );
56         });
57     }
58
59     /**
60      * Register the application services.
61      *
62      * @return void
63      */
64     public function register()
65     {
66         Auth::provider('external-users', function ($app, array $config) {
67             return new ExternalBaseUserProvider($config['model']);
68         });
69
70         // Bind and provide the default system user as a singleton to the app instance when needed.
71         // This effectively "caches" fetching the user at an app-instance level.
72         $this->app->singleton('users.default', function () {
73             return User::query()->where('system_name', '=', 'public')->first();
74         });
75     }
76 }