From 3aa2f1e892807fd21a2c14948dba1f8dd49f9912 Mon Sep 17 00:00:00 2001 From: Jens Langhammer Date: Mon, 17 Feb 2020 20:38:14 +0100 Subject: [PATCH] *: propertymapping template -> expression --- .../core/migrations/0007_auto_20200217_1934.py | 16 ++++++++++++++++ passbook/core/models.py | 10 +++++----- passbook/providers/saml/api.py | 2 +- passbook/providers/saml/forms.py | 2 +- .../0005_remove_samlpropertymapping_values.py | 17 +++++++++-------- passbook/providers/saml/processors/base.py | 2 +- .../saml/idp/property_mapping_form.html | 1 + passbook/sources/ldap/api.py | 2 +- passbook/sources/ldap/connector.py | 2 +- passbook/sources/ldap/forms.py | 2 +- ..._remove_ldappropertymapping_ldap_property.py | 7 ++++--- passbook/sources/ldap/models.py | 2 +- 12 files changed, 42 insertions(+), 23 deletions(-) create mode 100644 passbook/core/migrations/0007_auto_20200217_1934.py diff --git a/passbook/core/migrations/0007_auto_20200217_1934.py b/passbook/core/migrations/0007_auto_20200217_1934.py new file mode 100644 index 000000000..4fa3282bb --- /dev/null +++ b/passbook/core/migrations/0007_auto_20200217_1934.py @@ -0,0 +1,16 @@ +# Generated by Django 3.0.3 on 2020-02-17 19:34 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ("passbook_core", "0006_propertymapping_template"), + ] + + operations = [ + migrations.RenameField( + model_name="propertymapping", old_name="template", new_name="expression", + ), + ] diff --git a/passbook/core/models.py b/passbook/core/models.py index 13c718976..7f1e26c8e 100644 --- a/passbook/core/models.py +++ b/passbook/core/models.py @@ -296,15 +296,15 @@ class PropertyMapping(UUIDModel): """User-defined key -> x mapping which can be used by providers to expose extra data.""" name = models.TextField() - template = models.TextField() + expression = models.TextField() form = "" objects = InheritanceManager() - def render(self, user: User, request: HttpRequest, **kwargs) -> Any: - """Render `self.template` using `**kwargs` as Context.""" - template = NATIVE_ENVIRONMENT.from_string(self.template) - return template.render(user=user, request=request, **kwargs) + def evaluate(self, user: User, request: HttpRequest, **kwargs) -> Any: + """Evaluate `self.expression` using `**kwargs` as Context.""" + expression = NATIVE_ENVIRONMENT.from_string(self.expression) + return expression.render(user=user, request=request, **kwargs) def __str__(self): return f"Property Mapping {self.name}" diff --git a/passbook/providers/saml/api.py b/passbook/providers/saml/api.py index 8378391f0..1348c85ca 100644 --- a/passbook/providers/saml/api.py +++ b/passbook/providers/saml/api.py @@ -43,7 +43,7 @@ class SAMLPropertyMappingSerializer(ModelSerializer): class Meta: model = SAMLPropertyMapping - fields = ["pk", "name", "saml_name", "friendly_name", "template"] + fields = ["pk", "name", "saml_name", "friendly_name", "expression"] class SAMLPropertyMappingViewSet(ModelViewSet): diff --git a/passbook/providers/saml/forms.py b/passbook/providers/saml/forms.py index 11f9c1dc2..08dc95f89 100644 --- a/passbook/providers/saml/forms.py +++ b/passbook/providers/saml/forms.py @@ -64,7 +64,7 @@ class SAMLPropertyMappingForm(forms.ModelForm): class Meta: model = SAMLPropertyMapping - fields = ["name", "saml_name", "friendly_name", "template"] + fields = ["name", "saml_name", "friendly_name", "expression"] widgets = { "name": forms.TextInput(), "saml_name": forms.TextInput(), diff --git a/passbook/providers/saml/migrations/0005_remove_samlpropertymapping_values.py b/passbook/providers/saml/migrations/0005_remove_samlpropertymapping_values.py index 1e41fe964..6d7589bb2 100644 --- a/passbook/providers/saml/migrations/0005_remove_samlpropertymapping_values.py +++ b/passbook/providers/saml/migrations/0005_remove_samlpropertymapping_values.py @@ -23,41 +23,41 @@ def create_default_property_mappings(apps, schema_editor): { "FriendlyName": "eduPersonPrincipalName", "Name": "urn:oid:1.3.6.1.4.1.5923.1.1.1.6", - "Template": "{{ user.email }}", + "Expression": "{{ user.email }}", }, { "FriendlyName": "cn", "Name": "urn:oid:2.5.4.3", - "Template": "{{ user.name }}", + "Expression": "{{ user.name }}", }, { "FriendlyName": "mail", "Name": "urn:oid:0.9.2342.19200300.100.1.3", - "Template": "{{ user.email }}", + "Expression": "{{ user.email }}", }, { "FriendlyName": "displayName", "Name": "urn:oid:2.16.840.1.113730.3.1.241", - "Template": "{{ user.username }}", + "Expression": "{{ user.username }}", }, { "FriendlyName": "uid", "Name": "urn:oid:0.9.2342.19200300.100.1.1", - "Template": "{{ user.pk }}", + "Expression": "{{ user.pk }}", }, { "FriendlyName": "member-of", "Name": "member-of", - "Template": "[{% for group in user.groups.all() %}'{{ group.name }}',{% endfor %}]", + "Expression": "[{% for group in user.groups.all() %}'{{ group.name }}',{% endfor %}]", }, ] for default in defaults: SAMLPropertyMapping.objects.using(db_alias).get_or_create( saml_name=default["Name"], friendly_name=default["FriendlyName"], - template=default["Template"], + expression=default["Expression"], defaults={ - "name": f"Autogenerated SAML Mapping: {default['FriendlyName']} -> {default['Template']}" + "name": f"Autogenerated SAML Mapping: {default['FriendlyName']} -> {default['Expression']}" }, ) @@ -66,6 +66,7 @@ class Migration(migrations.Migration): dependencies = [ ("passbook_providers_saml", "0004_auto_20200217_1526"), + ("passbook_core", "0007_auto_20200217_1934"), ] operations = [ diff --git a/passbook/providers/saml/processors/base.py b/passbook/providers/saml/processors/base.py index a90cb7803..12942f8b7 100644 --- a/passbook/providers/saml/processors/base.py +++ b/passbook/providers/saml/processors/base.py @@ -98,7 +98,7 @@ class Processor: for mapping in self._remote.property_mappings.all().select_subclasses(): if isinstance(mapping, SAMLPropertyMapping): - value = mapping.render( + value = mapping.evaluate( user=self._http_request.user, request=self._http_request, provider=self._remote, diff --git a/passbook/providers/saml/templates/saml/idp/property_mapping_form.html b/passbook/providers/saml/templates/saml/idp/property_mapping_form.html index 792be3c6d..43ff158ff 100644 --- a/passbook/providers/saml/templates/saml/idp/property_mapping_form.html +++ b/passbook/providers/saml/templates/saml/idp/property_mapping_form.html @@ -12,6 +12,7 @@

diff --git a/passbook/sources/ldap/api.py b/passbook/sources/ldap/api.py index a09ab3ca9..a51a5ce12 100644 --- a/passbook/sources/ldap/api.py +++ b/passbook/sources/ldap/api.py @@ -35,7 +35,7 @@ class LDAPPropertyMappingSerializer(ModelSerializer): class Meta: model = LDAPPropertyMapping - fields = ["pk", "name", "template", "object_field"] + fields = ["pk", "name", "expression", "object_field"] class LDAPSourceViewSet(ModelViewSet): diff --git a/passbook/sources/ldap/connector.py b/passbook/sources/ldap/connector.py index 9dcd9748e..dc33c53e4 100644 --- a/passbook/sources/ldap/connector.py +++ b/passbook/sources/ldap/connector.py @@ -155,7 +155,7 @@ class Connector: properties = {"attributes": {}} for mapping in self._source.property_mappings.all().select_subclasses(): mapping: LDAPPropertyMapping - properties[mapping.object_field] = mapping.render( + properties[mapping.object_field] = mapping.evaluate( user=None, request=None, ldap=attributes ) if self._source.object_uniqueness_field in attributes: diff --git a/passbook/sources/ldap/forms.py b/passbook/sources/ldap/forms.py index 16d233028..76e616f0f 100644 --- a/passbook/sources/ldap/forms.py +++ b/passbook/sources/ldap/forms.py @@ -55,7 +55,7 @@ class LDAPPropertyMappingForm(forms.ModelForm): class Meta: model = LDAPPropertyMapping - fields = ["name", "object_field", "template"] + fields = ["name", "object_field", "expression"] widgets = { "name": forms.TextInput(), "ldap_property": forms.TextInput(), diff --git a/passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py b/passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py index c33da5f59..f6ac24740 100644 --- a/passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py +++ b/passbook/sources/ldap/migrations/0007_remove_ldappropertymapping_ldap_property.py @@ -22,12 +22,12 @@ def create_default_ad_property_mappings(apps: Apps, schema_editor): "email": "{{ ldap.mail }}", } db_alias = schema_editor.connection.alias - for object_field, template in mapping.items(): + for object_field, expression in mapping.items(): LDAPPropertyMapping.objects.using(db_alias).get_or_create( - template=template, + expression=expression, object_field=object_field, defaults={ - "name": f"Autogenerated LDAP Mapping: {template} -> {object_field}" + "name": f"Autogenerated LDAP Mapping: {expression} -> {object_field}" }, ) @@ -36,6 +36,7 @@ class Migration(migrations.Migration): dependencies = [ ("passbook_sources_ldap", "0006_auto_20200216_1116"), + ("passbook_core", "0007_auto_20200217_1934"), ] operations = [ diff --git a/passbook/sources/ldap/models.py b/passbook/sources/ldap/models.py index 063dce319..393cccfa2 100644 --- a/passbook/sources/ldap/models.py +++ b/passbook/sources/ldap/models.py @@ -64,7 +64,7 @@ class LDAPPropertyMapping(PropertyMapping): form = "passbook.sources.ldap.forms.LDAPPropertyMappingForm" def __str__(self): - return f"LDAP Property Mapping {self.template} -> {self.object_field}" + return f"LDAP Property Mapping {self.expression} -> {self.object_field}" class Meta: