commit 0487fa3ee0cfb8d11cac26c29e8e16b181325b1a Author: ErickSkrauch Date: Sun May 1 19:31:39 2016 +0300 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5440847 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.idea +build +composer.lock +vendor diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..8feccda --- /dev/null +++ b/CHANGELOG.md @@ -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 diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..860f591 --- /dev/null +++ b/CONTRIBUTING.md @@ -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 +``` diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..21c2611 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2016 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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..05f0427 --- /dev/null +++ b/README.md @@ -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 diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..2b99a3d --- /dev/null +++ b/composer.json @@ -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" + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..89366cb --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,29 @@ + + + + + tests + + + + + src/ + + + + + + + + + + diff --git a/src/TempmailValidator.php b/src/TempmailValidator.php new file mode 100644 index 0000000..e092f79 --- /dev/null +++ b/src/TempmailValidator.php @@ -0,0 +1,66 @@ +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; + } +} diff --git a/tests/TempmailValidatorTest.php b/tests/TempmailValidatorTest.php new file mode 100644 index 0000000..c76a6e7 --- /dev/null +++ b/tests/TempmailValidatorTest.php @@ -0,0 +1,91 @@ + '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()], + ]; + } +}