Skip to content

Commit

Permalink
🐛 Fix analysis query permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
dankolbman committed Aug 12, 2020
1 parent b2855bf commit 602c0f3
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 16 deletions.
17 changes: 17 additions & 0 deletions creator/analyses/migrations/0003_fix_permission_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 2.2.13 on 2020-08-12 14:53

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('analyses', '0002_analysis_error_message'),
]

operations = [
migrations.AlterModelOptions(
name='analysis',
options={'permissions': [('list_all_analysis', 'Can list all analyses'), ('view_my_study_analysis', 'Can view all analyses in studies user is a member of'), ('add_my_study_analysis', 'Can add analyses to studies the user is a member of'), ('change_my_study_analysis', 'Can change analyses in studies the user is a member of')]},
),
]
2 changes: 1 addition & 1 deletion creator/analyses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Meta:
permissions = [
("list_all_analysis", "Can list all analyses"),
(
"view_my_analysis",
"view_my_study_analysis",
"Can view all analyses in studies user is a member of",
),
(
Expand Down
12 changes: 7 additions & 5 deletions creator/analyses/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def get_node(cls, info, id):
# belongs to one of their studies
if user.has_perm("analyses.view_analysis") or (
user.has_perm("analyses.view_my_study_analysis")
and analysis.study
and user.studies.filter(kf_id=analysis.study.kf_id).exists()
and user.studies.filter(
kf_id=analysis.version.root_file.study.kf_id
).exists()
):
return analysis

Expand All @@ -46,7 +47,7 @@ def get_node(cls, info, id):

class AnalysisFilter(django_filters.FilterSet):
file_kf_id = django_filters.CharFilter(
field_name="root_file__kf_id", lookup_expr="exact"
field_name="version__root_file__kf_id", lookup_expr="exact"
)

class Meta:
Expand All @@ -69,7 +70,8 @@ class Query(object):
def resolve_all_analyses(self, info, **kwargs):
"""
Return all analyses if user has view_analysis
Return only analyses in user's studies if user has view_my_analysis
Return only analyses in user's studies if user has the
view_my_study_analysis permission
Return not allowed otherwise
"""
user = info.context.user
Expand All @@ -85,7 +87,7 @@ def resolve_all_analyses(self, info, **kwargs):

if user.has_perm("analyses.view_my_study_analysis"):
return Analysis.objects.filter(
root_file__study__in=user.studies.all()
version__root_file__study__in=user.studies.all()
).all()

raise GraphQLError("Not allowed")
12 changes: 10 additions & 2 deletions creator/groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"view_study",
"view_downloadtoken",
"add_downloadtoken",
"view_analysis",
"delete_downloadtoken",
"view_file",
"view_version",
Expand All @@ -69,7 +70,7 @@
"view_my_file",
"add_my_study_file",
"change_my_study_file",
"view_analysis",
"view_my_study_analysis",
"add_my_study_analysis",
"add_my_study_version",
"add_downloadtoken",
Expand All @@ -82,6 +83,7 @@
"view_study",
"view_file",
"view_version",
"view_analysis",
"add_downloadtoken",
"view_event",
"view_project",
Expand All @@ -92,5 +94,11 @@
"unlink_project",
"import_volume",
],
"Services": ["view_study", "add_file", "view_file", "view_version"],
"Services": [
"view_study",
"add_file",
"view_file",
"view_analysis",
"view_version",
],
}
16 changes: 8 additions & 8 deletions tests/analyses/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ def analysis(db):
"user_group,allowed",
[
("Administrators", True),
("Services", False),
("Developers", False),
("Services", True),
("Developers", True),
("Investigators", False),
("Bioinformatics", False),
("Bioinformatics", True),
(None, False),
],
)
Expand All @@ -71,10 +71,10 @@ def test_query_analysis(db, clients, analysis, user_group, allowed):
"user_group,allowed",
[
("Administrators", True),
("Services", False),
("Developers", False),
("Investigators", False),
("Bioinformatics", False),
("Services", True),
("Developers", True),
("Investigators", True),
("Bioinformatics", True),
],
)
def test_query_my_analysis(db, clients, analysis, user_group, allowed):
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_query_my_analysis(db, clients, analysis, user_group, allowed):
("Administrators", True, 1),
("Services", False, 0),
("Developers", False, 0),
("Investigators", False, 0),
("Investigators", True, 1),
("Bioinformatics", False, 0),
(None, False, 0),
],
Expand Down

0 comments on commit 602c0f3

Please sign in to comment.