Skip to content

Commit

Permalink
Working on forms
Browse files Browse the repository at this point in the history
- Adding CSRF.
- Making them PSR-12 compliant.
  • Loading branch information
rarog committed Mar 26, 2020
1 parent 87e28df commit a6c490e
Show file tree
Hide file tree
Showing 14 changed files with 385 additions and 362 deletions.
105 changes: 47 additions & 58 deletions src/ZfcUser/Form/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,97 +2,86 @@

namespace ZfcUser\Form;

use Laminas\Form\Element;
use Laminas\Form\Element\Button;
use Laminas\Form\Element\Hidden;
use Laminas\Form\Element\Password;
use Laminas\Form\Element\Text;

class Base extends ProvidesEventsForm
{
public function __construct($name = null)
{
parent::__construct($name);

$this->add(array(
$this->add([
'name' => 'username',
'options' => array(
'type' => Text::class,
'options' => [
'label' => 'Username',
),
'attributes' => array(
],
'attributes' => [
'id' => 'username',
'type' => 'text',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'email',
'options' => array(
'type' => Text::class,
'options' => [
'label' => 'Email',
),
'attributes' => array(
],
'attributes' => [
'id' => 'email',
'type' => 'text',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'display_name',
'options' => array(
'type' => Text::class,
'options' => [
'label' => 'Display Name',
),
'attributes' => array(
],
'attributes' => [
'id' => 'display_name',
'type' => 'text',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'password',
'type' => 'password',
'options' => array(
'type' => Password::class,
'options' => [
'label' => 'Password',
),
'attributes' => array(
],
'attributes' => [
'id' => 'password',
'type' => 'password',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'passwordVerify',
'type' => 'password',
'options' => array(
'type' => Password::class,
'options' => [
'label' => 'Password Verify',
),
'attributes' => array(
],
'attributes' => [
'id' => 'passwordVerify',
'type' => 'password',
),
));
],
]);

$submitElement = new Element\Button('submit');
$submitElement = new Button('submit');
$submitElement
->setLabel('Submit')
->setAttributes(array(
->setAttributes([
'type' => 'submit',
));
]);

$this->add($submitElement, array(
$this->add($submitElement, [
'priority' => -100,
));
]);

$this->add(array(
$this->add([
'name' => 'userId',
'type' => 'Laminas\Form\Element\Hidden',
'attributes' => array(
'type' => 'hidden'
),
));

// @TODO: Fix this... getValidator() is a protected method.
//$csrf = new Element\Csrf('csrf');
//$csrf->getValidator()->setTimeout($this->getRegistrationOptions()->getUserFormTimeout());
//$this->add($csrf);
}

public function init()
{
'type' => Hidden::class,
]);
}
}
82 changes: 48 additions & 34 deletions src/ZfcUser/Form/ChangeEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

namespace ZfcUser\Form;

use Laminas\Form\Element\Csrf;
use Laminas\Form\Element\Hidden;
use Laminas\Form\Element\Password;
use Laminas\Form\Element\Submit;
use Laminas\Form\Element\Text;
use ZfcUser\Options\AuthenticationOptionsInterface;

class ChangeEmail extends ProvidesEventsForm
Expand All @@ -17,58 +22,67 @@ public function __construct($name, AuthenticationOptionsInterface $options)

parent::__construct($name);

$this->add(array(
$this->add([
'name' => 'identity',
'options' => array(
'type' => Hidden::class,
'options' => [
'label' => '',
),
'attributes' => array(
],
'attributes' => [
'id' => 'identity',
'type' => 'hidden',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'newIdentity',
'options' => array(
'type' => Text::class,
'options' => [
'label' => 'New Email',
),
'attributes' => array(
],
'attributes' => [
'id' => 'newIdentity',
'type' => 'text',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'newIdentityVerify',
'options' => array(
'type' => Text::class,
'options' => [
'label' => 'Verify New Email',
),
'attributes' => array(
],
'attributes' => [
'id' => 'newIdentityVerify',
'type' => 'text',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'credential',
'type' => 'password',
'options' => array(
'type' => Password::class,
'options' => [
'label' => 'Password',
),
'attributes' => array(
],
'attributes' => [
'id' => 'credential',
'type' => 'password',
),
));
],
]);

$this->add(array(
$this->add([
'name' => 'submit',
'attributes' => array(
'type' => Submit::class,
'attributes' => [
'value' => 'Submit',
'type' => 'submit'
),
));
],
]);

$this->add([
'name' => 'csrf',
'type' => Csrf::class,
'options' => [
'csrf_options' => [
'timeout' => $this->getAuthenticationOptions()->getLoginFormTimeout(),
],
],
]);
}

/**
Expand Down
58 changes: 30 additions & 28 deletions src/ZfcUser/Form/ChangeEmailFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace ZfcUser\Form;

use Laminas\InputFilter\InputFilter;
use Laminas\Validator\EmailAddress;
use Laminas\Validator\Identical;
use ZfcUser\Options\AuthenticationOptionsInterface;

class ChangeEmailFilter extends InputFilter
Expand All @@ -13,43 +15,43 @@ public function __construct(AuthenticationOptionsInterface $options, $emailValid
{
$this->emailValidator = $emailValidator;

$identityParams = array(
'name' => 'identity',
'required' => true,
'validators' => array()
);
$identityParams = [
'name' => 'identity',
'required' => true,
'validators' => [],
];

$identityFields = $options->getAuthIdentityFields();
if ($identityFields == array('email')) {
$validators = array('name' => 'EmailAddress');
if ($identityFields == ['email']) {
$validators = ['name' => EmailAddress::class];
array_push($identityParams['validators'], $validators);
}

$this->add($identityParams);

$this->add(array(
'name' => 'newIdentity',
'required' => true,
'validators' => array(
array(
'name' => 'EmailAddress'
),
$this->emailValidator
),
));
$this->add([
'name' => 'newIdentity',
'required' => true,
'validators' => [
[
'name' => EmailAddress::class,
],
$this->emailValidator,
],
]);

$this->add(array(
'name' => 'newIdentityVerify',
'required' => true,
'validators' => array(
array(
'name' => 'identical',
'options' => array(
$this->add([
'name' => 'newIdentityVerify',
'required' => true,
'validators' => [
[
'name' => Identical::class,
'options' => [
'token' => 'newIdentity'
)
),
),
));
]
],
],
]);
}

public function getEmailValidator()
Expand Down
Loading

0 comments on commit a6c490e

Please sign in to comment.