Skip to content

Commit

Permalink
Phone 3pid in LDAP support
Browse files Browse the repository at this point in the history
Now users able search contacts from ldap by phone number.
  • Loading branch information
slipeer committed Apr 26, 2017
1 parent e00da0c commit 8f12635
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
7 changes: 5 additions & 2 deletions LDAP.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ Example LDAP config section
uri = ldap://example.com:389/
startls = false
base = dc=example,dc=com
mail_attr = mail
# Mail attribute name
email = mail
# Phone attribute name
msisdn = phone
id_attr = samaccountname
# if hs_name empty we assume that id_attr contain users matrix id
# othercase we generate matrix id as @id_attr:hs_name
hs_name = example.com
bind_dn = cn=namager,cn=users,dc=example,dc=com
bind_dn = cn=manager,cn=users,dc=example,dc=com
bind_pw = secret
filter = (&(objectClass=user)(objectCategory=person))
3 changes: 2 additions & 1 deletion sydent.example.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ ed25519.signingkey =
# uri = ldap://example.com:389/
# startls = false
# base = dc=example,dc=com
# mail_attr = mail
# email = mail
# msisdn = phone
# id_attr = samaccountname
# # if hs_name empty we assume that id_attr contain users matrix id
# # othercase we generate matrix id as @id_attr:hs_name
Expand Down
20 changes: 10 additions & 10 deletions sydent/db/ldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
# limitations under the License.

import logging
import os


try:
Expand Down Expand Up @@ -54,14 +53,13 @@ def __init__(self, syd):
logger.info("Missing ldap3 library. This is required for LDAP integration")
return



self.sydent = syd

self.ldap_uri = self.sydent.cfg.get("ldap", "uri")
self.start_tls = self.sydent.cfg.get("ldap", "startls")
self.base = self.sydent.cfg.get("ldap", "base")
self.mail_attr = self.sydent.cfg.get("ldap", "mail_attr")
self.email = self.sydent.cfg.get("ldap", "email")
self.msisdn = self.sydent.cfg.get("ldap", "msisdn")
self.id_attr = self.sydent.cfg.get("ldap", "id_attr").replace('"','').replace("'","")
self.hs_name = self.sydent.cfg.get("ldap", "hs_name").replace('"','').replace("'","")
self.bind_dn = self.sydent.cfg.get("ldap", "bind_dn")
Expand All @@ -77,8 +75,10 @@ def HasLdapConfiguration(self):
return False

def getMxid(self,medium,address):
if (not medium == "email"):
# Support only Email from LDAP
if hasattr(self, medium):
searchAttr = getattr(self, medium)
else:
logger.warning("Unsupported or unconfigured 3pid medium: %r", medium)
return None
try:
server = ldap3.Server(
Expand All @@ -101,8 +101,8 @@ def getMxid(self,medium,address):
else:
logger.debug("LDAP bind as %s error: %s", self.bind_dn, conn.result['description'])
conn.search(search_base=self.base,
search_filter="(&(" + self.mail_attr + "=" + address + ")" + self.ldap_filter + ")",
attributes=[self.mail_attr, self.id_attr]
search_filter="(&(" + searchAttr + "=" + address + ")" + self.ldap_filter + ")",
attributes=[self.id_attr, searchAttr]
)
responses = [
response
Expand All @@ -111,10 +111,10 @@ def getMxid(self,medium,address):
if response['type'] == 'searchResEntry'
]

logger.debug("LDAP return %d records for filter: %s", len(responses), "(&(" + self.mail_attr + "=" + address + ")" + self.ldap_filter + ")")
logger.debug("LDAP return %d records for filter: %s", len(responses), "(&(" + searchAttr + "=" + address + ")" + self.ldap_filter + ")")

if len(responses) == 1:
logger.debug("LDAP found one record with %s = %s", self.mail_attr, address)
logger.debug("LDAP found one record with %s = %s", searchAttr, address)
# # if hs_name empty we assume that id_attr contain users matrix id
# # othercase we generate matrix id as @id_attr:hs_name
if (self.hs_name):
Expand Down

0 comments on commit 8f12635

Please sign in to comment.