Skip to content

Commit bf07f60

Browse files
authored
Merge pull request #34 from utopia-php/feat-allow-certain-domains
Allow certain domains to pass public domain validation check
2 parents 8151984 + a8f3f77 commit bf07f60

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/Domains/Validator/PublicDomain.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
*/
1313
class PublicDomain extends Validator
1414
{
15+
/**
16+
* @var array
17+
*/
18+
protected static $allowedDomains = [];
1519
/**
1620
* Get Description
1721
*
@@ -27,7 +31,7 @@ public function getDescription(): string
2731
/**
2832
* Is valid
2933
*
30-
* Validation will pass when $value is a public domain
34+
* Validation will pass when $value is either a known domain or in the list of allowed domains
3135
*
3236
* @param mixed $value
3337
* @return bool
@@ -40,11 +44,8 @@ public function isValid($value): bool
4044
}
4145

4246
$domain = new Domain($value);
43-
if (!$domain->isKnown()) {
44-
return false;
45-
}
4647

47-
return true;
48+
return $domain->isKnown() || in_array($domain->get(), self::$allowedDomains);
4849
}
4950

5051
/**
@@ -70,4 +71,16 @@ public function getType(): string
7071
{
7172
return self::TYPE_STRING;
7273
}
74+
75+
/**
76+
* Allow domains
77+
*
78+
* Add domains to the allowed domains array
79+
*
80+
* @param array $domains
81+
*/
82+
public static function allow(array $domains): void
83+
{
84+
self::$allowedDomains = array_merge(self::$allowedDomains, $domains);
85+
}
7386
}

tests/Validator/PublicDomainTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,20 @@ public function tearDown(): void
2121
public function testIsValid(): void
2222
{
2323
$this->assertEquals('Value must be a public domain', $this->domain->getDescription());
24+
// Known public domains
2425
$this->assertEquals(true, $this->domain->isValid('example.com'));
2526
$this->assertEquals(true, $this->domain->isValid('google.com'));
2627
$this->assertEquals(true, $this->domain->isValid('bbc.co.uk'));
2728
$this->assertEquals(true, $this->domain->isValid('appwrite.io'));
2829
$this->assertEquals(true, $this->domain->isValid('usa.gov'));
2930
$this->assertEquals(true, $this->domain->isValid('stanford.edu'));
31+
32+
// URLs
3033
$this->assertEquals(true, $this->domain->isValid('http://google.com'));
3134
$this->assertEquals(true, $this->domain->isValid('http://www.google.com'));
3235
$this->assertEquals(true, $this->domain->isValid('https://example.com'));
36+
37+
// Private domains
3338
$this->assertEquals(false, $this->domain->isValid('localhost'));
3439
$this->assertEquals(false, $this->domain->isValid('http://localhost'));
3540
$this->assertEquals(false, $this->domain->isValid('sub.demo.localhost'));
@@ -39,4 +44,22 @@ public function testIsValid(): void
3944
$this->assertEquals(false, $this->domain->isValid('wiki.team.local'));
4045
$this->assertEquals(false, $this->domain->isValid('example.test'));
4146
}
47+
48+
public function testAllowDomains(): void
49+
{
50+
// Adding localhost to allowed domains
51+
PublicDomain::allow(['localhost']);
52+
53+
// Now localhost should be valid
54+
$this->assertEquals(true, $this->domain->isValid('localhost'));
55+
$this->assertEquals(true, $this->domain->isValid('http://localhost'));
56+
$this->assertEquals(false, $this->domain->isValid('test.app.internal'));
57+
58+
// Adding more domains to allowed domains
59+
PublicDomain::allow(['test.app.internal', 'home.local']);
60+
61+
// Now these domains should be valid
62+
$this->assertEquals(true, $this->domain->isValid('test.app.internal'));
63+
$this->assertEquals(true, $this->domain->isValid('home.local'));
64+
}
4265
}

0 commit comments

Comments
 (0)