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
379d1b68
Verified
Commit
379d1b68
authored
5 years ago
by
Nik | Klampfradler
Committed by
Tom Teichler
5 years ago
Browse files
Options
Downloads
Patches
Plain Diff
Dynamically generate constance configs for sync fields
parent
fe308727
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!2
Resolve "Sync additional LDAP fields after login"
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
aleksis/apps/ldap/apps.py
+4
-1
4 additions, 1 deletion
aleksis/apps/ldap/apps.py
aleksis/apps/ldap/settings.py
+1
-14
1 addition, 14 deletions
aleksis/apps/ldap/settings.py
aleksis/apps/ldap/util/ldap_sync.py
+39
-7
39 additions, 7 deletions
aleksis/apps/ldap/util/ldap_sync.py
with
44 additions
and
22 deletions
aleksis/apps/ldap/apps.py
+
4
−
1
View file @
379d1b68
...
...
@@ -3,7 +3,7 @@ from django.db.models.signals import post_save
from
aleksis.core.util.apps
import
AppConfig
from
.util.ldap_sync
import
ldap_sync_from_user
from
.util.ldap_sync
import
ldap_sync_from_user
,
update_constance_config_fields
class
LDAPConfig
(
AppConfig
):
name
=
"
aleksis.apps.ldap
"
...
...
@@ -11,5 +11,8 @@ class LDAPConfig(AppConfig):
def
ready
(
self
)
->
None
:
super
().
ready
()
update_constance_config_fields
()
User
=
get_user_model
()
post_save
.
connect
(
ldap_sync_from_user
,
sender
=
User
)
This diff is collapsed.
Click to expand it.
aleksis/apps/ldap/settings.py
+
1
−
14
View file @
379d1b68
from
django.apps
import
apps
from
django.utils.translation
import
gettext_lazy
as
_
CONSTANCE_ADDITIONAL_FIELDS
=
{
...
...
@@ -23,13 +24,6 @@ CONSTANCE_CONFIG = {
_
(
"
LDAP sync matching fields
"
),
"
matching-fields-select
"
,
),
"
LDAP_SYNC_FIELD_STREET
"
:
(
None
,
_
(
"
Field for street
"
),
str
),
"
LDAP_SYNC_FIELD_HOUSENUMBER
"
:
(
None
,
_
(
"
Field for house number
"
),
str
),
"
LDAP_SYNC_FIELD_POSTAL_CODE
"
:
(
None
,
_
(
"
Field for postal code
"
),
str
),
"
LDAP_SYNC_FIELD_PLACE
"
:
(
None
,
_
(
"
Field for place
"
),
str
),
"
LDAP_SYNC_FIELD_PHONE_NUMBER
"
:
(
None
,
_
(
"
Field for phone number
"
),
str
),
"
LDAP_SYNC_FIELD_MOBILE_NUMBER
"
:
(
None
,
_
(
"
Field for mobile number
"
),
str
),
"
LDAP_SYNC_FIELD_DATE_OF_BIRTH
"
:
(
None
,
_
(
"
Field for date of birth
"
),
str
),
"
ENABLE_LDAP_GROUP_SYNC
"
:
(
True
,
_
(
"
Enable ldap group sync
"
),
bool
),
"
LDAP_GROUP_SYNC_FIELD_SHORT_NAME
"
:
(
"
cn
"
,
_
(
"
Field for short name of group
"
),
str
),
"
LDAP_GROUP_SYNC_FIELD_NAME
"
:
(
"
cn
"
,
_
(
"
Field for name of group
"
),
str
),
...
...
@@ -39,13 +33,6 @@ CONSTANCE_CONFIG_FIELDSETS = {
"
ENABLE_LDAP_SYNC
"
,
"
LDAP_SYNC_ON_UPDATE
"
,
"
LDAP_MATCHING_FIELDS
"
,
"
LDAP_SYNC_FIELD_STREET
"
,
"
LDAP_SYNC_FIELD_HOUSENUMBER
"
,
"
LDAP_SYNC_FIELD_POSTAL_CODE
"
,
"
LDAP_SYNC_FIELD_PLACE
"
,
"
LDAP_SYNC_FIELD_PHONE_NUMBER
"
,
"
LDAP_SYNC_FIELD_MOBILE_NUMBER
"
,
"
LDAP_SYNC_FIELD_DATE_OF_BIRTH
"
,
"
ENABLE_LDAP_GROUP_SYNC
"
,
"
LDAP_GROUP_SYNC_FIELD_SHORT_NAME
"
,
"
LDAP_GROUP_SYNC_FIELD_NAME
"
,
...
...
This diff is collapsed.
Click to expand it.
aleksis/apps/ldap/util/ldap_sync.py
+
39
−
7
View file @
379d1b68
from
django.apps
import
apps
from
django.conf
import
settings
from
django.contrib.auth
import
get_user_model
from
constance
import
config
def
setting_name_from_field
(
model
,
field
):
"""
Generate a constance setting name from a model field
"""
return
"
LDAP_ADDITIONAL_FIELD_%s_%s
"
%
(
model
.
_meta
.
label
,
field
.
name
)
def
update_constance_config_fields
():
"""
Auto-generate sync field settings from models
"""
Person
=
apps
.
get_model
(
"
core
"
,
"
Person
"
)
for
model
in
(
Person
,):
# Collect fields that are matchable
setting_names
=
[]
for
field
in
model
.
_meta
.
fields
:
if
field
.
editable
and
not
field
.
auto_created
:
setting_name
=
setting_name_from_field
(
model
,
field
)
setting_desc
=
field
.
verbose_name
settings
.
CONSTANCE_CONFIG
[
setting_name
]
=
(
""
,
setting_desc
,
str
)
setting_names
.
append
(
setting_name
)
# Add separate constance section if settings were generated
if
setting_names
:
fieldset_name
=
"
LDAP-Sync: Additional fields for %s
"
%
model
.
_meta
.
verbose_name
settings
.
CONSTANCE_CONFIG_FIELDSETS
[
fieldset_name
]
=
setting_names
def
ldap_sync_from_user
(
sender
,
instance
,
created
,
raw
,
using
,
update_fields
,
**
kwargs
):
"""
Synchronise Person meta-data and groups from ldap_user on User update.
"""
...
...
@@ -28,6 +56,17 @@ def ldap_sync_from_user(sender, instance, created, raw, using, update_fields, **
return
person
.
user
=
instance
# Synchronise additional fields if enabled
for
field
in
Person
.
_meta
.
fields
:
if
field
.
editable
and
not
field
.
auto_created
:
setting_name
=
setting_name_from_field
(
Person
,
field
)
# Try sync if constance setting for this field is non-empty
ldap_field
=
getattr
(
config
,
setting_name
,
""
)
if
ldap_field
and
ldap_field
in
instance
.
ldap_user
.
attrs
.
data
:
setattr
(
person
,
field
.
_meta
.
name
,
instance
.
ldap_user
.
attrs
.
data
[
ldap_field
])
person
.
save
()
if
config
.
ENABLE_LDAP_GROUP_SYNC
:
...
...
@@ -51,10 +90,3 @@ def ldap_sync_from_user(sender, instance, created, raw, using, update_fields, **
# Sync additional fields if enabled in config.
ldap_user
=
instance
.
ldap_user
person
.
street
=
ldap_user
.
attrs
.
data
[
config
.
LDAP_SYNC_FIELD_STREET
]
person
.
housenumber
=
ldap_user
.
attrs
.
data
[
config
.
LDAP_SYNC_FIELD_HOUSENUMBER
]
person
.
postal_code
=
ldap_user
.
attrs
.
data
[
config
.
LDAP_SYNC_FIELD_POSTAL_CODE
]
person
.
place
=
ldap_user
.
attrs
.
data
[
config
.
LDAP_SYNC_FIELD_PLACE
]
person
.
phone_number
=
ldap_user
.
attrs
.
data
[
config
.
LDAP_SYNC_FIELD_PHONE_NUMBER
]
person
.
mobile_number
=
ldap_user
.
attrs
.
data
[
config
.
LDAP_SYNC_FIELD_MOBILE_NUMBER
]
person
.
date_of_birth
=
ldap_user
.
attrs
.
data
[
config
.
LDAP_SYNC_FIELD_DATE_OF_BIRTH
]
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