Auth for static page cakephp [closed]

落花浮王杯 提交于 2019-12-06 10:05:38
Miheretab Alemu

ok if that is the case look at this Allowing a Specific Page in Cakephp,

Please take a look at the modified code:

    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();
    }
}

Hope it helps

You can put all the publicly accessible methods in $this->Auth->allow('func1', 'func2'...);

If you want to allow all methods available for users then use $this->Auth->allow('*').

See the documentation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!