【问题标题】:Very strange behaviour from cake php after logging out注销后cake php的非常奇怪的行为
【发布时间】:2013-06-11 16:51:53
【问题描述】:

好的,这是场景:

  • 管理员登录
  • 管理员退出
  • 普通用户登录
  • 重定向到管理页面!

如果我在以普通用户身份登录之前删除存储的 cookie,登录将按预期工作。我注意到的另一件奇怪的事情是,当普通用户登录时,我的登录功能中的管理员重定向实际上并没有运行,因此它们被重定向到其他地方。

这里有一些代码:

用户控制器:

<?php
    public function login() {
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Auth->login()) {
                //if login successful update logged in User as login does not use recursive find
                $this->User->id = $this->Auth->user('id');
                $this->User->read();
                $this->Auth->login($this->User->data);
                if($this->Login->isRole($this->Auth->user(), 'Admin')) {
                    //redirect admins to admin page, not ran when error occurs!!
                    $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true));  
                } else {
                    //isAuthorised in AppController takes care of redirect to registration page if required
                    $this->redirect($this->Auth->redirect());   
                }
            } else {
                //if login unsuccessful
                $this->Session->setFlash(
                    __('Invalid username or password, please try again.'), 
                    'alert',
                    array(
                        'plugin' => 'TwitterBootstrap',
                        'class' => 'alert-error'
                    )
                );
            }
        }
        //set layout
        $this->layout = 'not_logged_in';
        //set title
        $this->set('title_for_layout', 'Login');
    }

    public function logout() {
        //logout
        $this->redirect($this->Auth->logout());
    }
public function isAuthorized($user) {
    if(parent::isAuthorized($user)) {
        //call parent method for redirect and admin permission
        return true;
    }
    switch ($this->action) {
        case 'add':
        case 'resetPassword':
        case 'login':
            //logged in users cannot access registration, login or password reset pages
            return false;
            break;
        case 'add_role':
            //check user is finshing registration or has a role request accepted
            return (!$this->Login->isRegistrationComplete($user) || $this->Login->isRoleRequestAccepted($user));
            break;
        default:
            //all loogged in users can access the rest of User controller
            return true;
            break;
    }
}
php?>

应用控制器:

<?php
public $components = array(
    'Session',
    'Auth' => array(
        'className' => 'UserAuth',
        'loginRedirect' => array('controller' => 'users', 'action' => 'view'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authorize' => array('Controller'),
        'authenticate' => array('Blowfish'),
        'authError' => 'Please login.',
        'loginError' => 'Invalid Username or Password entered, please try again.',
    ),
    'Login'
);
    public function isAuthorized($user) {
        //set values needed for all logged in pages
        $this->set('loggedIn', $user);
        $this->set('role', $user['User']['role']);  
        if($this->Login->isAccessing($this, 'users', 'logout')) {
            //never prevent user from logging out
            return true;
        }
        if($this->Login->isRole($user, 'Admin')) {
            //admin can access every action
            return true;
        } elseif ($this->request->prefix === 'admin') {
            //only admins allowed on admin pages
            throw new Exception('You do not have permission to access this page.', 1);
        }
        //get user role and requested role
        $roles = $this->Login->getRolesCurrentAndNew($user);
        if($this->Login->isRoleRequestAccepted($user)) {
            //user has an accepted role request
            $controller = 'users';
            $action = 'add_role';
            if($this->Login->isRedirectRequired($this, $controller, $action)) {
                //if user is already accessing registration this check prevents redirect loops
                if ($this->Login->isRegistrationComplete($user)) {
                    //display flash based on registration status
                    $this->Session->setFlash(
                        __('Your request for the role of '. strtolower($roles['new']) . ' has been accepted, please enter additional details.'), 
                        'alert',
                        array(
                            'plugin' => 'TwitterBootstrap',
                            'class' => 'alert-success'
                        )
                    );
                } else {
                    $this->Session->setFlash(
                        __('Please complete your registration.'), 
                        'alert',
                        array(
                            'plugin' => 'TwitterBootstrap',
                            'class' => 'alert-success'
                        )
                    );
                }
                $this->redirect(array('controller' => $controller, 'action' => $action));
            }
        } elseif (!$this->Login->isRegistrationComplete($user)) {
            //user has not registered yet and role request is not accepted
            if(!$this->Login->isRegistrationComplete($user)) {
                //user has not completed registration yet, awaiting approval
                throw new Exception('Your registration request is awaiting approval from a member of the admin team.', 1);
            }
        }
        return false;
    }
?>

路线配置:

    Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
    Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
    Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
    Router::connect('/register/role', array('controller' => 'users', 'action' => 'add_role'));
    Router::connect('/', array('controller' => 'users', 'action' => 'view'));
    Router::connect('/admin', array('controller' => 'users', 'action' => 'index', 'admin' => true));
    Router::connect('/users/manageRoleRequest', array('controller' => 'roleRequests', 'action' => 'add'));

【问题讨论】:

  • $this-&gt;Auth-&gt;login($this-&gt;User-&gt;data); 是什么东西?为什么在登录后覆盖会话数据(已经这样做了)?对我来说没有意义。请注意,您可以使用contain 包含更多数据(最终与使用递归相同)。另请注意,您的陈述是不正确的,因为您需要一个平面用户数组来传递(请参阅文档!)。
  • 因为 $this->Auth->login() 不尊重我的用户模型的递归属性。它在上面的评论中说。
  • 使用您的代码,您仍然只会产生 Auth.User.User.id 等,这并没有让它变得更好。您还应该始终包含您正在使用的当前 cakephp 版本 - 因为这对于正确答案至关重要。
  • 我想在会话中存储用户对象及其对象。我必须将递归设置为 2 才能做到这一点。 Auth->login() 只存储我不想要的用户。
  • 你读过关于“包含”的部分吗? book.cakephp.org/2.0/en/core-libraries/components/… - 如果你真的还想手动登录,你需要正确使用它:book.cakephp.org/2.0/en/core-libraries/components/…(注意没有 User 键的扁平 1-dim 数组)

标签: php cakephp web-applications


【解决方案1】:

我尝试了以下方法,它似乎对我有用

    public function logout() {
        $logout_redirect = $this->Auth->logout();
        $this->Auth->redirectUrl($this->Auth->redirect());
        return $this->redirect($logout_redirect);
    }

【讨论】:

    【解决方案2】:

    来自文档:

    属性 AuthComponent::$logoutRedirect
    默认操作为 用户注销后重定向到。虽然 AuthComponent 确实 不处理注销后重定向,将返回重定向 URL 来自 AuthComponent::logout()。 默认为 AuthComponent::$loginAction。

    意思:

    $this->Auth->logout()
    

    将返回一个字符串 url。而已。 你用重定向包装它以重定向到这个网址:

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

    所以,不,Cake 只会将您重定向到您通过“loginRedirect”设置指定的特定操作。 在您的情况下,您的登录视图。

    【讨论】:

    【解决方案3】:

    我最终意识到,无论您是否以其他用户身份登录,CakePHP 都会自动将您带到您在同一台机器上注销和登录时查看的最后一页。

    【讨论】:

    • 嗨,您可以标记一个已接受的答案吗?或者您是否需要一种解决方案来避免这种“自动化”?
    • 我想要一个解决方案,但如果这种行为是常态而不是我做过的事情,那么我愿意接受它。
    猜你喜欢
    • 2016-03-13
    • 2011-03-01
    • 2020-12-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-20
    • 2020-02-09
    • 1970-01-01
    • 2016-12-04
    相关资源
    最近更新 更多