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

Access permissions 5: DRY permission caching #6881

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

lukasbestle
Copy link
Member

@lukasbestle lukasbestle commented Dec 23, 2024

🚨 Merge first

Description

Summary of changes

Move caching of access and list permissions to ModelPermissions

Reasoning

DRY code, especially now that we would add five more of these methods to the User, Site and Language classes

Additional context

We could cache all other permissions as well, but I've wrestled with unit tests for too long before Christmas. The write permissions would also benefit less from the caching as there is usually only one write operation per request (in contrast to potentially hundreds of list/access permission checks).

Another advantage of the separate canFromCache() method is that it makes explicit which checks come from cache and which are dynamic. If we ever use canFromCache() in a place that needs dynamic checking, we will immediately be able to tell because of the LogicException that is thrown then.

The new UserPermissions::cacheKey() is currently uncovered as the User class does not have access/list permissions at the moment. This will be fixed in the PR 7.

Changelog

Refactoring

  • Globally cache access and list permissions per permission category, model type and user role to reduce code duplication

Breaking changes

None

Docs

None

Ready?

  • In-code documentation (wherever needed)
  • Unit tests for fixed bug/feature
  • Tests and CI checks all pass

For review team

  • Add lab and/or sandbox examples (wherever helpful)
  • Add changes & docs to release notes draft in Notion

@lukasbestle lukasbestle added this to the 5.0.0-beta.2 milestone Dec 23, 2024
@lukasbestle lukasbestle requested a review from a team December 23, 2024 10:10
@lukasbestle lukasbestle self-assigned this Dec 23, 2024
@lukasbestle
Copy link
Member Author

lukasbestle commented Dec 24, 2024

I think this PR really does have a performance impact, see #6880 (comment). With these changes, we need to create a ModelPermissions instance for each model and not just for each combination of template and user role.

I wonder if there is a better way to DRY the logic without this performance regression. Or otherwise we should probably discard this and add the same caching logic to the new isAccessible()/isListable() methods for site/user/language in upcoming PR 7.

The performance impact should now be fixed with a different approach: I moved the static caching logic to a static method inside ModelPermissions. This effectively does the same as before, just in one place to avoid the code duplication.

@lukasbestle lukasbestle force-pushed the v5/feature/access-permissions-4 branch from 13ea6fc to 6758c26 Compare January 18, 2025 16:48
@lukasbestle lukasbestle force-pushed the v5/feature/access-permissions-5 branch from 9a8321b to 3ae8380 Compare January 18, 2025 16:55
@lukasbestle lukasbestle marked this pull request as draft January 18, 2025 17:11
@lukasbestle lukasbestle force-pushed the v5/feature/access-permissions-4 branch from 6758c26 to 5181783 Compare January 18, 2025 17:28
lukasbestle added a commit that referenced this pull request Jan 18, 2025
Will allow access from the new static `canFromCache()` method (PR 5 #6881)
@lukasbestle lukasbestle force-pushed the v5/feature/access-permissions-5 branch from 3ae8380 to 1800a69 Compare January 18, 2025 17:35
@lukasbestle lukasbestle marked this pull request as ready for review January 18, 2025 17:46
Base automatically changed from v5/feature/access-permissions-4 to v5/develop January 20, 2025 11:50
@bastianallgeier bastianallgeier merged commit e125c5b into v5/develop Jan 21, 2025
11 of 12 checks passed
@bastianallgeier bastianallgeier deleted the v5/feature/access-permissions-5 branch January 21, 2025 17:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants