mirror of
https://github.com/elyby/yii2-tempmail-validator.git
synced 2024-11-22 21:22:57 +05:30
Initial commit
This commit is contained in:
commit
0487fa3ee0
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
.idea
|
||||
build
|
||||
composer.lock
|
||||
vendor
|
9
CHANGELOG.md
Normal file
9
CHANGELOG.md
Normal file
@ -0,0 +1,9 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to `elyby/yii2-tempmail-validator` are documented in this file using the
|
||||
[Keep a CHANGELOG](http://keepachangelog.com/) principles.
|
||||
|
||||
## 1.0.0 - 2016-05-01
|
||||
|
||||
### Added
|
||||
* Initial release
|
34
CONTRIBUTING.md
Normal file
34
CONTRIBUTING.md
Normal file
@ -0,0 +1,34 @@
|
||||
# Contributing
|
||||
|
||||
* Coding standard for the project is [PSR-2](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)
|
||||
* Any contribution must provide tests for additional introduced conditions
|
||||
|
||||
## Installation
|
||||
|
||||
To install the project and run the tests, you need to clone it first:
|
||||
|
||||
```sh
|
||||
$ git clone git@github.com:elyby/yii2-tempmail-validator.git
|
||||
```
|
||||
|
||||
You will then need to run a composer installation:
|
||||
|
||||
```sh
|
||||
$ cd php-tempmailbuster
|
||||
$ curl -s https://getcomposer.org/installer | php
|
||||
$ php composer.phar update
|
||||
```
|
||||
|
||||
## Testing
|
||||
|
||||
The PHPUnit version to be used is the one installed as a dev- dependency via composer:
|
||||
|
||||
```sh
|
||||
$ ./vendor/bin/phpunit
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```sh
|
||||
$ composer test
|
||||
```
|
21
LICENSE.md
Normal file
21
LICENSE.md
Normal file
@ -0,0 +1,21 @@
|
||||
# The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2016 Ely.by <team@ely.by>
|
||||
|
||||
> Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
> of this software and associated documentation files (the "Software"), to deal
|
||||
> in the Software without restriction, including without limitation the rights
|
||||
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
> copies of the Software, and to permit persons to whom the Software is
|
||||
> furnished to do so, subject to the following conditions:
|
||||
>
|
||||
> The above copyright notice and this permission notice shall be included in
|
||||
> all copies or substantial portions of the Software.
|
||||
>
|
||||
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
> THE SOFTWARE.
|
71
README.md
Normal file
71
README.md
Normal file
@ -0,0 +1,71 @@
|
||||
# Yii2 Tempmail Validator
|
||||
|
||||
[![Latest Version on Packagist][ico-version]][link-packagist]
|
||||
[![Software License][ico-license]](LICENSE.md)
|
||||
[![Total Downloads][ico-downloads]][link-downloads]
|
||||
|
||||
Yii2 validator, based on [https://github.com/elyby/php-tempmailbuster](https://github.com/elyby/php-tempmailbuster)
|
||||
library and [https://github.com/elyby/anti-tempmail-repo](https://github.com/elyby/anti-tempmail-repo) reference.
|
||||
Helps to protect you site from users, who use 10-minutes mail services.
|
||||
|
||||
## Installation
|
||||
|
||||
Install the latest version with
|
||||
|
||||
```sh
|
||||
$ composer require "ely/yii2-tempmail-validator:~1.0.0"
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Once the extension is installed, simply use it in your models:
|
||||
|
||||
```php
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['email'], \Ely\Yii2\TempmailValidator::className()],
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
Also you can configure validator:
|
||||
|
||||
```php
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['email'], \Ely\Yii2\TempmailValidator::className(),
|
||||
'message' => '{attribute} is tempmail. You will not pass',
|
||||
'whiteList' => false,
|
||||
'secondaryStorage' => ['spam4\.me'],
|
||||
],
|
||||
];
|
||||
}
|
||||
```
|
||||
|
||||
## Change log
|
||||
|
||||
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
|
||||
|
||||
## Contributing
|
||||
|
||||
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
|
||||
|
||||
## Credits
|
||||
|
||||
This package was designed and developed within the [Ely.by](http://ely.by) project team. We also thank all the
|
||||
[contributors](link-contributors) for their help.
|
||||
|
||||
## License
|
||||
|
||||
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
|
||||
|
||||
[ico-version]: https://img.shields.io/packagist/v/ely/yii2-tempmail-validator.svg?style=flat-square
|
||||
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
|
||||
[ico-downloads]: https://img.shields.io/packagist/dt/ely/yii2-tempmail-validator.svg?style=flat-square
|
||||
|
||||
[link-packagist]: https://packagist.org/packages/ely/yii2-tempmail-validator
|
||||
[link-author]: https://github.com/ErickSkrauch
|
||||
[link-contributors]: ../../contributors
|
||||
[link-downloads]: https://packagist.org/packages/ely/yii2-tempmail-validator
|
33
composer.json
Normal file
33
composer.json
Normal file
@ -0,0 +1,33 @@
|
||||
{
|
||||
"name": "ely/yii2-tempmail-validator",
|
||||
"description": "Yii2 validator to prevent using tempmail services or any variable domains",
|
||||
"keywords": ["yii2", "email", "validation"],
|
||||
"homepage": "https://github.com/elyby/yii2-tempmail-validator",
|
||||
"license": "MIT",
|
||||
"type": "library",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Ely.by team",
|
||||
"email": "team@ely.by"
|
||||
},
|
||||
{
|
||||
"name": "ErickSkrauch",
|
||||
"email": "erickskrauch@ely.by"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"yiisoft/yii2": "*",
|
||||
"ely/php-tempmailbuster": "~1.0.2"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.8 || ~5.0"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Ely\\Yii2\\": "src"
|
||||
}
|
||||
},
|
||||
"scripts": {
|
||||
"test": "phpunit"
|
||||
}
|
||||
}
|
29
phpunit.xml.dist
Normal file
29
phpunit.xml.dist
Normal file
@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit bootstrap="vendor/autoload.php"
|
||||
backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
verbose="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false">
|
||||
<testsuites>
|
||||
<testsuite name="Ely Test Suite">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory suffix=".php">src/</directory>
|
||||
</whitelist>
|
||||
</filter>
|
||||
<logging>
|
||||
<log type="tap" target="build/report.tap"/>
|
||||
<log type="junit" target="build/report.junit.xml"/>
|
||||
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
|
||||
<log type="coverage-text" target="build/coverage.txt"/>
|
||||
<log type="coverage-clover" target="build/logs/clover.xml"/>
|
||||
</logging>
|
||||
</phpunit>
|
66
src/TempmailValidator.php
Normal file
66
src/TempmailValidator.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
namespace Ely\Yii2;
|
||||
|
||||
use Ely\TempMailBuster\StorageInterface;
|
||||
use Ely\TempMailBuster\Validator as TempmailBuster;
|
||||
use Yii;
|
||||
use yii\validators\Validator;
|
||||
|
||||
class TempmailValidator extends Validator
|
||||
{
|
||||
/**
|
||||
* @var string class name for used tempmail loader
|
||||
*/
|
||||
public $loader = '\Ely\TempMailBuster\Loader\AntiTempmailRepo';
|
||||
/**
|
||||
* @var string class name for used storage object
|
||||
*/
|
||||
public $storage = '\Ely\TempMailBuster\Storage';
|
||||
/**
|
||||
* @var bool switcher for white/blacklist validation
|
||||
*/
|
||||
public $whitelistMode = false;
|
||||
/**
|
||||
* @var null|array|StorageInterface additional list to invert current mode validation
|
||||
* @see \Ely\TempMailBuster\Validator::validate() implementation for additional info
|
||||
*/
|
||||
public $secondaryStorage;
|
||||
|
||||
public function init()
|
||||
{
|
||||
parent::init();
|
||||
if ($this->message === null) {
|
||||
$this->message = Yii::t('yii', '{attribute} is not allowed email address.');
|
||||
}
|
||||
}
|
||||
|
||||
protected function validateValue($value)
|
||||
{
|
||||
$validator = $this->buildValidator();
|
||||
if ($validator->validate($value)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return [$this->message, []];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return TempmailBuster
|
||||
*/
|
||||
protected function buildValidator()
|
||||
{
|
||||
/** @var \Ely\TempMailBuster\LoaderInterface $loader */
|
||||
$loader = new $this->loader;
|
||||
/** @var StorageInterface $primaryStorage */
|
||||
$primaryStorage = new $this->storage($loader->load());
|
||||
$secondaryStorage = $this->secondaryStorage;
|
||||
if (is_array($this->secondaryStorage)) {
|
||||
$secondaryStorage = new $this->storage($this->secondaryStorage);
|
||||
}
|
||||
|
||||
$validator = new TempmailBuster($primaryStorage, $secondaryStorage);
|
||||
$validator->whitelistMode($this->whitelistMode);
|
||||
|
||||
return $validator;
|
||||
}
|
||||
}
|
91
tests/TempmailValidatorTest.php
Normal file
91
tests/TempmailValidatorTest.php
Normal file
@ -0,0 +1,91 @@
|
||||
<?php
|
||||
namespace Ely\Yii2;
|
||||
|
||||
include __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
|
||||
|
||||
use Ely\TempMailBuster\Storage;
|
||||
use yii\base\Model;
|
||||
use yii\console\Application;
|
||||
|
||||
class TempmailValidatorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
protected function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
new Application([
|
||||
'id' => 'testapp',
|
||||
'basePath' => __DIR__,
|
||||
'vendorPath' => __DIR__ . '/../vendor',
|
||||
]);
|
||||
}
|
||||
|
||||
public function testValidateValue()
|
||||
{
|
||||
$validator = new TempmailValidator();
|
||||
$this->assertNull($this->callValidateValue($validator, 'team@ely.by'));
|
||||
$this->assertEquals([$validator->message, []], $this->callValidateValue($validator, 'h4l29@spam4.me'));
|
||||
}
|
||||
|
||||
public function testBuildValidator()
|
||||
{
|
||||
$validator = new TempmailValidator();
|
||||
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $this->callBuildValidator($validator));
|
||||
|
||||
$domains = ['mojang\.com'];
|
||||
$validator = new TempmailValidator(['secondaryStorage' => $domains]);
|
||||
$buster = $this->callBuildValidator($validator);
|
||||
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $buster);
|
||||
$this->assertEquals($domains, $buster->getSecondaryStorage()->getItems());
|
||||
|
||||
$validator = new TempmailValidator(['secondaryStorage' => new Storage($domains)]);
|
||||
$buster = $this->callBuildValidator($validator);
|
||||
$this->assertInstanceOf('\Ely\TempMailBuster\Validator', $buster);
|
||||
$this->assertEquals($domains, $buster->getSecondaryStorage()->getItems());
|
||||
}
|
||||
|
||||
public function testValidatorOnModel()
|
||||
{
|
||||
$model = new DummyModel();
|
||||
$model->email = 'team@ely.by';
|
||||
$this->assertTrue($model->validate());
|
||||
|
||||
$model = new DummyModel();
|
||||
$model->email = 'spam@spam4.me';
|
||||
$this->assertFalse($model->validate());
|
||||
$this->assertNotEmpty($model->getErrors('email'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param TempmailValidator $object
|
||||
* @return \Ely\TempMailBuster\Validator
|
||||
*/
|
||||
private function callBuildValidator($object)
|
||||
{
|
||||
$class = new \ReflectionClass($object);
|
||||
$method = $class->getMethod('buildValidator');
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invoke($object);
|
||||
}
|
||||
|
||||
private function callValidateValue($object, $value)
|
||||
{
|
||||
$class = new \ReflectionClass($object);
|
||||
$method = $class->getMethod('validateValue');
|
||||
$method->setAccessible(true);
|
||||
|
||||
return $method->invokeArgs($object, [$value]);
|
||||
}
|
||||
}
|
||||
|
||||
class DummyModel extends Model
|
||||
{
|
||||
public $email;
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
[['email'], TempmailValidator::className()],
|
||||
];
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user