You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by br...@apache.org on 2014/06/13 20:34:57 UTC

[15/17] git commit: [#7406] LdapUserPreferencesProvider, for display_name

[#7406] LdapUserPreferencesProvider, for display_name


Project: http://git-wip-us.apache.org/repos/asf/allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/allura/commit/ca7c4aaa
Tree: http://git-wip-us.apache.org/repos/asf/allura/tree/ca7c4aaa
Diff: http://git-wip-us.apache.org/repos/asf/allura/diff/ca7c4aaa

Branch: refs/heads/db/7406
Commit: ca7c4aaa668b1fc48aae03f35a476f85a5a37dec
Parents: de5eed1
Author: Dave Brondsema <db...@slashdotmedia.com>
Authored: Thu Jun 12 14:33:23 2014 +0000
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Fri Jun 13 17:52:00 2014 +0000

----------------------------------------------------------------------
 Allura/allura/lib/plugin.py | 39 +++++++++++++++++++++++++++++++++++++++
 Allura/development.ini      |  7 +++++++
 Allura/setup.py             |  1 +
 3 files changed, 47 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/ca7c4aaa/Allura/allura/lib/plugin.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/plugin.py b/Allura/allura/lib/plugin.py
index 396da93..ffe9d29 100644
--- a/Allura/allura/lib/plugin.py
+++ b/Allura/allura/lib/plugin.py
@@ -1118,6 +1118,45 @@ class LocalUserPreferencesProvider(UserPreferencesProvider):
             setattr(user, pref_name, pref_value)
 
 
+class LdapUserPreferencesProvider(UserPreferencesProvider):
+    '''
+    Store preferences in LDAP, falling back to LocalUserPreferencesProvider
+    '''
+
+    @LazyProperty
+    def fields(self):
+        return h.config_with_prefix(config, 'user_prefs_storage.ldap.fields.')
+
+    def get_pref(self, user, pref_name):
+        from allura import model as M
+        if pref_name in self.fields and user != M.User.anonymous():
+            con = ldap_conn()
+            try:
+                rs = con.search_s(ldap_user_dn(user.username), ldap.SCOPE_BASE)
+            except ldap.NO_SUCH_OBJECT:
+                rs = []
+            else:
+                con.unbind_s()
+            if not rs:
+                log.warning('LdapUserPref: No user record found for: {}'.format(user.username))
+                return ''
+            user_dn, user_attrs = rs[0]
+            ldap_attr = self.fields[pref_name]
+            return user_attrs[ldap_attr][0]  # assume single-valued list
+        else:
+            return LocalUserPreferencesProvider().get_pref(user, pref_name)
+
+    def set_pref(self, user, pref_name, pref_value):
+        if pref_name in self.fields:
+            con = ldap_conn()
+            ldap_attr = self.fields[pref_name]
+            con.modify_s(ldap_user_dn(user.username),
+                         [(ldap.MOD_REPLACE, ldap_attr, pref_value.encode('utf-8'))])
+            con.unbind_s()
+        else:
+            return LocalUserPreferencesProvider().set_pref(user, pref_name, pref_value)
+
+
 class AdminExtension(object):
 
     """

http://git-wip-us.apache.org/repos/asf/allura/blob/ca7c4aaa/Allura/development.ini
----------------------------------------------------------------------
diff --git a/Allura/development.ini b/Allura/development.ini
index c091b32..4231737 100644
--- a/Allura/development.ini
+++ b/Allura/development.ini
@@ -102,6 +102,13 @@ auth.allow_user_messages_config = true
 # In seconds
 auth.recovery_hash_expiry_period = 600
 
+user_prefs_storage.method = local
+# user_prefs_storage.method = ldap
+# If using ldap, you can specify which fields to use for a preference.
+# Any fields not specified here will be stored locally in mongo
+user_prefs_storage.ldap.fields.display_name = cn
+
+
 # Set the locations of some static resources
 #  script_name is the path that is handled by the application
 #  url_base is the prefix that references to the static resources should have

http://git-wip-us.apache.org/repos/asf/allura/blob/ca7c4aaa/Allura/setup.py
----------------------------------------------------------------------
diff --git a/Allura/setup.py b/Allura/setup.py
index 1671283..8eabf60 100644
--- a/Allura/setup.py
+++ b/Allura/setup.py
@@ -112,6 +112,7 @@ setup(
 
     [allura.user_prefs]
     local = allura.lib.plugin:LocalUserPreferencesProvider
+    ldap = allura.lib.plugin:LdapUserPreferencesProvider
 
     [allura.project_registration]
     local = allura.lib.plugin:LocalProjectRegistrationProvider