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

Move module sharing settings capability filtering to REST controller #5259

Merged
merged 9 commits into from
May 26, 2022

Conversation

aaemnnosttv
Copy link
Collaborator

@aaemnnosttv aaemnnosttv commented May 24, 2022

Summary

Addresses issue:

Relevant technical choices

PR Author Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 4.7 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have added a QA Brief on the issue linked above.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

Do not alter or remove anything below. The following sections will be managed by moderators only.

Code Reviewer Checklist

  • Run the code.
  • Ensure the acceptance criteria are satisfied.
  • Reassess the implementation with the IB.
  • Ensure no unrelated changes are included.
  • Ensure CI checks pass.
  • Check Storybook where applicable.
  • Ensure there is a QA Brief.

Merge Reviewer Checklist

  • Ensure the PR has the correct target branch.
  • Double-check that the PR is okay to be merged.
  • Ensure the corresponding issue has a ZenHub release assigned.
  • Add a changelog message to the issue.


return $this->set( $settings );
return $this->set( array_merge( $settings, $updated ) );
Copy link
Collaborator

Choose a reason for hiding this comment

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

We can't just merge the original settings with updated ones. The problem here is that updated settings may not contain certain subkeys if the user doesn't have some of required capabilities which will lead to a removal of missing subkeys in the updated settings from the original settings.

For example, let's assume we get the following updated settings:

$updated = array(
    'moduleA' => array(
        'management' => 'owner',
    )
)

As you can see there, the moduleA lacks the sharedRoles property which has been unset in the Collection_Key_Cap_Filter class because the user doesn't have the MANAGE_MODULE_SHARING_OPTIONS capability.

Now if we have the following settings, then sharedRoles will be removed after merging both arrays.

$settings = array(
    'moduleA' => array(
        'management' => 'all_admins',
        'sharedRoles' => array( 'role1', 'role2' ),
    )
)

We need to preserve the current value of the sharedRoles property for the moduleA.

@kuasha420 kuasha420 marked this pull request as ready for review May 26, 2022 07:23
Copy link
Collaborator

@techanvil techanvil left a comment

Choose a reason for hiding this comment

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

LGTM, nice one @aaemnnosttv & @kuasha420 !

*
* @return array Merged array.
*/
private function array_merge_deep( $array1, $array2, $depth = 1 ) {
Copy link
Collaborator

@techanvil techanvil May 26, 2022

Choose a reason for hiding this comment

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

Might be nice to extract this to a shared utility method, this can wait though...

Copy link
Contributor

Choose a reason for hiding this comment

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

I remember having a similar discussion previously and colocating it in the consuming class was the consensus unless the util is being used in multiple place. So I followed that. We should definitely relocate it to a shared util class like the BC_Functions one if/when this gets used elsewhere.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thanks for pointing that out @kuasha420, it's a fair comment!

Also - apologies, as I didn't spot that you had worked on this too. I saw Evan had created the PR so gave him the shout out on my approval message, but hadn't spotted your commits so neglected to tag you too. It's a small thing, but I am glad to have now amended! :)

@techanvil techanvil merged commit 2b5abd4 into develop May 26, 2022
@techanvil techanvil deleted the enhance/5229-module-sharing-settings-cap-checks branch May 26, 2022 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants