Skip to content

Commit 8493577

Browse files
authored
Merge pull request #11 from byjg/5.0
Change Return of matchPassword
2 parents 72f7773 + a506bf1 commit 8493577

File tree

4 files changed

+55
-37
lines changed

4 files changed

+55
-37
lines changed

src/Definition/PasswordDefinition.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,49 +55,62 @@ public function getRule($rule): string|bool|int
5555
return $this->rules[$rule];
5656
}
5757

58-
public function matchPassword(string $password): bool
58+
const SUCCESS = 0;
59+
const FAIL_MINIMUM_CHARS = 1;
60+
const FAIL_UPPERCASE = 2;
61+
const FAIL_LOWERCASE = 4;
62+
const FAIL_SYMBOLS = 8;
63+
const FAIL_NUMBERS = 16;
64+
const FAIL_WHITESPACE = 32;
65+
const FAIL_SEQUENTIAL = 64;
66+
const FAIL_REPEATED = 128;
67+
68+
public function matchPassword(string $password): int
5969
{
70+
$result = 0;
71+
6072
// match password against the rules
6173
if (strlen($password) < $this->rules[self::MINIMUM_CHARS]) {
62-
return false;
74+
$result |= PasswordDefinition::FAIL_MINIMUM_CHARS;
6375
}
6476
if ($this->rules[self::REQUIRE_UPPERCASE] > 0) {
6577
if (preg_match_all('/[A-Z]/', $password, $matches) < $this->rules[self::REQUIRE_UPPERCASE]) {
66-
return false;
78+
$result |= PasswordDefinition::FAIL_UPPERCASE;
6779
}
6880
}
6981
if ($this->rules[self::REQUIRE_LOWERCASE] > 0) {
7082
if (preg_match_all('/[a-z]/', $password, $matches) < $this->rules[self::REQUIRE_LOWERCASE]) {
71-
return false;
83+
$result |= PasswordDefinition::FAIL_LOWERCASE;
7284
}
7385
}
7486
if ($this->rules[self::REQUIRE_SYMBOLS] > 0) {
7587
if (preg_match_all('/[!@#$%^&*()\-_=+{};:,<.>]/', $password, $matches) < $this->rules[self::REQUIRE_SYMBOLS]) {
76-
return false;
88+
$result |= PasswordDefinition::FAIL_SYMBOLS;
7789
}
7890
}
7991
if ($this->rules[self::REQUIRE_NUMBERS] > 0) {
8092
if (preg_match_all('/[0-9]/', $password, $matches) < $this->rules[self::REQUIRE_NUMBERS]) {
81-
return false;
93+
$result |= PasswordDefinition::FAIL_NUMBERS;
8294
}
8395
}
8496
if ($this->rules[self::ALLOW_WHITESPACE] == 0) {
8597
if (preg_match_all('/\s/', $password, $matches) > 0) {
86-
return false;
98+
$result |= PasswordDefinition::FAIL_WHITESPACE;
8799
}
88100
}
89101
if ($this->rules[self::ALLOW_SEQUENTIAL] == 0) {
90102
if (preg_match_all('/([aA][bB][cC]|[bB][cC][dD]|[cC][dD][eE]|[dD][eE][fF]|[eE][fF][gG]|[fF][gG][hH]|[gG][hH][iI]|[hH][iI][jJ]|[iI][jJ][kK]|[jJ][kK][lL]|[kK][lL][mM]|[lL][mM][nN]|[mM][nN][oO]|[nN][oO][pP]|[oO][pP][qQ]|[pP][qQ][rR]|[qQ][rR][sS]|[rR][sS][tT]|[sS][tT][uU]|[tT][uU][vV]|[uU][vV][wW]|[vV][wW][xX]|[wW][xX][yY]|[xX][yY][zZ]|012|123|234|345|456|567|678|789|890|987|876|765|654|543|432|321)/', $password, $matches) > 0) {
91-
return false;
103+
$result |= PasswordDefinition::FAIL_SEQUENTIAL;
92104
}
93105

94106
}
95107
if ($this->rules[self::ALLOW_REPEATED] == 0) {
96108
if (preg_match_all('/(..?)\1{2,}/', $password, $matches) > 0) {
97-
return false;
109+
$result |= PasswordDefinition::FAIL_REPEATED;
98110
}
99111
}
100-
return true;
112+
113+
return $result;
101114
}
102115

103116

src/Model/UserModel.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ public function getPassword(): ?string
117117
public function setPassword(?string $password): void
118118
{
119119
// Password len equals to 40 means that the password is already encrypted with sha1
120-
if (!empty($password) && strlen($password) != 40 && !empty($this->passwordDefinition) && !$this->passwordDefinition->matchPassword($password)) {
121-
throw new InvalidArgumentException("Password does not match the password definition");
120+
if (!empty($this->passwordDefinition) && !empty($password) && strlen($password) != 40) {
121+
$match = $this->passwordDefinition->matchPassword($password);
122+
if ($match != PasswordDefinition::SUCCESS) {
123+
throw new InvalidArgumentException("Password does not match the password definition [{$match}]");
124+
}
122125
}
126+
123127
$this->password = $password;
124128
}
125129

src/UsersDBDataset.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,11 @@ public function getProperty(string|HexUuidLiteral|int $userId, string $propertyN
411411
* Return all property's fields from this user
412412
*
413413
* @param UserModel $userRow
414+
* @throws RepositoryReadOnlyException
414415
*/
415416
protected function setPropertiesInUser(UserModel $userRow): void
416417
{
417-
$value = $this->propertiesRepository->getMapper()->getFieldMap(UserDefinition::FIELD_USERID)->getUpdateFunctionValue($userRow->getUserid(), $userRow);
418+
$value = $this->propertiesRepository->getMapper()->getFieldMap(UserDefinition::FIELD_USERID)->getUpdateFunctionValue($userRow->getUserid(), $userRow, $this->propertiesRepository->getDbDriverWrite()->getDbHelper());
418419
$query = Query::getInstance()
419420
->table($this->getUserPropertiesDefinition()->table())
420421
->where("{$this->getUserPropertiesDefinition()->getUserid()} = :id", ['id' => $value]);

tests/PasswordDefinitionTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ public function testMatchPasswordMinimumChars()
6464
PasswordDefinition::ALLOW_SEQUENTIAL => 1, // Allow sequential characters
6565
PasswordDefinition::ALLOW_REPEATED => 1 // Allow repeated characters
6666
]);
67-
$this->assertFalse($passwordDefinition->matchPassword('1234567'));
68-
$this->assertTrue($passwordDefinition->matchPassword('12345678'));
67+
$this->assertEquals(PasswordDefinition::FAIL_MINIMUM_CHARS, $passwordDefinition->matchPassword('1234567'));
68+
$this->assertEquals(PasswordDefinition::SUCCESS, $passwordDefinition->matchPassword('12345678'));
6969
}
7070

7171
public function testMatchPasswordUppercase()
@@ -80,9 +80,9 @@ public function testMatchPasswordUppercase()
8080
PasswordDefinition::ALLOW_SEQUENTIAL => 1, // Allow sequential characters
8181
PasswordDefinition::ALLOW_REPEATED => 1 // Allow repeated characters
8282
]);
83-
$this->assertFalse($passwordDefinition->matchPassword('12345678'));
84-
$this->assertFalse($passwordDefinition->matchPassword('12345678A'));
85-
$this->assertTrue($passwordDefinition->matchPassword('1234567BA'));
83+
$this->assertEquals(PasswordDefinition::FAIL_UPPERCASE, $passwordDefinition->matchPassword('12345678'));
84+
$this->assertEquals(PasswordDefinition::FAIL_UPPERCASE, $passwordDefinition->matchPassword('12345678A'));
85+
$this->assertEquals(PasswordDefinition::SUCCESS, $passwordDefinition->matchPassword('1234567BA'));
8686
}
8787

8888
public function testMatchPasswordLowercase()
@@ -97,9 +97,9 @@ public function testMatchPasswordLowercase()
9797
PasswordDefinition::ALLOW_SEQUENTIAL => 1, // Allow sequential characters
9898
PasswordDefinition::ALLOW_REPEATED => 1 // Allow repeated characters
9999
]);
100-
$this->assertFalse($passwordDefinition->matchPassword('12345678'));
101-
$this->assertFalse($passwordDefinition->matchPassword('12345678a'));
102-
$this->assertTrue($passwordDefinition->matchPassword('1234567ba'));
100+
$this->assertEquals(PasswordDefinition::FAIL_LOWERCASE, $passwordDefinition->matchPassword('12345678'));
101+
$this->assertEquals(PasswordDefinition::FAIL_LOWERCASE, $passwordDefinition->matchPassword('12345678a'));
102+
$this->assertEquals(PasswordDefinition::SUCCESS, $passwordDefinition->matchPassword('1234567ba'));
103103
}
104104

105105
public function testMatchPasswordSymbols()
@@ -114,9 +114,9 @@ public function testMatchPasswordSymbols()
114114
PasswordDefinition::ALLOW_SEQUENTIAL => 1, // Allow sequential characters
115115
PasswordDefinition::ALLOW_REPEATED => 1 // Allow repeated characters
116116
]);
117-
$this->assertFalse($passwordDefinition->matchPassword('12345678'));
118-
$this->assertFalse($passwordDefinition->matchPassword('12345678!'));
119-
$this->assertTrue($passwordDefinition->matchPassword('1234567!!'));
117+
$this->assertEquals(PasswordDefinition::FAIL_SYMBOLS, $passwordDefinition->matchPassword('12345678'));
118+
$this->assertEquals(PasswordDefinition::FAIL_SYMBOLS, $passwordDefinition->matchPassword('12345678!'));
119+
$this->assertEquals(PasswordDefinition::SUCCESS, $passwordDefinition->matchPassword('1234567!!'));
120120
}
121121

122122
public function testMatchPasswordNumbers()
@@ -131,9 +131,9 @@ public function testMatchPasswordNumbers()
131131
PasswordDefinition::ALLOW_SEQUENTIAL => 1, // Allow sequential characters
132132
PasswordDefinition::ALLOW_REPEATED => 1 // Allow repeated characters
133133
]);
134-
$this->assertFalse($passwordDefinition->matchPassword('abcdefgh'));
135-
$this->assertFalse($passwordDefinition->matchPassword('abcdefg1'));
136-
$this->assertTrue($passwordDefinition->matchPassword('abcdef11'));
134+
$this->assertEquals(PasswordDefinition::FAIL_NUMBERS, $passwordDefinition->matchPassword('abcdefgh'));
135+
$this->assertEquals(PasswordDefinition::FAIL_NUMBERS, $passwordDefinition->matchPassword('abcdefg1'));
136+
$this->assertEquals(PasswordDefinition::SUCCESS, $passwordDefinition->matchPassword('abcdef11'));
137137
}
138138

139139
public function testMatchPasswordWhitespace()
@@ -148,7 +148,7 @@ public function testMatchPasswordWhitespace()
148148
PasswordDefinition::ALLOW_SEQUENTIAL => 1, // Allow sequential characters
149149
PasswordDefinition::ALLOW_REPEATED => 1 // Allow repeated characters
150150
]);
151-
$this->assertFalse($passwordDefinition->matchPassword('1234 678'));
151+
$this->assertEquals(PasswordDefinition::FAIL_WHITESPACE, $passwordDefinition->matchPassword('1234 678'));
152152
}
153153

154154
public function testMatchPasswordSequential()
@@ -163,11 +163,11 @@ public function testMatchPasswordSequential()
163163
PasswordDefinition::ALLOW_SEQUENTIAL => 0, // Allow sequential characters
164164
PasswordDefinition::ALLOW_REPEATED => 1 // Allow repeated characters
165165
]);
166-
$this->assertFalse($passwordDefinition->matchPassword('123asdkls')); // 123 is sequential
167-
$this->assertFalse($passwordDefinition->matchPassword('sds456sks')); // 456 is sequential
168-
$this->assertFalse($passwordDefinition->matchPassword('aju654sks')); // 654 is sequential
169-
$this->assertFalse($passwordDefinition->matchPassword('791fghkalal')); // fgh is sequential
170-
$this->assertTrue($passwordDefinition->matchPassword('diykdsn132'));
166+
$this->assertEquals(PasswordDefinition::FAIL_SEQUENTIAL, $passwordDefinition->matchPassword('123asdkls')); // 123 is sequential
167+
$this->assertEquals(PasswordDefinition::FAIL_SEQUENTIAL, $passwordDefinition->matchPassword('sds456sks')); // 456 is sequential
168+
$this->assertEquals(PasswordDefinition::FAIL_SEQUENTIAL, $passwordDefinition->matchPassword('aju654sks')); // 654 is sequential
169+
$this->assertEquals(PasswordDefinition::FAIL_SEQUENTIAL, $passwordDefinition->matchPassword('791fghkalal')); // fgh is sequential
170+
$this->assertEquals(PasswordDefinition::SUCCESS, $passwordDefinition->matchPassword('diykdsn132'));
171171
}
172172

173173
public function testMatchCharsRepeated()
@@ -183,9 +183,9 @@ public function testMatchCharsRepeated()
183183
PasswordDefinition::ALLOW_REPEATED => 0 // Allow repeated characters
184184
]);
185185

186-
$this->assertFalse($passwordDefinition->matchPassword('hay111oihsc')); // 111 is repeated
187-
$this->assertFalse($passwordDefinition->matchPassword('haycccoihsc')); // ccc is repeated
188-
$this->assertFalse($passwordDefinition->matchPassword('oilalalapo')); // lalala is repeated
189-
$this->assertTrue($passwordDefinition->matchPassword('hay1d11oihsc'));
186+
$this->assertEquals(PasswordDefinition::FAIL_REPEATED, $passwordDefinition->matchPassword('hay111oihsc')); // 111 is repeated
187+
$this->assertEquals(PasswordDefinition::FAIL_REPEATED, $passwordDefinition->matchPassword('haycccoihsc')); // ccc is repeated
188+
$this->assertEquals(PasswordDefinition::FAIL_REPEATED, $passwordDefinition->matchPassword('oilalalapo')); // lalala is repeated
189+
$this->assertEquals(PasswordDefinition::SUCCESS, $passwordDefinition->matchPassword('hay1d11oihsc'));
190190
}
191191
}

0 commit comments

Comments
 (0)