]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/LdapAuthorization/LdapAuthorizationPlugin.php
Merge branch 'master' of git@gitorious.org:statusnet/mainline into testing
[quix0rs-gnu-social.git] / plugins / LdapAuthorization / LdapAuthorizationPlugin.php
index 91a343f4087ae0dfd5c8f953c70042a6428e4be2..042b2db8d8796905a8a1df2695d31395ae452d01 100644 (file)
@@ -31,7 +31,6 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
 }
 
-require_once INSTALLDIR.'/plugins/Authorization/AuthorizationPlugin.php';
 require_once 'Net/LDAP2.php';
 
 class LdapAuthorizationPlugin extends AuthorizationPlugin
@@ -53,7 +52,6 @@ class LdapAuthorizationPlugin extends AuthorizationPlugin
     public $attributes = array();
 
     function onInitializePlugin(){
-        parent::onInitializePlugin();
         if(!isset($this->host)){
             throw new Exception("must specify a host");
         }
@@ -133,13 +131,13 @@ class LdapAuthorizationPlugin extends AuthorizationPlugin
     {
         $ldap = $this->ldap_get_connection();
         $link = $ldap->getLink();
-        $r = ldap_compare($link, $groupDn, $this->uniqueMember_attribute, $userDn);
+        $r = @ldap_compare($link, $groupDn, $this->uniqueMember_attribute, $userDn);
         if ($r === true){
             return true;
         }else if($r === false){
             return false;
         }else{
-            common_log(LOG_ERR, ldap_error($r));
+            common_log(LOG_ERR, "LDAP error determining if userDn=$userDn is a member of groupDn=groupDn using uniqueMember_attribute=$this->uniqueMember_attribute error: ".ldap_error($link));
             return false;
         }
     }
@@ -158,24 +156,26 @@ class LdapAuthorizationPlugin extends AuthorizationPlugin
 
     //-----the below function were copied from LDAPAuthenticationPlugin. They will be moved to a utility class soon.----\\
     function ldap_get_connection($config = null){
-        if($config == null){
-            static $ldap = null;
-            if($ldap != null){
-                return $ldap;
-            }
-            $config = $this->ldap_get_config();
+        if($config == null && isset($this->default_ldap)){
+            return $this->default_ldap;
         }
         
         //cannot use Net_LDAP2::connect() as StatusNet uses
         //PEAR::setErrorHandling(PEAR_ERROR_CALLBACK, 'handleError');
         //PEAR handling can be overridden on instance objects, so we do that.
-        $ldap = new Net_LDAP2($config);
+        $ldap = new Net_LDAP2(isset($config)?$config:$this->ldap_get_config());
         $ldap->setErrorHandling(PEAR_ERROR_RETURN);
         $err=$ldap->bind();
         if (Net_LDAP2::isError($err)) {
-            common_log(LOG_WARNING, 'Could not connect to LDAP server: '.$err->getMessage());
+            // if we were called with a config, assume caller will handle
+            // incorrect username/password (LDAP_INVALID_CREDENTIALS)
+            if (isset($config) && $err->getCode() == 0x31) {
+                return null;
+            }
+            throw new Exception('Could not connect to LDAP server: '.$err->getMessage());
             return false;
         }
+        if($config == null) $this->default_ldap=$ldap;
         return $ldap;
     }
     
@@ -190,6 +190,9 @@ class LdapAuthorizationPlugin extends AuthorizationPlugin
         if($ldap==null) {
             $ldap = $this->ldap_get_connection();
         }
+        if(! $ldap) {
+            throw new Exception("Could not connect to LDAP");
+        }
         $filter = Net_LDAP2_Filter::create($this->attributes['username'], 'equals',  $username);
         $options = array(
             'attributes' => $attributes
@@ -211,4 +214,15 @@ class LdapAuthorizationPlugin extends AuthorizationPlugin
             return false;
         }
     }
+
+    function onPluginVersion(&$versions)
+    {
+        $versions[] = array('name' => 'LDAP Authorization',
+                            'version' => STATUSNET_VERSION,
+                            'author' => 'Craig Andrews',
+                            'homepage' => 'http://status.net/wiki/Plugin:LdapAuthorization',
+                            'rawdescription' =>
+                            _m('The LDAP Authorization plugin allows for StatusNet to handle authorization through LDAP.'));
+        return true;
+    }
 }