Skip to content

Commit e88bdc3

Browse files
fix: displayName and mail import/export (#151)
- Avoid failures when delete non-existent LDAP attributes. - Set value only if JSON attribute is present. - Empty string "" means delete during import. - Missing LDAP attribute means missing JSON attribute during export. Refs NethServer/dev#7666
1 parent eda9a39 commit e88bdc3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

imageroot/pypkg/samba.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -323,13 +323,14 @@ def export_users() -> list:
323323
continue
324324
out = {
325325
"user": rec[ACID],
326-
"display_name": rec[ACDISPLAY] or "",
327326
"locked": bool(rec[ACUAC] & 0x2), # ACCOUNTDISABLED
328327
"must_change_password": rec[ACPWDLASTSET] is None or rec[ACPWDLASTSET] <= 0,
329328
"no_password_expiration": bool(rec[ACUAC] & 0x10000), # DONT_EXPIRE_PASSWD
330329
}
331330
if rec[ACMAIL]:
332331
out["mail"] = rec[ACMAIL]
332+
if rec[ACDISPLAY]:
333+
out["display_name"] = rec[ACDISPLAY]
333334
groups = []
334335
# rec[ACGROUPS] contains group DNs (Distinguished Names).
335336
# The adb dictionary is keyed by both group DNs and canonical names,
@@ -386,7 +387,7 @@ def ldif_prepare(user, att, val, op='replace'):
386387
user = rec['user']
387388
all_users.add(user)
388389
pwd = rec.get('password', '')
389-
display_name = rec.get('display_name', user.title())
390+
display_name = rec.get('display_name')
390391
must_change = rec.get('must_change_password') is True
391392
mail_address = rec.get('mail')
392393

@@ -420,6 +421,8 @@ def ldif_prepare(user, att, val, op='replace'):
420421
continue # Skip further processing for this user
421422
udn = f'CN={user},CN=Users,' + LDAPSUFFIX # Assuming default DN
422423
adb[user] = (udn, [], 512, user, [], 'U', None, None, None) # (512 = NORMAL_ACCOUNT)
424+
if not display_name:
425+
display_name = user.title() # Initialize displayName with the Title Case of username
423426

424427
# 2a. Add user to its new groups. Non-existing group is created on
425428
# the fly.
@@ -452,8 +455,11 @@ def ldif_prepare(user, att, val, op='replace'):
452455
mod_groups[gna].remove(udn)
453456

454457
# 3. Prepare user LDIF changes
455-
ldif_prepare(user, 'displayName', display_name)
456-
if mail_address == "":
458+
if display_name == "" and adb[user][ACDISPLAY] is not None:
459+
ldif_prepare(user, 'displayName', None, "delete")
460+
elif display_name:
461+
ldif_prepare(user, 'displayName', display_name)
462+
if mail_address == "" and adb[user][ACMAIL] is not None:
457463
ldif_prepare(user, 'mail', None, "delete")
458464
elif mail_address:
459465
ldif_prepare(user, 'mail', mail_address)

0 commit comments

Comments
 (0)