Skip to content

Commit a33b696

Browse files
committed
Manually adding ZF-Commons#680 + tests.
1 parent a6c490e commit a33b696

File tree

8 files changed

+141
-24
lines changed

8 files changed

+141
-24
lines changed

config/zfcuser.global.php.dist

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* If you have a ./config/autoload/ directory set up for your project, you can
66
* drop this config file in it and change the values as you wish.
77
*/
8-
$settings = array(
8+
$settings = [
99
/**
1010
* Laminas\Db\Adapter\Adapter DI Alias
1111
*
@@ -50,7 +50,7 @@ $settings = array(
5050
* Default value: array containing 'ZfcUser\Authentication\Adapter\Db' with priority 100
5151
* Accepted values: array containing services that implement 'ZfcUser\Authentication\Adapter\ChainableAdapter'
5252
*/
53-
'auth_adapters' => array( 100 => 'ZfcUser\Authentication\Adapter\Db' ),
53+
'auth_adapters' => [100 => 'ZfcUser\Authentication\Adapter\Db'],
5454

5555
/**
5656
* Enable Display Name
@@ -71,7 +71,7 @@ $settings = array(
7171
* Default value: array containing 'email'
7272
* Accepted values: array containing one or more of: email, username
7373
*/
74-
//'auth_identity_fields' => array( 'email' ),
74+
//'auth_identity_fields' => ['email'],
7575

7676
/**
7777
* Login form timeout
@@ -111,21 +111,30 @@ $settings = array(
111111
*/
112112
//'use_registration_form_captcha' => false,
113113

114+
115+
/**
116+
* Login Form Captcha
117+
*
118+
* Determines if a captcha should be utilized on the user login form.
119+
* Default value is false.
120+
*/
121+
//'use_login_form_captcha' => false,
122+
114123
/**
115124
* Form Captcha Options
116125
*
117126
* Currently used for the registration form, but re-usable anywhere. Use
118127
* this to configure which Laminas\Captcha adapter to use, and the options to
119128
* pass to it. The default uses the Figlet captcha.
120129
*/
121-
/*'form_captcha_options' => array(
130+
/*'form_captcha_options' => [
122131
'class' => 'figlet',
123-
'options' => array(
132+
'options' => [
124133
'wordLen' => 5,
125134
'expiration' => 300,
126135
'timeout' => 300,
127-
),
128-
),*/
136+
],
137+
],*/
129138

130139
/**
131140
* Use Redirect Parameter If Present
@@ -210,7 +219,7 @@ $settings = array(
210219
* Include null if you want user's with no state to login as well.
211220
* Allowed value types: null and integer
212221
*/
213-
//'allowed_login_states' => array( null, 1 ),
222+
//'allowed_login_states' => [null, 1],
214223

215224
/**
216225
* User table name
@@ -220,16 +229,16 @@ $settings = array(
220229
/**
221230
* End of ZfcUser configuration
222231
*/
223-
);
232+
];
224233

225234
/**
226235
* You do not need to edit below this line
227236
*/
228-
return array(
237+
return [
229238
'zfcuser' => $settings,
230-
'service_manager' => array(
231-
'aliases' => array(
239+
'service_manager' => [
240+
'aliases' => [
232241
'zfcuser_zend_db_adapter' => (isset($settings['zend_db_adapter'])) ? $settings['zend_db_adapter']: 'Laminas\Db\Adapter\Adapter',
233-
),
234-
),
235-
);
242+
],
243+
],
244+
];

src/ZfcUser/Form/Login.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Laminas\Form\Element\Text;
88
use ZfcUser\Options\AuthenticationOptionsInterface;
99
use Laminas\Form\Element\Csrf;
10+
use Laminas\Form\Element\Captcha;
1011

1112
class Login extends ProvidesEventsForm
1213
{
@@ -61,11 +62,16 @@ public function __construct($name, AuthenticationOptionsInterface $options)
6162
],
6263
]);
6364

64-
// @todo: Fix this
65-
// 1) getValidator() is a protected method
66-
// 2) i don't believe the login form is actually being validated by the login action
67-
// (but keep in mind we don't want to show invalid username vs invalid password or
68-
// anything like that, it should just say "login failed" without any additional info)
65+
if ($this->getAuthenticationOptions()->getUseLoginFormCaptcha()) {
66+
$this->add([
67+
'name' => 'captcha',
68+
'type' => Captcha::class,
69+
'options' => [
70+
'label' => 'Please type the following text',
71+
'captcha' => $this->getAuthenticationOptions()->getFormCaptchaOptions(),
72+
],
73+
]);
74+
}
6975

7076
$submitElement = new Button('submit');
7177
$submitElement

src/ZfcUser/Form/LoginFilter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public function __construct(AuthenticationOptionsInterface $options)
1616
'name' => 'identity',
1717
'required' => true,
1818
'validators' => [],
19+
'filters' => [
20+
['name' => StringTrim::class],
21+
],
1922
];
2023

2124
$identityFields = $options->getAuthIdentityFields();

src/ZfcUser/Options/AuthenticationOptionsInterface.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,34 @@ public function setAuthIdentityFields($authIdentityFields);
3333
* @return array
3434
*/
3535
public function getAuthIdentityFields();
36+
37+
/**
38+
* set use a captcha in registration form
39+
*
40+
* @param bool $useRegistrationFormCaptcha
41+
* @return ModuleOptions
42+
*/
43+
public function setUseLoginFormCaptcha(bool $useRegistrationFormCaptcha): ModuleOptions;
44+
45+
/**
46+
* get use a captcha in registration form
47+
*
48+
* @return bool
49+
*/
50+
public function getUseLoginFormCaptcha(): bool;
51+
52+
/**
53+
* set form CAPTCHA options
54+
*
55+
* @param array $formCaptchaOptions
56+
* @return ModuleOptions
57+
*/
58+
public function setFormCaptchaOptions($formCaptchaOptions);
59+
60+
/**
61+
* get form CAPTCHA options
62+
*
63+
* @return array
64+
*/
65+
public function getFormCaptchaOptions();
3666
}

src/ZfcUser/Options/ModuleOptions.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ class ModuleOptions extends AbstractOptions implements
9898
*/
9999
protected $useRegistrationFormCaptcha = false;
100100

101+
/**
102+
* @var bool
103+
*/
104+
protected $useLoginFormCaptcha = false;
105+
101106
/**
102107
* @var int
103108
*/
@@ -473,6 +478,28 @@ public function getUseRegistrationFormCaptcha()
473478
return $this->useRegistrationFormCaptcha;
474479
}
475480

481+
/**
482+
* set use a captcha in login form
483+
*
484+
* @param bool $useRegistrationFormCaptcha
485+
* @return ModuleOptions
486+
*/
487+
public function setUseLoginFormCaptcha(bool $useLoginFormCaptcha): ModuleOptions
488+
{
489+
$this->useLoginFormCaptcha = $useLoginFormCaptcha;
490+
return $this;
491+
}
492+
493+
/**
494+
* get use a captcha in login form
495+
*
496+
* @return bool
497+
*/
498+
public function getUseLoginFormCaptcha(): bool
499+
{
500+
return $this->useLoginFormCaptcha;
501+
}
502+
476503
/**
477504
* set user entity class name
478505
*

tests/ZfcUserTest/Form/LoginTest.php

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace ZfcUserTest\Form;
44

5+
use Laminas\Captcha\AbstractAdapter;
56
use PHPUnit\Framework\TestCase;
67
use ZfcUser\Form\Login as Form;
78
use ZfcUser\Options\AuthenticationOptionsInterface;
@@ -12,13 +13,23 @@ class LoginTest extends TestCase
1213
* @covers ZfcUser\Form\Login::__construct
1314
* @dataProvider providerTestConstruct
1415
*/
15-
public function testConstruct($authIdentityFields = []): void
16+
public function testConstruct(array $authIdentityFields = [], bool $useCaptcha): void
1617
{
1718
$options = $this->getMockBuilder(AuthenticationOptionsInterface::class)
1819
->getMock();
1920
$options->expects($this->once())
2021
->method('getAuthIdentityFields')
2122
->will($this->returnValue($authIdentityFields));
23+
$options->expects($this->any())
24+
->method('getUseLoginFormCaptcha')
25+
->will($this->returnValue($useCaptcha));
26+
if ($useCaptcha && class_exists(AbstractAdapter::class)) {
27+
$captcha = $this->getMockForAbstractClass(AbstractAdapter::class);
28+
29+
$options->expects($this->once())
30+
->method('getFormCaptchaOptions')
31+
->will($this->returnValue($captcha));
32+
}
2233

2334
$form = new Form(null, $options);
2435

@@ -28,6 +39,12 @@ public function testConstruct($authIdentityFields = []): void
2839
$this->assertArrayHasKey('credential', $elements);
2940
$this->assertArrayHasKey('csrf', $elements);
3041

42+
if ($useCaptcha) {
43+
$this->assertArrayHasKey('captcha', $elements);
44+
} else {
45+
$this->assertArrayNotHasKey('captcha', $elements);
46+
}
47+
3148
$expectedLabel = '';
3249
if (count($authIdentityFields) > 0) {
3350
foreach ($authIdentityFields as $field) {
@@ -59,9 +76,10 @@ public function testSetGetAuthenticationOptions(): void
5976
public function providerTestConstruct(): array
6077
{
6178
return [
62-
[[]],
63-
[['email']],
64-
[['username','email']],
79+
[[], false],
80+
[['email'], false],
81+
[['username','email'], false],
82+
[[], true],
6583
];
6684
}
6785
}

tests/ZfcUserTest/Form/RegisterTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public function testConstruct($useCaptcha = false): void
4545
$this->assertArrayHasKey('password', $elements);
4646
$this->assertArrayHasKey('passwordVerify', $elements);
4747
$this->assertArrayHasKey('csrf', $elements);
48+
49+
if ($useCaptcha) {
50+
$this->assertArrayHasKey('captcha', $elements);
51+
} else {
52+
$this->assertArrayNotHasKey('captcha', $elements);
53+
}
4854
}
4955

5056
public function providerTestConstruct(): array

tests/ZfcUserTest/Options/ModuleOptionsTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,24 @@ public function testGetUseRegistrationFormCaptcha()
318318
$this->assertFalse($this->options->getUseRegistrationFormCaptcha());
319319
}
320320

321+
/**
322+
* @covers ZfcUser\Options\ModuleOptions::getUseLoginFormCaptcha
323+
* @covers ZfcUser\Options\ModuleOptions::setUseLoginFormCaptcha
324+
*/
325+
public function testSetGetUseLoginFormCaptcha()
326+
{
327+
$this->options->setUseLoginFormCaptcha(true);
328+
$this->assertTrue($this->options->getUseLoginFormCaptcha());
329+
}
330+
331+
/**
332+
* @covers ZfcUser\Options\ModuleOptions::getUseLoginFormCaptcha
333+
*/
334+
public function testGetUseLoginFormCaptcha()
335+
{
336+
$this->assertFalse($this->options->getUseLoginFormCaptcha());
337+
}
338+
321339
/**
322340
* @covers ZfcUser\Options\ModuleOptions::getUserEntityClass
323341
* @covers ZfcUser\Options\ModuleOptions::setUserEntityClass

0 commit comments

Comments
 (0)