diff --git a/.gitignore b/.gitignore index c422267..49dbaea 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,3 @@ -composer.phar /vendor/ - -# Commit your application's lock file http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file -# You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file -# composer.lock +/.idea/ +/composer.lock diff --git a/IpRateLimiter.php b/IpRateLimiter.php deleted file mode 100644 index ee1745b..0000000 --- a/IpRateLimiter.php +++ /dev/null @@ -1,61 +0,0 @@ -user; - - if ($this->separateRates) { - $user = $user ?: (Yii::$app->getUser() ? Yii::$app->getUser()->getIdentity(false) : null); - } - - /** @var IpRateLimitInterface $identityClass */ - $identityClass = Yii::$app->getUser()->identityClass; - - $user = $user ?: $identityClass::findByIp(Yii::$app->getRequest()->getUserIP(), $this->rateLimit, - $this->timePeriod); - - if ($user instanceof IpRateLimitInterface) { - Yii::trace('Check rate limit', __METHOD__); - - $this->checkRateLimit( - $user, - $this->request ?: Yii::$app->getRequest(), - $this->response ?: Yii::$app->getResponse(), - $action - ); - - return true; - } - - return parent::beforeAction($action); - } -} diff --git a/README.md b/README.md index 7481f77..91ee36a 100644 --- a/README.md +++ b/README.md @@ -10,13 +10,13 @@ The preferred way to install this extension is through [composer](http://getcomp Either run ``` -php composer.phar require ethercreative/yii2-ip-ratelimiter "1.*" +php composer.phar require andreyv/yii2-ip-ratelimiter "2.*" ``` or add ``` -"ethercreative/yii2-ip-ratelimiter": "1.*" +"andreyv/yii2-ip-ratelimiter": "2.*" ``` to the require section of your `composer.json` file. @@ -28,26 +28,36 @@ Modify the bahavior method of the controller you want to rate limit ``` public function behaviors() { - $behaviors = parent::behaviors(); - $behaviors['rateLimiter'] = [ - // Use class - 'class' => \ethercreative\ratelimiter\RateLimiter::className(), - - // The maximum number of allowed requests - 'rateLimit' => 100, - - // The time period for the rates to apply to - 'timePeriod' => 600, - - // Separate rate limiting for guests and authenticated users - // Defaults to true - // - false: use one set of rates, whether you are authenticated or not - // - true: use separate ratesfor guests and authenticated users - 'separateRates' => false, - - // Whether to return HTTP headers containing the current rate limiting information - 'enableRateLimitHeaders' => false, - ]; - return $behaviors; + $behaviors = parent::behaviors(); + $behaviors['rateLimiter'] = [ + // Use class + 'class' => \andreyv\ratelimiter\IpRateLimiter::class, + + // The maximum number of allowed requests + 'rateLimit' => 100, + + // The time period for the rates to apply to + 'timePeriod' => 600, + + // Separate rate limiting for guests and authenticated users + // Defaults to false + // - false: use one set of rates, whether you are authenticated or not + // - true: use separate ratesfor guests and authenticated users + 'separateRates' => true, + + // Whether to return HTTP headers containing the current rate limiting information + 'enableRateLimitHeaders' => false, + + // Array of actions on which to apply ratelimiter, if empty - applies to all actions + 'actions' => ['index'], + + // Allows to skip rate limiting for test environment + 'testMode' => true, + // Defines whether proxy enabled, list of headers getting from request ipHeaders. By default ['X-Forwarded-For'] + 'proxyEnabled' => false + ]; + return $behaviors; } ``` + +Forked from ethercreative/yii2-ip-ratelimiter. diff --git a/composer.json b/composer.json index 5fbbdae..e575994 100644 --- a/composer.json +++ b/composer.json @@ -1,23 +1,29 @@ { - "name": "ethercreative/yii2-ip-ratelimiter", - "description": "Allow guest clients to be rate limited, using their IP as the identifier.", - "type": "yii2-module", - "keywords": ["yii2", "ratelimiter"], - "license": "MIT", - "authors": [ - { - "name": "Matt Edmonston", - "email": "matt@ethercreative.co.uk" - }, + "name": "andreyv/yii2-ip-ratelimiter", + "description": "Allow guest clients to be rate limited, using their IP as the identifier.", + "type": "yii2-module", + "keywords": ["yii2", "ratelimiter"], + "license": "MIT", + "authors": [ + { + "name": "Matt Edmonston", + "email": "matt@ethercreative.co.uk" + }, { "name": "Yaroslav Lukyanov", "email": "c_sharp@mail.ru" + }, + { + "name": "Andreyv V", + "email": "skifbrt@gmail.com" + } + ], + "require": { + "yiisoft/yii2": "*" + }, + "autoload": { + "psr-4": { + "andreyv\\ratelimiter\\": "src" } - ], - "require": {}, - "autoload": { - "psr-4": { - "ethercreative\\ratelimiter\\": "" - } - } + } } diff --git a/IpRateLimitInterface.php b/src/IpRateLimitInterface.php similarity index 61% rename from IpRateLimitInterface.php rename to src/IpRateLimitInterface.php index 225c379..7f6e3b5 100644 --- a/IpRateLimitInterface.php +++ b/src/IpRateLimitInterface.php @@ -1,14 +1,9 @@ testMode) { + return true; + } + + if (is_array($this->actions) && (empty($this->actions) || in_array($action->id, $this->actions))) { + if ($this->separateRates && !$this->user) { + $this->user = Yii::$app->getUser() ? Yii::$app->getUser()->getIdentity(false) : null; + } + + if (!$this->user) { + /** @var IpRateLimitInterface $identityClass */ + $identityClass = Yii::$app->getUser()->identityClass; + if (!in_array(UserIdentity::class, class_implements($identityClass))) { + $identityClass = UserIdentity::class; + } + + $this->user = $identityClass::create( + $this->request->getUserIP(), + $this->rateLimit, + $this->timePeriod + ); + } + + return parent::beforeAction($action); + } + return true; + } +} diff --git a/UserExample.php b/src/UserIdentity.php similarity index 83% rename from UserExample.php rename to src/UserIdentity.php index 3e6a7d4..461f843 100644 --- a/UserExample.php +++ b/src/UserIdentity.php @@ -1,15 +1,10 @@