-
Notifications
You must be signed in to change notification settings - Fork 9
[OSDEV-2066] Enable POST/PATCH fields permission by attribute #737
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
Merged
roman-stolar
merged 23 commits into
main
from
OSDEV-2066-enable-patch-permission-by-attribute
Sep 15, 2025
Merged
Changes from 13 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
113bcf2
Migration to create PartnerField model and add partner_fields ManyToM…
roman-stolar 382b3cf
fix
roman-stolar d62745c
Create Permission Processor to validate contribution of Partner Fields
roman-stolar f4ffcf4
Merge branch 'main' into OSDEV-2066-enable-patch-permission-by-attribute
roman-stolar b1e8865
fix linter
roman-stolar c8769e7
updated release notes
roman-stolar ec24ecd
Merge commit 'e6bc42029b97ef1afca32d2f03fc44c1e2920d11' into OSDEV-20…
roman-stolar adfe0f7
added unit tests for contributor partner_field validation
roman-stolar 6e2d004
fix linter
roman-stolar b3f3777
fix
roman-stolar f7330c2
Merge branch 'main' into OSDEV-2066-enable-patch-permission-by-attribute
roman-stolar 45c860f
fix
roman-stolar c60cd34
clean up
roman-stolar 35c6c59
updated release notes
roman-stolar 437fe2d
remove useless import
roman-stolar ad3969f
address comments
roman-stolar 01a59e8
Merge commit '9c546dc5fbb0ad6ff40c893559c432c82549eeaf' into OSDEV-20…
roman-stolar dd8149a
addressed comments
roman-stolar 51fe538
updated release notes
roman-stolar cdc2ae9
fix
roman-stolar ead5954
addressed comments
roman-stolar 9e21098
fix
roman-stolar 0c65b6c
fix
roman-stolar 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
37 changes: 37 additions & 0 deletions
37
src/django/api/migrations/0177_add_partner_fields_to_contributor.py
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 |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from django.db import migrations, models | ||
| import uuid | ||
|
|
||
| class Migration(migrations.Migration): | ||
| """ | ||
| Migration to create PartnerField model and add partner_fields ManyToManyField to Contributor model. | ||
| """ | ||
|
|
||
| dependencies = [ | ||
| ('api', '0176_introduce_enable_dromo_uploading_switch'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='PartnerField', | ||
| fields=[ | ||
| ('name', models.CharField(help_text='The partner field name.', max_length=200, primary_key=True, serialize=False)), | ||
| ('uuid', models.UUIDField(default=uuid.uuid4, editable=False, help_text='Unique identifier for the partner field.', unique=True)), | ||
| ('type', models.CharField(help_text='The partner field type.', max_length=200, blank=False, null=False, choices=[('int','int'),('float','float'),('string','string'),('object','object')])), | ||
| ('created_at', models.DateTimeField(auto_now_add=True)), | ||
| ('updated_at', models.DateTimeField(auto_now=True)), | ||
| ], | ||
| options={ | ||
| 'verbose_name_plural': 'partner field', | ||
| }, | ||
| ), | ||
|
|
||
| migrations.AddField( | ||
| model_name='contributor', | ||
| name='partner_fields', | ||
| field=models.ManyToManyField( | ||
| blank=True, | ||
| help_text='Partner fields that this contributor can access', | ||
| to='api.partnerfield' | ||
| ), | ||
| ), | ||
| ] |
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
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 |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import uuid | ||
| from django.db import models | ||
|
|
||
|
|
||
| class PartnerField(models.Model): | ||
| """ | ||
| Partner Field that will be protected for contribution. | ||
| """ | ||
| INT = 'int' | ||
| FLOAT = 'float' | ||
| STRING = 'string' | ||
| OBJECT = 'object' | ||
|
|
||
| TYPE_CHOICES = ( | ||
| (INT, INT), | ||
| (FLOAT, FLOAT), | ||
| (STRING, STRING), | ||
| (OBJECT, OBJECT) | ||
| ) | ||
roman-stolar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| class Meta: | ||
| verbose_name_plural = "partner field" | ||
|
|
||
| uuid = models.UUIDField( | ||
vlad-shapik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| null=False, | ||
| default=uuid.uuid4, | ||
| unique=True, | ||
| editable=False, | ||
| help_text='Unique identifier for the partner field.' | ||
| ) | ||
| name = models.CharField( | ||
| max_length=200, | ||
| primary_key=True, | ||
| help_text=('The partner field name.')) | ||
| type = models.CharField( | ||
| max_length=200, | ||
| null=False, | ||
| blank=False, | ||
| choices=TYPE_CHOICES, | ||
| help_text=('The partner field type.')) | ||
|
|
||
| created_at = models.DateTimeField(auto_now_add=True) | ||
| updated_at = models.DateTimeField(auto_now=True) | ||
|
|
||
| def __str__(self): | ||
| return self.name | ||
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
67 changes: 67 additions & 0 deletions
67
...oderation_event_actions/creation/location_contribution/processors/permission_processor.py
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 |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from typing import Dict, List | ||
|
|
||
| from rest_framework import status | ||
|
|
||
| from api.moderation_event_actions.creation.location_contribution \ | ||
| .processors.contribution_processor import ContributionProcessor | ||
| from api.moderation_event_actions.creation.dtos.create_moderation_event_dto \ | ||
| import CreateModerationEventDTO | ||
| from api.models.partner_field import PartnerField | ||
| from api.constants import APIV1CommonErrorMessages | ||
|
|
||
|
|
||
| class PermissionProcessor(ContributionProcessor): | ||
|
|
||
| def process( | ||
| self, | ||
| event_dto: CreateModerationEventDTO) -> CreateModerationEventDTO: | ||
|
|
||
| partner_fields = PartnerField.objects.all() | ||
| partner_field_names = [field.name for field in partner_fields] | ||
vlad-shapik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if event_dto.raw_data and isinstance(event_dto.raw_data, dict): | ||
vlad-shapik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| matching_partner_field_names = [ | ||
| key for key in event_dto.raw_data.keys() | ||
| if key in partner_field_names | ||
| ] | ||
|
|
||
| if matching_partner_field_names: | ||
| contributor_partner_fields = event_dto.contributor \ | ||
| .partner_fields.all() | ||
| contributor_partner_field_names = [ | ||
| field.name for field in contributor_partner_fields | ||
| ] | ||
vlad-shapik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| unauthorized_partner_fields = [ | ||
| name for name in matching_partner_field_names | ||
| if name not in contributor_partner_field_names | ||
| ] | ||
|
|
||
| if unauthorized_partner_fields: | ||
| validation_errors = self.__transform_fields_errors( | ||
| unauthorized_partner_fields | ||
| ) | ||
| event_dto.errors = validation_errors | ||
| event_dto.status_code = status.HTTP_403_FORBIDDEN | ||
|
|
||
| return event_dto | ||
|
|
||
| return super().process(event_dto) | ||
roman-stolar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| @staticmethod | ||
| def __transform_fields_errors(fields_errors: List[str]) -> Dict: | ||
| validation_errors = { | ||
| 'detail': APIV1CommonErrorMessages.COMMON_REQ_BODY_ERROR, | ||
| 'errors': [] | ||
| } | ||
|
|
||
| for field_name in fields_errors: | ||
| validation_errors['errors'].append( | ||
| { | ||
| 'field': field_name, | ||
| 'detail': 'You do not have permission ' | ||
| 'to contribute to this field.' | ||
| } | ||
| ) | ||
|
|
||
| return validation_errors | ||
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.