Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refs #12771 \yii\web\User::can() and guest #12785

Closed

Conversation

pchapl
Copy link
Contributor

@pchapl pchapl commented Oct 19, 2016

Q A
Is bugfix? no
New feature? no
Breaks BC? no
Tests pass? yes
Fixed issues #12771

Skip \yii\rbac\PhpManager::checkAccessRecursive and \yii\rbac\DbManager::checkAccessRecursive if role assignments are empty.
I checked code of \yii\rbac\PhpManager::checkAccessRecursive and \yii\rbac\DbManager::checkAccessRecursive, and I'm sure that result always will be "false" if $assignments is empty array and \yii\rbac\BaseManager::$defaultRoles is empty array too.
On the other hand, I'm not sure it matters.

@samdark samdark added this to the 2.0.11 milestone Oct 19, 2016
@pchapl pchapl force-pushed the 12771_optimize_check_access_on_guest branch 2 times, most recently from d3b5cac to c8589e9 Compare October 20, 2016 15:20
@@ -94,6 +95,7 @@ Yii Framework 2 Change Log
- Enh #12580: Make `yii.js` comply with strict and non-strict javascript mode to allow concatenation with external code (mikehaertl)
- Enh #12664: Added support for wildcards for `optional` at `yii\filters\auth\AuthMethod` (mg-code)
- Enh #12744: Added `afterInit` event to `yii.activeForm.js` (werew01f)
- Enh #12499: When AJAX validation in enabled, `yii.activeForm.js` will run it forcefully on form submit to display all possible errors (silverfire)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line should be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @param Assignment[] $assignments list of user's role assignments
* @return bool true if empty
*/
protected function emptyRoles(array $assignments)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We prefix this kind of methods with has or is to make it easier to understand, what does the method do.

Maybe, hasNoAssignments()?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -220,4 +220,15 @@ protected function executeRule($user, $item, $params)
throw new InvalidConfigException("Rule not found: {$item->ruleName}");
}
}

/**
* Check that there no any roles in user's role assignments and in default roles
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method checks, whether $assignments array is empty and [[defaultRoles]] are empty as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

/**
* Check that there no any roles in user's role assignments and in default roles
*
* @param Assignment[] $assignments list of user's role assignments
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@param Assignment[] $assignments array of user's assignments

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* Check that there no any roles in user's role assignments and in default roles
*
* @param Assignment[] $assignments list of user's role assignments
* @return bool true if empty
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@return bool whether $assignments array is empty and [[defaultRoles]] are empty as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

*/
protected function emptyRoles(array $assignments)
{
return count($assignments) === 0 && count($this->defaultRoles) === 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplified to

return empty($assignments) && empty($this->defaultRoles);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer to use count() === 0 cause I know that is array (eg not zero or empty string). It does not matter. And empty works faster with large arrays, so done.

@@ -119,6 +119,12 @@ public function init()
public function checkAccess($userId, $permissionName, $params = [])
{
$assignments = $this->getAssignments($userId);

// nothing to check, user has no any rights
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -97,6 +97,12 @@ public function init()
public function checkAccess($userId, $permissionName, $params = [])
{
$assignments = $this->getAssignments($userId);

// nothing to check, user has no any rights
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be removed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@pchapl pchapl force-pushed the 12771_optimize_check_access_on_guest branch from c8589e9 to e7ebacc Compare November 15, 2016 05:41
Copy link
Member

@SilverFire SilverFire left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

Copy link
Member

@dynasource dynasource left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting effort in improving Yii2.

A few things:

  • there is duplication of logic in DbManager & PhpManager which should be prevented. I would expect a new function like 'getActiveAssignements' or something like that.
  • inverse logic is applied in the method name 'hasNoAssignments'. This should be formulated positively like 'hasAssignments'
  • I a still missing a clear explanation in the method docs why the added method is useful. It states what it does, but not why its useful.
  • new methods should get a @SInCE tag

@SilverFire
Copy link
Member

A few words what happen to this PR:

  1. I've synchronized it with master and pushed changes to the branch in @ni-san's fork. GitHub got crazy and showed a diff for this PR containing hundreds of commits.
  2. I've removed all this mess and force-pushed clear branch
  3. No changes suggested by @ni-san survived in his branch after this force-push. Sorry.

@SilverFire
Copy link
Member

Restored changes from git reflog, pushed to the master branch: 5795b39

@SilverFire
Copy link
Member

The PR is merged. Thank yoou

@SilverFire SilverFire self-assigned this Dec 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants