I make something horribly wrong in Cake 2.3.6 stable. I followed the Auth tutorial and added in the AppController:
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
But when I enter the site on the homepage, cake throws that I´m not authorized to access that location.
With no effect I tried in the PagesController:
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('index');
}
I double checked the tutorial and my code, there are no differences except I had to swap "$this->Post" with "$this->Calclulation" in CalculationsController.
Furthermore the login- and logout-redirects in AppController doesn´t work.
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'calculations', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => ''),
'authorize' => array('Controller')
)
);
How could it be solved? Thanks in advance :)
Please, check default routers. app/Config/routes.php
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
this mean, that home page is actulay rendered by pages controller and action display,
so, you should allow display
$this->Auth->allow('display');
Related
Here my controller name is AucUsersController. After use auth component it is finding userscontroller.I want to change this directory.I have tried by bellow code but it's not working.
public $components = array('Paginator'=>array('limit'=>2),'Auth'=>array(
'Controller'=>'AucUsers',
'loginRedirect' => array('controller' => 'aucusers','action' => 'index'),
'logoutRedirect' => array('controller' => 'aucusers','action' => 'index'),
'authError'=>'You can not access this page!!',
));
How can I change this default controller ?
CakePHP by default uses users/login for loginAction,
loginAction is the property where you define the controller and action where cake does the login
public $components = array('Paginator'=>array('limit'=>2),'Auth'=>array(
'loginAction' => array(
'controller' => 'aucusers',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'aucusers','action' => 'index'),
'logoutRedirect' => array('controller' => 'aucusers','action' => 'index'),
'authError'=>'You can not access this page!!',
));
loginRedirect - It represents where user should redirect to after login
logoutRedirect - It represents where user should redirect to after logout
I believe that if you want to change the default controller you have to set the UserModel option. I set it at beforeFilter method. So in your case it will be.
/**
* beforeFilter method
*
* #return void
*/
public function beforeFilter() {
$this->Auth->authenticate = array(
'Form' => array(
'userModel' => 'AucUser',
)
);
return parent::beforeFilter();
}
I haven't seen in the docs to have any controller option.
In my cake php site, after login page redirect to result display page, but I need to redirect home page. Here is AppController.php code:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'results', 'action' => 'add'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
),
'Security'
);
}
Just define loginRedirect
To force where a user is redirected to after login, just change the loginRedirect:
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => '/', # <-
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
),
'Security'
);
}
Note that ordinarily a user is redirected to whatever url they were attempting to access - with a default of /; as such it may be more appropriate to simply remove the loginRedirect key from the Auth component config.
Firstly, I understand there have been many questions and solutions about this but none I've found fix my issue.
In summary, the Html helper is not reverse routing correctly and instead of URLs like this:
http://website.com/user_of_website/slug_name
I get URLs like this:
http://website.com/things/view/username:user_of_website/slug:a_thing
Here is my router setup:
Router::connect('/', array('controller' => 'pages', 'action' => 'display', 'home'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
//User routes
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
Router::connect('/register', array('controller' => 'users', 'action' => 'add'));
Router::connect('/users/:action/*', array('controller' => 'users'));
Router::connect('/things/:action/*', array('controller' => 'things'));
Router::connect(
'/:username/:slug',
array(
'controller' => 'things',
'action' => 'view'
),array(
'username' => '[a-zA-Z0-9_]+',
'slug' => '[a-zA-Z0-9_]+',
'pass'=>array(
'username',
'slug'
)
)
);
Router::connect('/:userName', array('controller' => 'things', 'action' => 'user'),array('pass'=>array('userName')));
And my Html helper:
echo $this->Html->link('View', array(
'controller' => 'things',
'action' => 'view',
'username' => 'user_of_website',
'slug' => 'a_thing'
),array(
'class' => 'text-warning'
));
I really can't see what I'm doing wrong. Any help greatly appreciated.
NB, I'm using CakePHP 2.3
Have you tried if it works if you disable the 'regular' routes for things? E.g. /things/:action/*.
My guess is that, because of the wildcard, this route will match the URL as well, and, because it is defined before your custom route, will be matched in stead.
If that resolves the problem, you may try to move that route below your 'custom' route
I have a strange issue going on as I try to learn and program website using CakePHP 2.0. I have worked in the past with version 1.3 but never seen this problem before. I am running the Simple Authentication and Authorization Application tutorial from the Cookbook (p.638) and I have checked and doubled checked to make sure it is the same.
The issue I am having is that when I call /users/login and fill out the form with bogus info or simply leave it empty. Auth logs it in and if I do if($this->Auth->user()) I will receive true.
I have given up trying to understand why that is happening. It is strange....
/users/login
public function login(){
if($this->request->is('post')){
if($this->Auth->login($this->request->data)){
$this->redirect($this->Auth->redirect());
}else{
$this->Session->setFlash('Wrong login credentials!', 'default', array('class' => 'notification error closeable'));
}
}
}
// Appcontroller.php ---- Auth configuration
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'authorize' => array('Controller')
)
);
public function isAuthorized($user){
if(isset($user['role']) && $user['role'] === 'admin'){
return true;
}
return false;
}
Ok.Seems like i know what the problem is. Try doing:
if($this->Auth->login())
instead of passing it CakeRequest object.
The reason is:
In 2.0 $this->Auth->login($this->request->data) will log the user in with whatever data is posted, whereas in 1.3 $this->Auth->login($this->data) would try to identify the user first and only log in when successful. Maybe thats why you are able to log in without any data.
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
),
'loginAction' => array('admin' => false, 'controller' => 'users', 'action' => 'login')
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'authorize' => array('Controller')
)
);
Also try putting a debug($user); in beginning your isAuthorized method in AppController to see if its not getting a false from there.
For some reason beforefilter is not executed in appcontroller when I am in the admin section.
I test it with die(); and it still goes through. What could be the problem?
When I am logged out, it forwards to login, appcontroller is executed. When I log in, I get the problem.
Router:
Router::connect('/', array('controller' => 'static', 'action' => 'index'));
/**
* ...and connect the rest of 'Pages' controller's urls.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
/**
* PLUGIN MATCH
*/
if ($plugins = Configure::listObjects('plugin')) {
$pluginMatch = implode('|', array_map(array('Inflector', 'underscore'), $plugins));
Router::connect( "/:language/:plugin/:controller/:action/*", array('action' => null), array('plugin' => $pluginMatch) );
}
/**
* ADMIN
*/
Router::connect('/:language/admin/:controller/:action/*', array('action' => null, 'admin'=> true), array('language' => '[a-z]{3}'));
Router::connect('/:language/admin', array('controller' => 'admin', 'action' => 'index'), array('language' => '[a-z]{3}')); //...and set the admin default page
/**
* LANGUAGES
*/
Router::connect('/:language/home', array('controller' => 'static', 'action' => 'index'));
Router::connect('/:language/about', array('controller' => 'static', 'action' => 'about'));
// ...and more of those regular redirects here
Appcontroller beforeFilter:
function beforeFilter(){
die();
// LANGUAGES
$this->_setLanguage();
$this->Auth->authorize = 'actions'; // CAN SOMEBODY EXPLAIN TO ME WHAT THIS DOES?
$this->Auth->logoutRedirect = array( 'controller' => 'static', 'action' => 'index', 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginRedirect = array( 'controller' => 'galleries', 'action' => 'index', 'language'=>$this->Session->read('Config.language'));
$this->Auth->loginAction = array( 'controller'=>'users', 'action'=>'login', 'plugin'=>null,'language'=>$this->Session->read('Config.language'));
// ACO
$this->Auth->actionPath = 'controllers/'; // The main ACO. Maybe we need to change something for languages?
if($this->Auth->user()){
$this->set('u', $this->Auth->user());
}
}
Why is this?
does the specific controller have a beforeFilter? and does it call parent::beforeFilter?
the simple stuff sometimes is overlooked.