]> git.andy128k.dev Git - ipf.git/commitdiff
refactor auth forns
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Aug 2013 13:16:34 +0000 (16:16 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 17 Aug 2013 13:16:34 +0000 (16:16 +0300)
ipf/admin/templates/admin/login.html
ipf/admin/views.php
ipf/auth/forms/changepassword.php
ipf/auth/forms/changeselfpassword.php [deleted file]
ipf/auth/forms/login.php
ipf/auth/models/User.php
ipf/form.php

index 9af60b82ac0f6b1ecfc3d57d83eb1904afdd2a18..18b5c94e4382e7a47672557c77b6366142ce0383 100644 (file)
@@ -13,7 +13,6 @@
 <div id="content" class="colM">
   <div id="content-main">
     <form method="post">
-      {if $form.message}<ul class="errorlist"><li>{$form.message}</li></ul>{/if}
       {$form_html}
       <div class="submit-row">
         <input type="submit" value="Sign In" class="default" />
index 0e8264fec992462fe7dfa87d7a369d112f2f5fa0..8c985e87d52e47b861d1000d5259b7b6cab0bffc 100644 (file)
@@ -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,
index 16ceb5f95da745ff559728005b8a396adfe090d1..5162647f8c59a224fa32736a9138826fb89c9fc4 100644 (file)
@@ -1,22 +1,28 @@
 <?php
 
-class IPF_Auth_Forms_ChangePassword extends IPF_Form{
-
+class IPF_Auth_Forms_ChangePassword extends IPF_Form
+{
     function initFields($extra=array())
     {
-        $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.'));
+        $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 (file)
index af376ac..0000000
+++ /dev/null
@@ -1,29 +0,0 @@
-<?php
-
-class IPF_Auth_Forms_ChangeSelfPassword extends IPF_Form{
-
-    function initFields($extra=array())
-    {
-        $this->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;
-    }
-}
index 5813712e024658c87f1628933c99cb1b0a8ca95b..405e5d0aa4cf888b330d3aa9b2ea29db4a4f0b5c 100644 (file)
@@ -1,10 +1,26 @@
-<?php 
+<?php
 
-class IPF_Auth_Forms_Login extends IPF_Form{
-    var $message = null;
-    function initFields($extra=array()){
+class IPF_Auth_Forms_Login extends IPF_Form
+{
+    public $message = null;
+    public $user = null;
+
+    protected function initFields($extra=array())
+    {
         $this->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;
+    }
 }
+
index 99b62e7bbfb243d37035f56295261e30f0c0b628..6604a4a17e71c2105ac31884ac93f0c9c40981f8 100644 (file)
@@ -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;
     }
 }
 
index 7c884bb1e9e1c64ace7dd078502e3783e35bd45e..6b01cd8f489462b4b3e59ec6d1b1083bc216420a 100644 (file)
@@ -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()