Auth for static page cakephp

时间:2014-02-03 19:50:48

标签: php cakephp cakephp-2.0

我是cakephp的新手.. 我正在建立一个网站,没有用户层次结构,只有管理员和公共用户。 我想禁止公共用户进入某个静态页面。当然页面位于视图/页面中,因此其视图/ pages / adminPanel.ctp。 请说明我应该包含您将提供的代码的位置。 提前谢谢

这是我的显示功能

function __checkLayout($pageName)   
            {
                //$pageName = "";

                $temp = "";

                switch ($pageName) 
                {
                    case "home":
                        $temp = "atheer";
                        break;
                    case "":
                        $temp = "atheer";
                        break;
                    case "adminpanel":
                        $temp = "adminview";
                        break;
                }
                return $temp;
            }


            public function display() {
    $path = func_get_args();
            //$this->layout='atheer';
            //$this->layout = Configure::read('layout.'.$page);
    $count = count($path);
    if (!$count) {
        return $this->redirect('/');
    }
    $page = $subpage = $title_for_layout = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    if (!empty($path[$count - 1])) {
        $title_for_layout = Inflector::humanize($path[$count - 1]);
    }
            $this->layout = $this->__checkLayout($page);

    $this->set(compact('page', 'subpage', 'title_for_layout'));

    try {
        $this->render(implode('/', $path));
    } catch (MissingViewException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }
}

2 个答案:

答案 0 :(得分:2)

如果是这种情况,请查看Allowing a Specific Page in Cakephp

请查看修改后的代码:

    public $allowedPages = array('page1', 'page2'); //here you add allowed pages only

public function beforeFilter() {
    $this->Auth->allow('display');
}

function __checkLayout($pageName)   
            {
                //$pageName = "";

                $temp = "";

                switch ($pageName) 
                {
                    case "home":
                        $temp = "atheer";
                        break;
                    case "":
                        $temp = "atheer";
                        break;
                    case "adminpanel":
                        $temp = "adminview";
                        break;
                }
                return $temp;
            }


            public function display() {
    $path = func_get_args();
            //$this->layout='atheer';
            //$this->layout = Configure::read('layout.'.$page);
    $count = count($path);
    if (!$count) {
        return $this->redirect('/');
    }
    $page = $subpage = $title_for_layout = null;

    if (!empty($path[0])) {
        $page = $path[0];
    }
    if (!empty($path[1])) {
        $subpage = $path[1];
    }
    if (!empty($path[$count - 1])) {
        $title_for_layout = Inflector::humanize($path[$count - 1]);
    }
            $this->layout = $this->__checkLayout($page);

        if(!in_array($page, $this->allowedPages) && !$this->Auth->login()) {
            return $this->redirect('/login'); //here redirects to login page change the path if the path is different
        }
    $this->set(compact('page', 'subpage', 'title_for_layout'));

    try {
        $this->render(implode('/', $path));
    } catch (MissingViewException $e) {
        if (Configure::read('debug')) {
            throw $e;
        }
        throw new NotFoundException();
    }
}

希望有所帮助

答案 1 :(得分:0)

您可以将所有可公开访问的方法放在$this->Auth->allow('func1', 'func2'...);

如果您想允许用户使用所有方法,请使用$this->Auth->allow('*')

See the documentation