|
| 1 | +from crum import impersonate |
1 | 2 | from django.apps import apps |
| 3 | +from django.db import transaction |
| 4 | +from django.utils.translation import gettext_lazy as _ |
2 | 5 | from rest_framework import serializers |
3 | 6 |
|
4 | 7 | from ..models import DABContentType, DABPermission, RoleDefinition, RoleTeamAssignment, RoleUserAssignment |
@@ -35,33 +38,96 @@ def to_internal_value(self, data): |
35 | 38 | return resource.content_object |
36 | 39 |
|
37 | 40 |
|
| 41 | +class ObjectIDAnsibleIDField(serializers.Field): |
| 42 | + "This is an ansible_id field intended to be used with source pointing to object_id, so, does conversion" |
| 43 | + |
| 44 | + def to_representation(self, value): |
| 45 | + "The source for this field is object_id, which is ignored, use content_object instead" |
| 46 | + assignment = self.parent.instance |
| 47 | + content_object = assignment.content_object |
| 48 | + if isinstance(content_object, RemoteObject): |
| 49 | + return None |
| 50 | + if hasattr(content_object, 'resource'): |
| 51 | + return str(content_object.resource.ansible_id) |
| 52 | + return None |
| 53 | + |
| 54 | + def to_internal_value(self, value): |
| 55 | + "Targeting object_id, this converts ansible_id into object_id" |
| 56 | + resource_cls = apps.get_model('dab_resource_registry', 'Resource') |
| 57 | + resource = resource_cls.objects.get(ansible_id=value) |
| 58 | + return resource.object_id |
| 59 | + |
| 60 | + |
38 | 61 | assignment_common_fields = ('created', 'created_by_ansible_id', 'object_id', 'object_ansible_id', 'content_type', 'role_definition') |
39 | 62 |
|
40 | 63 |
|
41 | 64 | class BaseAssignmentSerializer(serializers.ModelSerializer): |
42 | 65 | content_type = serializers.SlugRelatedField(read_only=True, slug_field='api_slug') |
43 | 66 | role_definition = serializers.SlugRelatedField(slug_field='name', queryset=RoleDefinition.objects.all()) |
44 | 67 | created_by_ansible_id = ActorAnsibleIDField(source='created_by', required=False) |
45 | | - object_ansible_id = serializers.SerializerMethodField() |
| 68 | + object_ansible_id = ObjectIDAnsibleIDField(source='object_id', required=False) |
46 | 69 | # TODO: use the from_service to control what we sync back to |
47 | 70 | from_service = serializers.CharField(write_only=True) |
48 | 71 |
|
49 | 72 | def get_created_by_ansible_id(self, obj): |
50 | 73 | return str(obj.created_by.resource.ansible_id) |
51 | 74 |
|
52 | | - def get_object_ansible_id(self, obj): |
53 | | - content_object = obj.content_object |
54 | | - if isinstance(content_object, RemoteObject): |
55 | | - return None |
56 | | - if hasattr(content_object, 'resource'): |
57 | | - return str(content_object.resource.ansible_id) |
58 | | - return None |
| 75 | + def validate(self, attrs): |
| 76 | + """The object_id vs ansible_id is the only dual-write case, where we have to accept either |
| 77 | +
|
| 78 | + So this does the mutual validation to assure we have sufficient data. |
| 79 | + """ |
| 80 | + has_oid = 'object_id' in self.initial_data |
| 81 | + has_oaid = 'object_ansible_id' in self.initial_data |
| 82 | + |
| 83 | + if not self.partial and not has_oid and not has_oaid: |
| 84 | + raise serializers.ValidationError("You must provide either 'object_id' or 'object_ansible_id'.") |
| 85 | + |
| 86 | + # NOTE: right now not enforcing the case you provide both, could check for consistency later |
| 87 | + |
| 88 | + return attrs |
59 | 89 |
|
60 | 90 | def find_existing_assignment(self, queryset): |
61 | 91 | actor = self.validated_data[self.actor_field] |
62 | | - object_id = self.validated_data['object_id'] |
63 | 92 | role_definition = self.validated_data['role_definition'] |
64 | | - return queryset.filter(object_id=object_id, role_definition=role_definition, **{self.actor_field: actor}).first() |
| 93 | + object_id = self.validated_data['object_id'] |
| 94 | + filter_kwargs = {self.actor_field: actor} |
| 95 | + return queryset.filter(role_definition=role_definition, object_id=object_id, **filter_kwargs).first() |
| 96 | + |
| 97 | + def create(self, validated_data): |
| 98 | + rd = validated_data['role_definition'] |
| 99 | + actor = validated_data[self.actor_field] |
| 100 | + |
| 101 | + as_user = None |
| 102 | + if 'created_by' in validated_data: |
| 103 | + as_user = validated_data['created_by'] |
| 104 | + |
| 105 | + # Unlike the public view, the action is attributed to the specified user in data |
| 106 | + with impersonate(as_user): |
| 107 | + |
| 108 | + object_id = validated_data['object_id'] |
| 109 | + obj = None |
| 110 | + if object_id: |
| 111 | + model = rd.content_type.model_class() |
| 112 | + try: |
| 113 | + obj = model.objects.get(pk=object_id) |
| 114 | + except model.DoesNotExist as exc: |
| 115 | + raise serializers.ValidationError({'object_id': str(exc)}) |
| 116 | + |
| 117 | + # Validators not ran, because this should be an internal action |
| 118 | + |
| 119 | + if rd.content_type: |
| 120 | + # Object role assignment |
| 121 | + if not obj: |
| 122 | + raise serializers.ValidationError({'object_id': _('Object must be specified for this role assignment')}) |
| 123 | + |
| 124 | + with transaction.atomic(): |
| 125 | + assignment = rd.give_permission(actor, obj) |
| 126 | + else: |
| 127 | + with transaction.atomic(): |
| 128 | + assignment = rd.give_global_permission(actor) |
| 129 | + |
| 130 | + return assignment |
65 | 131 |
|
66 | 132 |
|
67 | 133 | class RoleUserAssignmentSerializer(BaseAssignmentSerializer): |
|
0 commit comments