用于用户身份验证的YII LDAP连接

时间:2013-11-21 10:58:10

标签: yii ldap yii-extensions

我是yii框架工作的新手我正在尝试为用户身份验证创建ldap配置 我采取了以下步骤来创建,但它会抛出如下的

include(Controller.php)[function.include]:无法打开流:没有这样的文件或目录

C:\ xampp \ htdocs \ yiif \ framework \ YiiBase.php(418)

步骤: 1.我已经包含以下ldaprecord / C:\ xampp \ htdocs \ seed2 \ protected \ extensions \ ldaprecord中的扩展名 2.我在config / main.php中包含了以下代码

`'components'=>array(
'user'=>array(
// enable cookie-based authentication
'allowAutoLogin'=>true,
),
'ldap'=>array(
'class' => 'ext.ldaprecord.LdapComponent',
'server' => 'ldap://192.168.x.xxxx',
'port' => 389,
//'bind_rdn' => 'cn=suren diran,cn=Users,dc=xxxx,dc=demo,dc=com',
//'bind_pwd' => 'pass@123',
'base_dn' => 'dc=rsales,dc=demo,dc=com'),);
'params'=>array(
// this is used in contact page
'adminEmail'=>'sundarapandian@rsalesarm.com',
),
'ldap'=>array(
'class' => 'ext.ldaprecord.LdapComponent',
'server' => 'ldap://192.168.x.xxx',
'port' => 389,
//'bind_rdn' => 'cn=xxxx xxxx,cn=Users,dc=xxxx,dc=demo,dc=com',
//'bind_pwd' => 'pass@123',
'base_dn' => 'dc=xxxxx,dc=demo,dc=com'),
);`
  1. 在index.php中包含了这一行

    $config=dirname(__FILE__).'/protected/extensions/ldaprecord/CLdapRecord.php';

  2. 我已将用户身份验证功能更改为

  3. `     public function authenticate()     {     $ USERNAME = $这 - >用户名;     $密码= $这 - >密码;     $ dname =' xxxxxxx&#39 ;;     $选项['主机'] =' LDAP://192.168.x.xxx' ;;     $选项['端口&#39] = 389;     $ ldap_username =" CN ="。$ username。",CN = Users,DC = xxxx,DC = demo,DC = com&#34 ;;

    $options = Yii::app()->params['ldap'];
    print_r($options);
    $connection = ldap_connect($options['host'], $options['port']);
    
    //print_r($connection);
    ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
    ldap_set_option($connection, LDAP_OPT_REFERRALS, 0);
    
    if($connection)
    {
    echo "success";
    //exit;
    try
    {
    //echo $connection.$ldap_username.$this->password;
    //print_r(ldap_bind($connection,$dname."\\". $ldap_username, $password));
    //exit;
    @$bind = ldap_bind($connection,$dname."\\". $ldap_username, $password);
    print_r(@$bind);
    if(@$bind)
    {
    echo "successfully logedin";
    }
    }
    catch (Exception $e){
    echo $e->getMessage();
    }
    if(!$bind) $this->errorCode = self::ERROR_PASSWORD_INVALID;
    else $this->errorCode = self::ERROR_NONE;
    }
    return !$this->errorCode;
    };`
    

    我被困在这里任何人都可以帮我解决这个问题。请帮助我,提前致谢..........

    controller.php文件存在于protected / components中 甚至我检查它是否包含在主.php中

    'import'=>array(
            'application.models.*',
            'application.components.*',
            'ext.ldaprecord.*',
        ),
    

    如果我删除以下行,将显示以下错误 代码:

    $config=dirname(__FILE__).'/protected/extensions/ldaprecord/CLdapRecord.php'
    

    错误: Property" CWebApplication.ldap"没有定义。

    是的我只使用了上面提到的链接([http://www.yiiframework.com/extension/ldaprecord/]),如果你有任何其他扩展名,请你能提供那个链接以及步骤是什么需要采取以实现.........提前感谢..........

    HIIIIII

    我在params中犯了错误,现在我已经改变了,如下所示,我删除了

    $config=dirname(__FILE__).'/protected/extensions/ldaprecord/CLdapRecord.php';
    

    它没有抛出任何错误。

    我在下面包含了我的main.php完整代码:

    <?php
       return array(
        'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
        'name'=>'xxxxxx',
    
        // preloading 'log' component
        'preload'=>array('log'),
    
        // autoloading model and component classes
        'import'=>array(
            'application.models.*',
            'application.components.*',
            //ldap config added code
             'ext.ldaprecord.*',
    
        ),
    
        'defaultController' => 'site/login',
    
        'modules'=>array(
            // uncomment the following to enable the Gii tool
    
            'gii'=>array(
                'class'=>'system.gii.GiiModule',
                'password'=>'pass',
                // If removed, Gii defaults to localhost only. Edit carefully to taste.
                'ipFilters'=>array('127.0.0.1','::1'),
            ),
    
        ),
    
        // application components
        'components'=>array(
            'user'=>array(
                // enable cookie-based authentication
                'allowAutoLogin'=>true,
            ),
    
            // uncomment the following to enable URLs in path-format
    
            /*'urlManager'=>array(
                'urlFormat'=>'path',
                'rules'=>array(
                    '<controller:\w+>/<id:\d+>'=>'<controller>/view',
                    '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>',
                    '<controller:\w+>/<action:\w+>'=>'<controller>/<action>',
                ),
            ),*/
    
                /*'db'=>array(
                'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/testdrive.db',
            ),*/
            // uncomment the following to use a MySQL database
    
            'db'=>array(
                'connectionString' => 'mysql:host=localhost;dbname=xxxxx',
                'emulatePrepare' => true,
                'username' => 'root',
                'password' => '',
                'charset' => 'utf8',
                'tablePrefix' => '',
            ),
    
            'errorHandler'=>array(
                // use 'site/error' action to display errors
                'errorAction'=>'site/error',
            ),
            'log'=>array(
                'class'=>'CLogRouter',
                'routes'=>array(
                    array(
                        'class'=>'CFileLogRoute',
                        'levels'=>'error, warning',
                    ),
                    // uncomment the following to show log messages on web pages
                    /*
                    array(
                        'class'=>'CWebLogRoute',
                    ),
                    */
                ),
            ),
    
    
    
            'ldap'=>array(
                'class' => 'ext.ldaprecord.LdapComponent',
                'server' => 'ldap://192.168.x.xxx',
                'port' => 389,
                //'bind_rdn' => 'cn=suren diran,cn=Users,dc=xxxxxx,dc=demo,dc=com',
                //'bind_pwd' => 'pass@123',
                'base_dn' => 'dc=xxxxx,dc=demo,dc=com'),
    
    
    
    
        ),
    
        // application-level parameters that can be accessed
        // using Yii::app()->params['paramName']
        'params'=>array(
            // this is used in contact page
            'adminEmail'=>'xxx@xxxx.com',
            'ldap'=>array(
                'class' => 'ext.ldaprecord.LdapComponent',
                'server' => 'ldap://192.168.x.xxx',
                'port' => 389,
                //'bind_rdn' => 'cn=suren diran,cn=Users,dc=xxxxx,dc=demo,dc=com',
                //'bind_pwd' => 'pass@123',
                'base_dn' => 'dc=xxxx,dc=demo,dc=com'),
        ),
    
    );
    

    我在下面包含了我的用户身份功能:

    <?php
     public function authenticate()
    
    {
            $options = Yii::app()->params['ldap'];
            //print_r($options);
            $connection = ldap_connect($options['host'], $options['port']);
            ldap_set_option($connection, LDAP_OPT_PROTOCOL_VERSION, 3);
            ldap_set_option($connection, LDAP_OPT_REFERRALS, 0);
    
            if($connection)
            {
               // try
              //  {
                    //echo $options['domain']."\\".$this->username. $this->password;
                    //exit;
                    $ldap_username = "CN=".$this->username.",CN=xxxx,DC=xxxx,DC=demo,DC=com";
    
            $ldap_password=$this->password;
            echo $connection.$ldap_username.$ldap_password;
            //$bind=ldap_bind($connection, $ldap_username, $ldap_password)
                    //exit;
                    //print_r(ldap_bind($connection,$ldap_username,$ldap_password));
                    $bind = @ldap_bind($connection,$ldap_username, $ldap_password);
                    print_r($bind);
                    //exit;
               //@$bind = ldap_bind($connection,$ldap_username, $ldap_password);
                //   $abc=@$bind;
                   //print_r($abc);
    
                   //exit;
                /*}
                catch (Exception $e){
                    echo $e->getMessage();
                }*/
                if(!$bind) $this->errorCode = self::ERROR_PASSWORD_INVALID;
                else $this->errorCode = self::ERROR_NONE;
            }
            return !$this->errorCode;
    }
    

    但是在上面的一个ldap_bind不工作可以帮助你........ 请帮助我如何克服这个.........非常感谢.....

2 个答案:

答案 0 :(得分:1)

我刚读过这篇文章:

$config=dirname(__FILE__).'/protected/extensions/ldaprecord/CLdapRecord.php'

你为什么这样做?我假设您正在使用this扩展名:它不建议在文档中的任何位置执行此操作。这几乎清空了应用程序的配置 - 包括最终让import的包含失败的Controller.php - 节。

答案 1 :(得分:0)

由于您要求进行其他扩展,我会指向this blog post。它不是一个扩展,但在如何设置它时相当详细,看起来它将为你实现相同的最终目标。