Source for file ldap.php

Documentation is available at ldap.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Plugin
  4.  * @subpackage  Authentication.ldap
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE.txt
  8.  */
  9.  
  10. defined('_JEXEC'or die;
  11.  
  12. /**
  13.  * LDAP Authentication Plugin
  14.  *
  15.  * @package     Joomla.Plugin
  16.  * @subpackage  Authentication.ldap
  17.  * @since       1.5
  18.  */
  19. {
  20.     /**
  21.      * This method should handle any authentication and report back to the subject
  22.      *
  23.      * @param   array   $credentials  Array holding the user credentials
  24.      * @param   array   $options      Array of extra options
  25.      * @param   object  &$response    Authentication response object
  26.      *
  27.      * @return  boolean 
  28.      *
  29.      * @since   1.5
  30.      */
  31.     public function onUserAuthenticate($credentials$options&$response)
  32.     {
  33.         $userdetails null;
  34.         $success 0;
  35.         $userdetails array();
  36.  
  37.         // For JLog
  38.         $response->type 'LDAP';
  39.  
  40.         // LDAP does not like Blank passwords (tries to Anon Bind which is bad)
  41.         if (empty($credentials['password']))
  42.         {
  43.             $response->status JAuthentication::STATUS_FAILURE;
  44.             $response->error_message JText::_('JGLOBAL_AUTH_PASS_BLANK');
  45.  
  46.             return false;
  47.         }
  48.  
  49.         // Load plugin params info
  50.         $ldap_email        $this->params->get('ldap_email');
  51.         $ldap_fullname    $this->params->get('ldap_fullname');
  52.         $ldap_uid        $this->params->get('ldap_uid');
  53.         $auth_method    $this->params->get('auth_method');
  54.  
  55.         $ldap new JClientLdap($this->params);
  56.  
  57.         if (!$ldap->connect())
  58.         {
  59.             $response->status JAuthentication::STATUS_FAILURE;
  60.             $response->error_message JText::_('JGLOBAL_AUTH_NO_CONNECT');
  61.  
  62.             return;
  63.         }
  64.  
  65.         switch ($auth_method)
  66.         {
  67.             case 'search':
  68.             {
  69.                 // Bind using Connect Username/password
  70.                 // Force anon bind to mitigate misconfiguration like [#7119]
  71.                 if (strlen($this->params->get('username')))
  72.                 {
  73.                     $bindtest $ldap->bind();
  74.                 }
  75.                 else
  76.                 {
  77.                     $bindtest $ldap->anonymous_bind();
  78.                 }
  79.  
  80.                 if ($bindtest)
  81.                 {
  82.                     // Search for users DN
  83.                     $binddata $ldap->simple_search(str_replace("[search]"$credentials['username']$this->params->get('search_string')));
  84.  
  85.                     if (isset($binddata[0]&& isset($binddata[0]['dn']))
  86.                     {
  87.                         // Verify Users Credentials
  88.                         $success $ldap->bind($binddata[0]['dn']$credentials['password']1);
  89.  
  90.                         // Get users details
  91.                         $userdetails $binddata;
  92.                     }
  93.                     else
  94.                     {
  95.                         $response->status JAuthentication::STATUS_FAILURE;
  96.                         $response->error_message JText::_('JGLOBAL_AUTH_USER_NOT_FOUND');
  97.                     }
  98.                 }
  99.                 else
  100.                 {
  101.                     $response->status JAuthentication::STATUS_FAILURE;
  102.                     $response->error_message JText::_('JGLOBAL_AUTH_NO_BIND');
  103.                 }
  104.             }    break;
  105.  
  106.             case 'bind':
  107.             {
  108.                 // We just accept the result here
  109.                 $success $ldap->bind($credentials['username']$credentials['password']);
  110.  
  111.                 if ($success)
  112.                 {
  113.                     $userdetails $ldap->simple_search(str_replace("[search]"$credentials['username']$this->params->get('search_string')));
  114.                 }
  115.                 else
  116.                 {
  117.                     $response->status JAuthentication::STATUS_FAILURE;
  118.                     $response->error_message JText::_('JGLOBAL_AUTH_BIND_FAILED');
  119.                 }
  120.             }    break;
  121.         }
  122.  
  123.         if (!$success)
  124.         {
  125.             $response->status JAuthentication::STATUS_FAILURE;
  126.  
  127.             if (!strlen($response->error_message))
  128.             {
  129.                 $response->error_message JText::_('JGLOBAL_AUTH_INCORRECT');
  130.             }
  131.         }
  132.         else
  133.         {
  134.             // Grab some details from LDAP and return them
  135.             if (isset($userdetails[0][$ldap_uid][0]))
  136.             {
  137.                 $response->username $userdetails[0][$ldap_uid][0];
  138.             }
  139.  
  140.             if (isset($userdetails[0][$ldap_email][0]))
  141.             {
  142.                 $response->email $userdetails[0][$ldap_email][0];
  143.             }
  144.  
  145.             if (isset($userdetails[0][$ldap_fullname][0]))
  146.             {
  147.                 $response->fullname $userdetails[0][$ldap_fullname][0];
  148.             }
  149.             else
  150.             {
  151.                 $response->fullname $credentials['username'];
  152.             }
  153.  
  154.             // Were good - So say so.
  155.             $response->status        JAuthentication::STATUS_SUCCESS;
  156.             $response->error_message '';
  157.         }
  158.  
  159.         $ldap->close();
  160.     }
  161. }

Documentation generated on Tue, 19 Nov 2013 15:06:43 +0100 by phpDocumentor 1.4.3