how to find out my current user id in other page controller after i login?

后端 未结 5 537
离开以前
离开以前 2021-02-06 12:30

i am planing to set a permission on my event index page, which just allow certain user to view which had set when i add the event. After user click into my event, the event cont

相关标签:
5条回答
  • 2021-02-06 12:43

    Use sessions to save and read data for a user between pages.

    Within Controllers:

    // store a user id in the session
    $this->Session->write('User.id', $userId);
    
    // read a user id from the session
    $userId = $this->Session->read('User.id');
    

    Within Views:

    // read a user id from the session
    $userId = $session->read('User.id');
    

    You can use any key you want if you prefer something over "User.id". I simply use this since it is what the AuthComponent defaults to if you are using that.

    0 讨论(0)
  • 2021-02-06 12:49

    Also, for a simple approach, have a look at the model and controller settings of AuthComponent::authorize. This allows you to define an isAuthorized() method in your controller or model (your choice) which will store logic that determines access (should return true if access allowed and false if denied).

    0 讨论(0)
  • 2021-02-06 12:50

    The recommended approach for getting logged in user data is via the AuthComponent itself:

    // in any controller
    $userId = $this->Auth->user('id');
    

    See Accessing the logged in user in the Auth section of the CakePHP Book.

    0 讨论(0)
  • 2021-02-06 13:04

    What you're looking for are ACLs (Access Control Lists). There's an AclComponent built into Cake which you should look into. It works together with the AuthComponent, which will hold the user id. It's a little complicated at first, but worth the hassle.

    0 讨论(0)
  • 2021-02-06 13:06

    to see sessions, queries, data, and everything else that is passed from page to page in cake use this amazing little helper http://thechaw.com/debug_kit

    0 讨论(0)
提交回复
热议问题