Logout an user in CakePHP - cakephp

I'd like to know whether it's possible to logout an user in CakePHP.
I don't want to logout the current user, but to end the session of a selected user.
Thank you.

I imagine you could do a simple conditional check:
With authsome component:
if (Authsome::get('User.id') == $idOfUserYouWantToLogout){
Authsome::logout();
}
Note: you could of course use $this->Authsome->get('User.id') and $this->Authsome->logout() if you wanted.
With normal auth component:
if ($this->Auth->get('id') == $idOfUserYouWantToLogout){
$this->Auth->logout();
}
This is kind of a work-around, because it will log out the current user, but only if they are the user that you want logged out.

Basically it isn't possible. CakePHP at least doesn't have some internal ways to do it.

Related

Restrict access to a page in Gatsby.js to logged in users

On my gatsby.js static site, I want to restrict access to my pages/dashboard page using client-side authentication. The plug-in, gatsby-plugin-meta-redirect says that I can create a redirect with this...
createRedirect({ fromPath: '/old-url', toPath: '/new-url', isPermanent: true });
...but I'm not sure where to place that in my code. I'm assuming gatsby-node.js, but I'm not sure how.
I want to do something like this,
if (user.loggedIn) {
// redirect to '/dashboard'
} else {
// redirect to '/'
}
This is assuming that the state of user is available throughout the site. Is this possible?
Also, in the gatsby-plugin-meta-redirect docs, it states that this plugin should be put last in the array in gatsby-config.js. But I already have gatsby-plugin-netlify placed last. Would that be a problem?
What you can do is give a programmatic, non-direct access to the /dashboard page using navigateTo in Link.
See this issue.

Privacy page redirection in drupal 7

In my project I have a privacy policy form page. After login, though custom code I am redirecting to that page. When user check the privacy policy, I will store the data in variable table.
What I need is if I click any page with out accepting the privacy policy, I need to again redirect to privacy page. I need to check this logic in every page load. Where I need to check this? Is it on hook_init(). When I given this in hook_init(), which is not working. Please see my below code.
$getAcc = variable_get('privacy_38');
if($getAcc == 1) {
global $user;
$accessRedirection = 'privacy/accept';
$afterUserView = 'user/' . $user->uid . '/view';
$options = array('query' => array('destination' => $afterUserView));
drupal_goto($accessRedirection, $options
}
Where I need to put this code. Is it in theme template. If so, in which hook, I need to put this code. Please anybody help me.
That's definitely the place to do it (hook_init), but will take a bit more than what you have to fully achieve what you need. There's a module that implements something very similar to this that you might want to have a look at if you haven't yet: https://www.drupal.org/project/legal
Hope this helps!

CakePHP 2.1 loggedIn user in views

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.

Cakephp One login function / multiple login views

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.

CakePHP Logs Me Out Prematurely

I have a CakePHP app that seems to be terminating my session on one specific action. I have a page which, when a link is clicked, launches a Fancybox overlay of the iframe type. In that overlay, the user fills out and submits a form. The form is submitted properly, does its work (including sending an email), loads the success view and lets me close the overlay, but as soon as I try to get to any other page, I'm sent to the login screen to reauthenticate.
The value of my Security.level config setting is medium and my Session.timeout is 120, so that shouldn't be the problem. Anyone have any idea what could be creating this?
Thanks.
is it possible that your ajax calls and redirects are not going to the same place, eg www.site.com and site.com? I have had that before and also kept getting logged out.
So this wasn't fun to track down, but it was me being an idiot. Buried in the code was some early-stage code to refresh user data in the authenticated session that wasn't doing what it should have been doing. It was attempting to update the entire Auth.User object directly (e.g. $this->Session->write( 'Auth', $user )) instead of calling the login method.
Once I changed the Session::write() code to $this->Auth->login( $user ), everything lined up nicely. A nice bit of reference material on this subject at http://milesj.me/blog/read/31/Refreshing-The-Auths-Session.

Resources