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

Fix #572 Import parent permissions #585

Merged
merged 4 commits into from
Feb 23, 2023

Conversation

m6121
Copy link
Member

@m6121 m6121 commented Feb 16, 2023

This PR aims at fixing issue #572

@m6121 m6121 added this to the 1.9.2 milestone Feb 16, 2023
@m6121 m6121 self-assigned this Feb 16, 2023
@coveralls
Copy link

coveralls commented Feb 16, 2023

Coverage Status

Coverage: 90.469% (+0.04%) from 90.433% when pulling a01ecfd on dev-1.9.2/572-import-parent-permissions into dd0a232 on dev.

@@ -126,14 +126,16 @@ def get_context_data(self, **kwargs):
.filter_catalog(self.object.catalog) \
.filter_group(self.request.user) \
.filter_availability(self.request.user).exists()
context['ancestors_import'] = ancestors.filter_user(user=self.request.user) \
Copy link
Member

Choose a reason for hiding this comment

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

I've changed the query to filter_user instead of filter.
I thought that sometimes the projects that should be in the import were filtered out by filter?

Copy link
Member

Choose a reason for hiding this comment

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

filter_user takes admins and site_managers into account, so this makes sense here.

Copy link
Member

Choose a reason for hiding this comment

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

On the second thought, I don't think this aproach is correct, since it will not account for "role inheritance". If you are the owner of a parent project, you are automatically owner of all descendants. I think it would be better to use the rules in the templates to filter the projects.

Copy link
Member

Choose a reason for hiding this comment

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

Alternatively we can use something like:

        queryset = Project.objects.filter(user=self.request.user)
        for instance in queryset:
            queryset |= instance.get_descendants()
        queryset = queryset.distinct()

which is the query for /projects/.

{{ node.title }}
{% endif %}
</a>
{% if node.id in user_project_family_ids %}
Copy link
Member

Choose a reason for hiding this comment

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

with this condition, only the projects that a user has permission for are also clickable in the hierarchy tree.

Thought it was closely related to this PR, but it can also be left out ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice extension. Looks good to me.

Copy link
Member

Choose a reason for hiding this comment

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

I think

{% has_perm 'projects.view_project_object' request.user project as can_view_project %}
{% if can_view_project %}

would be better.

@m6121 m6121 linked an issue Feb 21, 2023 that may be closed by this pull request
@@ -126,14 +126,16 @@ def get_context_data(self, **kwargs):
.filter_catalog(self.object.catalog) \
.filter_group(self.request.user) \
.filter_availability(self.request.user).exists()
context['ancestors_import'] = ancestors.filter_user(user=self.request.user) \
Copy link
Member

Choose a reason for hiding this comment

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

On the second thought, I don't think this aproach is correct, since it will not account for "role inheritance". If you are the owner of a parent project, you are automatically owner of all descendants. I think it would be better to use the rules in the templates to filter the projects.

{{ node.title }}
{% endif %}
</a>
{% if node.id in user_project_family_ids %}
Copy link
Member

Choose a reason for hiding this comment

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

I think

{% has_perm 'projects.view_project_object' request.user project as can_view_project %}
{% if can_view_project %}

would be better.

@@ -6,7 +6,7 @@
<input type="hidden" name="method" value="import_project">

<select class="form-control" name="source">
{% for project in project.get_ancestors %}
{% for project in ancestors_import %}
Copy link
Member

Choose a reason for hiding this comment

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

{% has_perm 'projects.view_project_object' request.user project as can_view_project %}
{% if can_view_project %}

same as above. We probably need to add some prefetch_related/select_related to the database query.

@@ -126,14 +126,16 @@ def get_context_data(self, **kwargs):
.filter_catalog(self.object.catalog) \
.filter_group(self.request.user) \
.filter_availability(self.request.user).exists()
context['ancestors_import'] = ancestors.filter_user(user=self.request.user) \
Copy link
Member

Choose a reason for hiding this comment

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

Alternatively we can use something like:

        queryset = Project.objects.filter(user=self.request.user)
        for instance in queryset:
            queryset |= instance.get_descendants()
        queryset = queryset.distinct()

which is the query for /projects/.

@m6121
Copy link
Member Author

m6121 commented Feb 22, 2023

Hi @MyPyDavid and @jochenklar, thanks for your reviews and suggestions. I tried to address them.

  • For the Project hierarchy, the template solution works well.
  • However, for the import we need to know if at least one project exists that can be imported in order to display respectively hide the import selection field.

Towards the use of prefetch_related/select_related I don't have an idea how to integrate this into the logic at the moment.

@m6121
Copy link
Member Author

m6121 commented Feb 22, 2023

Would it be a performance boost if the prefetch related is used as follows?

ancestors_import = []
for instance in ancestors.exclude(id=project.id).prefetch_related('user'):
    if self.request.user.has_perm('projects.view_project_object', instance):
        ancestors_import.append(instance)

@jochenklar jochenklar merged commit 77b1f79 into dev Feb 23, 2023
@jochenklar jochenklar deleted the dev-1.9.2/572-import-parent-permissions branch February 23, 2023 13:08
CalamityC pushed a commit to CalamityC/rdmo that referenced this pull request Nov 23, 2023
…mport-parent-permissions

Fix rdmorganiser#572 Import parent permissions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Import from parent project list doesn't check permissions
4 participants