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

CRM-21798 updates CRM_Event_BAO_Event::checkPermission so it can be called repeatedly to check permissions on a list of even #11716

Closed
wants to merge 1 commit into from

Conversation

spalmstr
Copy link
Contributor

@spalmstr spalmstr commented Feb 23, 2018

Removed setting the ID as a parameter before getting the list of events as this meant we only got the first event for which to check permissions.

Overview

This pull request updates CRM_Event_BAO_Event::checkPermission so it can be called repeatedly to check permissions on a list of event.

Before

When CRM_Event_BAO_Event::checkPermission() is called repeatedly, it only checks permissions for the first event. All other events are set to have no permissions.
After

When CRM_Event_BAO_Event::checkPermission() is called repeatedly, it checks the permissions for each event and returns them.

Technical Details

if ($eventId) {
$params['id'] = $eventId;
}
in lines 2078ff before getting the result meant that the function only remembered the permissions of the first event checked. Deleting this statement means that all relevant events are checked. The permission on a specific event is checked later on.

Comments

Anything else you would like the reviewer to note


Removed setting the ID as a parameter as this meant we only got the first event for which to check permissions.
@eileenmcnaughton
Copy link
Contributor

I think the issue is the caching in the function is a bit of a mess. It needs to cache it in a way that allows for the possibility of some events being populated but not others. In general we are moving to Civi::$statics so my suggestion would be to separate the getById to it's own function - the below is NOT a tested functional version - just a quick-write. We could then deprecate passing $eventId into the other function & set this static from the other function as well so we don't lose performance

  /**
   * Make sure that the user has permission to access this event.
   *
   * @param int $eventId
   * @param int $type
   *
   * @return string
   *   the permission that the user has (or null)
   */
  public static function checkPermissionById($eventId = NULL, $type = CRM_Core_Permission::VIEW) {
    if ($eventId && !isset(Civi::$statics[__CLASS__]['permissions'][$eventId])) {
      $permissions[CRM_Core_Permission::VIEW] = civicrm_api3('Event', 'getcount', [
        'check_permissions' => 1,
        'return' => 'id',
      ]);
      if (CRM_Core_Permission::check('edit all events')) {
        $permissions[CRM_Core_Permission::EDIT] = TRUE;
      }
      else {
        $permittedEvents = CRM_ACL_API::group(CRM_Core_Permission::EDIT, NULL, 'civicrm_event', [$eventId => 1], NULL);      
        if ($permittedEvents[$eventId]) {
          $permissions[CRM_Core_Permission::EDIT] = TRUE;
        }
        else {
          $permissions[CRM_Core_Permission::EDIT] = FALSE;
        }
      }
      Civi::$statics[__CLASS__]['permissions'][$eventId] = $permissions;
    }
    return Civi::$statics[__CLASS__]['permissions'][$eventId];
  }

@spalmstr
Copy link
Contributor Author

spalmstr commented Mar 5, 2018

Do you mean checkPermissionById as a separate function? Is anyone else working on caching in general?

I agree my workaround was a quick fix to cure the error. There may well be a better approach than the one I took.

@eileenmcnaughton
Copy link
Contributor

yes - I'm proposing it as a different function - in general the CiviCRM codebase does an awful lot of overloading functions to do completely different things in the same function & identifying those cases & splitting the functions out is a good approach.

Regarding Caching in general - the current approach is to use Civi::$statics which is very similar to a drupal pattern. The main advantage of that over an in-function static variable is that it is flushed between test runs, saving much painful debugging. I think in this sort of case php caching is fine

@eileenmcnaughton
Copy link
Contributor

@mlutfy this is actually arising from a patch you did for CRM-21393

@mlutfy
Copy link
Member

mlutfy commented May 16, 2018

hmm indeed, so it would revert the fix, re-introducing the performance problem?

Just to get a better overview, in which scenarios does this issue occur? i.e. as a user/admin, how do I reproduce the bug?

@eileenmcnaughton
Copy link
Contributor

@mlutfy it's probably reproducable in a unit test - although the static cache would need to be converted to a Civi::statics cache.

@eileenmcnaughton
Copy link
Contributor

@mlutfy were you going to look at this? I think it's should be replicable in a unit test by calling the function more than once in a row - otherwise we maybe should revert the performance fix for now?

@eileenmcnaughton eileenmcnaughton changed the title CRM-21798 CRM-21798 updates CRM_Event_BAO_Event::checkPermission so it can be called repeatedly to check permissions on a list of even Jun 12, 2018
@mlutfy
Copy link
Member

mlutfy commented Jun 12, 2018

@eileenmcnaughton We don't have (user) steps to reproduce this issue, so I find this a bit annoying.

@eileenmcnaughton
Copy link
Contributor

@mlutfy you should be able to reproduce in a test - ie. call that function twice with different event ids & make sure the result is correct

@eileenmcnaughton
Copy link
Contributor

#12424 may relate

@mattwire
Copy link
Contributor

So this PR has been superseded by #12424 and #12769 and should be closed. @spalmstr Could you review/test #12769 for me please?

@eileenmcnaughton
Copy link
Contributor

Closing as superceded per @mattwire

@spalmstr spalmstr deleted the CRM-21798 branch March 9, 2022 17:57
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.

5 participants