Auth for static page cakephp [closed]

2019-05-21 07:07发布

问题:

I am new in cakephp.. I am making a website , there no users hierarchy , there is only the admin and public users. I want to disallow the public users from entering a certain static page. of course the page is located in view/pages , so its view/pages/adminPanel.ctp . please specify where I should include the code you will give. Thank you in advance

here's my display function

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

回答1:

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



回答2:

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