Skip to content

Commit 0487fa3

Browse files
committed
Initial commit
0 parents  commit 0487fa3

File tree

9 files changed

+358
-0
lines changed

9 files changed

+358
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.idea
2+
build
3+
composer.lock
4+
vendor

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Change Log
2+
3+
All notable changes to `elyby/yii2-tempmail-validator` are documented in this file using the
4+
[Keep a CHANGELOG](http://keepachangelog.com/) principles.
5+
6+
## 1.0.0 - 2016-05-01
7+
8+
### Added
9+
* Initial release

CONTRIBUTING.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Contributing
2+
3+
* Coding standard for the project is [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
4+
* Any contribution must provide tests for additional introduced conditions
5+
6+
## Installation
7+
8+
To install the project and run the tests, you need to clone it first:
9+
10+
```sh
11+
$ git clone [email protected]:elyby/yii2-tempmail-validator.git
12+
```
13+
14+
You will then need to run a composer installation:
15+
16+
```sh
17+
$ cd php-tempmailbuster
18+
$ curl -s https://getcomposer.org/installer | php
19+
$ php composer.phar update
20+
```
21+
22+
## Testing
23+
24+
The PHPUnit version to be used is the one installed as a dev- dependency via composer:
25+
26+
```sh
27+
$ ./vendor/bin/phpunit
28+
```
29+
30+
or
31+
32+
```sh
33+
$ composer test
34+
```

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2016 Ely.by <[email protected]>
4+
5+
> Permission is hereby granted, free of charge, to any person obtaining a copy
6+
> of this software and associated documentation files (the "Software"), to deal
7+
> in the Software without restriction, including without limitation the rights
8+
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
> copies of the Software, and to permit persons to whom the Software is
10+
> furnished to do so, subject to the following conditions:
11+
>
12+
> The above copyright notice and this permission notice shall be included in
13+
> all copies or substantial portions of the Software.
14+
>
15+
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
> THE SOFTWARE.

README.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Yii2 Tempmail Validator
2+
3+
[![Latest Version on Packagist][ico-version]][link-packagist]
4+
[![Software License][ico-license]](LICENSE.md)
5+
[![Total Downloads][ico-downloads]][link-downloads]
6+
7+
Yii2 validator, based on [https://github.com/elyby/php-tempmailbuster](https://github.com/elyby/php-tempmailbuster)
8+
library and [https://github.com/elyby/anti-tempmail-repo](https://github.com/elyby/anti-tempmail-repo) reference.
9+
Helps to protect you site from users, who use 10-minutes mail services.
10+
11+
## Installation
12+
13+
Install the latest version with
14+
15+
```sh
16+
$ composer require "ely/yii2-tempmail-validator:~1.0.0"
17+
```
18+
19+
## Usage
20+
21+
Once the extension is installed, simply use it in your models:
22+
23+
```php
24+
public function rules()
25+
{
26+
return [
27+
[['email'], \Ely\Yii2\TempmailValidator::className()],
28+
];
29+
}
30+
```
31+
32+
Also you can configure validator:
33+
34+
```php
35+
public function rules()
36+
{
37+
return [
38+
[['email'], \Ely\Yii2\TempmailValidator::className(),
39+
'message' => '{attribute} is tempmail. You will not pass',
40+
'whiteList' => false,
41+
'secondaryStorage' => ['spam4\.me'],
42+
],
43+
];
44+
}
45+
```
46+
47+
## Change log
48+
49+
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
50+
51+
## Contributing
52+
53+
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
54+
55+
## Credits
56+
57+
This package was designed and developed within the [Ely.by](http://ely.by) project team. We also thank all the
58+
[contributors](link-contributors) for their help.
59+
60+
## License
61+
62+
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
63+
64+
[ico-version]: https://img.shields.io/packagist/v/ely/yii2-tempmail-validator.svg?style=flat-square
65+
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
66+
[ico-downloads]: https://img.shields.io/packagist/dt/ely/yii2-tempmail-validator.svg?style=flat-square
67+
68+
[link-packagist]: https://packagist.org/packages/ely/yii2-tempmail-validator
69+
[link-author]: https://github.com/ErickSkrauch
70+
[link-contributors]: ../../contributors
71+
[link-downloads]: https://packagist.org/packages/ely/yii2-tempmail-validator

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "ely/yii2-tempmail-validator",
3+
"description": "Yii2 validator to prevent using tempmail services or any variable domains",
4+
"keywords": ["yii2", "email", "validation"],
5+
"homepage": "https://github.com/elyby/yii2-tempmail-validator",
6+
"license": "MIT",
7+
"type": "library",
8+
"authors": [
9+
{
10+
"name": "Ely.by team",
11+
"email": "[email protected]"
12+
},
13+
{
14+
"name": "ErickSkrauch",
15+
"email": "[email protected]"
16+
}
17+
],
18+
"require": {
19+
"yiisoft/yii2": "*",
20+
"ely/php-tempmailbuster": "~1.0.2"
21+
},
22+
"require-dev": {
23+
"phpunit/phpunit": "~4.8 || ~5.0"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"Ely\\Yii2\\": "src"
28+
}
29+
},
30+
"scripts": {
31+
"test": "phpunit"
32+
}
33+
}

phpunit.xml.dist

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
convertErrorsToExceptions="true"
8+
convertNoticesToExceptions="true"
9+
convertWarningsToExceptions="true"
10+
processIsolation="false"
11+
stopOnFailure="false">
12+
<testsuites>
13+
<testsuite name="Ely Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
<filter>
18+
<whitelist>
19+
<directory suffix=".php">src/</directory>
20+
</whitelist>
21+
</filter>
22+
<logging>
23+
<log type="tap" target="build/report.tap"/>
24+
<log type="junit" target="build/report.junit.xml"/>
25+
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26+
<log type="coverage-text" target="build/coverage.txt"/>
27+
<log type="coverage-clover" target="build/logs/clover.xml"/>
28+
</logging>
29+
</phpunit>

src/TempmailValidator.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
namespace Ely\Yii2;
3+
4+
use Ely\TempMailBuster\StorageInterface;
5+
use Ely\TempMailBuster\Validator as TempmailBuster;
6+
use Yii;
7+
use yii\validators\Validator;
8+
9+
class TempmailValidator extends Validator
10+
{
11+
/**
12+
* @var string class name for used tempmail loader
13+
*/
14+
public $loader = '\Ely\TempMailBuster\Loader\AntiTempmailRepo';
15+
/**
16+
* @var string class name for used storage object
17+
*/
18+
public $storage = '\Ely\TempMailBuster\Storage';
19+
/**
20+
* @var bool switcher for white/blacklist validation
21+
*/
22+
public $whitelistMode = false;
23+
/**
24+
* @var null|array|StorageInterface additional list to invert current mode validation
25+
* @see \Ely\TempMailBuster\Validator::validate() implementation for additional info
26+
*/
27+
public $secondaryStorage;
28+
29+
public function init()
30+
{
31+
parent::init();
32+
if ($this->message === null) {
33+
$this->message = Yii::t('yii', '{attribute} is not allowed email address.');
34+
}
35+
}
36+
37+
protected function validateValue($value)
38+
{
39+
$validator = $this->buildValidator();
40+
if ($validator->validate($value)) {
41+
return null;
42+
}
43+
44+
return [$this->message, []];
45+
}
46+
47+
/**
48+
* @return TempmailBuster
49+
*/
50+
protected function buildValidator()
51+
{
52+
/** @var \Ely\TempMailBuster\LoaderInterface $loader */
53+
$loader = new $this->loader;
54+
/** @var StorageInterface $primaryStorage */
55+
$primaryStorage = new $this->storage($loader->load());
56+
$secondaryStorage = $this->secondaryStorage;
57+
if (is_array($this->secondaryStorage)) {
58+
$secondaryStorage = new $this->storage($this->secondaryStorage);
59+
}
60+
61+
$validator = new TempmailBuster($primaryStorage, $secondaryStorage);
62+
$validator->whitelistMode($this->whitelistMode);
63+
64+
return $validator;
65+
}
66+
}

tests/TempmailValidatorTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
namespace Ely\Yii2;
3+
4+
include __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
5+
6+
use Ely\TempMailBuster\Storage;
7+
use yii\base\Model;
8+
use yii\console\Application;
9+
10+
class TempmailValidatorTest extends \PHPUnit_Framework_TestCase
11+
{
12+
protected function setUp()
13+
{
14+
parent::setUp();
15+
new Application([
16+
'id' => 'testapp',
17+
'basePath' => __DIR__,
18+
'vendorPath' => __DIR__ . '/../vendor',
19+
]);
20+
}
21+
22+
public function testValidateValue()
23+
{
24+
$validator = new TempmailValidator();
25+
$this->assertNull($this->callValidateValue($validator, '[email protected]'));
26+
$this->assertEquals([$validator->message, []], $this->callValidateValue($validator, '[email protected]'));
27+
}
28+
29+
public function testBuildValidator()
30+
{
31+
$validator = new TempmailValidator();
32+
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $this->callBuildValidator($validator));
33+
34+
$domains = ['mojang\.com'];
35+
$validator = new TempmailValidator(['secondaryStorage' => $domains]);
36+
$buster = $this->callBuildValidator($validator);
37+
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $buster);
38+
$this->assertEquals($domains, $buster->getSecondaryStorage()->getItems());
39+
40+
$validator = new TempmailValidator(['secondaryStorage' => new Storage($domains)]);
41+
$buster = $this->callBuildValidator($validator);
42+
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $buster);
43+
$this->assertEquals($domains, $buster->getSecondaryStorage()->getItems());
44+
}
45+
46+
public function testValidatorOnModel()
47+
{
48+
$model = new DummyModel();
49+
$model->email = '[email protected]';
50+
$this->assertTrue($model->validate());
51+
52+
$model = new DummyModel();
53+
$model->email = '[email protected]';
54+
$this->assertFalse($model->validate());
55+
$this->assertNotEmpty($model->getErrors('email'));
56+
}
57+
58+
/**
59+
* @param TempmailValidator $object
60+
* @return \Ely\TempMailBuster\Validator
61+
*/
62+
private function callBuildValidator($object)
63+
{
64+
$class = new \ReflectionClass($object);
65+
$method = $class->getMethod('buildValidator');
66+
$method->setAccessible(true);
67+
68+
return $method->invoke($object);
69+
}
70+
71+
private function callValidateValue($object, $value)
72+
{
73+
$class = new \ReflectionClass($object);
74+
$method = $class->getMethod('validateValue');
75+
$method->setAccessible(true);
76+
77+
return $method->invokeArgs($object, [$value]);
78+
}
79+
}
80+
81+
class DummyModel extends Model
82+
{
83+
public $email;
84+
85+
public function rules()
86+
{
87+
return [
88+
[['email'], TempmailValidator::className()],
89+
];
90+
}
91+
}

0 commit comments

Comments
 (0)