From 912e2200db5a70c18ab9819124bbee50d6a1d81b Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Wed, 1 Sep 2021 14:51:50 +0200 Subject: [PATCH 1/2] require exportable attribute privacy_level on export --- castellum/execution/api.py | 5 ++++- castellum/execution/templates/execution/study_base.html | 5 +++-- castellum/execution/views.py | 5 ++++- castellum/studies/models.py | 5 +++++ 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/castellum/execution/api.py b/castellum/execution/api.py index b31d63788..225678cce 100644 --- a/castellum/execution/api.py +++ b/castellum/execution/api.py @@ -148,7 +148,10 @@ class APIAttributesView(StrongholdPublicMixin, StudyMixin, APIAuthMixin, View): get_object_or_404( Participation, subject=subject, study=self.study, status=Participation.INVITED ) - if not self.request.user.has_privacy_level(subject.privacy_level): + if not self.request.user.has_privacy_level(max( + subject.privacy_level, + self.study.get_exportable_attributes_max_privacy_level(), + )): raise PermissionDenied monitoring_logger.info('Attribute export: study {} subject {} by {}'.format( diff --git a/castellum/execution/templates/execution/study_base.html b/castellum/execution/templates/execution/study_base.html index 386b79909..4a32d3937 100644 --- a/castellum/execution/templates/execution/study_base.html +++ b/castellum/execution/templates/execution/study_base.html @@ -8,8 +8,9 @@

{{ study }}

- {% has_privacy_level study.get_invited_max_privacy_level user as has_privacy_level %} - {% if has_privacy_level %} + {% has_privacy_level study.get_invited_max_privacy_level user as has_subject_privacy_level %} + {% has_privacy_level study.get_exportable_attributes_max_privacy_level user as has_attribute_privacy_level %} + {% if has_subject_privacy_level and has_attribute_privacy_level %} {% trans 'Export recruitment attributes' %} {% endif %} {% if study.consent %} diff --git a/castellum/execution/views.py b/castellum/execution/views.py index b3e1e684f..44ba07f65 100644 --- a/castellum/execution/views.py +++ b/castellum/execution/views.py @@ -142,7 +142,10 @@ class ExportView(StudyMixin, PermissionRequiredMixin, DetailView): return response def get(self, request, **kwargs): - if not request.user.has_privacy_level(self.study.get_invited_max_privacy_level()): + if not request.user.has_privacy_level(max( + self.study.get_invited_max_privacy_level(), + self.study.get_exportable_attributes_max_privacy_level(), + )): raise PermissionDenied if 'domain' in self.request.GET: domain = get_object_or_404(self.study.domains.all(), key=self.request.GET['domain']) diff --git a/castellum/studies/models.py b/castellum/studies/models.py index 09387c8a8..28380b44a 100644 --- a/castellum/studies/models.py +++ b/castellum/studies/models.py @@ -281,6 +281,11 @@ class Study(models.Model): ) return result[key + '__max'] or 0 + def get_exportable_attributes_max_privacy_level(self): + key = 'privacy_level_read' + result = self.exportable_attributes.aggregate(models.Max(key)) + return result[key + '__max'] or 0 + @property def study_type(self): return StudyType.objects.filter(studysession__study=self).distinct() -- GitLab From c5fc2053165333bbcff28941c9b324784bb13a3c Mon Sep 17 00:00:00 2001 From: Tobias Bengfort Date: Wed, 1 Sep 2021 14:45:36 +0200 Subject: [PATCH 2/2] allow exportable_attributes of any privacy level --- .../0035_alter_study_exportable_attributes.py | 19 +++++++++++++++++++ castellum/studies/models.py | 1 - 2 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 castellum/studies/migrations/0035_alter_study_exportable_attributes.py diff --git a/castellum/studies/migrations/0035_alter_study_exportable_attributes.py b/castellum/studies/migrations/0035_alter_study_exportable_attributes.py new file mode 100644 index 000000000..a6d1638b5 --- /dev/null +++ b/castellum/studies/migrations/0035_alter_study_exportable_attributes.py @@ -0,0 +1,19 @@ +# Generated by Django 3.2.6 on 2021-09-01 12:45 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('recruitment', '0037_participation_excluded_by_cleanup'), + ('studies', '0034_study_name_unique'), + ] + + operations = [ + migrations.AlterField( + model_name='study', + name='exportable_attributes', + field=models.ManyToManyField(blank=True, related_name='_studies_study_exportable_attributes_+', to='recruitment.AttributeDescription', verbose_name='Exportable recruitment attributes'), + ), + ] diff --git a/castellum/studies/models.py b/castellum/studies/models.py index 28380b44a..f2ce526da 100644 --- a/castellum/studies/models.py +++ b/castellum/studies/models.py @@ -212,7 +212,6 @@ class Study(models.Model): verbose_name=_('Exportable recruitment attributes'), related_name='+', blank=True, - limit_choices_to={'privacy_level_read': 0}, ) mail_subject = models.CharField(_('E-mail subject'), max_length=254, blank=True) -- GitLab