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

Use url parameter in repository access condition #1800

Merged
merged 1 commit into from
Jan 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES/9670.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Added ``has_repository_obj_perms`` and ``has_repository_model_or_obj_perms`` as access conditions
that can be used by viewsets nested beneath repository viewsets.
66 changes: 64 additions & 2 deletions pulpcore/app/global_access_conditions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pulpcore.app.models import Group
from pulpcore.app.models import Group, Repository


# Model checks
Expand Down Expand Up @@ -190,7 +190,7 @@ def has_remote_param_model_or_obj_perms(request, view, action, permission):
return False


# 'Repository' attribute checks
# 'Repository' attribute checks for RepositoryVersionViewSet


def has_repo_attr_obj_perms(request, view, action, permission):
Expand Down Expand Up @@ -280,6 +280,68 @@ def has_repo_attr_model_or_obj_perms(request, view, action, permission):
return False


def has_repository_obj_perms(request, view, action, permission):
Copy link
Member

Choose a reason for hiding this comment

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

We don't have to import these through the plugin API do we?

Copy link
Member

Choose a reason for hiding this comment

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

@mdellweg if ^ is correct go ahead and merge plz.

Copy link
Member Author

Choose a reason for hiding this comment

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

no

"""
Checks whether a user has the requested object permission on the repository in the URL.

This check is meant to be used for relations nested beneath a repository endpoint, e.g. the
list of repository versions belonging to that repository. It will fail for other endpoints.

::

{
...
"condition": "has_repository_obj_perms:file.filerepository_delete",
},

Args:
request (rest_framework.request.Request): The request being made.
view (subclass rest_framework.viewsets.GenericViewSet): The view being checked for
authorization.
action (str): The action being performed, e.g. "destroy".
permission (str): The name of the Repository Permission to be checked. In the form
`app_label.codename`, e.g. "file.filerepository_change".

Returns:
True if the user has the Permission on the ``Repository`` specified in the URL named by the
``permission`` at object level. False otherwise.
"""
plugin_repository = Repository.objects.get(pk=view.kwargs["repository_pk"]).cast()
return request.user.has_perm(permission, plugin_repository)


def has_repository_model_or_obj_perms(request, view, action, permission):
"""
Checks whether a user has the requested model or object permission on the repository in the
URL.

This check is meant to be used for relations nested beneath a repository endpoint, e.g. the
list of repository versions belonging to that repository. It will fail for other endpoints.

::

{
...
"condition": "has_repository_model_or_obj_perms:file.filerepository_delete",
},

Args:
request (rest_framework.request.Request): The request being made.
view (subclass rest_framework.viewsets.GenericViewSet): The view being checked for
authorization.
action (str): The action being performed, e.g. "destroy".
permission (str): The name of the Repository Permission to be checked. In the form
`app_label.codename`, e.g. "file.filerepository_change".

Returns:
True if the user has the Permission on the ``Repository`` specified in the URL named by the
``permission`` at model or object level. False otherwise.
"""
return request.user.has_perm(permission) or has_repository_obj_perms(
request, view, action, permission
)


# `Group` permission checks


Expand Down