Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
AlekSIS-App-LDAP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
AlekSIS®
Official
AlekSIS-App-LDAP
Commits
8bf4b672
Verified
Commit
8bf4b672
authored
4 years ago
by
Nik | Klampfradler
Browse files
Options
Downloads
Patches
Plain Diff
Support synchronising file fields, e.g. jpegPhoto
Closes
#9
parent
faf2fff8
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!5
Resolve "Support jpegPhoto"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
aleksis/apps/ldap/util/ldap_sync.py
+20
-2
20 additions, 2 deletions
aleksis/apps/ldap/util/ldap_sync.py
with
20 additions
and
2 deletions
aleksis/apps/ldap/util/ldap_sync.py
+
20
−
2
View file @
8bf4b672
import
io
import
logging
import
re
...
...
@@ -6,6 +7,8 @@ from django.conf import settings
from
django.contrib.auth
import
get_user_model
from
django.db
import
DataError
,
IntegrityError
,
transaction
from
django.db.models
import
fields
from
django.db.models.fields.files
import
FieldFile
,
FileField
from
django.utils.text
import
slugify
from
django.utils.translation
import
gettext
as
_
from
constance
import
config
...
...
@@ -33,7 +36,13 @@ def syncable_fields(model):
field
.
editable
and
not
field
.
auto_created
and
not
field
.
is_relation
)]
def
from_ldap
(
value
,
field
):
def
ldap_field_to_filename
(
dn
,
fieldname
):
"""
Generate a reproducible filename from a DN and a field name
"""
return
"
%s__%s
"
%
(
slugify
(
dn
),
slugify
(
fieldname
))
def
from_ldap
(
value
,
instance
,
field
,
dn
,
ldap_field
):
"""
Convert an LDAP value to the Python type of the target field
This conversion is prone to error because LDAP deliberately breaks
...
...
@@ -46,6 +55,15 @@ def from_ldap(value, field):
if
isinstance
(
field
,
(
fields
.
DateField
,
fields
.
DateTimeField
)):
# Be opportunistic, but keep old value if conversion fails
value
=
datetime_from_ldap
(
value
)
or
value
elif
isinstance
(
field
,
FileField
):
name
=
ldap_field_to_filename
(
dn
,
ldap_field
)
content
=
File
(
io
.
BytesIO
(
value
))
# Pre-save field file instance
fieldfile
=
getattr
(
instance
,
field
.
attname
)
fieldfile
.
save
(
name
,
content
)
return
fieldfile
# Finally, use field's conversion method as default
return
field
.
to_python
(
value
)
...
...
@@ -194,7 +212,7 @@ def ldap_sync_from_user(user, dn, attrs):
value
=
apply_templates
(
value
,
patterns
,
templates
)
# Opportunistically convert LDAP string value to Python object
value
=
from_ldap
(
value
,
field
)
value
=
from_ldap
(
value
,
person
,
field
,
dn
,
ldap_
field
)
setattr
(
person
,
field
.
name
,
value
)
logger
.
debug
(
"
Field %s set to %s for %s
"
%
(
field
.
name
,
str
(
value
),
str
(
person
)))
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment