From 172ca526ceaae07538398af8dc5aa81321339b1e Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sun, 29 Sep 2013 18:23:13 +0300 Subject: [PATCH] user's profile. Just add line 'auth_profile_model' => 'ProfileModelName', to settings.php! --- ipf/auth/models/User.php | 78 ++++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/ipf/auth/models/User.php b/ipf/auth/models/User.php index 6604a4a..975bf6e 100644 --- a/ipf/auth/models/User.php +++ b/ipf/auth/models/User.php @@ -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'); -- 2.49.0