CakePHP Auth 组件重定向问题

发布于 2024-08-28 15:22:19 字数 1873 浏览 4 评论 0原文

我无法让 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->data 中的密码字段login() 函数显示为空。

下面,我发布了与 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('*');
      }
}

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

I am having trouble getting the Auth component do the redirects I want in a CakePHP 1.2.6 app.

I have a login form that appears on all pages and I want to keep the user on the page he logs in on. For example, if he is viewing another user's profile, I want to keep him there after logging in, not redirect him to the $this->Auth->loginRedirect action. Also, another thing about my app is that I have no "authenticated access only" pages, every page is accessible to everyone, but if you're logged in you get additional features.

What I understood from reading the documentation is that I need to set autoRedirect to false to get the code in the login() function to be executed:

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());
    }

    /* [...] */
}

This currently breaks my authentication. I've noticed (from the logs) that if I leave the redirect in the login function and set autoRedirect to false, the password field in $this->data in the login() function appears as empty.

Below, I've posted the contents of AppController that relate to the Auth component:

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('*');
      }
}

I can't seem to understand what I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-09-04 15:22:19

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

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

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

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

Auth->redirect() 返回用户在进入登录页面之前登陆的 url 或 Auth->redirect() 。登录重定向。

Add parent::beforeFilter(); to beforeFilter in the user controller:

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

You can also replace the redirect with this to the login method of your user controller:

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

Auth->redirect() returns the url where the user landed before being taken to the login page or Auth->loginRedirect.

丿*梦醉红颜 2024-09-04 15:22:19

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

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);
    }
}

Put this code to your controller:

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

and, add this for the login page:

function login() {
    if($this->Auth->User()) {
        $this->redirect(array('action'=>'welcome'), null, true);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文