I am using cakephp 2.1. So I am looking for getting loggedIn user in views. How to get the logged user in views.
You can take a look here:
http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user
or here:
http://api20.cakephp.org/class/auth-component#method-AuthComponentuser
Try this in $this->Auth->loggedIn() in your view. If it doesn't work then you have to use
$this->Auth->user('id').
For example;
if($this->Auth->loggedIn()) {
// then do something
}
or:
if($this->Auth->user('id')) {
// then do something
}
If you want to show the current logged in user details to all views. it will be better to put the logic in the layout file rather than putting the code in all view files.
to get the current logged in user details you can use $this->Auth->user
Lets say if you want to display the current loged in user name you can use echo $this->Auth->user('user_name');
If you are using the Auth Component the user data is also stored in the Session's "Auth.User" key.
So in a view it can be accessed with the SessionHelper::read() method:
$user = $this->Session->read("Auth.User");
Auth.User contains the user record from the database.
Don't forget to include the Session Helper in the $helpers array in your controller.
Related
I have these files:
Application.cfc:
Onrequeststart method:
session.remoteuser = cgi.REMOTE_USER
Based on the this user, the view is:
Admin view
or
user view
I have index.cfm
<div ng-app controller>
</div>
Angular code replaces the view here.
But from admin view i want to create a form which changes the session.remoteuser to any user they want to view as. I created a form in adminview.cfm page and checking to see if the form is submitted in application.cfc. But I see application.cfc is fired twice so the view is getting the user id, but goes back to admin id. How do I achieve this ?
To continue to set the session.remoteuser variable in OnRequestStart(), structure your logic like this.
if the form variable exists
set `session.remoteuser` accordingly
else if `session.remoteuser` does not exist
set `session.remoteuser` to cgi.remoteUser
else
do nothing
Then, when the admin person loads the form page, session.remoteuser will be set to that person. It will change when the form is submitted and will not change again for the rest of the session unless there is a way to access the form page again when your application thinks he's the person selected in the form.
I have the main.cfc file where I am setting the view based on the user that is logged in. I have admin view and I have user view.
Admin view is decided based on the ids i add to the list.
I want to give option for admin to change to see view like a specific user.
I have ng-click="switchView"
This calls a function in the AngularJS
$scope.switchView = function(){
// I want to cfset session.remoteuser = userid
// and redirect to main page to change the view
}
Is this possible? How can i achieve it?
I am using auth component and it works ok.
But in my default layout before the content I have some menu which is different if user is logged in. So I want to determine if user is logged in or not - normally I use $this->Auth->user('id') but $this->Auth doesnt work in layout (it only works in view which controller is using Auth component).
How to do it?
In beforeRender() just call
$this->set('userData', $this->Auth->user());
and set the data to the view and do your checks in the view.
In order to get data in layout, you should call beforeRender() method in AppController.
Passing it through session is not a good idea IMHO. It might be not the usual case but at least I prefer to do things solid: If you're using the session for that your code will fail in a system that is not using a session (stateless auth). Overall I'm not a big fan of accessing the session in a view at all. Session is for me more like a datasource.
You can read Auth data from session. Something like:
$user = $session->read('Auth');
Don`t forget to add Session helper in your AppController.
var $helpers = array('Session');
I am using CakePHP 2.0's integrated Auth component.
I have the following tables :
Users
Groups
Profiles
My model relations are as follows:
User belongsTo Group
User hasMany Profiles
While logged in to the site, I noticed the Auth session contains only User table information, but I need the information of Groups and Profiles tables too for the logged in user.
Is there any way to do that with the Auth component?
There is no way to do this with the AuthComponent because of the way it handles the session keys. You can, however, just save it to the session yourself.
The only way to do this is to add to the session when the user logs in:
function login() {
if ($this->Auth->login($this->data)) {
$this->User->id = $this->Auth->user('id');
$this->User->contain(array('Profile', 'Group'));
$this->Session->write('User', $this->User->read());
}
}
Then in your beforeFilter() in your AppController, save a var for the controllers to get to:
function beforeFilter() {
$this->activeUser = $this->Session->read('User');
}
// and allow the views to have access to user data
function beforeRender() {
$this->set('activeUser', $this->activeUser);
}
Update: As of CakePHP 2.2 (announced here), the AuthComponent now accepts the 'contain' key for storing extra information in the session.
As far as I'm aware the Auth component only caches the data from your Users model. You can use that information to retrieve the desired data from the other models, by for example using this in your controller:
$group_data = $this->Group->findById($this->Auth->user('group_id'));
Or
$profile_data = $this->Profile->findByUserId($this->Auth->user('id'));
But I don't think you can get it from the Auth component directly, as it doesn't cache the related model data out of the box.
Two ways:
1) Extend the FormAuthenticate class (see /Controller/Component/Auth) or whatever you use to login and override the _findUser() method and tell the Auth component to use this authorize class. See this page how to do all of that http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html
2) Simply implement a method in the model that will fetch all data you want and call it in the login method of your controller and write the data into the session. IMO it is handy to have such a method because sometimes you need to refresh the session data anyways.
Because of your comment on the other answer:
You will have to write a method and some code in a model that will return you the data. CakePHP can't read your mind and a database without code. No matter which of both suggested ways you're going to use, you'll have to write code.
I have one website with a login system. However, I would like the login view to be different depending on what link has the user used to get to the login screen.
Something like:
function login ($from_page = null) {
if (isset($page)) $this->render('login_alternate_view');
else $this->render('login'); //default login view
}
And then each of the login views (login.ctp, login_alternate_view.ctp) would have the login form plus other stuff specific to each one.
Is this possible in some way? I've already tried something like the example above but it doesn't work...
So I fixed it using GET variables:
/users/login?some_var=some_value
And then in the login function I catch that variable's value with:
$this->params['url']['some_var'];
This way I can "customize" my login function depending on the link the user uses
First show the real error message you're talking in the comments about and not "something".
I guess that you want the current page url the user is on when he logs in? How to you generate the modal? Request the whole form via ajax or is it embedded in the page you're on? If it's embedded I would put the current page url the user is on in a hidden field "from" in the login form and check that.