| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | namespace App\Auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use App\User\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use App\User\UserRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Throwable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Yiisoft\Auth\IdentityInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Yiisoft\User\CurrentUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | final class AuthService | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private CurrentUser $currentUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     private UserRepository $userRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     private IdentityRepository $identityRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 | 10 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     public function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         CurrentUser $currentUser, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         UserRepository $userRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         IdentityRepository $identityRepository, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 | 10 |  |     ) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 10 |  |         $this->currentUser = $currentUser; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 10 |  |         $this->userRepository = $userRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 | 10 |  |         $this->identityRepository = $identityRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public function login(string $login, string $password): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 3 |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         $user = $this->userRepository->findByLoginWithAuthIdentity($login); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 3 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 2 |  |         if ($user === null || !$user->validatePassword($password)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |             return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 1 |  |         return $this->currentUser->login($user->getIdentity()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @throws Throwable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function logout(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |         $identity = $this->currentUser->getIdentity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 1 |  |         if ($identity instanceof Identity) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |             $identity->regenerateCookieLoginKey(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |             $this->identityRepository->save($identity); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         return $this->currentUser->logout(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @throws Throwable | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |     public function signup(string $login, string $password): bool | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 60 | 2 |  |         $user = $this->userRepository->findByLogin($login); | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 62 | 2 |  |         if ($user !== null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |             return false; | 
            
                                                                        
                            
            
                                    
            
            
                | 64 | 2 |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 65 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |         $user = new User($login, $password); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |         $this->userRepository->save($user); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 | 1 |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 69 | 1 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 1 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     public function getIdentity(): IdentityInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 10 |  |         return $this->currentUser->getIdentity(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 10 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     public function isGuest(): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this->currentUser->isGuest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 82 |  |  |  |