-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
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
refs #12771 \yii\web\User::can() and guest #12785
Conversation
d3b5cac
to
c8589e9
Compare
@@ -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) |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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()
?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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);
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be removed
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be removed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
c8589e9
to
e7ebacc
Compare
There was a problem hiding this 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
There was a problem hiding this 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
8214ec2
to
dd4b5af
Compare
A few words what happen to this PR:
|
Restored changes from |
The PR is merged. Thank yoou |
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.