-
Notifications
You must be signed in to change notification settings - Fork 29
Refactor PackageListing ensure_x functions (TS-2580) #1172
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
Open
Roffenlund
wants to merge
5
commits into
master
Choose a base branch
from
refactor-package-listing-ensure-x-functions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1adf244
Implement check_user_permissions and result class
Roffenlund 4cfcec3
Refactor ensure_x functions in PackageListing
Roffenlund c600716
Update update_categories services
Roffenlund a889504
Update PermissionsChecker
Roffenlund b75856c
Add agent parameter to update_categories
Roffenlund File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from typing import TYPE_CHECKING, List, Optional | ||
from typing import TYPE_CHECKING, List, Optional, Tuple | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ValidationError | ||
|
@@ -11,14 +11,12 @@ | |
from thunderstore.cache.enums import CacheBustCondition | ||
from thunderstore.cache.tasks import invalidate_cache_on_commit_async | ||
from thunderstore.community.consts import PackageListingReviewStatus | ||
from thunderstore.core.exceptions import PermissionValidationError | ||
from thunderstore.core.mixins import AdminLinkMixin, TimestampMixin | ||
from thunderstore.core.types import UserType | ||
from thunderstore.core.utils import check_validity | ||
from thunderstore.frontend.url_reverse import get_community_url_reverse_args | ||
from thunderstore.permissions.mixins import VisibilityMixin | ||
from thunderstore.permissions.models.visibility import VisibilityFlagsQuerySet | ||
from thunderstore.permissions.utils import validate_user | ||
from thunderstore.permissions.utils import PermissionResult, check_user_permissions | ||
from thunderstore.webhooks.audit import ( | ||
AuditAction, | ||
AuditEvent, | ||
|
@@ -321,7 +319,9 @@ def is_rejected(self): | |
return self.review_status == PackageListingReviewStatus.rejected | ||
|
||
def update_categories(self, agent: UserType, categories: List["PackageCategory"]): | ||
self.ensure_update_categories_permission(agent) | ||
if not self.can_update_categories_permission(agent): | ||
raise PermissionError() | ||
|
||
Roffenlund marked this conversation as resolved.
Show resolved
Hide resolved
|
||
for category in categories: | ||
if category.community_id != self.community_id: | ||
raise ValidationError( | ||
|
@@ -336,33 +336,51 @@ def is_unavailable(self): | |
def can_be_moderated_by_user(self, user: Optional[UserType]) -> bool: | ||
return self.community.can_user_manage_packages(user) | ||
|
||
def ensure_user_can_manage_listing(self, user: Optional[UserType]) -> None: | ||
user = validate_user(user) | ||
def validate_user_can_manage_listing( | ||
self, user: Optional[UserType] | ||
) -> PermissionResult: | ||
|
||
result = check_user_permissions(user) | ||
if not result.is_valid: | ||
return result | ||
|
||
is_allowed = self.can_be_moderated_by_user( | ||
user | ||
) or self.package.owner.can_user_manage_packages(user) | ||
|
||
if not is_allowed: | ||
raise PermissionValidationError("Must have listing management permission") | ||
return PermissionResult(error="Must have listing management permission") | ||
|
||
return PermissionResult() | ||
|
||
def validate_update_categories_permissions( | ||
self, user: Optional[UserType] | ||
) -> PermissionResult: | ||
|
||
result: PermissionResult = check_user_permissions(user) | ||
|
||
def ensure_update_categories_permission(self, user: Optional[UserType]) -> None: | ||
user = validate_user(user) | ||
is_allowed = ( | ||
if not result.is_valid: | ||
return result | ||
|
||
if not ( | ||
self.can_be_moderated_by_user(user) | ||
or self.package.owner.can_user_manage_packages(user) | ||
or self.community.can_user_manage_categories(user) | ||
) | ||
if not is_allowed: | ||
raise PermissionValidationError( | ||
"User is missing necessary roles or permissions" | ||
) | ||
): | ||
error = "User is missing necessary roles or permissions" | ||
return PermissionResult(error=error) | ||
|
||
return PermissionResult() | ||
|
||
def check_update_categories_permission(self, user: Optional[UserType]) -> bool: | ||
return check_validity(lambda: self.ensure_update_categories_permission(user)) | ||
def can_update_categories_permission(self, user: Optional[UserType]) -> bool: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be called |
||
return self.validate_update_categories_permissions(user).is_valid | ||
|
||
def can_user_manage_approval_status(self, user: Optional[UserType]) -> bool: | ||
return self.can_be_moderated_by_user(user) | ||
|
||
def ensure_can_be_viewed_by_user(self, user: Optional[UserType]) -> None: | ||
def validate_can_be_viewed_by_user( | ||
self, user: Optional[UserType] | ||
) -> PermissionResult: | ||
def get_has_perms() -> bool: | ||
return ( | ||
user is not None | ||
|
@@ -378,16 +396,18 @@ def get_has_perms() -> bool: | |
self.review_status != PackageListingReviewStatus.approved | ||
and not get_has_perms() | ||
): | ||
raise PermissionValidationError("Insufficient permissions to view") | ||
return PermissionResult(error="Insufficient permissions to view") | ||
else: | ||
if ( | ||
self.review_status == PackageListingReviewStatus.rejected | ||
and not get_has_perms() | ||
): | ||
raise PermissionValidationError("Insufficient permissions to view") | ||
return PermissionResult(error="Insufficient permissions to view") | ||
|
||
return PermissionResult() | ||
|
||
def can_be_viewed_by_user(self, user: Optional[UserType]) -> bool: | ||
return check_validity(lambda: self.ensure_can_be_viewed_by_user(user)) | ||
return self.validate_can_be_viewed_by_user(user).is_valid | ||
|
||
def is_visible_to_user(self, user: Optional[UserType]) -> bool: | ||
if not self.visibility: | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.