cakephp login redirect problem? - cakephp

Hi I'm using cakephp auth component for login system, I would like,every time when user login to redirect him to users page,but when session is timeout and user login again he is redirected to previous page that he was on,and not back to users page.I hope you understand me.He is my code.
app controller :
function beforeFilter() {
$this->Auth->loginAction = array('admin' => false, 'controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('admin' => false, 'controller' => 'books', 'action' => 'index');
$this->Auth->allow('display');
}
users controller :
function login() {
}
function admin_logout() {
$this->Session->destroy();
$this->redirect($this->Auth->logout());
}

in beforeFilter in users controller, add $this->Auth->autoRedirect = false; and
function login() {
if($this->Auth->user())$this->redirect(array('controller' => 'books', 'action' => 'index'));
}

This is kind of a tricky one, because I think the Session component will not let you do what it is you want unless you modify it, what you can do is open config/core.php in your app and modify your Security.level and Session.timeout variables to be longer, the security level works as a multiplier for the timeout variable if it's high the multiplier is 10, 100 if it's medium and 300 if it's low

Related

cakePHP- setting loginRedirect from beforeFilter for admin role

I am facing this weird problem. I am trying to change the default loginRedirect of the admin role from that of normal user.
U have the auth key in the AppController's component variable set up as follows :
'Auth' => array(
'loginRedirect' => array(
'controller' => 'donors',
'action' => 'index'
)
)
Now in the beforeFilter callback I have this set up:
if($this->Auth->user('role') == 'admin'){
$this->Auth->loginRedirect = array(
'controller'=>'users',
'action'=>'admin_index',
'prefix'=>'admin',
'admin'=>true
);
}
However, this does not work and the if condition is never met. I am expecting this to run when the user logs in. If I add an else condition and repeat the same code shown above, it works and the admin is redirect to the desired page.
Can anyone instruct how I am able to do this correctly ? Thanks in advance
If the user is not logged in, $this->Auth->user() will return null. beforeFilter() will run before any action is run, so your login() action has still not been called.
Do the redirecting after $this->Auth->login() has been called and is successful. E.g. in your UsersController::login() action (or whichever action you use to login):
if ($this->Auth->login()) {
if($this->Auth->user('role') == 'admin') {
$this->redirect(array(
'controller'=>'users',
'action'=>'admin_index',
'prefix'=>'admin',
'admin'=>true
);
}
}
Instead of $this->Auth->loginRedirect use $this->redirect(
array('controller' => 'users', 'action' => 'admin_index');
Its less complicated

Routing all but certain parameters in CakePHP

I'm trying to set up a routing definition in my project which will allow users to have profiles accessible by simply using their username as the only parameter in the url, like www.example.com/username as opposed to www.example.com/user/view/username
I've set up a catch all route to go to an action which checks if the user exists, as the last route in the config file, and it works, but it over rides all of the basic routing that cake does. Meaning that I would have to define a route for every controller I want to provide access to just to make sure I never make it to the catchall. My routes:
Router::connect('/events/edit/*', array('controller' => 'events', 'action' => 'edit'));
Router::connect('/events/view/*', array('controller' => 'events', 'action' => 'view'));
Router::connect('/events/add', array('controller' => 'events', 'action' => 'add'));
Router::connect('/events/*', array('controller' => 'events', 'action' => 'index'));
Router::connect('/*', array('controller' => 'users', 'action' => 'view'));
So, this will allow me to access my events page, but any other pages get sent to the second router, expectedly.
What I'd like is to have is a route that does the basic cake function of /controller/action/param if the controller exists, but have it fall through to the last route which goes to the user/view page otherwise.
My question is, for what I'm trying to accomplish, am I doing this the right way? If I need to define a route for every controller I want access to, I will, but I have to think there's a better way to accomplish this.
Thanks
According to the my understanding of your question, I think You can proceed like this.
App::uses('UserRoute','Lib');
Router::connect('/:user_name', array('controller' => 'users', 'action' => 'view'),
array('routeClass'=>'UserRoute', 'pass' => array('user_name')));
and in your app/lib create a file UserRoute.php like this
<?php
App::uses('Lib', 'CakeRoute');
class UserRoute extends CakeRoute {
function parse($url) {
$params = parent::parse($url);
if (empty($params)) {
return false;
}
App::import('Model', 'User');
$User = new User();
$user_count = $User->find('count',array(
'conditions' => array('User.username' => $params['user_name']),
'recursive' => -1
));
if ($user_count) {
return $params;
}
return false;
}
}
Hope this will help you..

Cakephp Auth->loginredirect Problems

i have made a simple cakephp application . at the moment i am just working with auth component
to send user to their respective pages according to their. for ex if role =1 send to admin page and else if role = 2 send it to moderator page . i am using both session and auth component to see how they work and save data in them. below is the code for usercontroller login action
public function login(){
$this->Session->setFlash($this->Auth->user('role'));//checks for data in auth component if any
if($this->request->is('post') ){
$results = $this->User->findByEmail($this->request->data['User']['username']);
if($results &&$results['User']['password']== md5($this->request->data['User']['password']))
{
$this->Session->write('user',$results['User']);
$this->Auth->login($results['User']);
$this->Session->setFlash('User logged in successfully'.$this->Auth->user('role'));
return $this->redirect($this->Auth->redirect());
}
else
{
$this->Session->setFlash('Login is incorrect');
}
}
}
The problem is the login works fine all the data is stored in session and auth variable but loginredirect behave weird. in my chrome browser . it always redirects to admin page no matter what the role is , but it is flashing correct message which i set in flash. the code of beforefilter in appcontroller
public function beforeFilter(){
$this->Auth->allow('display');
$this->Auth->loginAction = array('controller' => 'Users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'Users', 'action' => 'login');
if($this->Auth->user('role') == '1'){
$this->Session->setFlash($this->Auth->user('role').'adminnnnnnnnnnnnnnnnnnnnn');
$this->Auth->loginRedirect = '/admins/index';
}
if($this->Auth->user('role') == '2'){
$this->Session->setFlash('moderatorrrrrrrrrrrrrrrrr');
$this->Auth->loginRedirect = '/users/index';
}
}
so the problem is the loop runs fine in before filter , the setflash display whether user is admin or moderator , but for some reason it redirects to only single page either admins/index page or users/index page no matter who logs in . This is behavior on chrome browser.
On firefox the loginredirects sends user to webroot/index page but again the flash messages are correct.
I am not sure what i am doing wrong is there a problem in my code or cakephp 2.0 auth component has measure bugs.
after user logs in it gets redirected via Auth->loginRedirect to dashboard() and here i check users role and use redirect to send particular user to the exact location
function dashboard() {
//get user's group (role)
//$role = $this->Session->read('user.role');
$role=$this->Auth->user('role');
//user selection logic here
if($role== '1'){
$this->redirect(array('controller' => 'users','action' => 'admin_index','admin' => false));
}
else if($role == '2'){
$this->redirect(array('controller' => 'users','action' => 'admin_index', 'admin' => false));
}
else if($role == '9'){
$this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => false));
$this->Session->setFlash('3');
}
}
This is just another way to work things out i included the dashboard function in my users controller and did auth login redirect to this function from appcontroller.
Hope it solves problem for others who are facing the issue. Thanks

Cakephp 2.x Admin Login not working,login redirect as well

I have done admin routing for my admin panel. Right now the url is localhost/app/admin.
Now I have 2 Tables Admins and Users.
I have created an url for the login localhost/app/admin/admins/login.
The page prompts for a username and a password.
But the Problem is when create component in appcontroller with loginredirect it is redirected to localhost/app/admin/users/login.I don't know why. I even tried changing the loginredirect path but it's nothing worked.
This is my appcontroller.php :
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'admins', 'action' => 'add'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
Even if I delete the user table, it redirects to the users login.
It sounds like your Auth component isn't working. instead of adding the auth redirects into the components variable, put them in your beforeFilter(). Your appController should be:
public $components = array('Auth','Session');
public function beforeFilter()
{
$this->Auth->loginRedirect = array('action' => 'add', 'controller' => 'admins');
$this->Auth->logoutRedirect = array('controller' => 'pages', 'action' => 'display', 'home');
$this->Auth->authError = 'You are not allowed to see that.';
}
Are you logging in successfully? if so, check routes.php to make sure you're routing things correctly. this could be tested by trying to navigat to example.com/admins/add manually.

i can not logout in my project , in tutorial of cakephp.org

i follow cakephp tutorial and when i log in to my account,
link of tutorial is :
http://book.cakephp.org/view/1543/Simple-Acl-controlled-Application
, i can't logout :
my code in logout method is
function logout() {
$this->Session->setFlash('Good-Bye');
$this->redirect($this->Auth->logout());
}
and login code is :
function login() {
if ($this->Session->read('Auth.User')) {
$this->Session->setFlash('You are logged in!');
$this->redirect('/', null, false);
}
}
this method is inside of users_controller .
but when i use this URL
http://localhost/newacl/users/logout
i comeback in
http://localhost/newacl/users
and i view this text
You are logged in!
therefore i can't log out.
can you say what is happen and say what the work am i doing.
I know this is an old topic, but maybe it'll be helpful for others.
It seems that adding this
function beforeFilter() {
$this->Auth->allow('login','logout');
}
to UsersController resolves the problem with logout
Its problem of you redirect thing you logout ..Add below code in your App_controller.php beforeFilter
$this->Auth->loginError = "Wrong credentials. Please provide a valid username and password.";
$this->Auth->authError = "You don't have sufficient privilege to access this resource.";
$this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
$this->Auth->loginRedirect = array('controller' => 'users', 'action' => 'home');
In you r logout
$this->Session->setFlash('You Succefully Logged Out');
$this->Auth->logout();
$this->redirect('/');
in your login try this
if($this->Auth->user('id')){
$this->Session->setFlash('You are logged in!');
$this->redirect('/', null, false);
}
hope this may help you....

Resources