]> BookStack Code Mirror - bookstack/blob - app/Auth/Access/LoginService.php
2bdef34a25388dd2c69dbd228e0e40734fca21be
[bookstack] / app / Auth / Access / LoginService.php
1 <?php
2
3 namespace BookStack\Auth\Access;
4
5 use BookStack\Actions\ActivityType;
6 use BookStack\Auth\User;
7 use BookStack\Facades\Activity;
8 use BookStack\Facades\Theme;
9 use BookStack\Theming\ThemeEvents;
10
11 class LoginService
12 {
13
14     /**
15      * Log the given user into the system.
16      */
17     public function login(User $user, string $method): void
18     {
19         auth()->login($user);
20         Activity::add(ActivityType::AUTH_LOGIN, "{$method}; {$user->logDescriptor()}");
21         Theme::dispatch(ThemeEvents::AUTH_LOGIN, $method, $user);
22
23         // Authenticate on all session guards if a likely admin
24         if ($user->can('users-manage') && $user->can('user-roles-manage')) {
25             $guards = ['standard', 'ldap', 'saml2'];
26             foreach ($guards as $guard) {
27                 auth($guard)->login($user);
28             }
29         }
30     }
31
32
33     /**
34      * Attempt the login of a user using the given credentials.
35      * Meant to mirror laravel's default guard 'attempt' method
36      * but in a manner that always routes through our login system.
37      */
38     public function attempt(array $credentials, string $method, bool $remember = false): bool
39     {
40         $result = auth()->attempt($credentials, $remember);
41         if ($result) {
42             $user = auth()->user();
43             auth()->logout();
44             $this->login($user, $method);
45         }
46
47         return $result;
48     }
49
50 }