Fix view_stats capability check for users with multiple roles#47991
Fix view_stats capability check for users with multiple roles#47991yaswanthkumar1995 wants to merge 1 commit intoAutomattic:trunkfrom
Conversation
|
/assign |
|
@coderabbitai review |
|
Thank you for your PR! When contributing to Jetpack, we have a few suggestions that can help us test and review your patch:
This comment will be updated as you work on your PR and make changes. If you think that some of those checks are not needed for your PR, please explain why you think so. Thanks for cooperation 🤖 |
The view_stats meta-cap mapping only checked the first role in the user's roles array via array_shift(). Users with multiple roles (e.g. customer + administrator) could be denied stats access if their first role wasn't in the stats roles allowlist. Use array_intersect() to check all user roles against the allowed stats roles instead of only the first. Fixes Automattic#47258
aad72de to
439f966
Compare
|
@claude review please. Out of interest, do WordPress roles get added to a user in chronological order? |
|
Claude finished @adamwoodnz's task in 1m 4s —— View job Code Review
SummaryThe fix is correct and well-targeted. The original
|
adamwoodnz
left a comment
There was a problem hiding this comment.
Thanks for the PR @yaswanthkumar1995!
I agree with Claude that a couple more tests would be good. One for a single allowed stats role, and one for a non-allowed, eg. subscriber.
kangzj
left a comment
There was a problem hiding this comment.
Thanks for fixing this. The changes look good to me!
Fixes #47258
Proposed changes
view_statsmeta-capability mapping to check all user roles instead of only the first one.array_shift()+in_array()witharray_intersect()so multi-role users (e.g.customer+administrator) are correctly granted stats access.administratoris not the first role.Other information
The bug occurs because
array_shift($user_roles)only returns the first role in the array. For users who were assigned a non-admin role before being promoted to administrator, the first role (e.g.customer) isn't in the stats allowlist, so they're denied access.Related product discussion/links
Does this pull request change what data or activity we track or use?
No.
Testing instructions
subscriberroleadministratoras a second role:$user->add_role('administrator')user_can($user_id, 'view_stats')returnstruefalsebecause only the first role (subscriber) was checked