How to find out my current user ID in another page controller after login?

I plan to set the resolution on my event index page, which allows a specific user to see which was set when the event was added. After the user clicks on my event, the 1st event controller will check the user ID and check the event database, which controls the user, can see what event is on his calendar. Permission is added when the user creates an event and passes it to another user. Also, how can I find the current user ID to compare with my event base, which is an exact 1?

Any suggestion for me to make this feature? I need to know the code and the concept of how to get the current user ID for comparison with the entire event database and let the current user see a specific event.

Thanks so much for your info.

+6
cakephp
source share
5 answers

The recommended approach for entering user data is through AuthComponent itself:

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

See Accessing a Registered User in the Auth CakePHP Book Section.

+18
source share

Use sessions to save and read user data between pages.

Inside the 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 above "User.id". I just use this because this is what AuthComponent uses by default if you use this.

+6
source share

What you are looking for is ACLs (access control lists). There's an AclComponent built into Cake that you should learn. It works together with AuthComponent , which will contain the user ID. At first it was a little complicated, but it was worth the hassle.

+2
source share

Also, for a simple approach, check out the model and controller settings of AuthComponent::authorize . This allows you to define the isAuthorized() method in your controller or model (your choice) that will store the logic that defines access (should return true if access is allowed, and false if rejected).

+1
source share

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

0
source share

All Articles