Browse Source

Merge pull request #12607 from dynasource/12592-improve-performance-accesscontrol

enhance performance by ignoring rules that are never used #12592
tags/3.0.0-alpha1
Dmitry Naumenko 8 years ago committed by GitHub
parent
commit
78224615ad
  1. 2
      framework/CHANGELOG.md
  2. 3
      framework/UPGRADE.md
  3. 24
      framework/filters/AccessControl.php

2
framework/CHANGELOG.md

@ -3,7 +3,7 @@ Yii Framework 2 Change Log
2.1.0 under development 2.1.0 under development
----------------------- -----------------------
- Enh #12592: Optimized `yii\filters\AccessController` on processing accessrules (dynasource)
- Removed methods marked as deprected in 2.0.x (samdark) - Removed methods marked as deprected in 2.0.x (samdark)
- Chg #10771: Consistent behavior of `run()` method in all framework widgets. All return the result now for better extensibility (pkirill99, cebe) - Chg #10771: Consistent behavior of `run()` method in all framework widgets. All return the result now for better extensibility (pkirill99, cebe)
- Chg #11397: Minimum required version of PHP is 5.5.0 now (samdark) - Chg #11397: Minimum required version of PHP is 5.5.0 now (samdark)

3
framework/UPGRADE.md

@ -34,6 +34,9 @@ Upgrade to Yii 2.1.0
`yii\db\QueryBuilderbuild::buildOrderByAndLimit($sql, $orderBy, $limit, $offset)` -> `buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params)` `yii\db\QueryBuilderbuild::buildOrderByAndLimit($sql, $orderBy, $limit, $offset)` -> `buildOrderByAndLimit($sql, $orderBy, $limit, $offset, &$params)`
`yii\widgets\ActiveField::hint($content = null, $options = [])` `yii\widgets\ActiveField::hint($content = null, $options = [])`
* `yii\filters\AccessControl` has been optimized by only instantiating rules at the moment of use.
This could lead to a potential BC-break if you are depending on $rules to be instantiated in init().
Upgrade from Yii 2.0.8 Upgrade from Yii 2.0.8
---------------------- ----------------------

24
framework/filters/AccessControl.php

@ -11,8 +11,8 @@ use Yii;
use yii\base\Action; use yii\base\Action;
use yii\base\ActionFilter; use yii\base\ActionFilter;
use yii\di\Instance; use yii\di\Instance;
use yii\web\User;
use yii\web\ForbiddenHttpException; use yii\web\ForbiddenHttpException;
use yii\web\User;
/** /**
* AccessControl provides simple access control based on a set of rules. * AccessControl provides simple access control based on a set of rules.
@ -88,21 +88,6 @@ class AccessControl extends ActionFilter
*/ */
public $rules = []; public $rules = [];
/**
* Initializes the [[rules]] array by instantiating rule objects from configurations.
*/
public function init()
{
parent::init();
$this->user = Instance::ensure($this->user, User::class);
foreach ($this->rules as $i => $rule) {
if (is_array($rule)) {
$this->rules[$i] = Yii::createObject(array_merge($this->ruleConfig, $rule));
}
}
}
/** /**
* This method is invoked right before an action is to be executed (after all possible filters.) * This method is invoked right before an action is to be executed (after all possible filters.)
* You may override this method to do last-minute preparation for the action. * You may override this method to do last-minute preparation for the action.
@ -111,10 +96,13 @@ class AccessControl extends ActionFilter
*/ */
public function beforeAction($action) public function beforeAction($action)
{ {
$user = $this->user; $user = $this->user = Instance::ensure($this->user, User::class);
$request = Yii::$app->getRequest(); $request = Yii::$app->getRequest();
/* @var $rule AccessRule */ /* @var $rule AccessRule */
foreach ($this->rules as $rule) { foreach ($this->rules as $key => $rule) {
if (!is_object($rule)) {
$rule = $this->rules[$key] = Yii::createObject(array_merge($this->ruleConfig, $rule));
}
if ($allow = $rule->allows($action, $user, $request)) { if ($allow = $rule->allows($action, $user, $request)) {
return true; return true;
} elseif ($allow === false) { } elseif ($allow === false) {

Loading…
Cancel
Save