cakepphp 2.3:通过自定义BaseAuthenticate进行身份验证

时间:2014-11-04 14:36:21

标签: php cakephp authentication

我写了一个类AlephBaseAuthenticate,它扩展了BaseAuthenticate的工作原理:

<?php

 // Authentication against Aleph X-Service
 // user and password for x-service see aleph.ini

 App::uses('BaseAuthenticate', 'Controller/Component/Auth');
 App::uses('IniReader', 'Configure');
 Configure::config('default', new IniReader());

 class AlephAuthenticate extends BaseAuthenticate {
    public function authenticate(CakeRequest $request, CakeResponse $response) {
        // Do things for Aleph here.
        // Return an array of user + group-id if they could authenticate the user,
        // return false if not
        // verification = empty works, so we have to check lenght of verification!
        $configfile = Configure::read('configfile');
        Configure::load($configfile, 'default');
        $alephxurl = Configure::read('aleph.xurl');
        $xlib = Configure::read('aleph.xlib');

        $username = $request->data['Users']['username'];
        $password = $request->data['Users']['password'];

        $req = $alephxurl . 'library=' . $xlib . '&verification=' . $password . '&op=bor_info&bor_id=' . $username;
        $userxml = file_get_contents($req);
        $xml = simplexml_load_string($userxml);
        $error = $xml->error;
        if($error == 'Error retrieving Patron System Key' || strlen($password) == 0){
            return FALSE;
        } else {
            $data['username'] = $xml->z303->{'z303-name'};
            $data['group'] = $xml->z303->{'z303-profile-id'};
            $data['id'] = $xml->z303->{'z303-id'};
            return $data;
        }        
    }
}

在我的UserController中,我添加了

public $components = array(
        'Session',
        'Auth' => array(
           'authenticate' => array(
              'Aleph'
            )
       )
);

function beforeFilter(){
        $this->Auth->allow('index', 'login', 'logout');
}

问题是:只有在UsersController.php,action&#34; login&#34;,我才能访问这个 - &gt; Auth-&gt;用户(&#39; id&#39;),以及$ this - &gt; Auth-&gt;允许(&#39;索引&#39;,&#39;登录&#39;,&#39;退出&#39;)根本不起作用。登录后访问每个操作&#34;您无权访问该位置&#34;。看起来认证根本不起作用。

这里有什么想法吗?

谢谢,

了Christoph

0 个答案:

没有答案