HOw to Prevent to access visited page after logout in cakephp? - cakephp-2.0

Please answer my question. I do not get satisfactory answer.
Question is : "How to Prevent to access visited page after logout in cakephp?"
Thanks in Advance

In the AppController beforeFilter:
// Check if the user is logged in
if (isset($this->Auth->user())) {
// Save the visited page in session
$this->Session->write('VisitedPages.', $this->here());
// If the user is not logged in
} else {
// If the user has logged out and visited the page before
if (isset($this->Session->read('VisitedPages.'.$this->here()))) {
$this->redirect('/errorPage');
}
}

Related

how to hide login screen if the user already signed in

In my app the first/main screen is login form. How to skip the login form
if he/she has already signed in? Presently every time, someone use the app,
the login form opens first.
How can I achieve this functionality in codename one. I didn't find anything in the group. Is there some tutorial or eg on doing this? Moreover I want the login form if someone logged out and then use the app. Thankyou
// change initial form:
#Override
protected String getFirstFormName() {
loginToken = Preferences.get("loginToken", null);
if (loginToken != null) {
return "MenuForm";
} else {
return "Login";
}
}
To check if this is the first activation ever use preferences:
String loginToken = Preferences.get("loginToken", null);
if(loginToken == null) {
// show login and after you get a token do
Preferences.set("loginToken", loginToken);
}
This assumes you have a token representing the used identity but you can use the username, email or whatever you need for login.

Spring Security + AngularJS + Permissions: disabling all pages for non authenticated users other than login

I want that users have to login before seeing other pages. If they try to access some other page, they have to login first.
I tried using the following, but it keeps giving me an HTTP Status 401 - Access Denied error.
http.csrf().disable().exceptionHandling()
.authenticationEntryPoint(unauthorizedHandler).and()
.formLogin().loginPage("/login").successHandler(authSuccess)
.failureHandler(authFailure).and().authorizeRequests()
.antMatchers("/login", "/#/login", "/login.html", "/login.jsp", "login", "/login")
.permitAll().anyRequest().authenticated();
Since I am using angularjs, it might have to be something with that. I however still tried to add the /#/login part, but still without any good result.
You can achieve this using routing. Have a look at the below code.
app.run(function($rootScope, $location,cacheLogOut) {
// register listener to watch route changes
$rootScope.$on("$routeChangeStart", function(event, next, current) {
if ($rootScope.loggedUser == null) {
// no logged user, we should be going to #login
if (next.templateUrl == "login.html") {
// already going to #login, no redirect needed
} else {
// not going to #login, we should redirect now
$location.path("/login");
}
}
});
});
This is borrowed from Redirecting to a certain route based on condition. I am using it for redirecting to the login page if the user is not logged in. The $rootScope.loggedUser value is set once the user is logged in.

CakePHP 3: How to automatically log a user in after registration

I am trying to log a user in using CakePHP 3 right after registration, but I have not been successful. This is what I am doing:
function register(){
// ....
if($result = $this->Users->save($user)){
// Retrieves corresponding user that was just saved
$authUser = $this->Users->get($result->id);
// Log user in using Auth
$this->Auth->setUser($authUser);
// Redirect user
$this->redirect('/users/account');
}
}
I guess posting this question opened my eyes to a fix. This is what I did to get it to work... if there is better way, I would be glad to change it...
function register(){
// .... Default CakePHP generated code
if($result = $this->Users->save($user)){
// Retrieve user from DB
$authUser = $this->Users->get($result->id)->toArray();
// Log user in using Auth
$this->Auth->setUser($authUser);
// Redirect user
$this->redirect(['action' => 'account']);
}
}
CakePHP 3.8 × cakephp/authentication update.
Any place you were calling AuthComponent::setUser(), you should now use setIdentity():
// Assume you need to read a user by access token
$user = $this->Users->find('byToken', ['token' => $token])->first();
// Persist the user into configured authenticators.
$this->Authentication->setIdentity($user);
Source: /authentication/1/en/migration-from-the-authcomponent.html#checking-identities

Logout does not work in Drupal

I am using the following code in page.tpl.php to redirect logged-in users to the first page:
if($is_front){if($user->uid != 0){header('Location:merchant-mobile');}
else{include("page-front.tpl.php"); return;}}
But this function conflicts with the logout function, since the logout function redirects the user to the front page, and does a logout. How can this be prevented?
You could use Rules for that or a simpler code with drupal_goto function:
global $user;
if($is_front){
if($user->uid != 0){
header('Location:merchant-mobile');
} else {
drupal_goto('<front>');
}
}
But why redirect a user to the frontpage if he is in the frontpage? if($is_front) means that the current page is frontpage. Did you mean to write if(!$is_front)?

logout a user automatically when the browser window is closed- salesforce

I have a basic question about salesforce session, i want to logout my users automatically when they close the browser window.
Thanks for your help.
This is an old thread but I thought I would go ahead and share my work around...
Add a controller to your site home page, and in the init action, redirect to secure page, if it is not already a secure page.
public PageReference init() {
String currentURL = Site.getCurrentSiteUrl();
if (!startsWith(currentUrl, 'https://')) {
return redirect(currentURL.replace('http://', 'https://'));
}
return null;
}

Resources