1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
# -*- coding: utf-8 -*-
# This software is distributed under the two-clause BSD license.
# Copyright (c) The django-ldapdb project
import datetime
import re
from django.db.models import fields, lookups
from django.utils import timezone
class LdapLookup(lookups.Lookup):
rhs_is_iterable = False
LDAP_PLACEHOLDER = '%s'
def _as_ldap(self, lhs):
raise NotImplementedError()
def process_lhs(self, compiler, connection):
return (self.lhs.target.column, [])
def as_sql(self, compiler, connection):
# lhs: field name; lhs_params: []
lhs, lhs_params = self.process_lhs(compiler, connection)
rhs_params = self.rhs if self.rhs_is_iterable else [self.rhs]
params = lhs_params + rhs_params
if self.rhs_is_iterable:
# Convert (x__in=[a, b, c]) to |(x=a)(x=b)(x=c)
return '|' + ''.join(['({})'.format(self._as_ldap(lhs))] * len(rhs_params)), params
else:
return self._as_ldap(lhs), params
def get_prep_lookup(self):
"""
Convert the Python value(s) used in the lookup to LDAP values.
"""
field = self.lhs.output_field
if self.rhs_is_iterable and not field.multi_valued_field:
# self.rhs is an iterable, and the field expects single-valued options.
return [field.get_prep_value(v) for v in self.rhs]
else:
# self.rhs is 'as multi-valued' as the field.
return field.get_prep_value(self.rhs)
class ContainsLookup(LdapLookup):
lookup_name = 'contains'
def _as_ldap(self, lhs):
return '%s=*%s*' % (lhs, self.LDAP_PLACEHOLDER)
class IContainsLookup(ContainsLookup):
lookup_name = 'icontains'
class StartsWithLookup(LdapLookup):
lookup_name = 'startswith'
def _as_ldap(self, lhs):
return '%s=%s*' % (lhs, self.LDAP_PLACEHOLDER)
class EndsWithLookup(LdapLookup):
lookup_name = 'endswith'
def _as_ldap(self, lhs):
return '%s=*%s' % (lhs, self.LDAP_PLACEHOLDER)
class ExactLookup(LdapLookup):
lookup_name = 'exact'
def _as_ldap(self, lhs):
return '%s=%s' % (lhs, self.LDAP_PLACEHOLDER)
class GteLookup(LdapLookup):
lookup_name = 'gte'
def _as_ldap(self, lhs):
return '%s>=%s' % (lhs, self.LDAP_PLACEHOLDER)
class LteLookup(LdapLookup):
lookup_name = 'lte'
def _as_ldap(self, lhs):
return '%s<=%s' % (lhs, self.LDAP_PLACEHOLDER)
class InLookup(LdapLookup):
lookup_name = 'in'
rhs_is_iterable = True
def _as_ldap(self, lhs):
return '%s=%s' % (lhs, self.LDAP_PLACEHOLDER)
class ListContainsLookup(ExactLookup):
lookup_name = 'contains'
class LdapFieldMixin(object):
multi_valued_field = False
binary_field = False
def get_db_prep_value(self, value, connection, prepared=False):
"""Prepare a value for DB interaction.
Returns:
- list(bytes) if not prepared
- list(str) if prepared
"""
if prepared:
return value
if value is None:
return []
values = value if self.multi_valued_field else [value]
prepared_values = [self.get_prep_value(v) for v in values]
# Remove duplicates.
# https://tools.ietf.org/html/rfc4511#section-4.1.7 :
# "The set of attribute values is unordered."
# We keep those values sorted in natural order to avoid useless
# updates to the LDAP server.
return list(sorted(set(v for v in prepared_values if v)))
def get_db_prep_save(self, value, connection):
values = self.get_db_prep_value(value, connection, prepared=False)
if self.binary_field:
# Already raw values; don't encode it twice.
return values
else:
return [v.encode(connection.charset) for v in values]
class CharField(LdapFieldMixin, fields.CharField):
def __init__(self, *args, **kwargs):
defaults = {'max_length': 200}
defaults.update(kwargs)
super().__init__(*args, **defaults)
def from_ldap(self, value, connection):
if len(value) == 0:
return ''
else:
return value[0].decode(connection.charset)
CharField.register_lookup(ContainsLookup)
CharField.register_lookup(IContainsLookup)
CharField.register_lookup(StartsWithLookup)
CharField.register_lookup(EndsWithLookup)
CharField.register_lookup(InLookup)
CharField.register_lookup(ExactLookup)
class ImageField(LdapFieldMixin, fields.Field):
binary_field = True
def from_ldap(self, value, connection):
if len(value) == 0:
return ''
else:
return value[0]
ImageField.register_lookup(ExactLookup)
class IntegerField(LdapFieldMixin, fields.IntegerField):
def from_ldap(self, value, connection):
if len(value) == 0:
return None if self.null else 0
else:
return int(value[0])
def get_prep_value(self, value):
value = super().get_prep_value(value)
return str(value)
IntegerField.register_lookup(ExactLookup)
IntegerField.register_lookup(GteLookup)
IntegerField.register_lookup(LteLookup)
IntegerField.register_lookup(InLookup)
class FloatField(LdapFieldMixin, fields.FloatField):
def from_ldap(self, value, connection):
if len(value) == 0:
return None if self.null else 0.0
else:
return float(value[0])
def get_prep_value(self, value):
value = super().get_prep_value(value)
return str(value)
FloatField.register_lookup(ExactLookup)
FloatField.register_lookup(GteLookup)
FloatField.register_lookup(LteLookup)
FloatField.register_lookup(InLookup)
class BooleanField(LdapFieldMixin, fields.BooleanField):
def from_ldap(self, value, connection):
if len(value) == 0:
return None if self.null else False
else:
return value[0].upper() == b'TRUE'
def get_prep_value(self, value):
value = super().get_prep_value(value)
return 'TRUE' if value else 'FALSE'
BooleanField.register_lookup(ExactLookup)
class ListField(LdapFieldMixin, fields.Field):
multi_valued_field = True
def from_ldap(self, value, connection):
return [x.decode(connection.charset) for x in value]
def from_db_value(self, value, expression, connection, context):
"""Convert from the database format.
This should be the inverse of self.get_prep_value()
"""
return self.to_python(value)
def to_python(self, value):
if not value:
return []
return value
ListField.register_lookup(ListContainsLookup)
ListField.register_lookup(ExactLookup)
class DateField(LdapFieldMixin, fields.DateField):
"""
A text field containing date, in specified format.
The format can be specified as 'format' argument, as strptime()
format string. It defaults to ISO8601 (%Y-%m-%d).
Note: 'lte' and 'gte' lookups are done string-wise. Therefore,
they will onlywork correctly on Y-m-d dates with constant
component widths.
"""
def __init__(self, *args, **kwargs):
if 'format' in kwargs:
self._date_format = kwargs.pop('format')
else:
self._date_format = '%Y-%m-%d'
super().__init__(*args, **kwargs)
def from_ldap(self, value, connection):
if len(value) == 0:
return None
else:
return datetime.datetime.strptime(value[0].decode(connection.charset),
self._date_format).date()
def get_prep_value(self, value):
value = super().get_prep_value(value)
if not isinstance(value, datetime.date) \
and not isinstance(value, datetime.datetime):
raise ValueError(
'DateField can be only set to a datetime.date instance; got {}'.format(repr(value)))
return value.strftime(self._date_format)
DateField.register_lookup(ExactLookup)
LDAP_DATETIME_RE = re.compile(
r'(?P<year>\d{4})'
r'(?P<month>\d{2})'
r'(?P<day>\d{2})'
r'(?P<hour>\d{2})'
r'(?P<minute>\d{2})?'
r'(?P<second>\d{2})?'
r'(?:[.,](?P<microsecond>\d+))?'
r'(?P<tzinfo>Z|[+-]\d{2}(?:\d{2})?)'
r'$'
)
LDAP_DATE_FORMAT = '%Y%m%d%H%M%S.%fZ'
def datetime_from_ldap(value):
"""Convert a LDAP-style datetime to a Python aware object.
See https://tools.ietf.org/html/rfc4517#section-3.3.13 for details.
Args:
value (str): the datetime to parse
"""
if not value:
return None
match = LDAP_DATETIME_RE.match(value)
if not match:
return None
groups = match.groupdict()
if groups['microsecond']:
groups['microsecond'] = groups['microsecond'].ljust(6, '0')[:6]
tzinfo = groups.pop('tzinfo')
if tzinfo == 'Z':
tzinfo = timezone.utc
else:
offset_mins = int(tzinfo[-2:]) if len(tzinfo) == 5 else 0
offset = 60 * int(tzinfo[1:3]) + offset_mins
if tzinfo[0] == '-':
offset = - offset
tzinfo = timezone.get_fixed_timezone(offset)
kwargs = {k: int(v) for k, v in groups.items() if v is not None}
kwargs['tzinfo'] = tzinfo
return datetime.datetime(**kwargs)
class DateTimeField(LdapFieldMixin, fields.DateTimeField):
"""
A field containing a UTC timestamp, in Generalized Time syntax.
That syntax is ``YYYYmmddHH[MM[SS[.ff](Z|+XX[YY]|-XX[YY])``.
See: https://tools.ietf.org/html/rfc4517#section-3.3.13
"""
def from_ldap(self, value, connection):
if len(value) == 0:
return None
return datetime_from_ldap(value[0].decode(connection.charset))
def get_prep_value(self, value):
value = super().get_prep_value(value)
if not isinstance(value, datetime.date) \
and not isinstance(value, datetime.datetime):
raise ValueError(
'DateTimeField can be only set to a datetime.datetime instance; got {}'.format(repr(value)))
value = timezone.utc.normalize(value)
return value.strftime(LDAP_DATE_FORMAT)
DateTimeField.register_lookup(ExactLookup)
DateTimeField.register_lookup(LteLookup)
DateTimeField.register_lookup(GteLookup)
DateTimeField.register_lookup(InLookup)
EPOCH = timezone.utc.localize(datetime.datetime.utcfromtimestamp(0))
def datetime_from_timestamp(ts):
return timezone.utc.localize(datetime.datetime.utcfromtimestamp(ts))
def timestamp_from_datetime(dt):
return int((timezone.utc.normalize(dt) - EPOCH).total_seconds())
class TimestampField(LdapFieldMixin, fields.DateTimeField):
"""
A field storing a datetime as a UNIX timestamp.
See for instance nis.schema > shadowAccount > shadowLastChange.
"""
def from_ldap(self, value, connection):
if len(value) == 0:
return None
return datetime_from_timestamp(value[0].decode(connection.charset))
def get_prep_value(self, value):
return str(timestamp_from_datetime(value))
TimestampField.register_lookup(ExactLookup)
TimestampField.register_lookup(LteLookup)
TimestampField.register_lookup(GteLookup)
TimestampField.register_lookup(InLookup)
|