users
[ class tree: users ] [ index: users ] [ all elements ]

Source for file auth.php

Documentation is available at auth.php

  1. <?php
  2. /**
  3.  * $Header$
  4.  *
  5.  * @package users
  6.  */
  7.  
  8. /**
  9.  * required setup
  10.  */
  11. if (file_exists(UTIL_PKG_PATH."pear/Auth/Auth.php")) {
  12.     require_once (UTIL_PKG_PATH."pear/Auth/Auth.php");
  13. else {
  14. // THIS may need changing if a different PEAR installation is used
  15.     include_once("Auth/Auth.php");
  16. }
  17.  
  18. /**
  19.  * Class that manages the PEAR:ldap autentication method
  20.  *
  21.  * @package users
  22.  * @subpackage auth
  23.  */
  24. class LDAPAuth extends BaseAuth {
  25.     function LDAPAuth({
  26.         parent::__construct('ldap');
  27.     }
  28.  
  29.     function validate($user,$pass,$challenge,$response{
  30.         parent::validate($user,$pass,$challenge,$response);
  31.         global $gBitDb;
  32.  
  33.         if empty($useror empty($pass) ) {
  34.             return USER_NOT_FOUND;
  35.         }
  36.  
  37.         $this->mInfo["real_name"'';  // This needs fixing in the base code - real_name will only exist if a user has been identiied
  38.  
  39.         // Use V3, which requires UTF-8:
  40.         $this->mConfig['version'3;
  41.         $user_utf8 utf8_encode$user );
  42.  
  43.         if $this->mConfig['reqcert'{
  44.             // Skip the SSL certificate check:
  45.             // (This assumes PHP is using the OpenLDAP client library.)
  46.             putenv('LDAPTLS_REQCERT=never');
  47.         }
  48.  
  49.         if $this->mConfig['activedirectory'{
  50.             $this->mConfig['attributes'= (array) null;
  51.             $this->mConfig['userfilter''(objectClass='.$this->mConfig['useroc'].')';
  52.             $this->mConfig['groupfilter''(objectClass='.$this->mConfig['groupoc'].')';
  53.             $this->mConfig['groupscope'$this->mConfig['userscope'];
  54.         else {
  55.             // Using bitweaver groups with LDAP still needs completing so disable for now
  56.             unset($this->mConfig['group']);
  57.         }
  58.  
  59.         $a new Auth('LDAP'$this->mConfig""false);
  60.         $a->_loadStorage();  // set up connection to ldap via user details
  61.  
  62.         // First, try by username.  If that fails, try by email address.
  63.         $success $a->storage->fetchData($user_utf8$passfalse);
  64.  
  65.         if ($success == false{
  66.             // The user wasn't found.  Try again by email address:
  67.             $this->mConfig['userattrsto'$this->mConfig['userattr'];  // Keep this for later
  68.             $this->mConfig['userattr'$this->mConfig['email'];  // Tell PEAR::Auth() to look at the 'mail' attribute
  69.  
  70.             // this needs testing better, should be no need to create second instance of Auth!
  71.             $a new Auth('LDAP'$this->mConfig""false);
  72.             $a->_loadStorage();  // set up connection to ldap via user details
  73.  
  74.             $success $a->storage->fetchData($user_utf8$passfalse);
  75.             if ($success == false{
  76.                 $this->mErrors['login'= isset($a->storage->options['status']$a->storage->options['status''Not authenticated';
  77.                 return PASSWORD_INCORRECT;
  78.             }
  79.         }
  80.  
  81.         // At this point, there was a successful ldap_bind() using the
  82.         // user's Distinguished Name (DN) and password for login.
  83.         // The call to ldap_get_attributes() has been saved into $a->getAuthData('attributes')
  84.  
  85.         if $this->mConfig['activedirectory'{
  86.             // Active Directory does some things differently - mainly in the returns
  87.             $attributes $a->getAuthData();
  88.             // Warning: ldap_get_attributes() uses case-sensitive array keys
  89.             $this->mInfo["login"$attributes$this->mConfig['userattr'] ];
  90.             $this->mInfo["email"$attributes$this->mConfig['email'] ];
  91.             $this->mInfo["real_name"empty($attributes[$this->mConfig['name']]$this->mInfo["login"$attributes[$this->mConfig['name']];
  92.         }
  93.         else {
  94.             $attributes $a->getAuthData('attributes');
  95.             // Warning: ldap_get_attributes() uses case-sensitive array keys
  96.             $this->mInfo["login"$attributes$this->mConfig['userattr'] ][0];
  97.             $this->mInfo["email"$attributes$this->mConfig['email'] ][0];
  98.             $this->mInfo["real_name"empty($attributes[$this->mConfig['name']][0]$this->mInfo["login"$attributes[$this->mConfig['name']][0];
  99.         }
  100.         // Note, the new (or updated) SQL user will be created by the calling BitUser class.
  101.  
  102.         return USER_VALID;  // Success!
  103.  
  104.     }
  105.  
  106.     function isSupported({
  107.         $ret true;
  108.         if (!class_exists("Auth")) {
  109.             $this->mErrors['support']=tra("LDAP Authentication is not supported as PEAR Package Auth is not availible.");
  110.             $ret false;
  111.         }
  112.         if (!function_exists('ldap_connect')) {
  113.             $this->mErrors['support']=tra("LDAP Authentication is not supported as PHP LDAP Extention not loaded.");
  114.             $ret false;
  115.         }
  116.         return $ret;
  117.     }
  118.  
  119.     // create a new user in the Auth directory
  120.     function createUser(&$userattr{
  121.         global $gBitDb;
  122.         // set additional attributes here
  123.         if (empty($userattr["email"])) {
  124.             $userattr["email"$gBitDb->getOne("select `email` from `".BIT_DB_PREFIX."users_users` where `login`=?"array($userattr["login"]));
  125.         }
  126.         // set the Auth options
  127.         $a new Auth("LDAP"$this->mConfig);
  128.         // check if the login correct
  129.         if ($a->addUser($userattr["login"]$userattr["password"]$userattr=== true{
  130.             return true;
  131.         else {
  132.             // otherwise use the error status given back
  133.             $this->mErrors['create'$a->getStatus();
  134.             return false;
  135.         }
  136.     }
  137.  
  138.     function canManageAuth({
  139.         return true;
  140.     }
  141.  
  142.     function getSettings({
  143.         global $gBitUser;
  144.         $listHash array();
  145.  
  146.         // Roles are not inteneded to match with ldap groups
  147.         // This area needs a closer look if it needs to be used
  148.         $groups array();
  149.         if !defined ('ROLE_MODEL') ) $groups $gBitUser->getAllGroups($listHash);
  150.         $groupsD array();
  151.         foreach ($groups as $g{
  152.             $groupsD[$g['group_id']]"{$g['group_name']} ( {$g['group_desc']} )";
  153.         }
  154.         $groups $groupsD;
  155.         return array(
  156.         'users_ldap_url' => array(
  157.             'label' => "LDAP Connection URL",
  158.             'type' => "text",
  159.             'note' => "You can specify an LDAP URL, like ldap://localhost/ or ldaps://some-server/.",
  160.             'default' => '',
  161.         ),
  162.         'users_ldap_host' => array(
  163.             'label' => "LDAP Host",
  164.             'type' => "text",
  165.             'note' => "Instead of a URL, you can specify a hostname and port explicitly.  Give either a URL, or else a hostname/port (but not both).",
  166.             'default' => 'localhost',
  167.         ),
  168.         'users_ldap_port' => array(
  169.             'label' => "LDAP Port",
  170.             'type' => "text",
  171.             'note' => "",
  172.             'default' => '389',
  173.         ),
  174.         'users_ldap_start_tls' => array(
  175.             'label' => "Use Start-TLS?",
  176.             'type' => "checkbox",
  177.             'note' => "Please note there is a difference between ldaps:// and Start-TLS for ldap.  Start-TLS uses port 389, while ldaps:// uses port 636.  Both encrypted LDAP (with Start-TLS) and unencrypted LDAP can run on port 389 concurrently.",
  178.             'default' => 'y',
  179.         ),
  180.         'users_ldap_reqcert' => array(
  181.             'label' => "Skip the SSL Cert validation?",
  182.             'type' => "checkbox",
  183.             'note' => "If Start-TLS is checked, then your LDAP server needs a trusted SSL cert -- unless you check this option, in which case you can use a self-signed (untrusted) cert.",
  184.             'default' => 'y',
  185.         ),
  186.         'users_ldap_referrals' => array(
  187.             'label' => "Use Referrals?",
  188.             'type' => "checkbox",
  189.             'note' => "This should probably be 'yes'.  (Only applies to LDAP V3 servers.)",
  190.             'default' => 'y',
  191.         ),
  192.         'users_ldap_basedn' => array(
  193.             'label' => "LDAP Base DN",
  194.             'type' => "text",
  195.             'note' => "",
  196.             'default' => '',
  197.         ),
  198.         'users_ldap_userdn' => array(
  199.             'label' => "LDAP User DN",
  200.             'type' => "text",
  201.             'note' => "",
  202.         'default' => '',
  203.         ),
  204.         'users_ldap_userattr' => array(
  205.             'label' => "LDAP User Attribute",
  206.             'type' => "text",
  207.             'note' => "The LDAP Attribute to use for the user's login in Bitweaver.  (This is the first attribute searched when the user logs in.)",
  208.             'default' => 'uid',
  209.         ),
  210.         'users_ldap_email' => array(
  211.             'label' => "LDAP User E-Mail Address",
  212.             'type' => "text",
  213.             'note' => "The LDAP Attribute to use for the user's email address in Bitweaver.  (This is the second attribute searched when the user logs in.)",
  214.             'default' => 'mail',
  215.         ),
  216.         'users_ldap_name' => array(
  217.             'label' => "LDAP User Display Name",
  218.             'type' => "text",
  219.             'note' => "The LDAP Attribute to use for the user's Full Name in Bitweaver.",
  220.             'default' => 'displayName',
  221.         ),
  222.         'users_ldap_useroc' => array(
  223.             'label' => "LDAP User OC",
  224.             'type' => "text",
  225.             'note' => "",
  226.         'default' => '(objectClass=inetOrgPerson)',
  227.         ),
  228.         'users_ldap_groupdn' => array(
  229.             'label' => "LDAP Group DN",
  230.             'type' => "text",
  231.             'note' => "",
  232.             'default' => '',
  233.         ),
  234.         'users_ldap_groupattr' => array(
  235.             'label' => "LDAP Group Atribute",
  236.             'type' => "text",
  237.             'note' => "",
  238.             'default' => 'cn',
  239.         ),
  240.         'users_ldap_groupoc' => array(
  241.             'label' => "LDAP Group OC",
  242.             'type' => "text",
  243.             'note' => "",
  244.             'default' => '(objectClass=groupOfUniqueNames)',
  245.         ),
  246.         'users_ldap_memberattr' => array(
  247.             'label' => "LDAP Member Attribute",
  248.             'type' => "text",
  249.             'note' => "",
  250.             'default' => 'uniqueMember',
  251.         ),
  252.         'users_ldap_memberisdn' => array(
  253.             'label' => "LDAP Member Is DN",
  254.             'type' => "checkbox",
  255.             'note' => "",
  256.             'default' => 'n',
  257.         ),
  258.         'users_ldap_binddn' => array(
  259.             'label' => "LDAP Bind DN",
  260.             'type' => "text",
  261.             'note' => "This DN will be used to search the LDAP directory for users.  If left blank, 'anonymous bind' is used.",
  262.             'default' => '',
  263.         ),
  264.         'users_ldap_bindpw' => array(
  265.             'label' => "LDAP Bind Pwd",
  266.             'type' => "password",
  267.             'note' => "",
  268.             'default' => '',
  269.         ),
  270.         'users_ldap_userscope' => array(
  271.             'label' => "LDAP Scope to use when searching for users",
  272.             'type' => "option",
  273.             'note' => "",
  274.             'default' => 'sub',
  275.             'options' => array(
  276.                 'sub' => "Sub",
  277.                 'one' => "One",
  278.                 'base' => "Base",
  279.             ),
  280.         ),
  281.         'users_ldap_group' => array(
  282.             'label' => "LDAP Group Requirement",
  283.             'type' => "text",
  284.             'note' => "If this is specified, then the LDAP user must also be a member of this LDAP group to connect.",
  285.             'default' => ''
  286.         ),
  287.         'users_ldap_activedirectory' => array(
  288.             'label' => "Active Directory?",
  289.             'type' => "checkbox",
  290.             'note' => "",
  291.             'default' => 'n'
  292.         ),
  293.     );
  294.     }
  295. }
  296.  
  297. ?>

Documentation generated on Wed, 29 Jul 2015 13:55:36 +0000 by phpDocumentor 1.5.0-lsces