*: 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."""
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}"

View File

@ -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):

View File

@ -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(),

View File

@ -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 = [

View File

@ -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,

View File

@ -12,6 +12,7 @@
<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>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>
</p>
</div>

View File

@ -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):

View File

@ -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:

View File

@ -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(),

View File

@ -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 = [

View File

@ -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: