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

REF - Normalize internal format of permissions #29173

Merged
merged 3 commits into from
Jan 31, 2024

Conversation

colemanw
Copy link
Member

@colemanw colemanw commented Jan 31, 2024

Overview

Note for extension authors: Switching your extension's hook_civicrm_permissions to return the preferred format (with 'label' & 'description' array keys) will avoid deprecation notices added by this PR. It is safe to make that change without requiring the latest version of CiviCRM, as the preferred format is supported for every 5.x.x. version.

Historically, there have been 3 formats for defining a permission. This consolidates the format internally, while maintaining backward-support for the other two.
Includes a documentation update: https://lab.civicrm.org/documentation/docs/dev/-/merge_requests/1148

Before

Permissions added via hook might be in associative or non-associative array format, or they might be a plain string.

After

Internally they are now always associative regardless of how they are passed in from hook_civicrm_permission().
Deprecation notice issued for extensions that pass them in the old ways.

Technical Details

Deprecation notice added to alert extension devs to update the format.

This also improves efficiency by only calling the hook once, and improves memory use by caching the array once.

Before: Permissions might be in associative or non-associative array format.
After: Always associative regardless of how they are passed in from hook_civicrm_permission().

This also improves efficiency by only calling the hook once,
and improves memory use by caching the array once.
Copy link

civibot bot commented Jan 31, 2024

🤖 Thank you for contributing to CiviCRM! ❤️ We will need to test and review this PR. 👷

Introduction for new contributors...
  • If this is your first PR, an admin will greenlight automated testing with the command ok to test or add to whitelist.
  • A series of tests will automatically run. You can see the results at the bottom of this page (if there are any problems, it will include a link to see what went wrong).
  • A demo site will be built where anyone can try out a version of CiviCRM that includes your changes.
  • If this process needs to be repeated, an admin will issue the command test this please to rerun tests and build a new demo site.
  • Before this PR can be merged, it needs to be reviewed. Please keep in mind that reviewers are volunteers, and their response time can vary from a few hours to a few weeks depending on their availability and their knowledge of this particular part of CiviCRM.
  • A great way to speed up this process is to "trade reviews" with someone - find an open PR that you feel able to review, and leave a comment like "I'm reviewing this now, could you please review mine?" (include a link to yours). You don't have to wait for a response to get started (and you don't have to stop at one!) the more you review, the faster this process goes for everyone 😄
  • To ensure that you are credited properly in the final release notes, please add yourself to contributor-key.yml
  • For more information about contributing, see CONTRIBUTING.md.
Quick links for reviewers...

➡️ Online demo of this PR 🔗

@civibot civibot bot added the master label Jan 31, 2024
@eileenmcnaughton
Copy link
Contributor

Cool - should we start adding deprecation notices for the oldest of the 3 formats?

@eileenmcnaughton eileenmcnaughton merged commit 64cd6ea into civicrm:master Jan 31, 2024
3 checks passed
@eileenmcnaughton eileenmcnaughton deleted the impliedPermission branch January 31, 2024 18:43
@colemanw
Copy link
Member Author

@eileenmcnaughton we just did! This PR adds noisy deprecation for both old formats & you merged it so now it's official :)

@eileenmcnaughton
Copy link
Contributor

@colemanw oh right - that makes me a bit nervous - if you have an extension can you already change the keys from 0,1 to 'label'? No older versions access them from the numbers? Cos if they do people can't update their extensions ?

@colemanw
Copy link
Member Author

@eileenmcnaughton The associative format has been supported for a long time... at least since
755a183#diff-410497db8c86f6ba1a897c533db8f391a25d2cee7b12852bb743b2dbed82d86aR905-R911 if not before.

Because the previous code never actually cared about the array keys it would just shift the first item off the array and take it as the label, then shift the second item as the description.

colemanw added a commit to colemanw/civicrm-drupal that referenced this pull request Feb 7, 2024
colemanw added a commit to colemanw/civicrm-backdrop that referenced this pull request Feb 7, 2024
colemanw added a commit to colemanw/civicrm-core that referenced this pull request Feb 7, 2024
totten added a commit to totten/civicrm-core that referenced this pull request Feb 7, 2024
There was a recent patch as part of civicrm#29173, so the version-number should be raised.
@@ -402,6 +402,9 @@ public function getAllModulePermissions(): array {
// Convert them all to associative arrays.
foreach ($permissions as $name => $defn) {
$defn = (array) $defn;
if (!isset($defn['label'])) {
CRM_Core_Error::deprecatedWarning("Permission '$name' should be declared with 'label' and 'description' keys. See https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_permission/");
Copy link
Member

Choose a reason for hiding this comment

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

Cool :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants