1

我正在为 CakePHP 2 编写自己的 Auth 组件,它继承自 BaseAuthenticate。该组件使用一个外部库(位于 /usr/share/php),它将信息保存在 $_SESSION 变量中。我的问题是,当我更改页面时,所有这些信息都会从 $_SESSION 中删除,因此外部库看不到我已经连接。

我试图通过在 Auth.MyAuthenticateComponent 中使用 $this->Session->write 来保存 lib 添加的 $_SESSION 的内容,但这也被删除了。

谢谢你的帮助。

编辑 :

编码 :

class AppController extends Controller {
    public $use = array('User');
    public $components = array(
        'Session',
        'Auth' => array(
            'loginRedirect' => array('controller' => 'Sheets', 'action' => 'index'),
            'logoutRedirect' => array('controller' => 'Users', 'action' => 'login')
        )
    );

    public function beforeFilter()
    {
        $this->Auth->authenticate = array('Arise');
    }
}

class UsersController extends AppController
{
    public $helpers = array('Html', 'Form');

    public function beforeFilter()
    {
        parent::beforeFilter();
        $this->Auth->allow('login');
        $this->Auth->authenticate = array('Arise');
    }

    public function login()
    {
        if ($this->request->is('post')) {
           if ($this->Auth->login())
                return $this->redirect($this->Auth->redirect());
            else
                $this->Session->setFlash('Error');
        }
    }

    public function logout()
    {
        $this->redirect($this->Auth->logout());
    }
}

App::uses('BaseAuthenticate', 'Controller/Component/Auth');
require_once('/usr/share/php/openid/consumer/consumer.php');

class AriseAuthenticate extends BaseAuthenticate
{
    protected function _ariseAuthenticate($openid_url)
    {
        $consumer =& AriseOpenID::getInstance();
        $required = array(
            'http://somewhere/types/identifiant',
            'http://axschema.org/namePerson/first',
            'http://axschema.org/namePerson/friendly'
        );

        $consumer->setReturnTo('http://mysite/users/login');
        $consumer->setTrustRoot('http://mysite/users/login');
        $consumer->authenticate($openid_url, $required);

        if ($consumer->isLogged()) {
            $first_name = $consumer->getSingle('http://axschema.org/namePerson/first');
            $nick = $consumer->getSingle('http://axschema.org/namePerson/friendly');
            $id_arise = $consumer->getSingle('http://openid.iiens.net/types/identifiant');

            return array(
                'id_arise' => $id_arise,
                'first_name' => $first_name,
                'nick' => $nick
            );
        }

        return false;
    }

    public function checkUser($result)
    {
        $User = ClassRegistry::init('User');
        $result = $User->find('first', array(
            'conditions' => array(
                'id_arise' => $result['id_arise']
            )
        ));

        if (!$result) {
            $User->create();
            $User->save(array(
                'id_arise' => $result['id_arise'],
                'first_name' => $result['first_name'],
                'nick' => $result['nick']
            ));

            $result = $User->find('first', array(
                'conditions' => array(
                    'id_arise' => $result['id_arise']
                )
            ));
        }

        $user = $result['User'];
        unset($result['User']);

        return array_merge($user, $result);
    }

    public function authenticate(CakeRequest $request, CakeResponse $response)        
    {
        if (!$request->is('post'))
            return false;

        $openid_url = (array_key_exists('login', $request->data))
            ? $request->data['login']['openid_url']
            : NULL;

        $openid_url = ($openid_url == '') ? NULL : $openid_url;

        if ($result = $this->_ariseAuthenticate($openid_url))
            return $this->checkUser($result);

        return false;
    }

    public function getUser()
    {
        if ($result = $this->_ariseAuthenticate(NULL))
            return $this->checkUser($result);

        return false;
    }
}
4

1 回答 1

0

您是否正在对您的身份验证适配器进行单元测试?很难判断出了什么问题,但我或多或少地确定 authenticate() 不会返回数组但总是错误的?因为如果 authenticate() 返回有效的用户数据,它将被写入会话。

请参阅http://api.cakephp.org/2.3/source-class-AuthComponent.html#543,调用http://api.cakephp.org/2.3/source-class-AuthComponent.html#690调用所有配置身份验证适配器。如果您的适配器返回一个用户数组,它应该可以工作。所以我假设您的适配器无法返回数组。

于 2013-07-02T12:05:33.567 回答