| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Http\Requests\enso\Core; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Http\Request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Illuminate\Support\Str; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Illuminate\Validation\Validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use App\Models\User; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class PasswordValidator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     private Request $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     private Validator $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     private User $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     public function __construct(Request $request, Validator $validator, User $user) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         $this->request = $request; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->validator = $validator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->user = $user; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     public function handle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         if ($this->user->currentPasswordIs($this->request->get('password'))) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |             $this->validator->errors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |                 ->add('password', __('You cannot use the existing password')); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |         if (! $this->hasMinUppercase()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |             $this->validator->errors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |                 ->add('password', __('Minimum upper case letters count is :number', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |                     'number' => config('enso.auth.password.minUpperCase'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |                 ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         if (! $this->hasMinNumeric()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |             $this->validator->errors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |                 ->add('password', __('Minimum numeric characters count is :number', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |                     'number' => config('enso.auth.password.minNumeric'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |                 ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         if (! $this->hasMinSpecial()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $this->validator->errors() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |                 ->add('password', __('Minimum special characters count is :number', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                     'number' => config('enso.auth.password.minSpecial'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 ])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     private function hasMinUppercase() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         if (! config('enso.auth.password.minUpperCase')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         preg_match_all('/[A-Z]+/', $this->request->get('password'), $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->length($matches) >= config('enso.auth.password.minUpperCase'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     private function hasMinNumeric() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         if (! config('enso.auth.password.minNumeric')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         preg_match_all('/[0-9]+/', $this->request->get('password'), $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         return $this->length($matches) >= config('enso.auth.password.minNumeric'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     private function hasMinSpecial() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         if (! config('enso.auth.password.minSpecial')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         preg_match_all('/[^\da-zA-Z0-9]+/', $this->request->get('password'), $matches); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $this->length($matches) >= config('enso.auth.password.minSpecial'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |     private function length($matches) | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |         return Str::length((new Collection($matches))->flatten()->implode('')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 90 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |  |