| 1 |  |  | <?php namespace Modules\User\Services; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Modules\Core\Contracts\Authentication; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Modules\User\Events\UserHasBegunResetProcess; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Modules\User\Exceptions\InvalidOrExpiredResetCode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Modules\User\Exceptions\UserNotFoundException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Modules\User\Repositories\UserRepository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class UserResetter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var UserRepository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var Authentication | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     private $auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     public function __construct(UserRepository $user, Authentication $auth) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->user = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         $this->auth = $auth; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Start the reset password process for given credentials (email) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param array $credentials | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @throws UserNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     public function startReset(array $credentials) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $user = $this->findUser($credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         $code = $this->auth->createReminderCode($user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         event(new UserHasBegunResetProcess($user, $code)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Finish the reset process | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @throws InvalidOrExpiredResetCode | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @throws UserNotFoundException | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |     public function finishReset(array $data) | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 49 |  |  |         $user = $this->user->find(array_get($data, 'userId')); | 
            
                                                                        
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 51 |  |  |         if ($user === null) { | 
            
                                                                        
                            
            
                                    
            
            
                | 52 |  |  |             throw new UserNotFoundException(); | 
            
                                                                        
                            
            
                                    
            
            
                | 53 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 55 |  |  |         $code = array_get($data, 'code'); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |         $password = array_get($data, 'password'); | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |         if (! $this->auth->completeResetPassword($user, $code, $password)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |             throw new InvalidOrExpiredResetCode(); | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |         return $user; | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param array $credentials | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @throws UserNotFoundException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     private function findUser(array $credentials) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         $user = $this->user->findByCredentials((array) $credentials); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         if ($user === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |             throw new UserNotFoundException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         return $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 78 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 79 |  |  |  |