【问题标题】:Restricting and redirecting other user from admin in cakePHP在 cakePHP 中限制和重定向其他用户
【发布时间】:2013-07-11 10:43:25
【问题描述】:

我在登录网站后遇到问题。有两种用户,即'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';,因为我不太清楚。 请帮帮我..

【问题讨论】:

  • 关于$scaffold = 'admin';阅读:book.cakephp.org/2.0/en/controllers/scaffolding.html。你试过了吗:$this->redirect(array('controller' => 'employer' , 'action' => 'index', 'admin'=>false));
  • 谢谢亚历克斯,我已经浏览了链接。我以前从未尝试过,希望它运作良好.. 非常感谢..

标签: cakephp cakephp-2.0 cakephp-2.1


【解决方案1】:

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

我更改了代码

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

$this->redirect('employer');

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

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

它也在工作..

【讨论】:

  • 如果你想重定向,这就是方法。您还可以将 HTTP 状态代码作为第二个参数传递给此函数 - 因此所有 3xx 代码都适用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 2021-10-27
  • 2013-07-13
  • 1970-01-01
  • 2014-07-04
相关资源
最近更新 更多