5

我无法让 Auth 组件在 CakePHP 1.2.6 应用程序中执行我想要的重定向。

我有一个出现在所有页面上的登录表单,我想让用户留在他登录的页面上。例如,如果他正在查看另一个用户的个人资料,我想在登录后将他保留在那里,而不是将他重定向到该$this->Auth->loginRedirect操作。另外,关于我的应用程序的另一件事是,我没有“仅经过身份验证的访问”页面,每个人都可以访问每个页面,但是如果您已登录,您将获得其他功能。

我从阅读文档中了解到,我需要设置autoRedirect为 false 才能执行 login() 函数中的代码:

class UsersController extends AppController {    
    var $name = 'Users';
    var $helpers = array('Html', 'Form','Text');

    function beforeFilter() {
        $this->Auth->autoRedirect = false;
    }

    function login() {
        $this->redirect($this->referer());
    }

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

    /* [...] */
}

这目前破坏了我的身份验证。我注意到(从日志中)如果我将重定向留在登录函数中并将其设置autoRedirect为 false,则函数中的密码字段$this->datalogin()显示为空。

下面,我发布了与 Auth 组件相关的 AppController 的内容:

public function beforeFilter() {

    $this->Auth->fields = array(
        'username' => 'email',             
        'password' => 'password'            
    );

    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');     
    $this->Auth->loginRedirect = array('controller' => 'usercars', 'action' => 'homepage');

    $this->allowAccess();

    // build wishlist if the user is logged in
    if ($currentUser = $this->Auth->user()) {
        $wishlists = $this->buildWishlist($currentUser);
        $this->set('wishlists', $wishlists);
    }

}

private function allowAccess() {
      if(in_array($this->name, /* all my controller names */)) {
          $this->Auth->allow('*');
      }
}

我似乎无法理解我做错了什么。

4

2 回答 2

11

添加父::beforeFilter(); 到用户控制器中的 beforeFilter:

function beforeFilter() {
    $this->Auth->autoRedirect = false;
    parent::beforeFilter();
}

您还可以将重定向替换为用户控制器的登录方法:

$this->redirect($this->Auth->redirect());

Auth->redirect() 返回用户在被带到登录页面或 Auth->loginRedirect 之前登陆的 url。

于 2010-04-14T11:33:22.077 回答
0

将此代码放入您的控制器:

function beforeFilter() {
    $this->Auth->allow('login', 'logout');
    $this->Auth->autoRedirect = false;
    parent::beforeFilter();
}

并且,为登录页面添加这个:

function login() {
    if($this->Auth->User()) {
        $this->redirect(array('action'=>'welcome'), null, true);
    }
}
于 2011-04-05T12:32:12.430 回答