0
votes

I have my application running in cakephp with Front login authentication with the "LADP" AD(Active Directory). I have integrated the admin Panel By "Routing Prefix to Admin". So that I have my admin actions in the same controller as of front actions. Like UsersController having actions login(), logout(), admin_login(), admin_logout(). And AdminContoller having

public function index() {

    $username = $this->Session->read('Admin.username');
    if (empty($username)) {
        $this->redirect(array('controller' => 'users', 'action' => 'login', 'admin' => true));
    } else {
        $this->redirect(array('action' => 'dashboard', 'admin' => true));
    }
}
public function admin_dashboard() {
    $this->loadModel('User');
    $this->loadModel('Group');
    $this->loadModel('News');
    $username = $this->Session->read('Admin.username');
    $group_id = $this->Session->read('Admin.group_id');

    if (empty($username) and ( $group_id = 1)) {
        $this->Session->setFlash(__('You are not authorized to view this Page!!'), 'default', array('class' => 'alert alert-error'));
        $this->redirect(array('controller' => 'users', 'action' => 'index', 'admin' => true));
    }

    $users = $this->User->find('count', array('conditions' => array('User.group_id !=' => 1)));
    $groups = $this->Group->find('count');
    $news = $this->News->find('count', array('conditions' => array('News.expiry_date >= NOW()')));
    $this->set(compact('users', 'groups', 'news'));
}

And having AppController as below

class AppController extends Controller {

    public $helpers = array('Paginator','Acl.AclHtml');
    public $components = array('Acl', 'Session',
        'Auth' => array(

            'authError' => 'You are not authorized to access that location.',
            'authorize' => array(
                'Actions' => array(
                    'actionPath' => 'controllers')
            ),
            'controllers' => array('users')
        ));

    public function beforeFilter() {
        // LDAP
        $server_ip = $_SERVER['SERVER_ADDR'];
        $ldapIp = ClassRegistry::init('LdapIp');
        $ldapIpCount = $ldapIp->find('count', array('conditions' => array('ldap_ip' => $server_ip)));
        if ($ldapIpCount >= 1) {
            $this->Auth->authenticate = array('Ldap');
        } else {
            $this->Auth->authenticate = array('Form');
        }

        $this->Auth->allow();

        if (!$this->Auth->isAllow($this)) {
            $this->set(array(
                'message' => array(
                    'text' => __('un aunthaticated request'),
                    'type' => 'error',
                    'status' => "401"
                ),
                '_serialize' => array('message')
            ));
            throw new ForbiddenException();
        }
    }
}        

How do I redirect Admin to admin/admin_dashboard if he is logged in but redirect him to users/admin_login if he is not, without having check into every controllers action? Can we check somewhere in beforeFilter() of AppController?

Please provide any suggestion with code to achieve this. I will integrate "alaxos ACL plugin 2.0" after this so please suggest me the code by keeping this scenario.

1

1 Answers

0
votes

I used prefix 'admin' [if($this->params['prefix'] == 'admin')] for my project, in your case, change to controller:

public function beforeFilter() {

    ....

    //Configure AuthComponent
    if($this->params['controller'] == 'admin') {
        $this->Auth->loginAction = array(
            'controller' => 'users',
            'action' => 'admin_login',
        );
        $this->Auth->logoutRedirect = array(
            'controller' => 'users',
            'action' => 'admin_login',
        );
        $this->Auth->loginRedirect = array(
            'controller' => 'admin',
            'action' => 'dashboard',
        );
    }
    ....
}

I think you should use prefix 'admin'.

More info: Admin Prefix