summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJule Anger <ja@sernet.de>2020-08-24 16:39:01 +0200
committerDouglas Bagnall <dbagnall@samba.org>2020-10-01 01:18:39 +0000
commit899e66d47ff08a299cb12b33b987e7f98692a3b8 (patch)
tree9ece595bdfdd730c64280f2a6bd6fde864f3faca /python
parenta22a80ed6e8494b3edb717c3b288c7adde5092ee (diff)
downloadsamba-899e66d47ff08a299cb12b33b987e7f98692a3b8.tar.gz
samdb: add fullname_from_names() method
Add a method to construct the fullname, using the given name, the initials and the surname. If one of this values is empty, try to use the old one, given by an attributs set. If the combination is empty, the method will return the fallback-default parameter. Use this method to construct the CN or the displayName of users or contacts. Signed-off-by: Jule Anger <ja@sernet.de> Reviewed-by: Björn Baumbach <bb@samba.org> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Diffstat (limited to 'python')
-rw-r--r--python/samba/samdb.py56
1 files changed, 37 insertions, 19 deletions
diff --git a/python/samba/samdb.py b/python/samba/samdb.py
index 75a0af6f230..f1af3c28c30 100644
--- a/python/samba/samdb.py
+++ b/python/samba/samdb.py
@@ -415,6 +415,37 @@ member: %s
el = ldb.MessageElement(value, ldb.FLAG_MOD_REPLACE, attr_name)
msg.add(el)
+ def fullname_from_names(self, given_name=None, initials=None, surname=None,
+ old_attrs={}, fallback_default=""):
+ """Prepares new combined fullname, using the name parts.
+ Used for things like displayName or cn.
+ Use the original name values, if no new one is specified."""
+
+ attrs = {"givenName": given_name,
+ "initials": initials,
+ "sn": surname}
+
+ # if the attribute is not specified, try to use the old one
+ for attr_name, attr_value in attrs.items():
+ if attr_value == None and attr_name in old_attrs:
+ attrs[attr_name] = str(old_attrs[attr_name])
+
+ # add '.' to initials if initals are not None and not "" and if the initials
+ # don't have already a '.' at the end
+ if attrs["initials"] and not attrs["initials"].endswith('.'):
+ attrs["initials"] += '.'
+
+ # remove empty values (None and '')
+ attrs_values = list(filter(None, attrs.values()))
+
+ # fullname is the combination of not-empty values as string, separated by ' '
+ fullname = ' '.join(attrs_values)
+
+ if fullname == '':
+ return fallback_default
+
+ return fullname
+
def newuser(self, username, password,
force_password_change_at_next_login_req=False,
useusernameascn=False, userou=None, surname=None, givenname=None,
@@ -460,16 +491,9 @@ member: %s
:param smartcard_required: set the UF_SMARTCARD_REQUIRED bit of the new user
"""
- displayname = ""
- if givenname is not None:
- displayname += givenname
-
- if initials is not None:
- displayname += ' %s.' % initials
-
- if surname is not None:
- displayname += ' %s' % surname
-
+ displayname = self.fullname_from_names(given_name=givenname,
+ initials=initials,
+ surname=surname)
cn = username
if useusernameascn is None and displayname != "":
cn = displayname
@@ -623,15 +647,9 @@ member: %s
"""
# Prepare the contact name like the RSAT, using the name parts.
- cn = ""
- if givenname is not None:
- cn += givenname
-
- if initials is not None:
- cn += ' %s.' % initials
-
- if surname is not None:
- cn += ' %s' % surname
+ cn = self.fullname_from_names(given_name=givenname,
+ initials=initials,
+ surname=surname)
# Use the specified fullcontactname instead of the previously prepared
# contact name, if it is specified.