i have a big problem
i have 3 view folder {admin , teacher , user }
and in all of them use index.ctp , and logout.ctp
when i want to logout ,url redirect to /dashboard
my appcontroller.php
public $components = array(
'Cookie',
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
));
// only allow the login controllers only
public function beforeFilter() {
$this->response->disableCache();
$this->Auth->allow('login','logout');
}
and my routes.php
Router::connect('/', array('controller' => 'users', 'action' => 'login'));
/**
* ...and connect the rest of 'Pages' controller's URLs.
*/
Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));
Router::connect('/dashboard', array('controller' => 'users', 'action' => 'index'));
Router::connect('/login', array('controller' => 'users', 'action' => 'login'));
Router::connect('/logout', array('controller' => 'users', 'action' => 'logout'));
/**
* Load all plugin routes. See the CakePlugin documentation on
* how to customize the loading of plugin routes.
*/
CakePlugin::routes();
/**
* Load the CakePHP default routes. Only remove this if you do not want to use
* the built-in default routes.
*/
require CAKE . 'Config' . DS . 'routes.php';
and use this function for logout in my controller
public function logout()
{
$this->redirect($this->Auth->logout());
}
pls help my to solve that.tnx
Simply replace this in your controller
public function logout()
{
$this->Auth->logout();
$this->redirect( '/dashboard' );
}
It will work perfectly.
Related
I am trying to make an URL like these:
www.website.com/,
www.website.com
redirect to
www.website.com/members/login
through routes.php.
I have this at the moment: Router::connect('/', array('controller' => 'home', 'action' => 'index'));
How can i setup the route / to reach my desired url?
Thank you!
You can just replace the line:
Router::connect('/', array('controller' => 'home', 'action' => 'index'));
with:
Router::connect('/', array('controller' => 'members', 'action' => 'login'));
However, I believe you don't want to do this.
Just leave your routes untouched, and set up AuthComponent properly:
class AppController extends Controller {
// Pass settings in $components array
public $components = array(
'Auth' => array(
'loginAction' => array(
'controller' => 'members',
'action' => 'login',
),
//[...] rest of your Auth options
)
);
For further reference, see Cookbook 2.x: Authentication.
Probably you have AuthComponent somewhere and '/' is set as dissallowed.
$this->Auth->allow('/') should help.
I want to make an aplication where users can login normally and back-end where admins can login.
So far I created this:
routes.php
$prefix = 'admin';
Router::connect(
"/{$prefix}/:plugin/:controller",
array('action' => 'index', 'prefix' => $prefix, $prefix => true)
);
Router::connect(
"/{$prefix}/:plugin/:controller/:action/*",
array('prefix' => $prefix, $prefix => true)
);
Router::connect(
"/{$prefix}/:controller",
array('action' => 'index', 'prefix' => $prefix, $prefix => true)
);
Router::connect(
"/{$prefix}/:controller/:action/*",
array('prefix' => $prefix, $prefix => true)
);
AppController:
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'loginRedirect' => array(
'controller' => 'pages',
'action' => 'display'
),
'logoutRedirect' => array(
'controller' => 'pages',
'action' => 'display',
'home'
),
'authorize' => 'Controller',
'authError' => 'Access denied! Did you really think that you can access that?'
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow('display');
//$this->recordActivity();
if($this->request->prefix == 'admin'){
$this->layout = 'admin';
}
}
With this when I try to access pages on front-end that needs auth it gives me login() action but when a try to access /admin it redirects me to /users/login.
I want to have two separate login systems with diferrent layouts. One for normal users and one for admin users.
Can anybody help please?
I don't recommend two login() actions just for the sake of a different view. You can move the if statement from your beforeFilter() to UsersController::login() to set the layout correctly. However, if you do want to proceed with separate actions, set the AuthComponent::loginAction property in AppController::beforeFilter() like:
if($this->request->prefix == 'admin'){
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'admin_login', 'plugin' => false);
}
where admin_login will be another action that you create in UsersController.
On a side note, I recommend using cake's default prefix routing as mentioned in the book. It is very similar to what you have done but you won't have to manually create the routes. Also, as mentioned in there, to access /admin you will need to define a route like:
Router::connect(
'/admin',
array('controller' => 'pages', 'action' => 'index', 'admin' => true)
);
Hello everyone I have tried the basic auth login and its working fine but when used with admin prefix the
$this->Auth->login() is returning false.
here are my code snippets,
In Model i have added,
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
In AppController I added the following code,
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index', 'admin' => true),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login', 'admin' => true)
)
);
In UsersController my admin login is as follows,
public function admin_login() {
if ($this->request->is('post')) {
//$this->request->data['User']['password'] = AuthComponent::password($this->request->data['User']['password']);
debug($this->request->data['User']['password']);
debug($this->Auth->login());
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
}
In the above,
debug($this->request->data['User']['password']) is returning the correct password but
debug($this->Auth->login()) is returning false.
I am not able to figure out whats the reason it is returning false, is there any changes that needs to be done for admin routing.
In routes.php
You can write this code :
Router::connect('/admin', array('admin' => true, 'controller' => 'users', 'action' => 'login'));
and In AppController.php
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'login')
)
);
Try this.
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,
),
),
);
}
Is it a way to customize the authError URL on CakePHP? If I look into the Auth component I've placed in the AppController i have a redirect action loginRedirect and logoutRedirect but i don't know if is it possible to set something like authErrorRedirect:
<?php
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'users', 'action' => 'index'),
'authError' => 'You don\'t have the right to go there.',
// something like this
'authErrorRedirect' => array('controller' => 'users', 'action' => 'login'),
'authorize' => array(
'Controller',
'Actions' => array(
'actionPath' => 'controllers'
)
),
'authenticate' => array(
'Form' => array(
'fields' => array('username' => 'email')
)
)
),
'Acl'
);
Can I set an authError redirect action?
No. If you follow the Simple Authentication and Authorization Application Example # CakePHP Cookbook v2.x documentation you should redirect in the login action where the login fails for 'Invalid username or password...'. These only makes sense if you want to redirect for a different action, in the following example login2
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__('Invalid username or password, try again'));
$this->redirect(array('controller' => 'users', 'action' => 'login2'));
}
}
}