How to change cakephp auth component controller? - cakephp

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.

Related

$controller does not implement an isAuthorized() method in cakephp

I have used Auth component in my OrdersController as follows:
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email', //Default is 'username' in the userModel
'password' => 'password' //Default is 'password' in the userModel
),
'userModel' => 'Agent'
)
),
'loginAction' => array(
'controller' => 'admins',
'action' => 'login'
),
'loginRedirect' => array('controller' => 'admins', 'action' => 'deshboard'),
'logoutRedirect' => array('controller' => 'admins', 'action' => 'login'),
'authError' => "You can't acces that page",
'authorize' => 'Controller'
)
);
public function beforeFilter() {
parent::beforeFilter();
// Allow users to register and logout.
$this->Auth->allow('login','index');
}
When I tried to login and username and password matched it redirect to adminc/deshboard with the following error message:
$controller does not implement an isAuthorized() method.
Error: An Internal Error Has Occurred.
I searched google for couple of hours no solution. What am I doing for this error? Thanks for your time.
You need to implement isAuthorized(), like so:
class OrdersController extends Controller {
//...
public function isAuthorized($user) {
//auth check
//return boolean
}
//...
}
See http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html for more info.

CakePHP Auth login non sense

I'm trying to make a simple login system for my users, but I can't figure out why it won't log me in, the Auth->login() method always returns FALSE (incorrect information) for some reason... might be something with password hashing.
I have cakePHP 2.5.2.
Here is a screenshot of my issue: ISSUE
My beforeSave() method in UsersController:
public function beforeSave($options = array()) {
$this->request->data['User']['password'] = Security::hash($this->request->data['User']['password']);
}
and the login() method:
function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->setFlash(__('You\'ve successfully logged in.' . ' <b>' . $this->Session->read('User.login') . '</b>'), 'alert', array(
'plugin' => 'BoostCake',
'class' => 'alert-success'
), 'success');
return $this->redirect($this->Auth->redirectUrl());
//// $this->redirect($this->Auth->redirectUrl());
} else {
// var_dump($this->Auth->user());
$this->Session->setFlash(__('Sorry, the information you\'ve entered is incorrect.'), 'alert', array(
'plugin' => 'BoostCake',
'class' => 'alert-danger'
), 'danger');
}
}
}
here's the Auth component:
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'pages', 'action' => 'home'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home'),
'loginAction' => array('controller' => 'users', 'action' => 'login'),
'authError' => 'You are not authorized to access this page.',
'authenticate' => array(
'Form' => array(
'userModel'=>'User',
'fields' => array(
'username' => 'login',
'password'=>'password')
)
),
'flash' => array(
'element' => 'alert',
'key' => 'auth',
'params' => array(
'plugin' => 'BoostCake',
'class' => 'alert-danger'
)
),'authorize'=>array('Controller'),
)
,'DebugKit.Toolbar'
);
Yes it's incorrect I removed everything and it works I don't know how
Move your beforeSave method to your Model, not the Controller.
When saving data Cake looks for any functions that should run before inserting the data in your Model.
You will also need to create a new user (if you look in your database you should find that the password has been stored as plaintext because the hashing in the beforeSave would never have been called.
I think you should provide the Security::hash() function blowfish or set the app's internal salt to true.
Like this:
public function beforeSave($options = array()) {
$this->request->data['User']['password'] = Security::hash($this->request->data['User']['password'], null, true);
}
This is the way, the deprecated AuthComponent::password() function works.
Just tested it this way in my Cake App and it work's fine.
See http://api.cakephp.org/2.4/class-Security.html#_hash
Edit:
beforeSave() should be in the User's Model, not in the User's Controller

How redirect home page in cake php

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.

How to allow actions in CakePHP?

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');

CakePHP 2.0: Odd Auth Login Issue

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.

Resources