diff options
-rw-r--r-- | okupy/accounts/models.py | 3 | ||||
-rw-r--r-- | okupy/common/fields.py | 34 |
2 files changed, 36 insertions, 1 deletions
diff --git a/okupy/accounts/models.py b/okupy/accounts/models.py index 277f470..4da7ee4 100644 --- a/okupy/accounts/models.py +++ b/okupy/accounts/models.py @@ -3,9 +3,10 @@ from django.conf import settings from django.db import models from ldapdb.models.fields import (CharField, IntegerField, ListField, - FloatField, ACLField, DateField) + FloatField, DateField) import ldapdb.models +from okupy.common.fields import ACLField from okupy.crypto.models import EncryptedPKModel diff --git a/okupy/common/fields.py b/okupy/common/fields.py new file mode 100644 index 0000000..86754f0 --- /dev/null +++ b/okupy/common/fields.py @@ -0,0 +1,34 @@ +# vim:fileencoding=utf8:et:ts=4:sts=4:sw=4:ft=python + +from django.db.models import fields + +from ldapdb import escape_ldap_filter + +class ACLField(fields.Field): + def _group(self): + return self.name.split('_')[1] + '.group' + + def from_ldap(self, value, connection): + if self._group() in value: + return True + else: + return False + + def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False): + "Returns field's value prepared for database lookup." + return [self.get_prep_lookup(lookup_type, value)] + + def get_db_prep_save(self, value, connection): + return [x.encode(connection.charset) for x in value] + + def get_prep_lookup(self, lookup_type, value): + "Perform preliminary non-db specific lookup checks and conversions" + if value not in (False, True): + raise TypeError("Invalid value") + if lookup_type == 'exact': + if value: + return escape_ldap_filter(self._group()) + else: + raise NotImplementedError( + "Negative lookups on ACLField are not yet implemented") + raise TypeError("ACLField has invalid lookup: %s" % lookup_type) |