From bea8ed1c6dfb16044674787c7e4e0cab6b871c89 Mon Sep 17 00:00:00 2001 From: Wesley Milan Date: Wed, 17 Sep 2014 00:26:38 -0300 Subject: [PATCH 1/6] rule type for username --- src/Confide/UserValidator.php | 33 ++++++++++++++++++++++++--------- src/config/config.php | 12 ++++++++++++ 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/Confide/UserValidator.php b/src/Confide/UserValidator.php index 9f77633..7157390 100644 --- a/src/Confide/UserValidator.php +++ b/src/Confide/UserValidator.php @@ -42,15 +42,29 @@ class UserValidator implements UserValidatorInterface * @var array */ public $rules = [ - 'create' => [ - 'username' => 'required|alpha_dash', - 'email' => 'required|email', - 'password' => 'required|min:4', + 'alpha' => [ + 'create' => [ + 'username' => 'required|alpha_dash', + 'email' => 'required|email', + 'password' => 'required|min:4', + ], + 'update' => [ + 'username' => 'required|alpha_dash', + 'email' => 'required|email', + 'password' => 'required|min:4', + ] ], - 'update' => [ - 'username' => 'required|alpha_dash', - 'email' => 'required|email', - 'password' => 'required|min:4', + 'email' => [ + 'create' => [ + 'username' => 'required|email|same:email', + 'email' => 'required|email', + 'password' => 'required|min:4', + ], + 'update' => [ + 'username' => 'required|email|same:email', + 'email' => 'required|email', + 'password' => 'required|min:4', + ] ] ]; @@ -155,12 +169,13 @@ public function validateIsUnique(ConfideUserInterface $user) */ public function validateAttributes(ConfideUserInterface $user, $ruleset = 'create') { + $type = Config::get('confide::username_type'); $attributes = $user->toArray(); // Force getting password since it may be hidden from array form $attributes['password'] = $user->getAuthPassword(); - $rules = $this->rules[$ruleset]; + $rules = $this->rules[$type][$ruleset]; $validator = App::make('validator') ->make($attributes, $rules); diff --git a/src/config/config.php b/src/config/config.php index 69dd08f..983d9ca 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -2,6 +2,18 @@ return array( + /* + |-------------------------------------------------------------------------- + | Username type + |-------------------------------------------------------------------------- + | + | You can use an alpha numeric username (alpha) or e-mail as username (email) + | + | Default: email + | + */ + 'username_type' => 'email', + /* |-------------------------------------------------------------------------- | Login Throttle From d0307f63e446d8236fbd214468d66cb792f12376 Mon Sep 17 00:00:00 2001 From: Wesley Milan Date: Wed, 17 Sep 2014 00:37:14 -0300 Subject: [PATCH 2/6] rule type for username. Fix Config --- src/Confide/UserValidator.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Confide/UserValidator.php b/src/Confide/UserValidator.php index 7157390..108c26f 100644 --- a/src/Confide/UserValidator.php +++ b/src/Confide/UserValidator.php @@ -2,6 +2,7 @@ use Illuminate\Support\Facades\App as App; use Illuminate\Support\Facades\Lang as Lang; +use Illuminate\Support\Facades\Config as Config; use Illuminate\Support\MessageBag; /** @@ -169,13 +170,12 @@ public function validateIsUnique(ConfideUserInterface $user) */ public function validateAttributes(ConfideUserInterface $user, $ruleset = 'create') { - $type = Config::get('confide::username_type'); $attributes = $user->toArray(); // Force getting password since it may be hidden from array form $attributes['password'] = $user->getAuthPassword(); - $rules = $this->rules[$type][$ruleset]; + $rules = $this->rules[Config::get('confide::username_type')][$ruleset]; $validator = App::make('validator') ->make($attributes, $rules); From 8b19f79fc4435987d75a3562ce985a5264659a51 Mon Sep 17 00:00:00 2001 From: Wesley Milan Date: Wed, 1 Oct 2014 13:27:25 -0300 Subject: [PATCH 3/6] Validation rules into Config --- .gitignore | 3 ++- src/Confide/UserValidator.php | 33 +++++++-------------------------- src/config/config.php | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 27 deletions(-) diff --git a/.gitignore b/.gitignore index dfd6caa..fc7ea17 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor -composer.lock \ No newline at end of file +composer.lock +.DS_Store diff --git a/src/Confide/UserValidator.php b/src/Confide/UserValidator.php index 108c26f..47dbf97 100644 --- a/src/Confide/UserValidator.php +++ b/src/Confide/UserValidator.php @@ -42,32 +42,13 @@ class UserValidator implements UserValidatorInterface * * @var array */ - public $rules = [ - 'alpha' => [ - 'create' => [ - 'username' => 'required|alpha_dash', - 'email' => 'required|email', - 'password' => 'required|min:4', - ], - 'update' => [ - 'username' => 'required|alpha_dash', - 'email' => 'required|email', - 'password' => 'required|min:4', - ] - ], - 'email' => [ - 'create' => [ - 'username' => 'required|email|same:email', - 'email' => 'required|email', - 'password' => 'required|min:4', - ], - 'update' => [ - 'username' => 'required|email|same:email', - 'email' => 'required|email', - 'password' => 'required|min:4', - ] - ] - ]; + public $rules = array(); + + public function __construct() + { + //This provides you configure you validation rules directly in Config file + $this->rules = Config::get('confide::rules'); + } /** * Validates the given user. Should check if all the fields are correctly. diff --git a/src/config/config.php b/src/config/config.php index 983d9ca..32cf6ae 100644 --- a/src/config/config.php +++ b/src/config/config.php @@ -138,4 +138,39 @@ */ 'email_queue' => 'default', + /* + |-------------------------------------------------------------------------- + | Validation Rules + |-------------------------------------------------------------------------- + | + | Modify the lines below to customize your own User Model Validation with + | no needs to extend or overwrite the UserInterface of Confide + | + */ + 'rules' => [ + 'alpha' => [ + 'create' => [ + 'username' => 'required|alpha_dash', + 'email' => 'required|email', + 'password' => 'required|min:4', + ], + 'update' => [ + 'username' => 'required|alpha_dash', + 'email' => 'required|email', + 'password' => 'required|min:4', + ] + ], + 'email' => [ + 'create' => [ + 'username' => 'required|email|same:email', + 'email' => 'required|email', + 'password' => 'required|min:4', + ], + 'update' => [ + 'username' => 'required|email|same:email', + 'email' => 'required|email', + 'password' => 'required|min:4', + ] + ] + ], ); From 0b1a8bddeaa4578cdd92c26e456905a52ce1a625 Mon Sep 17 00:00:00 2001 From: Wesley Milan Date: Thu, 9 Oct 2014 12:04:55 -0300 Subject: [PATCH 4/6] confirmed with date and block multiple times confirm action --- src/Confide/EloquentRepository.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Confide/EloquentRepository.php b/src/Confide/EloquentRepository.php index 1434f02..9fa86b3 100644 --- a/src/Confide/EloquentRepository.php +++ b/src/Confide/EloquentRepository.php @@ -118,7 +118,7 @@ public function getUserByEmailOrUsername($emailOrUsername) * * @param string $code * - * @return bool Success + * @return bool Success or Null for nothing affected */ public function confirmByCode($code) { @@ -126,8 +126,10 @@ public function confirmByCode($code) $user = $this->getUserByIdentity($identity); - if ($user) { + if ($user->confirmed == null) { return $this->confirmUser($user); + } else if ($user) { + return null; } else { return false; } @@ -143,7 +145,10 @@ public function confirmByCode($code) */ protected function confirmUser($user) { - $user->confirmed = true; + $date = new \DateTime(); + $date->setTimezone(new \DateTimeZone('UTC')); + + $user->confirmed = $date->format('Y-m-d H:i:s'); return $user->save(); } From f4b0d31854f7d22d4488a483ee226f2f90358d31 Mon Sep 17 00:00:00 2001 From: Wesley Milan Date: Thu, 16 Oct 2014 23:43:23 -0300 Subject: [PATCH 5/6] without validation --- src/Confide/ConfideUser.php | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/src/Confide/ConfideUser.php b/src/Confide/ConfideUser.php index 19edc05..0b03dbb 100644 --- a/src/Confide/ConfideUser.php +++ b/src/Confide/ConfideUser.php @@ -46,27 +46,6 @@ public function forgotPassword() return ConfideFacade::forgotPassword($this->email); } - /** - * Checks if the current user is valid using the ConfideUserValidator. - * - * @return bool - */ - public function isValid() - { - // Instantiate the Zizaco\Confide\UserValidator and calls the - // validate method. Feel free to use your own validation - // class. - $validator = App::make('confide.user_validator'); - - // If the model already exists in the database we call validate with - // the update ruleset - if ($this->exists) { - return $validator->validate($this, 'update'); - } - - return $validator->validate($this); - } - /** * Overwrites the original save method in order to perform * validation before actually saving the object. @@ -77,11 +56,7 @@ public function isValid() */ public function save(array $options = array()) { - if ($this->isValid()) { - return parent::save($options); - } - - return false; + return parent::save($options); } /** From 1705461b3666bd1b89a699108c65fc9a2f4726f3 Mon Sep 17 00:00:00 2001 From: Wesley Milan Date: Fri, 17 Oct 2014 13:57:49 -0300 Subject: [PATCH 6/6] validating --- src/Confide/ConfideUser.php | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/Confide/ConfideUser.php b/src/Confide/ConfideUser.php index 0b03dbb..19edc05 100644 --- a/src/Confide/ConfideUser.php +++ b/src/Confide/ConfideUser.php @@ -46,6 +46,27 @@ public function forgotPassword() return ConfideFacade::forgotPassword($this->email); } + /** + * Checks if the current user is valid using the ConfideUserValidator. + * + * @return bool + */ + public function isValid() + { + // Instantiate the Zizaco\Confide\UserValidator and calls the + // validate method. Feel free to use your own validation + // class. + $validator = App::make('confide.user_validator'); + + // If the model already exists in the database we call validate with + // the update ruleset + if ($this->exists) { + return $validator->validate($this, 'update'); + } + + return $validator->validate($this); + } + /** * Overwrites the original save method in order to perform * validation before actually saving the object. @@ -56,7 +77,11 @@ public function forgotPassword() */ public function save(array $options = array()) { - return parent::save($options); + if ($this->isValid()) { + return parent::save($options); + } + + return false; } /**