Cakephp Auth logs out only remotely - php

On localhost i have no problem at all. i can login access all sites and go through the process of the site without a problem.
Now when i put it remotely i get logged out as soon as i change page (aka i am being redirected to my login view no matter what link i press)
My AppController loos like this:
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers')
)
),
'Session'
);
public $uses = array
(
'Category'
);
public $helpers = array('Html', 'Form', 'Session');
public function beforeFilter() {
$this->Auth->allow('*');
//Configure AuthComponent
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'home', 'action' => 'index');
$this->set('menu_categories', $this->Category->find('all'));
}
}
Please tell me if you need more information (also if you wish to try this issue go to my page (my domain
log in as test password test123

Related

CakePHP - Controller::redirect and Auth->logoutRedirect not working

My CakePHP 2.5.3 app lives in a subdomain (domain/project_name) and apache rewrite rules are working correctly.
After I set App.fullBaseUrl='domain/project_name' in app/Config/core.php, Router::fullBaseUrl() works fine but, all the $this->Controller->redirect and all AuthComponent redirect to http://domain/project_name/project_name/controller/action.
Has anyone else encountered this and how did you fix it?
Many thanks in advance!
This is pattern for redirecting after log out:
// app/Controller/AppController.php
class AppController extends Controller {
//...
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'posts',
'action' => 'index'
),
'logoutRedirect' => array( // <-- Let's focus at here.
'controller' => 'pages',
'action' => 'display',
'home'
),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish'
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
//...
}
Source: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html#authentication-login-and-logout
In your problem context, check logoutRedirect configuration array.
If you want handle redirecting by other ways:
public function logout() {
return $this->redirect($this->Auth->logout());
}
Source: http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html#authentication-login-and-logout

Redirect unauthorised cakePHP users to referrer page

Ok, so I have my site set-up in the following way.
The index redirects to the Login page as the entire site is for logged in users only. When a user attempts to view a page they do not have access to they are dumped into the login page with a "You are not authorized to access that location" message.
I want to change this so if a logged in user tried to access a unauthenticated page they get redirected back to the referrer page.
In my individual controllers I have a isAuthorised method that checks the user role and allows or disallows access.
class AppController extends Controller {
public $theme = 'Default';
public function beforeRender(){
$this->set('referer',$this->referer());
$this->set('userData', $this->Auth->user());
}
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login',
'home'
),
'authorize' => array('Controller')
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
}
Looking through the documentation I have found unauthorizedRedirect and thought this might be able to do it but I've yet to find a way to implement it. I've tried a couple of things but as yet can't find anything that works properly.
You can add this line in your auth component to redirect the unauthorized users back to the page they came from.
'unauthorizedRedirect' => $this->referer()
Looks something like this:
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login',
'home'
),
'authorize' => array('Controller'),
'unauthorizedRedirect' => $this->referer()
)
);
public function beforeRender(){
$user = READ USER SESSION
if(!$this->isAuthorized($user)){
WRITE REDIRECT CODE HERE
}
$this->set('referer',$this->referer());
$this->set('userData', $this->Auth->user());
}

can not set cakephp AuthComponent sessionKey

i am developing with cakephp 2.4.7 where i am using the auth component for multiple login (a user and a company login).
My goal is to set the right sessionKey (Auth.User or Auth.Company) in the beforeFilter. Auth.User is the default value in cakephp.
AppController:
public $helpers = array('Cache','Html','Session','Form');
public $components = array(
'Security',
'Cookie',
'RequestHandler',
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'users',
'action' => 'index'
),
'logoutRedirect' => array(
'controller' => 'users',
'action' => 'login'
),
'authError' => 'You must be loggedin to view this page.',
'loginError' => 'Invalid user credentials.',
'authorize' => array('Controller')
)
);
public function beforeFilter() {
$this->Auth->deny('*');
}
CompaniesController:
public function beforeFilter() {
parent::beforeFilter();
AuthComponent::$sessionKey = 'Company';
//$this->Auth->sessionKey = 'Auth.Company';
$this->Auth->authenticate = array(
'Form' => array(
'userModel' => 'Company', // set the new userModel
'fields' => array('username' => 'email')
)
);
$this->Auth->allow('register', 'login', 'logout');
}
The login works perfectly, but the auth-session is still Auth.User. (Tested with debug($this->Auth->User());)
What i am doing wrong? How can i set the AuthComponent::$sessionKey correctly?
I had a same problem today, I have jumped into code (here) to check why its not working for me.
It seems that you have to set it like this
public function beforeFilter()
{
AuthComponent::$sessionKey = 'Auth.Company'; // static property so we have to
// access in static way so you want get strict errors
...
}
and then logout and login user again. In your action just var_dump() or pr() the $this->Session->read('Auth')
Btw $this->Auth->user() will always return you array that is in the Auth by [$sessionKey] and its same for AuthComponent::user() static call.

Authorization adapter "actions" was not found. CakePHP

I use the CakePHP Auth componenet in my web site. The code works fine in windows but after uploading it to linux online host, it give the message
Authorization adapter "actions" was not found. CakePHP
Any idea regarding the problem ?
<?php
App::uses('AppController', 'Controller');
class AppController extends Controller {
public $mobile;
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array('actionPath' => 'controllers/'),
),
),
'Session',
'RequestHandler',
);
public $helpers = array('Html', 'Form', 'Session', 'Js' => array('Jquery'));
public function beforeFilter() {
parent::beforeFilter();
// print_r($this->request); die;
if ($this->request->is('post') && isset($this->request['data']['access_token'])) {
App::uses('User', 'Model');
$this->User = new User();
// print_r($this->request['data']['access_token']);die;
$this->mobile = $this->User->authenticateMobile($this->request['data']['access_token']);
}
// print_r('APPCONT');
// print_r($this->request);
// die;
$this->Auth->authenticate = array(
AuthComponent::ALL => array('userModel' => 'User'),
//'ChangeEg',
'Form'
);
//Configure AuthComponent
$this->Auth->authorize = 'actions';
$this->Auth->loginError = "Wrong credentials";
$this->Auth->authError = "This part of the website is protected.";
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'feedbacks', 'action' => 'add');
$this->Auth->logoutRedirect = array('controller' => 'home', 'action' => 'index');
}
}
I had de same problem, was a CamelCased mistake,
instead write $this->Auth->authorize = 'Controller';
i wrote $this->Auth->authorize = 'controller'.
In windows works ok, but in Linux not.
see this post https://groups.google.com/forum/?fromgroups#!topic/cake-php/wZGFoJ4ayx0

Auth component in cakephp supports only UsersController?

I might hav askd question related to this earlier but not satisfied by answers and no answer is working.....My doubt is little different , i have two controllers
1.UsersController.
2.MembersController.
My doubt is the Auth component is working wonders for UsersControllers, but the Auth is not working for MembersController. In simple terms whenever i try to use Auth component for my MembersController, instead of redirecting to Members view. It is displaying UsersController pages....And when i delete the UsersController i get below error...
Error: UsersController could not be found.
Is there any connection between Auth and Users. How to set Auth component for my MembersController......
This is how i am using it....
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
In your App Controller
class AppController extends Controller {
public $components = array(
'Auth' => array(
'authorize' => 'actions',
'actionPath' => 'controllers/',
'loginAction' => array(
'controller' => 'members',
'action' => 'login',
'plugin' => false,
'admin' => false,
),
),
);
}

Categories