From 42a996930481783af1a5a3fbbae18dc1201fbb0b Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Tue, 28 Aug 2012 14:24:31 +0200 Subject: [PATCH] LDAP: implement getHome() function, use either username (default) or specify an LDAP attribute value to use --- apps/user_ldap/lib/connection.php | 4 ++- apps/user_ldap/settings.php | 8 +++++- apps/user_ldap/templates/settings.php | 1 + apps/user_ldap/user_ldap.php | 40 ++++++++++++++++++++++++++- 4 files changed, 50 insertions(+), 3 deletions(-) diff --git a/apps/user_ldap/lib/connection.php b/apps/user_ldap/lib/connection.php index dc160a1642..1922e7ff1f 100644 --- a/apps/user_ldap/lib/connection.php +++ b/apps/user_ldap/lib/connection.php @@ -55,6 +55,7 @@ class Connection { 'ldapCacheTTL' => null, 'ldapUuidAttribute' => null, 'ldapOverrideUuidAttribute' => null, + 'homeFolderNamingRule' => null, ); public function __construct($configID = 'user_ldap') { @@ -200,6 +201,7 @@ class Connection { $this->config['ldapCacheTTL'] = \OCP\Config::getAppValue($this->configID, 'ldap_cache_ttl', 10*60); $this->config['ldapUuidAttribute'] = \OCP\Config::getAppValue($this->configID, 'ldap_uuid_attribute', 'auto'); $this->config['ldapOverrideUuidAttribute'] = \OCP\Config::getAppValue($this->configID, 'ldap_override_uuid_attribute', 0); + $this->config['homeFolderNamingRule'] = \OCP\Config::getAppValue($this->configID, 'home_folder_naming_rule', 'opt:username'); $this->configured = $this->validateConfiguration(); } @@ -218,7 +220,7 @@ class Connection { $params = array('ldap_host'=>'ldapHost', 'ldap_port'=>'ldapPort', 'ldap_dn'=>'ldapAgentName', 'ldap_agent_password'=>'ldapAgentPassword', 'ldap_base'=>'ldapBase', 'ldap_base_users'=>'ldapBaseUsers', 'ldap_base_groups'=>'ldapBaseGroups', 'ldap_userlist_filter'=>'ldapUserFilter', 'ldap_login_filter'=>'ldapLoginFilter', 'ldap_group_filter'=>'ldapGroupFilter', 'ldap_display_name'=>'ldapUserDisplayName', 'ldap_group_display_name'=>'ldapGroupDisplayName', - 'ldap_tls'=>'ldapTLS', 'ldap_nocase'=>'ldapNoCase', 'ldap_quota_def'=>'ldapQuotaDefault', 'ldap_quota_attr'=>'ldapQuotaAttribute', 'ldap_email_attr'=>'ldapEmailAttribute', 'ldap_group_member_assoc_attribute'=>'ldapGroupMemberAssocAttr', 'ldap_cache_ttl'=>'ldapCacheTTL'); + 'ldap_tls'=>'ldapTLS', 'ldap_nocase'=>'ldapNoCase', 'ldap_quota_def'=>'ldapQuotaDefault', 'ldap_quota_attr'=>'ldapQuotaAttribute', 'ldap_email_attr'=>'ldapEmailAttribute', 'ldap_group_member_assoc_attribute'=>'ldapGroupMemberAssocAttr', 'ldap_cache_ttl'=>'ldapCacheTTL', 'home_folder_naming_rule' => 'homeFolderNamingRule'); foreach($config as $parameter => $value) { if(isset($this->config[$parameter])) { diff --git a/apps/user_ldap/settings.php b/apps/user_ldap/settings.php index 39aa329626..23018890ea 100644 --- a/apps/user_ldap/settings.php +++ b/apps/user_ldap/settings.php @@ -20,7 +20,7 @@ * License along with this library. If not, see . * */ -$params = array('ldap_host', 'ldap_port', 'ldap_dn', 'ldap_agent_password', 'ldap_base', 'ldap_base_users', 'ldap_base_groups', 'ldap_userlist_filter', 'ldap_login_filter', 'ldap_group_filter', 'ldap_display_name', 'ldap_group_display_name', 'ldap_tls', 'ldap_turn_off_cert_check', 'ldap_nocase', 'ldap_quota_def', 'ldap_quota_attr', 'ldap_email_attr', 'ldap_group_member_assoc_attribute', 'ldap_cache_ttl'); +$params = array('ldap_host', 'ldap_port', 'ldap_dn', 'ldap_agent_password', 'ldap_base', 'ldap_base_users', 'ldap_base_groups', 'ldap_userlist_filter', 'ldap_login_filter', 'ldap_group_filter', 'ldap_display_name', 'ldap_group_display_name', 'ldap_tls', 'ldap_turn_off_cert_check', 'ldap_nocase', 'ldap_quota_def', 'ldap_quota_attr', 'ldap_email_attr', 'ldap_group_member_assoc_attribute', 'ldap_cache_ttl', 'home_folder_naming_rule'); OCP\Util::addscript('user_ldap', 'settings'); OCP\Util::addstyle('user_ldap', 'settings'); @@ -36,6 +36,9 @@ if ($_POST) { $ldap->clearCache(); OCP\Config::setAppValue('user_ldap', $param, $_POST[$param]); } + } elseif('home_folder_naming_rule' == $param) { + $value = empty($_POST[$param]) ? 'opt:username' : 'attr:'.$_POST[$param]; + OCP\Config::setAppValue('user_ldap', $param, $value); } else { OCP\Config::setAppValue('user_ldap', $param, $_POST[$param]); } @@ -67,5 +70,8 @@ $tmpl->assign( 'ldap_group_display_name', OCP\Config::getAppValue('user_ldap', ' $tmpl->assign( 'ldap_group_member_assoc_attribute', OCP\Config::getAppValue('user_ldap', 'ldap_group_member_assoc_attribute', 'uniqueMember')); $tmpl->assign( 'ldap_agent_password', base64_decode(OCP\Config::getAppValue('user_ldap', 'ldap_agent_password'))); $tmpl->assign( 'ldap_cache_ttl', OCP\Config::getAppValue('user_ldap', 'ldap_cache_ttl', '600')); +$hfnr = OCP\Config::getAppValue('user_ldap', 'home_folder_naming_rule', 'opt:username'); +$hfnr = ($hfnr == 'opt:username') ? '' : substr($hfnr, strlen('attr:')); +$tmpl->assign( 'home_folder_naming_rule', $hfnr, ''); return $tmpl->fetchPage(); diff --git a/apps/user_ldap/templates/settings.php b/apps/user_ldap/templates/settings.php index 861c9ba605..d1aaeedbdf 100644 --- a/apps/user_ldap/templates/settings.php +++ b/apps/user_ldap/templates/settings.php @@ -27,6 +27,7 @@

+

t('Help');?> diff --git a/apps/user_ldap/user_ldap.php b/apps/user_ldap/user_ldap.php index a97df7b4fd..d297374600 100644 --- a/apps/user_ldap/user_ldap.php +++ b/apps/user_ldap/user_ldap.php @@ -161,6 +161,44 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface { } /** + * @brief determine the user's home directory + * @param string $uid the owncloud username + * @return boolean + */ + private function determineHomeDir($uid) { + if(strpos($this->connection->homeFolderNamingRule, 'attr:') === 0) { + $attr = substr($this->connection->homeFolderNamingRule, strlen('attr:')); + $homedir = $this->readAttribute($this->username2dn($uid), $attr); + if($homedir) { + $homedir = \OCP\Config::getSystemValue( "datadirectory", \OC::$SERVERROOT."/data" ) . '/' . $homedir[0]; + \OCP\Config::setUserValue($uid, 'user_ldap', 'homedir', $homedir); + return $homedir; + } + } + + //fallback and default: username + $homedir = \OCP\Config::getSystemValue( "datadirectory", \OC::$SERVERROOT."/data" ) . '/' . $uid; + \OCP\Config::setUserValue($uid, 'user_ldap', 'homedir', $homedir); + return $homedir; + } + + /** + * @brief get the user's home directory + * @param string $uid the username + * @return boolean + */ + public function getHome($uid){ + if($this->userExists($uid)) { + $homedir = \OCP\Config::getUserValue($uid, 'user_ldap', 'homedir', false); + if(!$homedir) { + $homedir = $this->determineHomeDir($uid); + } + return $homedir; + } + return false; + } + + /** * @brief Check if backend implements actions * @param $actions bitwise-or'ed actions * @returns boolean @@ -169,7 +207,7 @@ class USER_LDAP extends lib\Access implements \OCP\UserInterface { * compared with OC_USER_BACKEND_CREATE_USER etc. */ public function implementsActions($actions) { - return (bool)(OC_USER_BACKEND_CHECK_PASSWORD & $actions); + return (bool)((OC_USER_BACKEND_CHECK_PASSWORD | OC_USER_BACKEND_GET_HOME) & $actions); } } \ No newline at end of file