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

Don't allow Super Admins to use disabled PCH features #2895

Merged
merged 6 commits into from
Nov 4, 2024

Conversation

vaurdan
Copy link
Contributor

@vaurdan vaurdan commented Oct 30, 2024

Description

Super Admins, by default, will have no user roles unless they are added to that specific site. By doing a validation if the user is a Super Admin - after validating that the feature is enabled - we can allow super admins to use any PCH feature, unless they are intentionally disabled.

Motivation and context

  • Fixes undesired behavior for super users using PCH.

How has this been tested?

Tested locally on a multisite with a super admin account.

Summary by CodeRabbit

  • New Features

    • Enhanced user role permissions to allow super administrators access to specific features regardless of their roles.
  • Bug Fixes

    • Maintained existing logic for user access checks, ensuring consistent functionality for all users.
  • Tests

    • Improved test coverage for permissions, particularly for super administrators accessing enabled and disabled features.

@vaurdan vaurdan added this to the 3.17.0 milestone Oct 30, 2024
@vaurdan vaurdan self-assigned this Oct 30, 2024
@vaurdan vaurdan requested a review from a team as a code owner October 30, 2024 14:03
Copy link
Contributor

coderabbitai bot commented Oct 30, 2024

📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough
📝 Walkthrough

Walkthrough

The Permissions class in src/class-permissions.php has been updated to improve user role permissions. A new conditional check has been added to the current_user_can_use_pch_feature method, allowing super administrators in a multisite environment to access features regardless of their roles. The existing logic for user access, including checks for feature enablement and role capabilities, remains unchanged. The method signature has not been altered, ensuring the interface remains consistent.

Changes

File Change Summary
src/class-permissions.php Updated current_user_can_use_pch_feature method to include a check for super administrators, allowing them access regardless of their roles. No changes to method signatures or overall class structure.
tests/Integration/PermissionsTest.php Modified test_disallowed_user_role_attempts_to_access_enabled_pch_features to test 'administrator' role instead of 'editor'; added tests for super admin access to disabled and enabled features.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Permissions
    participant Feature

    User->>Permissions: current_user_can_use_pch_feature(feature_name, pch_options)
    alt User is Super Admin
        Permissions-->>User: return true
    else User has necessary roles
        Permissions-->>User: return true
    else User lacks necessary roles
        Permissions-->>User: return false
    end
Loading

Possibly related PRs

Suggested labels

Feature: Content Helper, Type: Maintenance


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
src/class-permissions.php (1)

118-121: Consider enhancing the comment and handling non-multisite environments.

The current implementation could be improved in two ways:

  1. The comment should be more descriptive about the multisite requirement
  2. Consider handling super admins in non-multisite environments

Here's a suggested improvement:

-		// If the user is a super admin, they can access the feature.
+		// Allow access for super administrators in multisite environments.
+		// Note: In non-multisite environments, super admin status is equivalent to regular admin.
 		if ( is_multisite() && is_super_admin( $current_user->ID ) ) {
 			return true;
 		}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 601414a and cb9ab02.

📒 Files selected for processing (1)
  • src/class-permissions.php (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
src/class-permissions.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
🔇 Additional comments (1)
src/class-permissions.php (1)

118-121: Implementation looks good, verify multisite behavior.

The super admin check is correctly implemented and placed in the right order within the validation chain.

Let's verify that this is the only place where super admin permissions need to be checked:

✅ Verification successful

Let me check the complete context of the permissions handling to ensure there are no other places where super admin permissions should be considered.


Super admin check implementation is correctly isolated and complete

After analyzing the complete permissions class, the super admin check is properly implemented in the only location where permission checks are performed (current_user_can_use_pch_feature). The implementation:

  • Correctly handles multisite environments
  • Is placed at the right position in the validation chain
  • Is the only place where such a check is needed as all feature-specific permissions flow through this method
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other potential places where super admin permissions might need to be handled
rg -l "is_super_admin|super admin|superadmin" --type php

Length of output: 84


Script:

#!/bin/bash
# Get the full content of class-permissions.php to analyze all permission checks
cat src/class-permissions.php

Length of output: 5976

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (1)
tests/Integration/PermissionsTest.php (1)

157-161: LGTM! Consider enhancing the test method's documentation.

The changes improve test clarity by explicitly setting the user role. Consider updating the method's documentation to reflect that it specifically tests editor access when only administrator role is allowed.

 /**
  * Verifies that permissions are correct when a disallowed User Role tries
  * to access enabled Content Helper features.
+ * Specifically tests editor access when only administrator role is allowed.
  *
  * @since 3.17.0
  */
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between cb9ab02 and de75afd.

📒 Files selected for processing (1)
  • tests/Integration/PermissionsTest.php (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
tests/Integration/PermissionsTest.php (1)

Pattern **/*.{html,php}: "Perform a detailed review of the provided code with following key aspects in mind:

  • Review the HTML and PHP code to ensure it is well-structured and adheres to best practices.
  • Ensure the code follows WordPress coding standards and is well-documented.
  • Confirm the code is secure and free from vulnerabilities.
  • Optimize the code for performance, removing any unnecessary elements.
  • Validate comments for accuracy, currency, and adherence to WordPress coding standards.
  • Ensure each line comment concludes with a period.
  • Verify code compatibility with the latest version of WordPress, avoiding deprecated functions or features."
🔇 Additional comments (2)
tests/Integration/PermissionsTest.php (2)

182-216: LGTM! Comprehensive test coverage for super admin access to disabled features.

The test method thoroughly verifies that super admins cannot access disabled PCH features, including proper filter handling and edge cases with unset options.


218-261: LGTM! Consider adding edge case test.

The test comprehensively verifies super admin access behavior in both multisite and non-multisite environments. Consider adding a test case where a super admin is also assigned to a specific site to verify the interaction between super admin privileges and site-specific roles.

Let's verify if there are any existing tests covering this scenario:

@acicovic
Copy link
Collaborator

Looking at the code, I have difficulty understanding how this disables features for Super Admins when a feature is disabled. Can you please elaborate?

Alternatively, a quick meeting to show me the behavior with and without the fix is fine, even maybe better.

Thank you! 🙂

@vaurdan vaurdan merged commit e7562bd into develop Nov 4, 2024
39 checks passed
@vaurdan vaurdan deleted the update/permissions-super-admin branch November 4, 2024 16:49
@acicovic acicovic changed the title Allow Super Admins to use any PCH features unless they are explicitly disabled Don't allow Super Admins to use disabled PCH features Nov 6, 2024
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.

2 participants