From f47a809168897cb5dd48161598f113b76abcacb0 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 25 Jan 2014 10:33:53 +0200 Subject: [PATCH] fix auth models --- ipf/auth/models/Role.php | 6 ++---- ipf/auth/models/User.php | 14 ++++++-------- ipf/form/model.php | 27 +++++++++++++++++++++++---- 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/ipf/auth/models/Role.php b/ipf/auth/models/Role.php index f42ef0a..b79bd59 100644 --- a/ipf/auth/models/Role.php +++ b/ipf/auth/models/Role.php @@ -13,14 +13,14 @@ class IPFAdminRoleForm extends IPF_Form_Model public function add__Permissions__field() { if (!IPF_Auth_App::ArePermissionsEnabled()) - return; + return null; $choices = array(); foreach (IPF_ORM::getTable('Permission')->findAll() as $o) $choices[$o->__toString()] = $o->id; ksort($choices); - $field = new IPF_Form_Field_ModelMultipleChoice(array( + return new IPF_Form_Field_ModelMultipleChoice(array( 'required' => false, 'label' => 'Permissions', 'help_text' => '', @@ -31,8 +31,6 @@ class IPFAdminRoleForm extends IPF_Form_Model 'choices' => $choices, 'widget_attrs' => array('class' => 'checkgroup'), )); - - $this->fields['Permissions'] = $field; } } diff --git a/ipf/auth/models/User.php b/ipf/auth/models/User.php index 18237ff..5e5ba93 100644 --- a/ipf/auth/models/User.php +++ b/ipf/auth/models/User.php @@ -4,6 +4,8 @@ class IPFAuthAdminUserForm extends IPF_Form_Model { function initFields($extra=array()) { + ArrayTools::pushToKey($extra, 'exclude', 'UserRole'); + ArrayTools::pushToKey($extra, 'exclude', 'UserPermission'); parent::initFields($extra); $this->fields['email']->label = 'E-mail'; @@ -78,14 +80,14 @@ class IPFAuthAdminUserForm extends IPF_Form_Model public function add__Permissions__field() { if (!IPF_Auth_App::ArePermissionsEnabled()) - return; + return null; $choices = array(); foreach (IPF_ORM::getTable('Permission')->findAll() as $o) $choices[$o->__toString()] = $o->id; ksort($choices); - $field = new IPF_Form_Field_ModelMultipleChoice(array( + return new IPF_Form_Field_ModelMultipleChoice(array( 'required' => false, 'label' => 'Permissions', 'help_text' => '', @@ -96,20 +98,18 @@ class IPFAuthAdminUserForm extends IPF_Form_Model 'choices' => $choices, 'widget_attrs' => array('class' => 'checkgroup'), )); - - $this->fields['Permissions'] = $field; } public function add__Roles__field() { if (!IPF_Auth_App::ArePermissionsEnabled()) - return; + return null; $choices = array(); foreach (IPF_ORM::getTable('Role')->findAll() as $o) $choices[$o->__toString()] = $o->id; - $field = new IPF_Form_Field_ModelMultipleChoice(array( + return new IPF_Form_Field_ModelMultipleChoice(array( 'required' => false, 'label' => 'Groups', 'help_text' => 'In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in.', @@ -120,8 +120,6 @@ class IPFAuthAdminUserForm extends IPF_Form_Model 'choices' => $choices, 'widget_attrs' => array('class' => 'checkgroup'), )); - - $this->fields['Roles'] = $field; } function isValid() diff --git a/ipf/form/model.php b/ipf/form/model.php index 9ed750a..817a243 100644 --- a/ipf/form/model.php +++ b/ipf/form/model.php @@ -36,7 +36,15 @@ class IPF_Form_Model extends IPF_Form continue; if (isset($col['exclude']) && $col['exclude']) continue; - $field = self::createDBField($name, $table, $col); + + if ($form && method_exists($form, 'add__'.$name.'__field')) { + $f = call_user_func(array($form, 'add__'.$name.'__field')); + if ($f) + $field = array($name, $f); + } else { + $field = self::createDBField($name, $table, $col); + } + if ($field) $result[] = $field; } @@ -46,7 +54,15 @@ class IPF_Form_Model extends IPF_Form continue; if (isset($relation['exclude']) && $relation['exclude']) continue; - $field = self::createDBRelation($name, $relation, $db_columns); + + if ($form && method_exists($form, 'add__'.$name.'__field')) { + $f = call_user_func(array($form, 'add__'.$name.'__field')); + if ($f) + $field = array($name, $f); + } else { + $field = self::createDBRelation($name, $relation, $db_columns); + } + if ($field) $result[] = $field; } @@ -54,7 +70,9 @@ class IPF_Form_Model extends IPF_Form foreach ($fields as $uname) { $field = null; if ($form && method_exists($form, 'add__'.$uname.'__field')) { - $field = array($uname, array($form, 'add__'.$uname.'__field')); + $f = call_user_func(array($form, 'add__'.$uname.'__field')); + if ($f) + $field = array($uname, $f); } elseif (array_key_exists($uname, $db_columns)) { $field = self::createDBField($uname, $table, $db_columns[$uname]); } elseif (array_key_exists($uname, $db_relations)) { @@ -152,6 +170,7 @@ class IPF_Form_Model extends IPF_Form 'label' => isset($col['verbose']) ? $col['verbose'] : IPF_Utils::humanTitle($name), 'help_text' => '', 'table' => $table, + 'model' => $relation->getClass(), ); if ($rt === IPF_ORM_Relation::ONE_AGGREGATE) { @@ -161,7 +180,7 @@ class IPF_Form_Model extends IPF_Form $pk = $table->getIdentifier(); $choices = array(); - foreach ($table->findAll() as $o){ + foreach ($table->findAll() as $o) { $choices[$o->__toString()] = $o->$pk; } -- 2.49.0