CakePHP - 我无法访问$ this-> Auth->登录后的user()数据

时间:2013-07-14 10:35:10

标签: cakephp

这是我的AppController:

class AppController extends Controller {

public $components = array('Auth');

public function beforeFilter() { 

//Set up Auth Component
        $this->Auth->userModel = 'CmsUser';
        $this->Auth->authenticate ='Form';
        $this->Auth->fields = array('username' => 'username', 'password' => 'password');
        $this->Auth->loginAction = array('controller' => 'admin_cms_users', 'action' => 'login','admin' => true);
        $this->Auth->loginRedirect = array('controller' => 'admin_cms_helps', 'action' => 'index');
        $this->Auth->logoutRedirect = '/';
        $this->Auth->loginError = 'Pogrešno korisničko ime/lozinka. Pokušajte ponovo!';
        $this->Auth->autoRedirect = false; // najvažnija postavka za redirekciju
    $this->Auth->allow('display');
        // Additional criteria for loging.
        $this->Auth->userScope = array('CmsUser.active' => 1); //user needs to be active.

}

这是我的AdminCmsUsersController:

class AdminCmsUsersController extends AppController {

    public $name = 'AdminCmsUsers';
    public $uses = array('CmsUser'); 

public function beforeFilter(){
parent::beforeFilter();
}

.
.
.
.
public function admin_login() {
$this->layout='cms_main';
if($this->request->is('post') AND $this->Auth->login($this->request->data)){
$this->CmsUser->id = $this->Auth->user('id');
print_r($this->Auth->user());
print_r($this->Session->read());
$this->redirect($this->Auth->redirect());
}

}
.
.
.

print_r($ this-> Auth-> user())结果:

Array
(
    [CmsUser] => Array
        (
            [username] => someuser
            [password] => somepass
        )

)

// print_r($ this-> Session-> read())结果:

    [Auth] => Array
        (
            [User] => Array
                (
                    [CmsUser] => Array
                        (
                            [username] => lupus10
                            [password] => canis10
                        )

                )

        )

)

我的用户有姓名,电子邮件和其他数据。为什么无法访问这些数据?

3 个答案:

答案 0 :(得分:2)

摆脱

$this->Auth->fields = array('username' => 'username', 'password' => 'password');
在AppController中

$this->CmsUser->id = $this->Auth->user('id');

应该是

$this->CmsUser->id = $this->Auth->user('CmsUser.id');

答案 1 :(得分:0)

确保在AppController

中加载Auth组件
App::uses('AuthComponent', 'Controller/Component');

答案 2 :(得分:0)

Authentication Cakephp Cookbook- 2.x Documentation所述:

  

请务必手动将新用户ID添加到传递给login方法的数组中。否则您将无法使用用户ID。

以下是我在项目中处理它的方法:

    public function login() {
    if ($this->request->is('post')) {
        $this_user = $this->User->find('first', array(
            'conditions' => array('email_address' => $this->request->data['User']['email_address'])
        ));
        $user_id = $this_user['User']['user_id'];
        $this->request->data['User'] = array_merge(
                $this->request->data['User'],
                array('user_id' => $user_id)
        );
        if ($this->Auth->login($this->request->data['User'])) {
            return $this->redirect($this->Auth->redirectUrl());
        }
        $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}

然后您应该能够在其他控制器中使用$ this-> Auth-> user(' id')。 (在我的情况下,它是$ this-> Auth->用户(' user_id'))