1

这是我的应用控制器:

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
                        )

                )

        )

)

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

4

3 回答 3

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');
于 2013-07-14T23:00:22.470 回答
0

确保在 AppController 中加载了 Auth 组件

App::uses('AuthComponent', 'Controller/Component');
于 2014-02-11T15:57:07.460 回答
0

正如Authentication Cakephp Cookbook- 2.x Documentation提到的:

请务必手动将新的用户 ID 添加到传递给登录方法的数组中。否则,您将没有可用的用户 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('user_id') )

于 2014-04-23T00:54:23.617 回答