*: propertymapping template -> expression

This commit is contained in:
Jens Langhammer 2020-02-17 20:38:14 +01:00
parent bc4b7ef44d
commit 3aa2f1e892
12 changed files with 42 additions and 23 deletions

View File

@ -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",
),
]

View File

@ -296,15 +296,15 @@ class PropertyMapping(UUIDModel):
"""User-defined key -> x mapping which can be used by providers to expose extra data.""" """User-defined key -> x mapping which can be used by providers to expose extra data."""
name = models.TextField() name = models.TextField()
template = models.TextField() expression = models.TextField()
form = "" form = ""
objects = InheritanceManager() objects = InheritanceManager()
def render(self, user: User, request: HttpRequest, **kwargs) -> Any: def evaluate(self, user: User, request: HttpRequest, **kwargs) -> Any:
"""Render `self.template` using `**kwargs` as Context.""" """Evaluate `self.expression` using `**kwargs` as Context."""
template = NATIVE_ENVIRONMENT.from_string(self.template) expression = NATIVE_ENVIRONMENT.from_string(self.expression)
return template.render(user=user, request=request, **kwargs) return expression.render(user=user, request=request, **kwargs)
def __str__(self): def __str__(self):
return f"Property Mapping {self.name}" return f"Property Mapping {self.name}"

View File

@ -43,7 +43,7 @@ class SAMLPropertyMappingSerializer(ModelSerializer):
class Meta: class Meta:
model = SAMLPropertyMapping model = SAMLPropertyMapping
fields = ["pk", "name", "saml_name", "friendly_name", "template"] fields = ["pk", "name", "saml_name", "friendly_name", "expression"]
class SAMLPropertyMappingViewSet(ModelViewSet): class SAMLPropertyMappingViewSet(ModelViewSet):

View File

@ -64,7 +64,7 @@ class SAMLPropertyMappingForm(forms.ModelForm):
class Meta: class Meta:
model = SAMLPropertyMapping model = SAMLPropertyMapping
fields = ["name", "saml_name", "friendly_name", "template"] fields = ["name", "saml_name", "friendly_name", "expression"]
widgets = { widgets = {
"name": forms.TextInput(), "name": forms.TextInput(),
"saml_name": forms.TextInput(), "saml_name": forms.TextInput(),

View File

@ -23,41 +23,41 @@ def create_default_property_mappings(apps, schema_editor):
{ {
"FriendlyName": "eduPersonPrincipalName", "FriendlyName": "eduPersonPrincipalName",
"Name": "urn:oid:1.3.6.1.4.1.5923.1.1.1.6", "Name": "urn:oid:1.3.6.1.4.1.5923.1.1.1.6",
"Template": "{{ user.email }}", "Expression": "{{ user.email }}",
}, },
{ {
"FriendlyName": "cn", "FriendlyName": "cn",
"Name": "urn:oid:2.5.4.3", "Name": "urn:oid:2.5.4.3",
"Template": "{{ user.name }}", "Expression": "{{ user.name }}",
}, },
{ {
"FriendlyName": "mail", "FriendlyName": "mail",
"Name": "urn:oid:0.9.2342.19200300.100.1.3", "Name": "urn:oid:0.9.2342.19200300.100.1.3",
"Template": "{{ user.email }}", "Expression": "{{ user.email }}",
}, },
{ {
"FriendlyName": "displayName", "FriendlyName": "displayName",
"Name": "urn:oid:2.16.840.1.113730.3.1.241", "Name": "urn:oid:2.16.840.1.113730.3.1.241",
"Template": "{{ user.username }}", "Expression": "{{ user.username }}",
}, },
{ {
"FriendlyName": "uid", "FriendlyName": "uid",
"Name": "urn:oid:0.9.2342.19200300.100.1.1", "Name": "urn:oid:0.9.2342.19200300.100.1.1",
"Template": "{{ user.pk }}", "Expression": "{{ user.pk }}",
}, },
{ {
"FriendlyName": "member-of", "FriendlyName": "member-of",
"Name": "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: for default in defaults:
SAMLPropertyMapping.objects.using(db_alias).get_or_create( SAMLPropertyMapping.objects.using(db_alias).get_or_create(
saml_name=default["Name"], saml_name=default["Name"],
friendly_name=default["FriendlyName"], friendly_name=default["FriendlyName"],
template=default["Template"], expression=default["Expression"],
defaults={ 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 = [ dependencies = [
("passbook_providers_saml", "0004_auto_20200217_1526"), ("passbook_providers_saml", "0004_auto_20200217_1526"),
("passbook_core", "0007_auto_20200217_1934"),
] ]
operations = [ operations = [

View File

@ -98,7 +98,7 @@ class Processor:
for mapping in self._remote.property_mappings.all().select_subclasses(): for mapping in self._remote.property_mappings.all().select_subclasses():
if isinstance(mapping, SAMLPropertyMapping): if isinstance(mapping, SAMLPropertyMapping):
value = mapping.render( value = mapping.evaluate(
user=self._http_request.user, user=self._http_request.user,
request=self._http_request, request=self._http_request,
provider=self._remote, provider=self._remote,

View File

@ -12,6 +12,7 @@
<ul> <ul>
<li><code>user</code>: Passbook User Object (<a href="https://beryju.github.io/passbook/reference/property-mappings/user-object/">Reference</a>)</li> <li><code>user</code>: Passbook User Object (<a href="https://beryju.github.io/passbook/reference/property-mappings/user-object/">Reference</a>)</li>
<li><code>request</code>: Django HTTP Request Object (<a href="https://docs.djangoproject.com/en/3.0/ref/request-response/#httprequest-objects">Reference</a>) </li> <li><code>request</code>: Django HTTP Request Object (<a href="https://docs.djangoproject.com/en/3.0/ref/request-response/#httprequest-objects">Reference</a>) </li>
<li><code>provider</code>: Passbook SAML Provider Object (<a href="https://github.com/BeryJu/passbook/blob/master/passbook/providers/saml/models.py#L16">Reference</a>) </li>
</ul> </ul>
</p> </p>
</div> </div>

View File

@ -35,7 +35,7 @@ class LDAPPropertyMappingSerializer(ModelSerializer):
class Meta: class Meta:
model = LDAPPropertyMapping model = LDAPPropertyMapping
fields = ["pk", "name", "template", "object_field"] fields = ["pk", "name", "expression", "object_field"]
class LDAPSourceViewSet(ModelViewSet): class LDAPSourceViewSet(ModelViewSet):

View File

@ -155,7 +155,7 @@ class Connector:
properties = {"attributes": {}} properties = {"attributes": {}}
for mapping in self._source.property_mappings.all().select_subclasses(): for mapping in self._source.property_mappings.all().select_subclasses():
mapping: LDAPPropertyMapping mapping: LDAPPropertyMapping
properties[mapping.object_field] = mapping.render( properties[mapping.object_field] = mapping.evaluate(
user=None, request=None, ldap=attributes user=None, request=None, ldap=attributes
) )
if self._source.object_uniqueness_field in attributes: if self._source.object_uniqueness_field in attributes:

View File

@ -55,7 +55,7 @@ class LDAPPropertyMappingForm(forms.ModelForm):
class Meta: class Meta:
model = LDAPPropertyMapping model = LDAPPropertyMapping
fields = ["name", "object_field", "template"] fields = ["name", "object_field", "expression"]
widgets = { widgets = {
"name": forms.TextInput(), "name": forms.TextInput(),
"ldap_property": forms.TextInput(), "ldap_property": forms.TextInput(),

View File

@ -22,12 +22,12 @@ def create_default_ad_property_mappings(apps: Apps, schema_editor):
"email": "{{ ldap.mail }}", "email": "{{ ldap.mail }}",
} }
db_alias = schema_editor.connection.alias 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( LDAPPropertyMapping.objects.using(db_alias).get_or_create(
template=template, expression=expression,
object_field=object_field, object_field=object_field,
defaults={ 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 = [ dependencies = [
("passbook_sources_ldap", "0006_auto_20200216_1116"), ("passbook_sources_ldap", "0006_auto_20200216_1116"),
("passbook_core", "0007_auto_20200217_1934"),
] ]
operations = [ operations = [

View File

@ -64,7 +64,7 @@ class LDAPPropertyMapping(PropertyMapping):
form = "passbook.sources.ldap.forms.LDAPPropertyMappingForm" form = "passbook.sources.ldap.forms.LDAPPropertyMappingForm"
def __str__(self): 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: class Meta: