<?php

App::uses('Component', 'Controller');

/**
 * AclFilter Component
 *
 * PHP version 5
 *
 * @category Component
 * @package  Bemmelhor
 * @version  1.0
 * @author   Fabricio S Costa <fabriciojf@gmail.com>
 *
 * @link     http://www.bemmelhor.org
 */
class AclFilterComponent extends Component {

    /**
     * _controller
     *
     * @var Controller
     */
    protected $_controller = null;

    /**
     * @param object $controller controller
     * @param array  $settings   settings
     */
    public function initialize(Controller $controller) {
        $this->_controller = & $controller;
    }

    /**
     * acl and auth
     *
     * @return void
     */
    public function auth() {
        //Configure AuthComponent
        $this->_controller->Auth->authenticate = array(
            AuthComponent::ALL => array(
                'userModel' => 'User',
                'fields' => array(
                    'username' => 'username',
                    'password' => 'password',
                ),
                'scope' => array(
                    'User.status' => 1,
                ),
            ),
            'Form',
        );
        $actionPath = 'controllers';
        $this->_controller->Auth->authorize = array(
            AuthComponent::ALL => array('actionPath' => $actionPath),
            'Actions',
        );
        $this->_controller->Auth->loginAction = array(
            'plugin' => null,
            'admin' => false,
            'controller' => 'users',
            'action' => 'login',
        );
        $this->_controller->Auth->logoutRedirect = array(
            'plugin' => null,
            'admin' => true,
            'controller' => 'repertoires',
            'action' => 'index',
        );
        $this->_controller->Auth->loginRedirect = array(
            'plugin' => false,
            'admin' => true,
            'controller' => 'repertoires',
            'action' => 'index',
        );

        if ($this->_controller->Auth->user() && $this->_controller->Auth->user('role_id') == 1) {
            // Role: Admin
            $this->_controller->Auth->allow();
        } else {
            if ($this->_controller->Auth->user()) {
                $roleId = $this->_controller->Auth->user('role_id');
            } else {
                $roleId = 3; // Role: Public
            }

            $allowedActions = ClassRegistry::init('Acl.AclPermission')->getAllowedActionsByRoleId($roleId);
            $linkAction = Inflector::camelize($this->_controller->request->params['controller']) . '/' . $this->_controller->request->params['action'];
            if (in_array($linkAction, $allowedActions)) {
                $this->_controller->Auth->allowedActions = array($this->_controller->request->params['action']);
            }
        }
    }

}
