CakePHP:在会话中保存信息

时间:2013-06-30 02:39:09

标签: php cakephp cakephp-2.0 session-variables cakephp-2.1

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

我试图通过在Auth.MyAuthenticateComponent中使用$ this-> Session->写入来保存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;
    }
}

1 个答案:

答案 0 :(得分:0)

您是否正在测试您的auth适配器?很难说出错是什么,但我或多或少确定authenticate()不返回数组但总是假的?因为如果authenticate()返回有效的用户数据,它将被写入会话。

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