diff --git a/README.md b/README.md index 6bfc709..e70a506 100644 --- a/README.md +++ b/README.md @@ -268,7 +268,7 @@ See [Entrust](https://github.com/Zizaco/entrust) #### Redirecting to previous route after login -When defining your filter you should set the `'loginRedirect'` session variable. For example: +When defining your filter you should use the Redirect::guest('user/login') within your auth filter. For example: // filters.php @@ -276,11 +276,7 @@ When defining your filter you should set the `'loginRedirect'` session variable. { if ( Auth::guest() ) // If the user is not logged in { - // Set the loginRedirect session variable - Session::put( 'loginRedirect', Request::url() ); - - // Redirect back to user login - return Redirect::to( 'user/login' ); + return Redirect::guest('user/login'); } }); @@ -293,9 +289,11 @@ or, if you are using [Entrust](https://github.com/Zizaco/entrust) ;) // filters.php Entrust::routeNeedsRole( 'admin*', 'Admin', function(){ - Session::put( 'loginRedirect', Request::url() ); - return Redirect::to( 'user/login' ); + return Redirect::guest('user/login'); } ); + +Finally, it'll auto redirect if your controller's user/login function uses Redirect:intended('a/default/url/here') after a successful login. +The [generated controller](https://github.com/Zizaco/confide/blob/master/src/views/generators/controller.blade.php) does exactly this. #### Validating a route diff --git a/composer.json b/composer.json index 8f0e203..18658c1 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require": { "php": ">=5.3.0", "illuminate/support": "*", - "laravelbook/ardent": "1.1.x" + "laravelbook/ardent": "2.1.x" }, "require-dev": { "mockery/mockery": "0.7.2", diff --git a/src/Zizaco/Confide/Confide.php b/src/Zizaco/Confide/Confide.php index 04dbe4f..7bc5f3e 100644 --- a/src/Zizaco/Confide/Confide.php +++ b/src/Zizaco/Confide/Confide.php @@ -290,7 +290,6 @@ protected function attemptCacheKey( $credentials ) { return 'confide_flogin_attempt_' .$this->app['request']->server('REMOTE_ADDR') - .$this->app['request']->server('HTTP_X_FORWARDED_FOR') .$credentials['email']; } diff --git a/src/Zizaco/Confide/ConfideEloquentRepository.php b/src/Zizaco/Confide/ConfideEloquentRepository.php index ade0273..15e92c7 100644 --- a/src/Zizaco/Confide/ConfideEloquentRepository.php +++ b/src/Zizaco/Confide/ConfideEloquentRepository.php @@ -273,4 +273,10 @@ public function confirmUser( $user ) return true; } + + public function validate(array $rules = array(), array $customMessages = array()) + { + return $this->model()->validate($rules, $customMessages); + } + } diff --git a/src/Zizaco/Confide/ConfideRepository.php b/src/Zizaco/Confide/ConfideRepository.php index d6fd1e9..87278c1 100644 --- a/src/Zizaco/Confide/ConfideRepository.php +++ b/src/Zizaco/Confide/ConfideRepository.php @@ -99,4 +99,6 @@ public function userExists( $user ); * @return boolean Success */ public function confirmUser( $user ); + + public function validate(array $rules, array $customMessages); } diff --git a/src/Zizaco/Confide/ConfideUser.php b/src/Zizaco/Confide/ConfideUser.php index 1623846..a4208b7 100644 --- a/src/Zizaco/Confide/ConfideUser.php +++ b/src/Zizaco/Confide/ConfideUser.php @@ -181,6 +181,7 @@ public function save( array $rules = array(), array $customMessages = array(), a } else { + static::$app['confide.repository']->validate(); $this->validationErrors->add( 'duplicated', static::$app['translator']->get('confide::confide.alerts.duplicated_credentials') @@ -270,11 +271,12 @@ protected function real_save( array $rules = array(), array $customMessages = ar /* * This will make sure that a non modified password * will not trigger validation error. + * @fixed Pull #110 */ - if( empty($rules) && $this->password == $this->getOriginal('password') ) + if( isset($rules['password']) && $this->password == $this->getOriginal('password') ) { - $rules = static::$rules; - $rules['password'] = 'required'; + unset($rules['password']); + unset($rules['password_confirmation']); } return parent::save( $rules, $customMessages, $options, $beforeSave, $afterSave ); diff --git a/src/views/generators/controller.blade.php b/src/views/generators/controller.blade.php index 2158d49..8af8999 100644 --- a/src/views/generators/controller.blade.php +++ b/src/views/generators/controller.blade.php @@ -103,16 +103,11 @@ public function {{ (! $restful) ? 'do_login' : 'postLogin' }}() // Get the value from the config file instead of changing the controller if ( Confide::logAttempt( $input, Config::get('confide::signup_confirm') ) ) { - // If the session 'loginRedirect' is set, then redirect - // to that route. Otherwise redirect to '/' - $r = Session::get('loginRedirect'); - if (!empty($r)) - { - Session::forget('loginRedirect'); - return Redirect::to($r); - } - - return Redirect::to('/'); // change it to '/admin', '/dashboard' or something + // Redirect the user to the URL they were trying to access before + // caught by the authentication filter IE Redirect::guest('user/login'). + // Otherwise fallback to '/' + // Fix pull #145 + return Redirect::intended('/'); // change it to '/admin', '/dashboard' or something } else { diff --git a/tests/ConfideTest.php b/tests/ConfideTest.php index 67f0692..f208cfb 100644 --- a/tests/ConfideTest.php +++ b/tests/ConfideTest.php @@ -120,6 +120,10 @@ public function testShouldThrottleLogAttempt() { $tries = 15; + $this->confide->app['request'] = m::mock( 'Request' ); + $this->confide->app['request']->shouldReceive('server') + ->andReturn( '12.34.56.78' ); + $confide_user = $this->mockConfideUser(); $confide_user->shouldReceive('get','first') ->andReturn( null ); @@ -132,7 +136,7 @@ public function testShouldThrottleLogAttempt() $this->confide->app['hash']->shouldReceive('check') ->andReturn( false ); - for ($i=0; $i < $tries; $i++) { + for ($i=0; $i < $tries; $i++) { // Simulates cache values $this->useCacheForThrottling($i); @@ -341,7 +345,7 @@ private function useCacheForThrottling( $value ) { $cache = m::mock('Illuminate\Cache\Store'); $cache->shouldReceive('put') - ->with( "confide_flogin_attempt_wrong", $value+1, 2 ) + ->with( "confide_flogin_attempt_12.34.56.78wrong", $value+1, 2 ) ->once(); $cache->shouldReceive('get') ->andReturn( $value ); diff --git a/tests/ConfideUserTest.php b/tests/ConfideUserTest.php index 98f2d3a..193f89c 100644 --- a/tests/ConfideUserTest.php +++ b/tests/ConfideUserTest.php @@ -121,6 +121,10 @@ public function testShouldNotSaveDuplicated() ->andReturn( 1 ) ->once(); + ConfideUser::$app['confide.repository']->shouldReceive('validate') + ->andReturn( true ) + ->once(); + $this->populateUser(); $this->confide_user->confirmation_code = ''; $this->confide_user->confirmed = false;