Skip to content

Commit 2600158

Browse files
committed
Added support for multiple domains
1 parent 15bd185 commit 2600158

File tree

4 files changed

+102
-10
lines changed

4 files changed

+102
-10
lines changed

Plugin.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getPluginAuthor()
4646

4747
public function getPluginVersion()
4848
{
49-
return '1.0.0';
49+
return '1.0.1';
5050
}
5151

5252
public function getPluginHomepage()

Template/config/application.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
<?= $this->form->label(t('Email domain restriction for sign up'), 'registration_email_domain') ?>
2-
<?= $this->form->text('registration_email_domain', $values, $errors, array('placeholder="mycompany.tld"')) ?>
3-
<p class="form-help"><?= t('Only people with this email address will be allowed to sign up.') ?></p>
2+
<?= $this->form->text('registration_email_domain', $values, $errors, array('placeholder="domain1.tld, domain2.tld, domain3.tld"')) ?>
3+
<p class="form-help"><?= t('Only people with this email address will be allowed to sign up.') ?></p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
use Kanboard\Plugin\Registration\Validator\RegistrationValidator;
4+
5+
class RegistrationValidatorTest extends Base
6+
{
7+
public function testWithoutDomainRestriction()
8+
{
9+
$validator = new RegistrationValidator($this->container);
10+
list($result,) = $validator->validateCreation(array(
11+
'username' => 'test',
12+
'email' => 'test@localhost',
13+
'password' => 'test123',
14+
'confirmation' => 'test123',
15+
));
16+
17+
$this->assertTrue($result);
18+
}
19+
20+
public function testWithDomainRestriction()
21+
{
22+
$this->container['config']->save(array('registration_email_domain' => 'mydomain.tld'));
23+
$validator = new RegistrationValidator($this->container);
24+
25+
list($result,) = $validator->validateCreation(array(
26+
'username' => 'test',
27+
'email' => 'test@localhost',
28+
'password' => 'test123',
29+
'confirmation' => 'test123',
30+
));
31+
32+
$this->assertFalse($result);
33+
34+
list($result,) = $validator->validateCreation(array(
35+
'username' => 'test',
36+
'email' => 'test@mydomain.tld',
37+
'password' => 'test123',
38+
'confirmation' => 'test123',
39+
));
40+
41+
$this->assertTrue($result);
42+
}
43+
44+
public function testWithMultipleDomainRestriction()
45+
{
46+
$this->container['config']->save(array('registration_email_domain' => 'domain1.tld, domain2.tld ,domain3.tld'));
47+
$validator = new RegistrationValidator($this->container);
48+
49+
list($result,) = $validator->validateCreation(array(
50+
'username' => 'test',
51+
'email' => 'test@localhost',
52+
'password' => 'test123',
53+
'confirmation' => 'test123',
54+
));
55+
56+
$this->assertFalse($result);
57+
58+
list($result,) = $validator->validateCreation(array(
59+
'username' => 'test',
60+
'email' => 'test@domain1.tld',
61+
'password' => 'test123',
62+
'confirmation' => 'test123',
63+
));
64+
65+
$this->assertTrue($result);
66+
67+
list($result,) = $validator->validateCreation(array(
68+
'username' => 'test',
69+
'email' => 'test@domain2.tld',
70+
'password' => 'test123',
71+
'confirmation' => 'test123',
72+
));
73+
74+
$this->assertTrue($result);
75+
76+
list($result,) = $validator->validateCreation(array(
77+
'username' => 'test',
78+
'email' => 'test@domain3.tld',
79+
'password' => 'test123',
80+
'confirmation' => 'test123',
81+
));
82+
83+
$this->assertTrue($result);
84+
}
85+
}

Validator/RegistrationValidator.php

+14-7
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class RegistrationValidator extends UserValidator
2323
*/
2424
public function validateCreation(array $values)
2525
{
26+
$domains = $this->config->get('registration_email_domain', '');
27+
2628
$rules = array(
2729
new Validators\Required('username', t('The username is required')),
2830
new Validators\Required('email', t('The email is required')),
@@ -32,7 +34,7 @@ public function validateCreation(array $values)
3234
$result = $v->execute();
3335
$errors = $v->getErrors();
3436

35-
if ($result && !$this->validateDomainRestriction($values)) {
37+
if ($result && $domains !== '' && !$this->validateDomainRestriction($values, $domains)) {
3638
$result = false;
3739
$errors = array('email' => array(t('You are not allowed to register')));
3840
}
@@ -45,17 +47,22 @@ public function validateCreation(array $values)
4547

4648
/**
4749
* Validate domain restriction
48-
* @param array $values
50+
*
51+
* @access private
52+
* @param array $values
53+
* @param string $domains
4954
* @return bool
5055
*/
51-
private function validateDomainRestriction(array $values)
56+
private function validateDomainRestriction(array $values, $domains)
5257
{
53-
$domain = $this->config->get('registration_email_domain', '');
58+
foreach (explode(',', $domains) as $domain) {
59+
$domain = trim($domain);
5460

55-
if ($domain !== '') {
56-
return strpos($values['email'], $domain) > 0;
61+
if (strpos($values['email'], $domain) > 0) {
62+
return true;
63+
}
5764
}
5865

59-
return true;
66+
return false;
6067
}
6168
}

0 commit comments

Comments
 (0)