B
B
boozzd2014-10-30 11:46:33
Zend Framework
boozzd, 2014-10-30 11:46:33

How to access zfcuser zf2 forms?

Hello, I am working on a project on zf2 + doctrine. For authorization I use the zfcuser module, for separation of access rights (ACL) bjyauthorize.
I create a custom form to change the password:

<?php
namespace Application\Form;

use Zend\Form\Form;

class ClientpassForm extends Form{

    public function __construct($em){

        parent::__construct('security');

        $this->add(array(
                'name' => 'password-old',
                'attributes' => array(
                    'type' => 'password',
                    'placeholder' => 'пароль',
                    'id' => 'key',
                    'class' => ''
                ),
                'options' => array(
                    'label' => 'Введите ваш старый пароль',
                    'label_attributes' => array(
                        'class' => ''
                    ),
                ),
            ));

        $this->add(array(
                'name' => 'password',
                'attributes' => array(
                    'type' => 'password',
                    'placeholder' => 'новый пароль',
                    'id' => 'key',
                    'class' => ''
                ),
                'options' => array(
                    'label' => 'Введите новый пароль',
                    'label_attribute' => array(
                        'class' => ''
                    ),
                ),
            ));
        $this->add(array(
                'name' => 'password-confirm',
                'attributes' => array(
                    'type' => 'password',
                    'placeholder' => 'новый пароль',
                    'id' => 'key',
                    'class' => ''
                ),
                'options' => array(
                    'label' => 'Повторите новый пароль',
                    'label_attributes' => array(
                        'class' => ''
                    ),
                ),
            ));
    }
}

and a filter to the form:
<?php
namespace Application\Form;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;

class ClientpassFilter extends InputFilter{

    public function __construct($sm){

        $this->add(array(
            'name' => 'password',
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => 4,
                        'max' => 100,
                    ),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'password-old',
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => 4,
                        'max' => 100,
                    ),
                ),
            ),
        ));

        $this->add(array(
            'name' => 'password-confirm',
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name' => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min' => 4,
                        'max' => 100,
                    ),
                ),
                array(
                    'name' => 'Identical',
                    'options' => array(
                        'token' => 'password',
                        'messages' => array(
                            \Zend\Validator\Identical::NOT_SAME => 'Введенные пароли не совпадают'
                        ),
                    ),
                ),
            ),
        ));
    }
}

Controller(password change action):
public function securityAction(){
        $this->getServiceLocator()->get('Zend\View\Renderer\PhpRenderer')->headTitle('Изменение данных пользователя');
        $em = $this->getEntityManager();

        $form = new \Application\Form\ClientpassForm($em);
return new ViewModel(array(
                'form' => $form,
            ));
    }

And finally view:
<?=$this->form()->openTag($form);?>
<?=$this->formRow($form->get('password-old'));?>
<br/>
<?=$this->formRow($form->get('password'));?>
<br/>
<?=$this->formRow($form->get('password-confirm'));?>
<br/>
<button type="submit">Сохранить</button>
<?=$this->form()->closeTag();?>

Can't link form to module.
It would be possible to bypass the zfcuser module by simply checking the old form password against the database, but Zend\Crypt\Password\Bcrypt is used to encrypt the password and each time it is encrypted, a correspondingly different output string is used. You cannot change the encryption method, there are already users.
The registration form works with a redirect (auth action):
if ($request->isPost())
            return $this->forward()->dispatch('zfcuser', array('action' => 'authenticate'));

With the password change form, this trick does not work.
Are there any solutions to this problem? Thank you for reading to the end.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
boozzd, 2014-10-31
@boozzd

Found a solution. You can simply redefine the view for the necessary actions in your module. I found an example here: https://github.com/codeforest/zend-framework-2-tut...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question