1
votes

I just started learning about the Auth component and I'm having a problem with redirection. The path of my local aplication is: localhost/school but when a logged user tries to acces to a url he isnt't allowed the site redirects to localhost/school/school and it says "The requested address '/school/school/' was not found on this server". I want no redirection when this happens, just show "you are not allowed" in the same page or maybe redirect to specific error page, how can I do that?. I have no problems with login or logout redirection, only what I said before. This is my App Controller:

public $components = array( 'Acl', 'Auth' => array( 'authorize' => array( 'Actions' => array('actionPath' => 'controllers') ) ), 'Session' ); public $helpers = array('Html', 'Form', 'Session');

public function beforeFilter() {
    //Configure AuthComponent

    $this->Auth->loginAction = array(
        'controller' => 'users',
        'action' => 'login'
    );
    $this->Auth->logoutRedirect = array(
        'controller' => 'users',
        'action' => 'login'
    );

    $this->set('current_user',$this->Auth->User());
    $this->Auth->authError = "You're not allowed.";
}
4

4 Answers

0
votes

I had the same problem and I solved it.

Try this code in AppController

    public function beforeFilter() {

    //Configure AuthComponent

// note just these two lines
    $this->Auth->unauthorizedRedirect=FALSE ;
    $this->Auth->authError="Access Denied";


    $this->Auth->loginAction = array(
    'controller' => 'users',
    'action' => 'login'
    );
    $this->Auth->logoutRedirect = array(
    'controller' => 'users',
    'action' => 'login'
    );
    $this->Auth->loginRedirect = array(
    'controller' => 'posts',
    'action' => 'add'
    );

    $this->Auth->allow('display');
    //$this->Auth->allow();




    }
0
votes
class AppController extends Controller {

// added the debug toolkit
// sessions support
// authorization for login and logut redirect
public $components = array(
    'Session','Flash',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
        'authError' => 'You must be logged in to view this page.',
        'loginError' => 'Invalid Username or Password entered, please try again.'

    ));

// only allow the login controllers only
public function beforeFilter() {
    $this->Auth->allow('login');
}

public function isAuthorized($user) {
    // Here is where we should verify the role and give access based on role

    return true;
}

}

and in your controller it should be like this :

class UsersController extends AppController {

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('login','add'); 
}



public function login() {

    //if already logged-in, redirect
    if($this->Session->check('Auth.User')){
        $this->redirect(array('action' => 'index'));        
    }

    // if we get the post information, try to authenticate
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->Flash->set(__('Welcome, '. $this->Auth->user('username')));
            $this->redirect($this->Auth->redirectUrl());
        } else {
            $this->Flash->set(__('Invalid username or password'));
        }
    } 
}
0
votes

If you're not allowing someone access to a page, then what do you want the controller to do when they request it?

For example, you can set a redirect with :

$this->redirect(array(
'controller'=>'users', 
'action' => 'login'));`

You can display a message using Session::setFlash();

0
votes

localhost/projectName/projectName is a redirection when you don't have permission to this action. I had same problem. I comment for a moment 'Actions' => array('actionPath' => 'controllers') ) in $components. After that I set aros_acos by executing this code:

$group = $this->User->Group->read(null,'1');
$this->Acl->allow($group, 'controllers/Users/controlPanel');

After that I uncomment code, and in action 'controlPanel' and error disappear :) I don't know how I can change this redirection, but if I have record in aros_acos everything works.