开发者

cannot check if user is logged in from any controller other than users

开发者 https://www.devze.com 2023-02-17 06:13 出处:网络
I am making a users profile page and if i route it like this Router::connect(\'/users/profile\', array(\'controller\' => \'users\', \'action\' => \'profile\'));

I am making a users profile page and if i route it like this

 Router::connect('/users/profile', array('controller' => 'users', 'action' => 'profile'));

then $this->Auth->user() work fine in UsersController->profile() but if iroute it like this

Router::connect('/pages/profile', array('controller' => 'pages', 'action' => 'profile'));

then it doesnt in PagesCotroller->profile() the profile function is the same in both cases

my app controller:

 class AppController extends Controller {
     var $components = array(
         'Auth' ,
         'Acl',
         'Session',
         'Cookie'
     );
     var $userdata = array();
     function beforeFilter(){
             $this->Auth->userModel = 'Users';

         $this->Auth->loginRedirect = array();
         $this->Auth->allow('display');
             $this->Auth->allow('index');

             var_dump($this->Auth->user());
             parent::beforeFilter();
     }
     function isAuthorized() {
        return true;
     }


}

and var_dump actually works if i go to开发者_开发问答 /users/profile and doesnt if i go to /pages/profile without any additional logging in or anything

$this->Auth->allow() works properly in both controllers by the way, so Auth component is available in both

here's pagescontroller

class PagesController extends AppController {
        var $components = array('RequestHandler');
    var $name = 'Pages';
    var $helpers = array('Html', 'Session','Form','Ajax', 'Jquery');

    var $uses = array('Users');

        function beforeFilter() {
            parent::beforeFilter();
            $this->Auth->allow('profile');
        }

        function beforeRender() {
            parent::beforeRender();

        }

        function profile(){

                var_dump($this->Auth->user());
        }
}


Eugene,

I do wonder why you would try to achieve things like that. The first route is already in existence, so no need to set up one. The second route seems (imho) to be against conventions. The pages controller is used to serve more or less static pages, so I would not expect to see it routed to an action.

My two cents.

Edit0: After your first comment your intentions are now more obvious:

Put

if ($this->Session->read(Auth.User)) ...

or something like this in your page, possibly as an element. Just check out via debug() what values you can read in your view/page.

Edit1:

  1. Download and install debug_kit from github.
  2. If the Session helper is enabled in the pages_controller.php, then you should be able to reach your objective.

Edit2:

You could check if the session is visible in other views except users. But you should definitly be aware that you are breaking CakePHP's conventions when naming a model in plural and the table alike. Unfortunatly, I cannot tell you atm if that could be relevant to the case at hand, but it is certainly something I would strive to get rid of.

Does the debug_kit show the AUTH component?


I found the answer it was captcha component. It started session every time it was called and it wass called from all the controllers except users. So the user log in data was not preserved.


I wonder if var $uses = array('Users'); Users should be singular, User

If this doesn't fix it... leave it singular anyway because you are calling the 'User' model.

0

精彩评论

暂无评论...
验证码 换一张
取 消