From: Andrey Kutejko Date: Sat, 17 Aug 2013 13:16:34 +0000 (+0300) Subject: refactor auth forns X-Git-Tag: 0.5~51 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=73a86edc30432b74327c3719d40ad16f0a6cc738;p=ipf.git refactor auth forns --- diff --git a/ipf/admin/templates/admin/login.html b/ipf/admin/templates/admin/login.html index 9af60b8..18b5c94 100644 --- a/ipf/admin/templates/admin/login.html +++ b/ipf/admin/templates/admin/login.html @@ -13,7 +13,6 @@
- {if $form.message}
  • {$form.message}
{/if} {$form_html}
diff --git a/ipf/admin/views.php b/ipf/admin/views.php index 0e8264f..8c985e8 100644 --- a/ipf/admin/views.php +++ b/ipf/admin/views.php @@ -280,17 +280,13 @@ function IPF_Admin_Views_Login($request, $match) if ($request->method == 'POST') { $form = new IPF_Auth_Forms_Login($request->POST); if ($form->isValid()) { - $users = new User(); - if (false === ($user = $users->checkCreditentials($form->cleaned_data['username'], $form->cleaned_data['password']))) { - $form->message = __('The login or the password is not valid. The login and the password are case sensitive.'); - } else { - IPF_Auth_App::login($request, $user); - return new IPF_HTTP_Response_Redirect($success_url); - } + IPF_Auth_App::login($request, $form->user); + return new IPF_HTTP_Response_Redirect($success_url); } } else { $form = new IPF_Auth_Forms_Login(array('next'=>$success_url)); } + $context = array( 'page_title' => IPF::get('admin_title'), 'form' => $form, diff --git a/ipf/auth/forms/changepassword.php b/ipf/auth/forms/changepassword.php index 16ceb5f..5162647 100644 --- a/ipf/auth/forms/changepassword.php +++ b/ipf/auth/forms/changepassword.php @@ -1,22 +1,28 @@ fields['password1'] = new IPF_Form_Field_Varchar(array('required'=>true,'widget'=>'IPF_Form_Widget_PasswordInput')); - $this->fields['password2'] = new IPF_Form_Field_Varchar(array('required'=>true,'widget'=>'IPF_Form_Widget_PasswordInput','help_text'=>'Enter the same password as above, for verification.')); + $this->fields['password1'] = new IPF_Form_Field_Varchar(array( + 'required' => true, + 'widget' =>'IPF_Form_Widget_PasswordInput', + )); + $this->fields['password2'] = new IPF_Form_Field_Varchar(array( + 'required' => true, + 'widget' => 'IPF_Form_Widget_PasswordInput', + 'help_text' => __('Enter the same password as above, for verification.'), + )); } - function isValid(){ - $ok = parent::isValid(); - if ($ok===true){ - if ($this->cleaned_data['password1']!=$this->cleaned_data['password2']){ - $this->is_valid = false; - $this->errors['password2'][] = "The two password fields didn't match."; - $ok = false; - } - } - return $ok; + public function clean() + { + $data = parent::clean(); + + if ($data['password1'] != $data['password2']) + $this->errors['password2'][] = __('The two password fields didn\'t match.'); + + return $data; } } + diff --git a/ipf/auth/forms/changeselfpassword.php b/ipf/auth/forms/changeselfpassword.php deleted file mode 100644 index af376ac..0000000 --- a/ipf/auth/forms/changeselfpassword.php +++ /dev/null @@ -1,29 +0,0 @@ -fields['oldpassword'] = new IPF_Form_Field_Varchar(array('label'=>'Current Password', 'required'=>true, 'widget'=>'IPF_Form_Widget_PasswordInput')); - $this->fields['password1'] = new IPF_Form_Field_Varchar(array('label'=>'New Password', 'required'=>true,'widget'=>'IPF_Form_Widget_PasswordInput')); - $this->fields['password2'] = new IPF_Form_Field_Varchar(array('label'=>'New Password (repeat)','required'=>true,'widget'=>'IPF_Form_Widget_PasswordInput','help_text'=>'Enter the same password as above, for verification.')); - } - - function isValid($request){ - $ok = parent::isValid(); - if ($ok===true){ - if ($this->cleaned_data['password1']!=$this->cleaned_data['password2']){ - $this->is_valid = false; - $this->errors['password2'][] = "The two password fields didn't match."; - $ok = false; - } - $u = new User(); - if ($u->checkCreditentials($request->user->username, $this->cleaned_data['oldpassword'])===false){ - $this->is_valid = false; - $this->errors['oldpassword'][] = "Incorrect old password"; - $ok = false; - } - } - return $ok; - } -} diff --git a/ipf/auth/forms/login.php b/ipf/auth/forms/login.php index 5813712..405e5d0 100644 --- a/ipf/auth/forms/login.php +++ b/ipf/auth/forms/login.php @@ -1,10 +1,26 @@ -fields['username'] = new IPF_Form_Field_Varchar(array('required'=>true)); $this->fields['password'] = new IPF_Form_Field_Varchar(array('required'=>true,'widget'=>'IPF_Form_Widget_PasswordInput')); $this->fields['next'] = new IPF_Form_Field_Varchar(array('required'=>false,'widget'=>'IPF_Form_Widget_HiddenInput')); } + + public function clean() + { + $data = parent::clean(); + + $this->user = User::checkCreditentials($data['username'], $data['password']); + if (!$this->user) + throw new IPF_Exception_Form(__('The login or the password is not valid. The login and the password are case sensitive.')); + + return $data; + } } + diff --git a/ipf/auth/models/User.php b/ipf/auth/models/User.php index 99b62e7..6604a4a 100644 --- a/ipf/auth/models/User.php +++ b/ipf/auth/models/User.php @@ -273,16 +273,13 @@ class User extends BaseUser return 0 === (int)$this->id; } - function checkCreditentials($username, $password) + public static function checkCreditentials($username, $password) { - $user = $this->getTable()->findOneByUsername($username); - if ($user === false) { - return false; - } - if ($user->is_active and $user->checkPassword($password)) { + $user = self::table()->findOneByUsername($username); + if ($user && $user->is_active && $user->checkPassword($password)) return $user; - } - return false; + else + return false; } } diff --git a/ipf/form.php b/ipf/form.php index 7c884bb..6b01cd8 100644 --- a/ipf/form.php +++ b/ipf/form.php @@ -50,9 +50,9 @@ abstract class IPF_Form implements Iterator function isValid() { - if ($this->is_valid !== null) { + if ($this->is_valid !== null) return $this->is_valid; - } + $this->cleaned_data = array(); $this->errors = array(); $form_methods = get_class_methods($this); @@ -77,20 +77,25 @@ abstract class IPF_Form implements Iterator } } } - try { - $this->cleaned_data = $this->clean(); - } catch (IPF_Exception_Form $e) { - if (!isset($this->errors['__all__'])) $this->errors['__all__'] = array(); - $this->errors['__all__'][] = $e->getMessage(); + + if (empty($this->errors)) { + try { + $this->cleaned_data = $this->clean(); + } catch (IPF_Exception_Form $e) { + if (!isset($this->errors['__all__'])) $this->errors['__all__'] = array(); + $this->errors['__all__'][] = $e->getMessage(); + } } + if (empty($this->errors)) { $this->is_valid = true; return true; + } else { + // as some errors, we do not have cleaned data available. + $this->cleaned_data = array(); + $this->is_valid = false; + return false; } - // as some errors, we do not have cleaned data available. - $this->cleaned_data = array(); - $this->is_valid = false; - return false; } public function clean()