2

我登录网站后遇到问题。有两种用户,即'admin','employer'。当我通过雇主登录后,我可以访问管理员的限制区域。下面是网站的AppController..

class AppController extends Controller {
        public $helpers = array('Form', 'Html', 'Js', 'Time', 'Auth');

        // Change template extension to .php instead of .ctp
        var $ext = '.php';
        public $components = array(
            'Session',
            'Auth' => array(
                'loginAction' => array(
                    'controller' => 'users',
                    'action' => 'login'
                ),
                'loginRedirect' => array('controller' => 'users', 'action' => 'index'),
                'logoutRedirect' => array('controller' => 'users', 'action' => 'login'),
                'authenticate' => array('Form' => array('fields' => array('username' => 'email'))),
                'authorize' => array('Controller')
            )
        );

        public function isAuthorized($user) {

            // Admin can access every action
            if (isset($user['type']) && $user['type'] === 'admin') {
                return true;
            }

            // Default deny
            return false;
        }

        public function beforeFilter() {
            $this->Auth->allow(array('view', 'index','assessment','question'));
        } 
    }

现在这里是具有管理方法的控制器。

class TopicsController extends AppController {

    public $scaffold = 'admin';
    public function beforeFilter() {

        if($this->Auth->user('type')!='employer'){
           parent::beforeFilter();
           $this->Auth->allow(array('view', 'index','moveup'));
        } else {
           $this->Auth->deny(array('view', 'index','moveup'));
           $this->redirect(array('controller' => 'employer' , 'action' => 'index'));
        }

    }
    public function isAuthorized($user) {
        return true;
    }

    public function index() {
      $this->set('topics', $this->Topic->children());
    }

}

如果管理员 URL 是www.example.com/admin/topics,雇主将被重定向到www.example.com/admin/employer,这不是要重定向的正确 URL。

也想知道public $scaffold = 'admin';因为这对我来说有点不清楚。请帮我..

4

1 回答 1

3

好的..找到了一种重定向方法,这使我的问题暂时解决了..如果有人有的话,仍在寻找正确的答案..

我改变了代码

$this->redirect(array('controller' => 'employer' , 'action' => 'index'));

$this->redirect('employer');

.. 编辑:谢谢亚历克斯,我用过

$this->redirect(array('controller' => 'employer' , 'action' => 'index', 'admin'=>false));

它也在工作..

于 2013-07-11T12:21:30.437 回答