]> git.andy128k.dev Git - ipf.git/commitdiff
user's profile.
authorAndrey Kutejko <andy128k@gmail.com>
Sun, 29 Sep 2013 15:23:13 +0000 (18:23 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sun, 29 Sep 2013 15:23:13 +0000 (18:23 +0300)
Just add line
'auth_profile_model' => 'ProfileModelName',
to settings.php!

ipf/auth/models/User.php

index 6604a4a17e71c2105ac31884ac93f0c9c40981f8..975bf6ec0cf21931236cb2e0bf2fe3cd84de82fe 100644 (file)
@@ -54,6 +54,25 @@ class IPFAuthAdminUserForm extends IPF_Form_Model
             array('fields' => array('email', 'first_name', 'last_name'), 'label' => 'Personal info'),
             array('fields' => $permissions, 'label' => 'Permissions'),
         );
+
+        /*
+         * User's profile
+         */
+
+        $profileTable = IPF::get('auth_profile_model');
+        if ($profileTable) {
+            $table = IPF_ORM::getTable($profileTable);
+            $profileFields = IPF_Form_Model::suggestFields($table);
+
+            $fieldGroup = array();
+            foreach ($profileFields as $field) {
+                list($n, $f) = $field;
+                $this->fields[$n] = $f;
+                $fieldGroup[] = $n;
+            }
+
+            $this->field_groups[] = array('fields' => $fieldGroup, 'label' => __('Profile'));
+        }
     }
 
     public function add__Permissions__field()
@@ -122,6 +141,19 @@ class IPFAuthAdminUserForm extends IPF_Form_Model
 
         return $ok;
     }
+
+    function save($commit=true)
+    {
+        $user = parent::save($commit);
+        $profileTable = IPF::get('auth_profile_model');
+        if ($profileTable) {
+            $profile = $user->getProfile();
+
+            $profile->SetFromFormData($this->cleaned_data);
+            $profile->save();
+        }
+        return $user;
+    }
 }
 
 class AdminUser extends IPF_Admin_Model
@@ -151,25 +183,6 @@ class AdminUser extends IPF_Admin_Model
         return implode(' / ', $roles);
     }
 
-    public function fields()
-    {
-        $fields = array(
-            'username',
-            'password',
-            'email',
-            'first_name',
-            'last_name',
-            'is_active',
-            'is_staff',
-            'is_superuser',
-        );
-        if (IPF_Auth_App::ArePermissionsEnabled()) {
-            $fields[] = 'Permissions';
-            $fields[] = 'Roles';
-        }
-        return $fields;
-    }
-
     function _searchFields()
     {
         return array(
@@ -196,6 +209,7 @@ class User extends BaseUser
 {
     const UNUSABLE_PASSWORD = '!';
     public $session_key = 'IPF_User_auth';
+    private $profile = null;
 
     public function __toString()
     {
@@ -281,6 +295,32 @@ class User extends BaseUser
         else
             return false;
     }
+
+    public function getData()
+    {
+        $data = parent::getData();
+        $profile = $this->getProfile();
+        if ($profile)
+            $data = $data + $profile->getData();
+        return $data;
+    }
+
+    public function getProfile()
+    {
+        $profileTable = IPF::get('auth_profile_model');
+        if (!$profileTable)
+            return null;
+
+        if (!$this->profile)
+            $this->profile = IPF_ORM::getTable($profileTable)->findOneByUserId($this->id);
+
+        if (!$this->profile) {
+            $this->profile = new $profileTable;
+            $this->profile->user = $this;
+        }
+
+        return $this->profile;
+    }
 }
 
 IPF_Admin_Model::register('User', 'AdminUser');