-
-
Notifications
You must be signed in to change notification settings - Fork 825
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
Fix caching issues with Event permissions #12769
Conversation
Hey @mattwire, I can take a look at this later this week. |
I tested this lightly locally, I logged in as a user with the permission "CiviEvent: access CiviEvent" and could create but not edit an event. I got a permissions error immediately after creating the event: Expected behaviour as outlined in #12424 is that users with the permission "CiviEvent: access CiviEvent" can create events and then edit the events they create. I like the concept of the code @mattwire, thoughts? |
CRM/Event/BAO/Event.php
Outdated
*/ | ||
public static function checkPermission($eventId = NULL, $type = CRM_Core_Permission::VIEW) { | ||
if (empty($eventId)) { | ||
return self::getAllPermissions($type); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agree - ideally add a deprecation notice
CRM/Event/BAO/Event.php
Outdated
// Note: for a multisite setup, a user with edit all events, can edit all events | ||
// including those from other sites | ||
if (($type == CRM_Core_Permission::EDIT) && CRM_Core_Permission::check('edit all events')) { | ||
Civi::$statics[__CLASS__]['permission'][$eventId] = TRUE; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you need an action nuance in your keys? ie
Civi::$statics[CLASS]['permission']['edit'][$eventId] = TRUE;
Civi::$statics[CLASS]['permission']['view'][$eventId] = TRUE;
(set both on edit perm, only one on view )
aae6cec
to
f20dc79
Compare
@alifrumin Do you have time to test this again? I've refactored it since you found the original error but it could really do with some more tests as it was a very messy function before. |
Taking a look now |
I tested this by:
|
f20dc79
to
01e5965
Compare
@alifrumin Thanks. I just found the same thing :-( and realised how fragile it all is. So I've just written a pile of tests. |
@alifrumin And it looks like we may need to "improve" the descriptions of some of the event permissions once we've worked out how they are actually meant to work! |
e96aa1d
to
b263942
Compare
I worked through this & it looks pretty good -some thoughts
e.g
(you'd need to change the tests slightly for this assumption) NOTE - other than resolving the test issue my comments are non-blocking & it would be fine to leave those thoughts for later |
b263942
to
544571f
Compare
544571f
to
069f4ab
Compare
Deprecated warning added when we should be calling checkAllPermissions instead.
Yes, good idea. Done.
So do I, but for now it solves the main issue here, that caching for a single event was preventing caching from all from happening. And because of the difference in the way each of the caches are generated and the general confusion about what each permission actually does... this seemed most sensible for now. But I've added a code comment.
I think there are definitely further improvements but my head starts to spin if I improve it too much.
Yes. So I've updated the tests so that 'Delete in CiviEvent' is required. I can see someone submitting a PR with either a new permission 'Delete own events' or an adjustment to how the existing permissions work. @eileenmcnaughton @alifrumin Assuming tests pass are you both happy with this now from a "it's less broken than it was before" point of view? |
@mattwire style error |
069f4ab
to
d0853f7
Compare
I tested this and it looks good to me! There is an error that shows up on the events dashboard for a user with the permissions:
but it sounds like that might be on purpose. Error text:
|
Thanks @seamuslee001 - @mattwire @alifrumin We should try to fix those places where it calls the now-deprecated version of the function |
Overview
Follow up to #12424
Calling checkPermission() with an event ID and then with no ID (to get all permissions) only returns permission for the first event, meaning all subsequent events are returned as having no permission when they probably do.
Before
If
CRM_Event_BAO_Event::checkPermission()
is called with a single event ID the static cache is "populated" but actually only has a single event ID in. If the same function is called again with no ID the cache is returned rather than building an array of all events with permissions.After
Refactor
CRM_Event_BAO_Event::checkPermission()
into two separate functions as they return two different things - an array of permissions in the case of all events, and TRUE/FALSE in the case of a single event ID.A separate cache is implemented for all event permissions and for a single event permission.
So if you:
checkPermission()
with an event ID you will get a TRUE/FALSE response and that will be cached.checkPermission()
with no event ID you will be "redirected" togetAllPermissions()
which will build it's own cache and return an array of all event permissions.Technical Details
If
CRM_Event_BAO_Event::checkPermission()
is called with a single event ID the static cache is "populated" but actually only has a single event ID in. If the same function is called again with no ID the cache is returned rather than building an array of all events with permissions.Comments
@alifrumin Would you be able to review this one? It may need some further work on the existing unit tests as well if you have chance to take a look?