0

我有一个注册表单 User\UserForm,其中包含一个字段集 User\UserFieldset。在 User\UserFieldset 中,我放置了一个名为“passwordVerify”的字段,该字段应与字段集中名为“password”的另一个字段相同。这工作正常。

但是,如果管理员想要修改 Admin\UserForm 中的用户帐户,其中也包含 User\UserFieldset,则应删除字段集中 User\UserFieldset 中的字段“passwordVerify”。因此,我在 Admin\UserForm 中调用以下内容:

$this->get('user')->remove('passwordVerify');
$this->getInputFilter()->get('user')->remove('passwordVerify')

正如预期的那样,表单现在缺少“passwordVerify”字段。

如果我在编辑某些内容后保存表单,我的自定义过滤器“PasswordFilter”将无法再检索字段集的绑定对象($this->getOption('object');返回一个用户对象) - 但绑定对象的所有属性都为空。如果我在不删除“passwordVerify”-field 和“passwordVerify”-inputfilter 的情况下使用 Admin\UserForm,则一切正常,绑定对象将传递给具有填充属性的“PasswordFilter”(就值而言,由用户在 Admin\用户窗体)。打破一切的线是$this->getInputFilter()->get('user')->remove('passwordVerify')。所以这导致了我的假设,即通过删除输入过滤器,水合对象会以某种方式被清空/清空。以下是我的一些代码摘录,如果需要,我可以提供有关工厂等的更多信息。

管理员\用户窗体:

class UserForm extends Form
{

    /**
     * @var EntityManager
     */
    protected $entityManager = null;

    /**
     * @var Translator
     */
    protected $translator = null;

    public function __construct(EntityManager $entityManager, Translator $translator)
    {
        $this->entityManager = $entityManager;
        $this->translator = $translator;

        parent::__construct("userForm");
        $this->setHydrator(new DoctrineHydrator($entityManager));
    }

    public function init()
    {
        // Adding UserFieldset
        $this->add(array(
            'name' => 'user',
            'type' => \User\Form\UserFieldset::class,
            'options' => array(
                'use_as_base_fieldset' => true,
            ),
        ));

        $this->get('user')->remove('passwordVerify');
        $this->getInputFilter()->get('user')->remove('passwordVerify');

        $this->add(array(
            'type' => 'Zend\Form\Element\Csrf',
            'name' => 'csrf',
        ));

        $this->add(array(
            'type' => 'submit',
            'name' => 'submit',
            'options' => array(
                'label' => $this->translator->translate('Btn.submit.user', 'Form')
            ),
        ));
    }
}

用户\用户字段集:

class UserFieldset extends Fieldset implements InputFilterProviderInterface
{

    /**
     * @var EntityManager
     */
    protected $entityManager = null;

    /**
     * @var Translator
     */
    protected $translator = null;

    public function __construct(EntityManager $entityManager, Translator $translator)
    {
        $this->entityManager = $entityManager;
        $this->translator = $translator;

        parent::__construct("userFieldset");
        $this->setHydrator(new DoctrineHydrator($entityManager))->setObject(new User());
    }

    public function init()
    {
        $this->add(array(
            'type' => 'text',
            'name' => 'firstName',
            'options' => array(
                'label' => $this->translator->translate('label.firstName', 'Form'),
                'label_attributes' => array(
                    'class' => 'col-sm-3',
                ),
                'column-size' => 'sm-5',
            ),
            'attributes' => array(
                'id' => 'firstName',
            ),
        ));
        /* ... */
        $this->add(array(
            'type' => 'text',
            'name' => 'password',
            'options' => array(
                'label' => $this->translator->translate('label.password', 'Form'),
                'label_attributes' => array(
                    'class' => 'col-sm-3',
                ),
                'column-size' => 'sm-5',
            ),
            'attributes' => array(
                'id' => 'password',
            ),
        ));

        $this->add(array(
            'type' => 'password',
            'name' => 'passwordVerify',
            'options' => array(
                'label_attributes' => array(
                    'class' => 'col-sm-3 control-label'
                ),
                'label' => $this->translator->translate('label.verifyPassword', 'Form'),
                'column-size' => 'sm-8',
            ),
            'attributes' => array(
                'class' => 'form-control',
                'id' => 'password'),
        ));

         /* ... */

        // Adding AddressFieldset
        $this->add(array(
            'name' => 'address',
            'type' => \User\Form\AddressFieldset::class,
        ));

         /* ... */

        $this->add(array(
            'type' => 'datetime',
            'name' => 'created',
            'options' => array(
                'label' => $this->translator->translate('label.created', 'Form'),
                'format' => 'd.m.Y H:i',
                'label_attributes' => array(
                    'class' => 'col-sm-3',
                ),
                'column-size' => 'sm-5',
            ),
            'attributes' => array(
                'id' => 'created',
            ),
        ));
    }

    public function getInputFilterSpecification()
    {
        return array(
            'firstName' => array(
                'required' => true,
                'filters' => array(
                    array('name' => 'StringTrim'),
                    array('name' => 'StripTags'),
                ),
                'validators' => array(),
            ),
            /* ... */
            'password' => array(
                'required' => true,
                'filters' => array(
                    array('name' => 'StringTrim'),
                    array('name' => 'StripTags'),
                    [
                        'name' => PasswordFilter::class,
                        'options' => [
                            'object' => $this->getObject(),
                            'field' => 'password'
                        ]
                    ]
                ),
                'validators' => array(),
            ),
            'passwordVerify' => array(
                'required' => true,
                'filters' => [
                    [
                        'name' => PasswordFilter::class,
                        'options' => [
                            'object' => $this->getObject(),
                            'field' => 'password'
                        ]
                    ]
                ],
                'validators' => array(
                    array(
                        'name' => 'StringLength',
                        'options' => array(
                            'min' => 6
                        )
                    ),
                    array(
                        'name' => 'Identical',
                        'options' => array(
                            'token' => 'password'
                        )
                    )
                )
            ),
           /* ... */
            'created' => array(
                'required' => false,
                'filters' => array(
                    array('name' => 'StringTrim'),
                ),
                'validators' => array(
                    array(
                        'name' => 'Date',
                        'options' => array('format' => 'd.m.Y H:i')
                    ),
                ),
            )
        );
    }
}

密码过滤器:

class PasswordFilter extends AbstractFilter
{
    /** @var  EntityManager */
    protected $entityManager;

    /** @var  PasswordInterface */
    protected $passwordManager;

    /**
     * PasswordFilter constructor.
     * @param EntityManager $entityManager
     * @param PasswordInterface $passwordManager
     * @param array $options
     */
    public function __construct(EntityManager $entityManager, PasswordInterface $passwordManager, $options = [])
    {
        $this->entityManager = $entityManager;
        $this->passwordManager = $passwordManager;

        $this->options = $options;
    }

    public function filter($value)
    {
        $object = $this->getOption('object');
        $field = $this->getOption('field');
        $getter = 'get'.ucfirst($field);

        if (!$object || !$field) {
            throw new \Exception('Options "object" and "field" are required.');
        }

        if ($object->getId()) {
            $dbObject = $this->entityManager->getRepository(get_class($object))->find($object->getId());

            if ($value === $dbObject->{$getter}()) {
                return $value;
            }
        }

        // hash password here...
        return $this->passwordManager->create($value);
    }

    private function getOption($option)
    {
        if (array_key_exists($option, $this->options)) {
            return $this->options[$option];
        }
        return false;
    }
}

有什么线索吗?我是否在实例化过程的早期调用“passwordVerify”的删除输入过滤器?我还测试了在我的控制器中删除“$this->form->bind($user)”之后的 inputFilter 和字段,这也可以。如果我在 Admin\UserForm 中删除它,为什么它不起作用,我认为这是管理“passwordVerify”-stuff 的更干净的方式?

4

1 回答 1

0

如果你打电话$this->getInputFilter(),你的InputProviderInterface::getInputSpecification方法UserForm正在被调用。

如果您尚未附加对象,则无法检索它。但我不明白为什么你甚至需要那个。如果该值不适合数据库值,则您正在对密码进行哈希处理,但显然数据库值似乎是纯文本作为输入,或者您为什么要比较它?

恕我直言,无论数据库中的当前值是什么,您都应该散列密码。

如果您使用的是学说并且密码不会更改,则无论如何它都不会执行 UPDATE 查询。

于 2017-07-13T18:08:04.147 回答