Skip to content

Commit 5e535b8

Browse files
author
dereuromark
committed
Require current password as soon as new password is entered.
1 parent de77ab4 commit 5e535b8

File tree

2 files changed

+71
-6
lines changed

2 files changed

+71
-6
lines changed

src/Model/Behavior/PasswordableBehavior.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,13 @@ public function initialize(array $config) {
179179

180180
if ($this->_config['current'] && !count($validator->field($formFieldCurrent))) {
181181
$validator->add($formFieldCurrent, $rules['formFieldCurrent']);
182-
$validator->allowEmpty($formFieldCurrent, !$this->_config['require']);
182+
$require = $this->_config['require'];
183+
$validator->allowEmpty($formFieldCurrent, function ($context) use ($require, $formField) {
184+
if (!$require && !empty($context['data'][$formField])) {
185+
return false;
186+
}
187+
return !$require;
188+
});
183189

184190
if (!$this->_config['allowSame']) {
185191
$validator->add($formField, 'validateNotSame', [

tests/TestCase/Model/Behavior/PasswordableBehaviorTest.php

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public function testValidate() {
6767
$this->Users->patchEntity($user, $data);
6868
$is = $this->Users->save($user);
6969
$this->assertFalse($is);
70-
$this->assertEquals(['pwd_repeat'], array_keys($user->errors()));
70+
$this->assertEquals(['pwd_repeat'], array_keys((array)$user->errors()));
7171

7272
$user = $this->Users->newEntity();
7373
$data = [
@@ -121,7 +121,7 @@ public function testValidateRequired() {
121121
$this->Users->patchEntity($user, $data);
122122
$is = $this->Users->save($user);
123123
$this->assertFalse($is);
124-
$this->assertEquals(['pwd', 'pwd_repeat'], array_keys($user->errors()));
124+
$this->assertEquals(['pwd', 'pwd_repeat'], array_keys((array)$user->errors()));
125125
}
126126

127127
/**
@@ -171,9 +171,8 @@ public function testValidateEmptyWithCurrentPassword() {
171171
];
172172
$this->Users->patchEntity($user, $data);
173173
$is = $this->Users->save($user);
174-
//debug($user->errors());
175174
$this->assertFalse($is);
176-
$this->assertEquals(['pwd', 'pwd_repeat', 'pwd_current'], array_keys($user->errors()));
175+
$this->assertEquals(['pwd', 'pwd_repeat', 'pwd_current'], array_keys((array)$user->errors()));
177176

178177
$this->tearDown();
179178
$this->setUp();
@@ -435,7 +434,67 @@ public function testValidateCurrent() {
435434

436435
// Validation errors triggered - as expected
437436
$this->assertFalse($is);
438-
$this->assertSame(['pwd', 'pwd_repeat', 'pwd_current'], array_keys($user->errors()));
437+
$this->assertSame(['pwd', 'pwd_repeat', 'pwd_current'], array_keys((array)$user->errors()));
438+
}
439+
440+
/**
441+
* Needs faking of pwd check...
442+
*
443+
* @return void
444+
*/
445+
public function testValidateCurrentOptional() {
446+
$this->assertFalse($this->Users->behaviors()->has('Passwordable'));
447+
$user = $this->Users->newEntity();
448+
$data = [
449+
'name' => 'xyz',
450+
'password' => $this->hasher->hash('somepwd')];
451+
$this->Users->patchEntity($user, $data);
452+
$result = $this->Users->save($user);
453+
$this->assertTrue(!empty($result));
454+
$userCopy = clone($user);
455+
$uid = $user->id;
456+
457+
$this->Users->removeBehavior('Passwordable');
458+
$this->Users->addBehavior('Tools.Passwordable', ['current' => true, 'require' => false]);
459+
$user = clone($userCopy);
460+
$data = [
461+
'name' => 'Yeah',
462+
'current' => '',
463+
'pwd' => '',
464+
'pwd_repeat' => '',
465+
];
466+
$this->Users->patchEntity($user, $data);
467+
$this->assertTrue($this->Users->behaviors()->has('Passwordable'));
468+
$is = $this->Users->save($user);
469+
$this->assertTrue((bool)$is);
470+
471+
$user = clone($userCopy);
472+
$data = [
473+
'name' => 'Yeah',
474+
'pwd_current' => '',
475+
'pwd' => '123456',
476+
'pwd_repeat' => '123456'
477+
];
478+
$this->Users->patchEntity($user, $data);
479+
$is = $this->Users->save($user);
480+
$this->assertFalse($is);
481+
482+
$user = clone($userCopy);
483+
$data = [
484+
'name' => 'Yeah',
485+
'pwd_current' => 'somepwd',
486+
'pwd' => '123456',
487+
'pwd_repeat' => '123456'
488+
];
489+
$user->accessible('*', false); // Mark all properties as protected
490+
$user->accessible(['id'], true); // Allow id to be accessible by default
491+
$user = $this->Users->patchEntity($user, $data, ['fields' => ['id']]);
492+
493+
$this->assertNotSame($is['password'], $user['password']);
494+
$this->assertTrue($user->dirty('pwd'));
495+
496+
$is = $this->Users->save($user);
497+
$this->assertTrue((bool)$is);
439498
}
440499

441500
/**

0 commit comments

Comments
 (0)